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

后端 未结 6 1402
孤街浪徒
孤街浪徒 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:23

    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.

提交回复
热议问题