Newbie Question: Working with dates

Discussion in 'C Programming' started by Erik, Jun 8, 2005.

  1. Erik

    Erik Guest

    Is there a variable for storing dates and how can I count with dates:
    For example, how do I code how much days there are between 01-01-2005
    and 03-03-2005?
     
    Erik, Jun 8, 2005
    #1
    1. Advertising

  2. Erik

    Eric Sosman Guest

    Erik wrote:
    > Is there a variable for storing dates and how can I count with dates:
    > For example, how do I code how much days there are between 01-01-2005
    > and 03-03-2005?


    The library provides some rudimentary support for date
    computations, but they're not really very well-supported by
    C as such. Here's something you could try:

    #include <time.h>
    ...
    struct tm date1 = { 0 }, date2 = { 0 };
    time_t time1, time2;
    int days;

    date1.tm_year = 2005 - 1900;
    date1.tm_mon = 1 - 1;
    date1.tm_mday = 1;
    date1.tm_isdst = -1;
    time1 = mktime(&date1);

    date2.tm_year = 2005 - 1900;
    date2.tm_mon = 3 - 1;
    date2.tm_mday = 3;
    date2.tm_isdst = -1;
    time2 = mktime(&date2);

    days = difftime(time2, time1) / (60.0 * 60.0 * 24.0);

    Unfortunately, this is not guaranteed to work as you'd hope.
    Different implementations of C support different ranges of
    dates, and either or both of the mktime() calls could fail.
    (All implementations I know of can handle dates in 2005, but
    if you start trying to work with dates in 2100 or 1776 your
    results may be disappointing.) Also, the "divide by one day's
    worth of seconds" step is a little too naive: Most days are
    24 hours long, but many places observe one 23-hour and one
    25-hour day each year -- some C implementations can handle
    this adjustment, others can't. Days of 24:00:01 or 23:59:59
    are also possible, and (believe it or not) some systems are
    actually *forbidden* to pay attention to this!

    The method illustrated above will work reasonably well on
    many systems for a limited range of dates most of the time,
    but that's about all that can be said for it.

    --
     
    Eric Sosman, Jun 8, 2005
    #2
    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. David Lozzi

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

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    869
    Rob Schieber
    Sep 30, 2005
  2. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    198
    Mark Schupp
    Aug 9, 2004
  3. Replies:
    1
    Views:
    206
    Jano Svitok
    Jul 17, 2007
  4. kellygreer1

    RFC-822 dates into Ruby dates

    kellygreer1, Jun 8, 2008, in forum: Ruby
    Replies:
    1
    Views:
    200
    Eric I.
    Jun 8, 2008
  5. Dr J R Stockton

    FAQ Dates; Opera Dates.

    Dr J R Stockton, Jun 18, 2007, in forum: Javascript
    Replies:
    0
    Views:
    161
    Dr J R Stockton
    Jun 18, 2007
Loading...

Share This Page