Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is time_t returned by time() zone specific?

Tags:

c++

c

timezone

time

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?

like image 732
SiLent SoNG Avatar asked Sep 15 '11 06:09

SiLent SoNG


People also ask

What does time_t mean in C++?

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.

How does Mktime work?

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).


2 Answers

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.

like image 70
sashang Avatar answered Oct 22 '22 11:10

sashang


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.)

like image 37
Jon Skeet Avatar answered Oct 22 '22 11:10

Jon Skeet