467,117 Members | 1,064 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,117 developers. It's quick & easy.

Calculating number of days between two dates.

I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.

Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?
- Clint

Apr 25 '07 #1
  • viewed: 20973
Share:
9 Replies
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.

Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?
Sure, you could do that. Have you already tried? If not, why not?
If yes, why do you ask? Did it work?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 25 '07 #2
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.

Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?
Well, how many days are between 3pm Friday and 8am Saturday? 0? 1? Some
fraction? You need to decide that first.

Brian
Apr 25 '07 #3
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.

Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?
- Clint
The key phrase is "Julian Date," which is a sort of free-running day
counter that counts from a point way far back in prehistory, like 4713
BC. Astronomers use the Julian date, and it's easy to subtract two such
dates to get the number of days. It's up to you how you want to count
partial days.

The only problem with Julian date is that the number is so huge.
Especially back when computers had 16 bits -- or even 8 -- it required
multiple words to store a JD. So several groups and companies chose to
standardize on a date whose basis is not quite so far back. It's my
understanding that Microsoft defined such a date. I guess that's what
they use in their library of functions.

If not, you can always use the true JD. Any good book on astronomy --
or, for that matter, many web sites -- will have pretty short algorithms
for generating the number.

Jack
Jack
Apr 26 '07 #4
On Apr 26, 1:10 am, Jack Crenshaw <jcr...@earthlink.netwrote:
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.
Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?
- Clint

The key phrase is "Julian Date," which is a sort of free-running day
counter that counts from a point way far back in prehistory, like 4713
BC. Astronomers use the Julian date, and it's easy to subtract two such
dates to get the number of days. It's up to you how you want to count
partial days.

The only problem with Julian date is that the number is so huge.
Especially back when computers had 16 bits -- or even 8 -- it required
multiple words to store a JD. So several groups and companies chose to
standardize on a date whose basis is not quite so far back. It's my
understanding that Microsoft defined such a date. I guess that's what
they use in their library of functions.

If not, you can always use the true JD. Any good book on astronomy --
or, for that matter, many web sites -- will have pretty short algorithms
for generating the number.

Jack
Jack

Jack,
That's pretty interesting. In the end, I didn't do that, but I
appreciate the suggestion.
Thanks.

- Clint

May 22 '07 #5
On Apr 25, 6:37 pm, "Default User" <defaultuse...@yahoo.comwrote:
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.
Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?

Well, how many days are between 3pm Friday and 8am Saturday? 0? 1? Some
fraction? You need to decide that first.

Brian
Thanks for bringing up that issue.
I decided to measure between the starts of each day in the date range.

- Clint

May 22 '07 #6
On Apr 25, 5:42 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.
Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?

Sure, you could do that. Have you already tried? If not, why not?
If yes, why do you ask? Did it work?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Victor, Thanks for the response.

I tried it. It seems to work.

I was actually trying to port some C# .NET code we had in one of our
applications to our C++ applications. .NET's datetime class has a
function for determining the number of days between two dates, so I
needed my C++ code to produce the same results. But I was wondering
if I had to handle stuff like daylight savings time, leap year, etc.
myself or whether the difftime() function took all that stuff into
account.

- Clint

May 22 '07 #7
On Apr 25, 6:37 pm, "Default User" <defaultuse...@yahoo.comwrote:
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.
Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?

Well, how many days are between 3pm Friday and 8am Saturday? 0? 1? Some
fraction? You need to decide that first.

Brian
Thanks for bringing up that issue.
I decided to measure between the starts of each day in the date range.

- Clint

May 22 '07 #8
On May 22, 8:50 pm, clintonb <cba...@centurytel.netwrote:
On Apr 25, 5:42 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.
Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?
Sure, you could do that. Have you already tried? If not, why not?
If yes, why do you ask? Did it work?
I tried it. It seems to work.
Which, of course, doesn't mean much. Testing to find out
whether something is guaranteed to work is useless; it will only
tell you whether it worked with the exact input values you used,
on the implementation you tested. (This doesn't mean that you
shouldn't test your application, or unit test your individual
components. Only that you shouldn't count exclusively on
testing. And above all, that you shouldn't count at all on
testing to determine whether something is guaranteed by the
standard, on all implementations.)

The problem is that difftime returns the number of seconds
between the two *times*. Nothing about dates. So while the
number of days between May 23, 2007 and May 22, 2007 is 1,
that's not what you'll get if you simply do a difftime between
1:00 May 23, 2007 and 23:00 May 22, 2007. And any rounding
algorithm which will return 1 then will probably return 2 when
you compare 23:00 May 23, 2007 and 1:00 May 22, 2007. In order
to get correct results, you probably have to force the time_t to
a common time, by breaking them out into a tm struct, forcing
the tm_hour, tm_min and tm_sec to a "standard" value, then using
mktime to get a new time_t, and doing difftime on those. (Note
too that some systems store time_t in UTC, so local time zones,
summer time, etc., may have an influence on the results.)
I was actually trying to port some C# .NET code we had in one of our
applications to our C++ applications. .NET's datetime class has a
function for determining the number of days between two dates, so I
needed my C++ code to produce the same results. But I was wondering
if I had to handle stuff like daylight savings time, leap year, etc.
myself or whether the difftime() function took all that stuff into
account.
difftime gives the number of seconds between two times. There
are no leap years, daylight savings time, etc. in the number of
seconds. There are leap seconds, however (although some systems
choose to ignore them).

If you're only concerned about local time, calling localtime on
the two values, forcing the time to noon, then using mktime to
convert back to time_t, difftime on those, and dividing by the
number of seconds in a day, and rounding the results to nearest
should be adequate. The results of difftime could be off about
an hour, if there was a shift between summer time and standard
time in the interval; the error should never be enough that
rounding to an integral number of days doesn't give the correct
results, however.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 23 '07 #9
On May 22, 6:37 pm, clintonb <cba...@centurytel.netwrote:
On Apr 25, 6:37 pm, "Default User" <defaultuse...@yahoo.comwrote:
clintonb wrote:
I'm looking for a way to calculate the number of days between two
dates using standard C++ functions.
Would it be as simple as just using the difftime() function and then
dividing that result by the number of seconds in a day?
Well, how many days are between 3pm Friday and 8am Saturday? 0? 1? Some
fraction? You need to decide that first.
Thanks for bringing up that issue.
I decided to measure between the starts of each day in the date range.
Intuitively, I'd go for noon. An off by one hour error, due
maybe because of a switch between summer time and standard time,
won't cause a change of day. (In practice, we use the start of
the day here, in code that was written many, many years ago, and
it has never caused problems. On the other hand, we use UTC, so
there is no summer time, and we are locked into Posix, so we
also know that time_t represents seconds directly, and we do the
arithmetic directly on it.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 23 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Hans Gruber | last post: by
10 posts views Thread by riki | last post: by
8 posts views Thread by =?Utf-8?B?QWw=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.