Difference between Python datetime vs time modules

前端 未结 4 937
遇见更好的自我
遇见更好的自我 2020-11-28 21:59

I am trying to figure out the differences between the datetime and time modules, and what each should be used for.

I know that dateti

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 22:33

    Stick to time to prevent DST ambiguity.

    Use exclusively the system time module instead of the datetime module to prevent ambiguity issues with daylight savings time (DST).

    Conversion to any time format, including local time, is pretty easy:

    import time
    t = time.time()
    
    time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(t))
    '2019-05-27 12:03 CEST'
    
    time.strftime('%Y-%m-%d %H:%M %Z', time.gmtime(t))
    '2019-05-27 10:03 GMT'
    

    time.time() is a floating point number representing the time in seconds since the system epoch. time.time() is ideal for unambiguous time stamping.

    If the system additionally runs the network time protocol (NTP) dæmon, one ends up with a pretty solid time base.

    Here is the documentation of the time module.

提交回复
热议问题