Re: stuck on time_t

Discussion in 'C Programming' started by Jim Showalter, Aug 11, 2004.

  1. Thank all of you who helped me on the "stuck on time_t" thread!
    As I mentioned, this was a "Programming Challenge" from Peter
    van der Linden's "Expert C Programming".

    Please let me know if the following solution is portable and
    feel free to point-out any flaws.

    /*
    * When will time_t wrap around?
    * time_wrap.c
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    /*
    * The actual length of the intervals is arbitrary,
    * and I'm sure powers of 2 would be more efficient,
    * but traditional time intervals seemed more appropriate.
    */
    #define SEC 1
    #define MIN 60 * SEC
    #define HOUR 60 * MIN
    #define DAY 24 * HOUR
    #define YEAR 365 * DAY

    time_t zero_in(time_t now, time_t time, time_t interval)
    {
    while (time >= now) { /* add interval until time_t 'wraps'*/
    time += interval;
    }
    return (time-=interval); /* then go back one time interval */
    }

    int main (void)
    {
    int i;
    time_t now, tod, interval[]={SEC, MIN, HOUR, DAY, YEAR};

    tod=now=time(NULL); /* first, get the current date/time */
    for (i=4; i>=0; i--) {
    tod=zero_in(now,tod,interval);
    }
    printf("time_t will wrap-around one second after %s\n", ctime(&tod));
    tod+=SEC;
    printf("to become %s\n", ctime(&tod));

    return EXIT_SUCCESS;
    }

    jim
    --
    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    _/ Everything should be made as simple as possible, but not simpler.
    _/ -- Albert Einstein
    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    Jim Showalter, Aug 11, 2004
    #1
    1. Advertising

  2. Jim Showalter wrote on 11/08/04 :

    > #define SEC 1
    > #define MIN 60 * SEC
    > #define HOUR 60 * MIN
    > #define DAY 24 * HOUR
    > #define YEAR 365 * DAY


    #define MIN (60 * SEC)
    #define HOUR (60 * MIN)
    #define DAY (24 * HOUR)
    #define YEAR (365 * DAY)

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"
    Emmanuel Delahaye, Aug 11, 2004
    #2
    1. Advertising

  3. Jim Showalter

    SM Ryan Guest

    Emmanuel Delahaye <> wrote:
    # Jim Showalter wrote on 11/08/04 :
    #
    # > #define SEC 1
    # > #define MIN 60 * SEC
    # > #define HOUR 60 * MIN
    # > #define DAY 24 * HOUR
    # > #define YEAR 365 * DAY
    #
    # #define MIN (60 * SEC)
    # #define HOUR (60 * MIN)
    # #define DAY (24 * HOUR)
    # #define YEAR (365 * DAY)

    A year is not an integral number of days.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Mention something out of a Charleton Heston movie, and suddenly
    everybody's a theology scholar.
    SM Ryan, Aug 11, 2004
    #3
  4. Jim Showalter

    Joe Wright Guest

    SM Ryan wrote:
    > Emmanuel Delahaye <> wrote:
    > # Jim Showalter wrote on 11/08/04 :
    > #
    > # > #define SEC 1
    > # > #define MIN 60 * SEC
    > # > #define HOUR 60 * MIN
    > # > #define DAY 24 * HOUR
    > # > #define YEAR 365 * DAY
    > #
    > # #define MIN (60 * SEC)
    > # #define HOUR (60 * MIN)
    > # #define DAY (24 * HOUR)
    > # #define YEAR (365 * DAY)
    >
    > A year is not an integral number of days.
    >


    But a century is..

    #define CENT (36525 * DAY)
    #define YEAR (CENT / 100)
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Aug 11, 2004
    #4
  5. On Wed, 11 Aug 2004 11:56:54 -0000, in comp.lang.c , SM Ryan
    <> wrote:

    >Emmanuel Delahaye <> wrote:
    ># Jim Showalter wrote on 11/08/04 :
    >#
    ># > #define SEC 1
    ># > #define MIN 60 * SEC
    ># > #define HOUR 60 * MIN
    ># > #define DAY 24 * HOUR
    ># > #define YEAR 365 * DAY
    >#
    ># #define MIN (60 * SEC)
    ># #define HOUR (60 * MIN)
    ># #define DAY (24 * HOUR)
    ># #define YEAR (365 * DAY)
    >
    >A year is not an integral number of days.


    Since when? Where do they insert the fractional day in the calendar?


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Mark McIntyre, Aug 12, 2004
    #5
  6. Joe Wright <> writes:
    > SM Ryan wrote:
    > > Emmanuel Delahaye <> wrote:
    > > # Jim Showalter wrote on 11/08/04 :
    > > # # > #define SEC 1
    > > # > #define MIN 60 * SEC
    > > # > #define HOUR 60 * MIN
    > > # > #define DAY 24 * HOUR
    > > # > #define YEAR 365 * DAY
    > > # # #define MIN (60 * SEC)
    > > # #define HOUR (60 * MIN)
    > > # #define DAY (24 * HOUR)
    > > # #define YEAR (365 * DAY)
    > > A year is not an integral number of days.
    > >

    >
    > But a century is..
    >
    > #define CENT (36525 * DAY)
    > #define YEAR (CENT / 100)


    You're ignoring the Gregorian leap year rules.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 12, 2004
    #6
  7. Jim Showalter

    SM Ryan Guest

    Mark McIntyre <> wrote:
    # On Wed, 11 Aug 2004 11:56:54 -0000, in comp.lang.c , SM Ryan
    # <> wrote:
    #
    # >Emmanuel Delahaye <> wrote:
    # ># Jim Showalter wrote on 11/08/04 :
    # >#
    # ># > #define SEC 1
    # ># > #define MIN 60 * SEC
    # ># > #define HOUR 60 * MIN
    # ># > #define DAY 24 * HOUR
    # ># > #define YEAR 365 * DAY
    # >#
    # ># #define MIN (60 * SEC)
    # ># #define HOUR (60 * MIN)
    # ># #define DAY (24 * HOUR)
    # ># #define YEAR (365 * DAY)
    # >
    # >A year is not an integral number of days.
    #
    # Since when? Where do they insert the fractional day in the calendar?

    Where were you 29 Feb 2004?

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Wow. A sailboat.
    SM Ryan, Aug 12, 2004
    #7
  8. Emmanuel Delahaye wrote:

    > Jim Showalter wrote on 11/08/04 :
    >
    >> #define SEC 1
    >> #define MIN 60 * SEC
    >> #define HOUR 60 * MIN
    >> #define DAY 24 * HOUR
    >> #define YEAR 365 * DAY

    >
    > #define MIN (60 * SEC)
    > #define HOUR (60 * MIN)
    > #define DAY (24 * HOUR)
    > #define YEAR (365 * DAY)
    >


    Duh - you got me. And I do (or did) know better than that. But it's been
    some time since I've written any C. Thanks for reminding me.

    jim
    --
    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    _/ Everything should be made as simple as possible, but not simpler.
    _/ -- Albert Einstein
    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    Jim Showalter, Aug 12, 2004
    #8
  9. SM Ryan wrote:

    > Emmanuel Delahaye <> wrote:
    > # Jim Showalter wrote on 11/08/04 :
    > #
    > # > #define SEC 1
    > # > #define MIN 60 * SEC
    > # > #define HOUR 60 * MIN
    > # > #define DAY 24 * HOUR
    > # > #define YEAR 365 * DAY
    > #
    > # #define MIN (60 * SEC)
    > # #define HOUR (60 * MIN)
    > # #define DAY (24 * HOUR)
    > # #define YEAR (365 * DAY)
    >
    > A year is not an integral number of days.
    >


    I'm certainly aware of that. Emmanuel clipped the comment I
    placed above those defines, so here it is:
    /*
    * The actual length of the intervals is arbitrary
    * and I'm sure powers of 2 would be more efficient,
    * but traditional time intervals seemed more appropriate.
    */
    The values aren't really important. But in code maintained by others,
    I agree that something like: #define YEAR (365 * DAY) could cause some
    concern.

    jim
    --
    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    _/ Everything should be made as simple as possible, but not simpler.
    _/ -- Albert Einstein
    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    Jim Showalter, Aug 12, 2004
    #9
  10. Jim Showalter

    CBFalconer Guest

    Mark McIntyre wrote:
    > SM Ryan <> wrote:
    >

    .... snip ...
    >>
    >>A year is not an integral number of days.

    >
    > Since when? Where do they insert the fractional day in the calendar?


    Right after February 28th :) (except on years that are multiples
    of 100) (except on years that are multiples of 400)

    --
    "Churchill and Bush can both be considered wartime leaders, just
    as Secretariat and Mr Ed were both horses." - James Rhodes.
    "A man who is right every time is not likely to do very much."
    - Francis Crick, co-discover of DNA
    CBFalconer, Aug 12, 2004
    #10
  11. On Thu, 12 Aug 2004 01:57:15 -0000, in comp.lang.c , SM Ryan
    <> wrote:

    >Mark McIntyre <> wrote:
    ># On Wed, 11 Aug 2004 11:56:54 -0000, in comp.lang.c , SM Ryan
    ># <> wrote:
    >#
    ># >Emmanuel Delahaye <> wrote:
    ># ># Jim Showalter wrote on 11/08/04 :
    ># >#
    ># ># > #define SEC 1
    ># ># > #define MIN 60 * SEC
    ># ># > #define HOUR 60 * MIN
    ># ># > #define DAY 24 * HOUR
    ># ># > #define YEAR 365 * DAY
    ># >#
    ># ># #define MIN (60 * SEC)
    ># ># #define HOUR (60 * MIN)
    ># ># #define DAY (24 * HOUR)
    ># ># #define YEAR (365 * DAY)
    ># >
    ># >A year is not an integral number of days.
    >#
    ># Since when? Where do they insert the fractional day in the calendar?
    >
    >Where were you 29 Feb 2004?


    Was that a fractional day? Funny. I could have sworn there were 24 hours,
    as normal.

    Hint: the period of the earth's orbit is not a calendar year.


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Mark McIntyre, Aug 12, 2004
    #11
  12. On Thu, 12 Aug 2004 03:23:49 GMT, in comp.lang.c , CBFalconer
    <> wrote:

    >Mark McIntyre wrote:
    >> SM Ryan <> wrote:
    >>

    >... snip ...
    >>>
    >>>A year is not an integral number of days.

    >>
    >> Since when? Where do they insert the fractional day in the calendar?

    >
    >Right after February 28th :) (except on years that are multiples
    >of 100) (except on years that are multiples of 400)


    I /think/, if you check, you'll see that the day you refer to is not a
    fractional one. Its an entirely normal whole day. Now I'm not entirely
    certain about this, maybe I hallucinated the other 18 hours. On the other
    hand its quite possible that you USAliens got so drunk on corn whiskey and
    grits that you missed them.... :)


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Mark McIntyre, Aug 12, 2004
    #12
  13. SM Ryan <> wrote in message news:<>...
    > Emmanuel Delahaye <> wrote:
    > # Jim Showalter wrote on 11/08/04 :
    > #
    > # > #define SEC 1
    > # > #define MIN 60 * SEC
    > # > #define HOUR 60 * MIN
    > # > #define DAY 24 * HOUR
    > # > #define YEAR 365 * DAY
    > #
    > # #define MIN (60 * SEC)
    > # #define HOUR (60 * MIN)
    > # #define DAY (24 * HOUR)
    > # #define YEAR (365 * DAY)
    >
    > A year is not an integral number of days.


    The "years" that most of us deal with most of the time certainly
    are an integral number of days, just not always the same number.
    J. J. Farrell, Aug 13, 2004
    #13
  14. Jim Showalter

    SM Ryan Guest

    Mark McIntyre <> wrote:
    # On Thu, 12 Aug 2004 01:57:15 -0000, in comp.lang.c , SM Ryan
    # <> wrote:
    #
    # >Mark McIntyre <> wrote:
    # ># On Wed, 11 Aug 2004 11:56:54 -0000, in comp.lang.c , SM Ryan
    # ># <> wrote:
    # >#
    # ># >Emmanuel Delahaye <> wrote:
    # ># ># Jim Showalter wrote on 11/08/04 :
    # ># >#
    # ># ># > #define SEC 1
    # ># ># > #define MIN 60 * SEC
    # ># ># > #define HOUR 60 * MIN
    # ># ># > #define DAY 24 * HOUR
    # ># ># > #define YEAR 365 * DAY
    # ># >#
    # ># ># #define MIN (60 * SEC)
    # ># ># #define HOUR (60 * MIN)
    # ># ># #define DAY (24 * HOUR)
    # ># ># #define YEAR (365 * DAY)
    # ># >
    # ># >A year is not an integral number of days.
    # >#
    # ># Since when? Where do they insert the fractional day in the calendar?
    # >
    # >Where were you 29 Feb 2004?
    #
    # Was that a fractional day? Funny. I could have sworn there were 24 hours,
    # as normal.
    #
    # Hint: the period of the earth's orbit is not a calendar year.

    Hint: there are different, incompatible definitions of 'year'.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    We found a loophole; they can't keep us out anymore.
    SM Ryan, Aug 13, 2004
    #14
  15. Jim Showalter

    CBFalconer Guest

    Mark McIntyre wrote:
    > <> wrote:
    >>Mark McIntyre wrote:
    >>> SM Ryan <> wrote:
    >>>

    >>... snip ...
    >>>>
    >>>> A year is not an integral number of days.
    >>>
    >>> Since when? Where do they insert the fractional day in the calendar?

    >>
    >> Right after February 28th :) (except on years that are multiples
    >> of 100) (except on years that are multiples of 400)

    >
    > I /think/, if you check, you'll see that the day you refer to is not a
    > fractional one. Its an entirely normal whole day. Now I'm not entirely
    > certain about this, maybe I hallucinated the other 18 hours. On the
    > other hand its quite possible that you USAliens got so drunk on corn
    > whiskey and grits that you missed them.... :)


    The calendar is running Bresenhams algorithm, and the error signal
    exceeds 1 day each February 28th. (with some exceptions, noted
    above). :) This is a digital world we are talking about, and all
    we have available are integers.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Aug 13, 2004
    #15
  16. On Fri, 13 Aug 2004 03:39:42 GMT, in comp.lang.c , CBFalconer
    <> wrote:

    >This is a digital world we are talking about, and all
    >we have available are integers.


    Exactly. Which is why I'm puzzled by mention of fractional days....


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Mark McIntyre, Aug 14, 2004
    #16
  17. On Fri, 13 Aug 2004 03:14:20 -0000, in comp.lang.c , SM Ryan
    <> wrote:

    >Mark McIntyre <> wrote:
    ># ># Since when? Where do they insert the fractional day in the calendar?
    ># >
    ># >Where were you 29 Feb 2004?
    >#
    ># Was that a fractional day? Funny. I could have sworn there were 24 hours,
    ># as normal.
    >#
    ># Hint: the period of the earth's orbit is not a calendar year.
    >
    >Hint: there are different, incompatible definitions of 'year'.


    New Hint: the one you're using does not contain fractional days. Nor does
    any other calendar one.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Mark McIntyre, Aug 14, 2004
    #17
  18. >>This is a digital world we are talking about, and all
    >>we have available are integers.

    >
    >Exactly. Which is why I'm puzzled by mention of fractional days....


    Leap seconds.

    Gordon L. Burditt
    Gordon Burditt, Aug 14, 2004
    #18
  19. Jim Showalter

    Mabden Guest

    "Gordon Burditt" <> wrote in message
    news:cfjte5$...
    > >>This is a digital world we are talking about, and all
    > >>we have available are integers.

    > >
    > >Exactly. Which is why I'm puzzled by mention of fractional days....

    >
    > Leap seconds.


    But never leap frog a unicorn.
    Mabden, Aug 14, 2004
    #19
  20. Jim Showalter

    CBFalconer Guest

    Mark McIntyre wrote:
    > CBFalconer <> wrote:
    >
    >> This is a digital world we are talking about, and all
    >> we have available are integers.

    >
    > Exactly. Which is why I'm puzzled by mention of fractional days....


    Because the year actually has fractional days, and the mechanism
    keeps the error between the actual value and the integral value
    minimized.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Aug 14, 2004
    #20
    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. =?Utf-8?B?TWFyaw==?=

    DateTime to time_t bug

    =?Utf-8?B?TWFyaw==?=, May 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,648
    Steven Cheng[MSFT]
    May 18, 2004
  2. Anand CS

    converting to time_t

    Anand CS, Jul 3, 2003, in forum: C Programming
    Replies:
    1
    Views:
    7,681
    Luke Driscoll
    Jul 3, 2003
  3. Dave Sinkula

    Casting (time_t)(-1)

    Dave Sinkula, Mar 2, 2004, in forum: C Programming
    Replies:
    4
    Views:
    630
    Dan Pop
    Mar 2, 2004
  4. j0mbolar

    time_t in seconds

    j0mbolar, Apr 18, 2004, in forum: C Programming
    Replies:
    6
    Views:
    10,233
    Dan Pop
    Apr 19, 2004
  5. Jim Showalter

    stuck on time_t

    Jim Showalter, Aug 8, 2004, in forum: C Programming
    Replies:
    8
    Views:
    2,895
    Emmanuel Delahaye
    Aug 8, 2004
Loading...

Share This Page