Is Python's time.time() timezone specific?

2020-05-24 20:01发布

Apologies for asking too basic question but I couldn't get it cleared after reading docs. It just seems that I am missing or have misunderstood something too basic here.

Does calling time.time() from different timezones, at the same time produce different results? This maybe comes down to definition of epoch, which on the docs (and on my not-so-deep search on the Internet), has no mentions of the timezone.

Also, suppose time.time() has been called from places with different timezones, and converted to UTC datetimes on their machines, will they all give same UTC time?

5条回答
闹够了就滚
2楼-- · 2020-05-24 20:09

From the documentation:

Most of the functions defined in this module call platform C library functions with the same name. It may sometimes be helpful to consult the platform documentation, because the semantics of these functions varies among platforms.

http://docs.python.org/library/time.html?highlight=time.time#module-time

So the answer is: it depends.

查看更多
欢心
3楼-- · 2020-05-24 20:16

time.time() returns the number of seconds since the UNIX epoch began at 0:00 UTC, Jan 1, 1970. Assuming the machines have their clocks set correctly, it returns the same value on every machine.

查看更多
疯言疯语
4楼-- · 2020-05-24 20:20

The return value should be the same, since it's the offset in seconds to the UNIX Epoch.

That being said, if you convert it to a Date using different timezones, the values will, of course, differ.

If, from those Dates, you convert each of them to UTC, then the result has to be the same.

查看更多
再贱就再见
5楼-- · 2020-05-24 20:28

Per the documentation:

Return the time in seconds since the epoch as a floating point number. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.

Wikipedia says about "Unix epoch":

The Unix epoch is the time 00:00:00 UTC on 1 January 1970 (or 1970-01-01T00:00:00Z ISO 8601).

and it continues

There is a problem with this definition, in that UTC did not exist in its current form until 1972; this issue is discussed below. For brevity, the remainder of this section uses ISO 8601 date format, in which the Unix epoch is 1970-01-01T00:00:00Z.

Time and date is fun.

Little known fact: The time zone of Switzerland before 1894 was 34:08 (34 minutes and 8 seconds). After June 1894, it was updated to 29:44. (link)

查看更多
劫难
6楼-- · 2020-05-24 20:29

Yes, time.time() returns the number of seconds since an unspecified epoch. Note that on most systems, this does not include leap seconds, although it is possible to configure your system clock to include them. On cpython, time.time is implemented as a call to the C function time, which per §27.23.2.4.2 of the C standard does not have to use a specified epoch:

The time function determines the current calendar time. The encoding of the value is unspecified.

On virtually every OS (including Linux, Mac OSX, Windows, and all other Unixes), the epoch is 1970-1-1, 00:00 UTC, and on these systems time.time is timezone-independent.

查看更多
登录 后发表回答