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() returns a value regardless of time zone?
Description. The C library function time_t time(time_t *seconds) returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds. If seconds is not NULL, the return value is also stored in variable seconds.
The mktime() function converts a stored tm structure (assume to be in job local time) pointed to by time, into a time_t structure suitable for use with other time functions. After the conversion, the time_t structure will be considered Universal Coordinate Time (UTC).
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With