Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: stuck on time_t

Reply
Thread Tools

Re: stuck on time_t

 
 
Jim Showalter
Guest
Posts: n/a
 
      08-11-2004
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[i]);
}
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
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      08-11-2004
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"

 
Reply With Quote
 
 
 
 
SM Ryan
Guest
Posts: n/a
 
      08-11-2004
Emmanuel Delahaye <(E-Mail Removed)> 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.
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      08-11-2004
SM Ryan wrote:
> Emmanuel Delahaye <(E-Mail Removed)> 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 (E-Mail Removed)
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      08-11-2004
On Wed, 11 Aug 2004 11:56:54 -0000, in comp.lang.c , SM Ryan
<(E-Mail Removed)> wrote:

>Emmanuel Delahaye <(E-Mail Removed)> 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 =---
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-12-2004
Joe Wright <(E-Mail Removed)> writes:
> SM Ryan wrote:
> > Emmanuel Delahaye <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      08-12-2004
Mark McIntyre <(E-Mail Removed)> wrote:
# On Wed, 11 Aug 2004 11:56:54 -0000, in comp.lang.c , SM Ryan
# <(E-Mail Removed)> wrote:
#
# >Emmanuel Delahaye <(E-Mail Removed)> 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.
 
Reply With Quote
 
Jim Showalter
Guest
Posts: n/a
 
      08-12-2004
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
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
 
Reply With Quote
 
Jim Showalter
Guest
Posts: n/a
 
      08-12-2004
SM Ryan wrote:

> Emmanuel Delahaye <(E-Mail Removed)> 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
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-12-2004
Mark McIntyre wrote:
> SM Ryan <(E-Mail Removed)> 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


 
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
stuck on time_t Jim Showalter C Programming 8 08-08-2004 03:30 PM
DateTime to time_t bug =?Utf-8?B?TWFyaw==?= ASP .Net 2 05-18-2004 06:11 AM
time_t in seconds j0mbolar C Programming 6 04-19-2004 04:14 PM
Casting (time_t)(-1) Dave Sinkula C Programming 4 03-02-2004 03:58 PM
converting to time_t Anand CS C Programming 1 07-03-2003 03:42 PM



Advertisments