time() question

S

Stu

I knwo that clock = time(NULL) returns the current system time in
seconds since 00:00:00 UTC, January 1, 1970.

Is there a way I can do this for a value other than the system time.
For example, lets say I want to pass "20070101" (a date that has
passed or "20071231" (a future date).

What would be the equivelant calls. A code snippet would be greate.
Thanks in advance to all that answer
 
R

Roberto Waltman

Stu said:
... clock = time(NULL) returns the current system time in
seconds ..
Is there a way I can do this for a value other than the system time.
...
What would be the equivelant calls. A code snippet would be greate.

Look at the recent thread "Unix time (predating epoch of Jan-1970)"

Roberto Waltman

[ Please reply to the group,
return address is invalid ]
 
R

Richard Heathfield

Stu said:
I knwo that clock = time(NULL) returns the current system time in
seconds since 00:00:00 UTC, January 1, 1970.

Three problems with that. Firstly, since there is a standard C library
function called clock(), using the name as an identifier is a bad idea.

Secondly, nothing in the Standard says it has to be seconds.

Thirdly, nothing in the standard mentions any particular epoch date.
Is there a way I can do this for a value other than the system time.
For example, lets say I want to pass "20070101" (a date that has
passed or "20071231" (a future date).

#include <time.h>

time_t foo(int year, int month, int day)
{
struct tm tmt = {0};
/* add validation here */
tmt.tm_year = year - 1900;
tmt.tm_mon = month - 1;
tmt.tm_mday = day;
return mktime(&tmt);
}
 
W

Walter Roberson

I knwo that clock = time(NULL) returns the current system time in
seconds since 00:00:00 UTC, January 1, 1970.

Not correct.

"The time function determines the current calendar time.
The encoding of the value is unspecified."
Is there a way I can do this for a value other than the system time.
For example, lets say I want to pass "20070101" (a date that has
passed or "20071231" (a future date).

There is no standard call to parse a time string.

I suggest that you mktime(&X) where X is a struct tm
whose parts you have filled in.

If you want the time in seconds since any particular fixed time,
then mktime() for the test time, and mktime() for the base era time,
and difftime() the two results. difftime() returns the difference
expressed in seconds (as a double).
 
C

Chris Torek

time_t foo(int year, int month, int day)
{
struct tm tmt = {0};
/* add validation here */
tmt.tm_year = year - 1900;
tmt.tm_mon = month - 1;
tmt.tm_mday = day;
return mktime(&tmt);
}

This is not *wrong*, but it may be better in general to set
tmt.tm_isdst to -1 first, to tell the mktime() routine to do its
best to figure out whether DST is in use. Setting tmt.tm_isdst to
0 means "DST is not in effect for the given time even if it would
normally be in effect". In this case, the "normalized" time
(computed and stored into tmt by mktime, and returned encoded as
a time_t, if all goes well) may differ from the supplied time,
typically by one hour. If DST goes into effect at an "unusual"
time (other than the typical 2 AM, that is), this could even shift
the result by a whole day (e.g., from 12:30 AM on the day given as
arguments to 11:30 PM on the previous day).

(More likely, of course, is that the routine above might get
expanded to take hours, minutes, and seconds, and then whoever
is using it would get a surprise for "times during which DST is
normally in effect".)
 
R

Richard Heathfield

Chris Torek said:
This is not *wrong*, but it may be better in general to set
tmt.tm_isdst to -1 first,

s/may be/is/

Thanks for the catch, Chris, but isn't that kind of Dave Thompson's job?
He'll be going straight to his union rep...

<snip>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top