views:

5639

answers:

8

I searched in linux box and saw it being typedef to

typedef __time_t time_t;

But could not find the __time_t definition.

A: 

time_t is an unsigned long

Tree77
is that guaranteed? I thought the point of a time_t was so that you didn't depend on that fact.
rmeador
please cite sources
Jason S
@rmeador: it is not guaranteed by the C standard. It is a very common implementation of time_t, though.
Lars Wirzenius
+1  A: 

Under Visual Studio 2008, it defaults to an __int64 unless you define _USE_32BIT_TIME_T. You're better off just pretending that you don't know what it's defined as, since it can (and will) change from platform to platform.

Eclipse
That usually works, but if your program is meant to keep track of things that will happen 30 years from now, it's pretty important that you *not* have a signed 32-bit time_t.
Rob Kennedy
@Rob, bah, leave it! We'll just start running around like headless chickens in 2036, the same as we did for Y2K. Some of us will make a bucketload of money from being Y2k38 consultants, Leonard Nimoy will bring out another hilarious book about how we should all go and hide in the forest...
paxdiablo
... and it'll all blow over, the public wondering what all the fuss was about. I may even come out of retirement to make some money for the kids' inheritance :-).
paxdiablo
BTW, we only found one Y2K bug and that was a web page which listed the date as Jan 1, 19100. Exercise for the reader as to why...
paxdiablo
@Pax - very nice!
Eclipse
If the event to happen in 30 years is "expire this backup," then you might be in trouble NOW, not in 2038. Add 30 years to today's 32-bit time_t, and you get a date in the past. Your program looks for events to process, finds one that's overdue (by 100 years!), and executes it. Oops, no more backup.
Rob Kennedy
+6  A: 

[root]# cat time.c

#include <time.h>

int main(int argc, char** argv)
{
        time_t test;
        return 0;
}

[root]# gcc -E time.c | grep __time_t

typedef long int __time_t;

It's defined in $INCDIR/bits/types.h through:

# 131 "/usr/include/bits/types.h" 3 4
# 1 "/usr/include/bits/typesizes.h" 1 3 4
# 132 "/usr/include/bits/types.h" 2 3 4
Quassnoi
+1  A: 

It's a 32-bit signed integer type on most legacy platforms. However, that causes your code to suffer from the year 2038 bug. So modern C libraries should be defining it to be a signed 64-bit int instead, which is safe for a few billion years.

+7  A: 

The time_t Wikipedia article article sheds some light on this. The bottom line is that the type of time_t is not guaranteed in the C specification.

The time_t datatype is a data type in the ISO C library defined for storing system time values. Such values are returned from the standard time() library function. This type is a typedef defined in the standard header. ISO C defines time_t as an arithmetic type, but does not specify any particular type, range, resolution, or encoding for it. Also unspecified are the meanings of arithmetic operations applied to time values.

Unix and POSIX-compliant systems implement the time_t type as a signed integer (typically 32 or 64 bits wide) which represents the number of seconds since the start of the Unix epoch: midnight UTC of January 1, 1970 (not counting leap seconds). Some systems correctly handle negative time values, while others do not. Systems using a 32-bit time_t type are susceptible to the Year 2038 problem.

William Brendel
Note, however, that time_t values are usually only stored in memory, not on disk. Instead, time_t is converted to text or some other portable format for persistent storage. That makes the Y2038 problem to not really be a problem.
Lars Wirzenius
+5  A: 

...how much time could a time_t time if a time_t could time time...?

Jason S
A: 

..how much time could a time_t time if a time_t could time time...?

TIME_MAX

anon
A: 

I could do a

time_t current_time = time(0);

and measure off of that ... but is there a preferred way ... mainly this is a best practices kind of question ....

Xofo