Is time_t a time return () of a specific zone?

I am only new to <time.h> and ask a question regarding time_t and time() .

I read the time () function registered 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 indicated by the timer.

The documentation does not mention the time zone.

So for the following C ++ code:

time_t t = time (NULL);

If two machines, one in the US and one in the UK, simultaneously call the time(NULL) function, will the returned time_t objects be identical?

Will time () return a value regardless of time zone?

+6
source share
6 answers

No, this is not a zone. It returns a value that counts the number of seconds since January 1, 1970 in UTC, ignoring the seconds of the jump. Therefore (in principle), if two machines make a call at the same time, the return value will be the same even if they work in two separate time zones.

+6
source

Well, he documented the return of time_t - documented with:

An integral value is expected almost universally, representing the number of seconds elapsed from 00:00 hours, January 1, 1970 UTC. This is due to historical reasons, as it corresponds to the unix timestamp, but is widely implemented in C libraries on all platforms.

Strictly speaking, this does not guarantee a cross-platform in appearance, but in practice it can be viewed in a cross-platform way and is in UTC.

(Of course, for starters there will be several documentation sources for time_t ... I'm not sure what exactly can be considered final here.)

+2
source
Values

time_t does not depend on time zone differences, since they count the time from epoch . If you want to have local calendar time, you can take this time_t value and pass it to localtime() , which returns a pointer to struct tm with your local time.

+2
source

Not; this function returns the second account from 00:00:00 UTC on January 1, 1970.

Wikipedia

+1
source

According to the latest C programming language standard released in 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 the calendar time in terms of standard represents the current date (according to the Gregorian calendar) and time as opposed to the 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 should not be time zone specific. If this is not the case, the implementation of the C library does not meet the standard, and this situation can be considered an error in the library.
  • the encoding of the time_t value is not defined! It can be specified in the POSIX standard, but definitely not specified in the C standard. Because of this, you should not rely on assumptions about its implementation details, for example, that it counts the resolution time in one second or that it is an integer, containing the number of seconds elapsed from 00:00 hours, January 1, 1970 UTC. Use the appropriate functions from the C Standard Library, such as gmtime () and localtime (), instead, to convert time_t to struct tm and access the timestamp data. At least if your application is not considered limited to * NIX systems only.
+1
source

This will give you your “local era”:

 time_t t = time(NULL); t = timegm(localtime(&t); 
0
source

Source: https://habr.com/ru/post/897366/


All Articles