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

后端 未结 6 1332
孤街浪徒
孤街浪徒 2021-02-05 00:42

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.

<
相关标签:
6条回答
  • 2021-02-05 01:13

    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)

    0 讨论(0)
  • 2021-02-05 01:14

    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.

    0 讨论(0)
  • 2021-02-05 01:21

    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.

    0 讨论(0)
  • 2021-02-05 01:23

    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.

    0 讨论(0)
  • time.time() returns the same value, on any machine running time.time() simultaneously, even though those machines are using different timezones.

    I have two machines, one is using UTC, the other one is using UTC+7.

    I run this script on both machines almost simultaneously (the UTC one is about three seconds earlier):

    import time
    from datetime import datetime
    import pytz
    
    print("GENERATING TIMESTAMP:")
    print("                                           time.time()", int(time.time()))
    print("                            datetime.now().timestamp()", int(datetime.now().timestamp()))
    print("datetime.utcnow().replace(tzinfo=pytz.UTC).timestamp()", int(datetime.utcnow().replace(tzinfo=pytz.UTC).timestamp()))
    print("                         datetime.utcnow().timestamp()", int(datetime.utcnow().timestamp()))
    
    ts = datetime.utcnow().replace(tzinfo=pytz.UTC).timestamp()
    
    print("\nGENERATING DATETIME FROM TIMESTAMP ts =", ts)
    print("                         datetime.fromtimestamp(ts)", datetime.fromtimestamp(ts))
    print("            datetime.fromtimestamp(ts, tz=pytz.UTC)", datetime.fromtimestamp(ts, tz=pytz.UTC))
    print("datetime.fromtimestamp(ts).replace(tzinfo=pytz.UTC)", datetime.fromtimestamp(ts).replace(tzinfo=pytz.UTC))
    

    The UTC machine printed this:

    GENERATING TIMESTAMP:
                                               time.time() 1601469475
                                datetime.now().timestamp() 1601469475
    datetime.utcnow().replace(tzinfo=pytz.UTC).timestamp() 1601469475
                             datetime.utcnow().timestamp() 1601469475
    
    GENERATING DATETIME FROM TIMESTAMP ts = 1601469475.713351
                             datetime.fromtimestamp(ts) 2020-09-30 12:37:55.713351
                datetime.fromtimestamp(ts, tz=pytz.UTC) 2020-09-30 12:37:55.713351+00:00
    datetime.fromtimestamp(ts).replace(tzinfo=pytz.UTC) 2020-09-30 12:37:55.713351+00:00
    

    The UTC+7 machine printed this:

    GENERATING TIMESTAMP:
                                               time.time() 1601469478
                                datetime.now().timestamp() 1601469478
    datetime.utcnow().replace(tzinfo=pytz.UTC).timestamp() 1601469478
                             datetime.utcnow().timestamp() 1601444278
    
    GENERATING DATETIME FROM TIMESTAMP ts = 1601469478.637603
                             datetime.fromtimestamp(ts) 2020-09-30 19:37:58.637603
                datetime.fromtimestamp(ts, tz=pytz.UTC) 2020-09-30 12:37:58.637603+00:00
    datetime.fromtimestamp(ts).replace(tzinfo=pytz.UTC) 2020-09-30 19:37:58.637603+00:00
    

    You can see that time.time() is supposed to return the same value on any machine regardless of the timezone used by that machine.

    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?

    Yes, the second line from the bottom of experiment result shows that.

    0 讨论(0)
  • 2021-02-05 01:26

    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.

    0 讨论(0)
提交回复
热议问题