Calculating number of days between two dates.

C

clintonb

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
 
V

Victor Bazarov

clintonb said:
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
 
D

Default User

clintonb said:
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
 
J

Jack Crenshaw

clintonb said:
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
 
C

clintonb

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
 
C

clintonb

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
 
C

clintonb

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

V

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
 
C

clintonb

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
 
J

James Kanze

Sure, you could do that. Have you already tried? If not, why not?
If yes, why do you ask? Did it work?
[/QUOTE]
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.
 
J

James Kanze

Well, how many days are between 3pm Friday and 8am Saturday? 0? 1? Some
fraction? You need to decide that first.
[/QUOTE]
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top