Calculating number of days between two dates.

Discussion in 'C++' started by clintonb, Apr 25, 2007.

  1. clintonb

    clintonb Guest

    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
    clintonb, Apr 25, 2007
    #1
    1. Advertising

  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?


    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 Bazarov, Apr 25, 2007
    #2
    1. Advertising

  3. clintonb

    Default User Guest

    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
    Default User, Apr 26, 2007
    #3
  4. 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 Crenshaw, Apr 26, 2007
    #4
  5. clintonb

    clintonb Guest

    On Apr 26, 1:10 am, Jack Crenshaw <> 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
    clintonb, May 22, 2007
    #5
  6. clintonb

    clintonb Guest

    On Apr 25, 6:37 pm, "Default User" <> 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
    clintonb, May 22, 2007
    #6
  7. clintonb

    clintonb Guest

    On Apr 25, 5:42 pm, "Victor Bazarov" <> 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
    clintonb, May 22, 2007
    #7
  8. clintonb

    clintonb Guest

    On Apr 25, 6:37 pm, "Default User" <> 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
    clintonb, May 22, 2007
    #8
  9. clintonb

    James Kanze Guest

    On May 22, 8:50 pm, clintonb <> wrote:
    > On Apr 25, 5:42 pm, "Victor Bazarov" <> 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:
    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
    James Kanze, May 23, 2007
    #9
  10. clintonb

    James Kanze Guest

    On May 22, 6:37 pm, clintonb <> wrote:
    > On Apr 25, 6:37 pm, "Default User" <> 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:
    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
    James Kanze, May 23, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Henry
    Replies:
    6
    Views:
    16,156
    Brian Henry
    Nov 19, 2003
  2. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    843
    Rob Schieber
    Sep 30, 2005
  3. David Stockwell
    Replies:
    2
    Views:
    688
    Anna Martelli Ravenscroft
    Sep 20, 2004
  4. chrismo
    Replies:
    2
    Views:
    171
    Phrogz
    Jan 16, 2007
  5. Luca Villa
    Replies:
    2
    Views:
    142
    Luca Villa
    Nov 11, 2007
Loading...

Share This Page