Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calculating number of days between two dates.

Reply
Thread Tools

Calculating number of days between two dates.

 
 
clintonb
Guest
Posts: n/a
 
      04-25-2007
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-25-2007
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


 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      04-25-2007
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
 
Reply With Quote
 
Jack Crenshaw
Guest
Posts: n/a
 
      04-26-2007
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
 
Reply With Quote
 
clintonb
Guest
Posts: n/a
 
      05-22-2007
On Apr 26, 1:10 am, Jack Crenshaw <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
clintonb
Guest
Posts: n/a
 
      05-22-2007
On Apr 25, 6:37 pm, "Default User" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
clintonb
Guest
Posts: n/a
 
      05-22-2007
On Apr 25, 5:42 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
clintonb
Guest
Posts: n/a
 
      05-22-2007
On Apr 25, 6:37 pm, "Default User" <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-23-2007
On May 22, 8:50 pm, clintonb <(E-Mail Removed)> wrote:
> On Apr 25, 5:42 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:


> > 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:(E-Mail Removed)
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

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-23-2007
On May 22, 6:37 pm, clintonb <(E-Mail Removed)> wrote:
> On Apr 25, 6:37 pm, "Default User" <(E-Mail Removed)> wrote:


> > 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:(E-Mail Removed)
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
list of days between two days kirke Javascript 7 10-20-2006 07:17 AM
Re: Calculating days using Business Days for the Calendar Terry Reedy Python 0 09-20-2004 10:44 PM
Calculating days using Business Days for the Calendar David Stockwell Python 2 09-20-2004 09:58 PM
Re: Calculating days using Business Days for the Calendar Gerrit Python 0 09-20-2004 04:39 PM
subtracting two dates to get number of days between Brian Henry ASP .Net 6 11-19-2003 09:50 PM



Advertisments