Is time_t returned by time() zone specific?
I am just new to <time.h>
and have a question regarding to time_t
and time()
.
I read the function time() documented as follows:
time_t time ( time_t * timer ); Get current time
Get the current calendar time as a time_t object.
The function returns this value, and if the argument is not a null pointer, the value is also set to the object pointed by timer.
The documentation does not talk about time zone.
Thus, for the following C++ code:
time_t t = time(NULL);
If two machines, one in US and the other one in UK, both execute the function call time(NULL)
at the same time, will the returned time_t objects be identical?
Will time() return开发者_运维百科s a value regardless of time zone?
No it's not zone specific. It returns a value that's a count of the number of seconds since 1 Jan 1970 in UTC, ignoring leap seconds. So (in principle) if two machines execute the call at the exact same time, the value returned will be the same, even if they work in two separate time zones.
Well, it's documented to return a time_t
- which is documented with:
It is almost universally expected to be an integral value representing the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC. This is due to historical reasons, since it corresponds to a unix timestamp, but is widely implemented in C libraries across all platforms.
So strictly speaking it's not guaranteed cross-platform by the looks of it, but in practice can be treated in a cross-platform way and is in UTC.
(Of course there will be multiple sources of documentation for time_t
to start with... I'm not sure what exactly can be deemed definitive here.)
time_t
values are independent of time zone differences, as they count time from epoch. If you want to have a local calendar time you can take that time_t
value and pass it to the localtime()
function, which returns a pointer to struct tm
with your local time.
No; this function returns the second count from 00:00:00 UTC on 1 January 1970.
Wikipedia
According to the latest standard of the C programming language issued at 2011:
1. The *time* function determines the current **calendar time**.
2. The encoding of the value is unspecified.
3. The *time* function returns the implementation’s best approximation
to the current calendar time.
where calendar time in terms of the standard represents the current
date (according to the Gregorian calendar) and time
in contrast to local
time, which is the calendar time expressed for some specific time zone
.
And The range and precision of times representable in clock_t and time_t are implementation-defined.
As a result:
- time_t values returned by time() from the C library must be not time zone specific. If it is not true, the C library implementation doesn't comply with standard and this situation can be considered as a bug in library.
- time_t value encoding unspecified! It can be specified in the POSIX standard, but definitely not specified in C standard. Due to this you must not rely on the assumptions about its implementation details such as that it counts time in the one second resolution or that it is an integer containing number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC. Use appropriate functions from C Standard Library such as gmtime() and localtime() instead to convert time_t into struct tm and get access to the time stamp details. At least if your application is not considered to be limited only by *NIX systems.
This will give you your "local epoch":
time_t t = time(NULL);
t = timegm(localtime(&t);
精彩评论