Python: Figure out local timezone

前端 未结 18 1700
野性不改
野性不改 2020-12-07 15:50

I want to compare UTC timestamps from a log file with local timestamps. When creating the local datetime object, I use something like:

>>&         


        
18条回答
  •  旧时难觅i
    2020-12-07 16:04

    Avoiding non-standard module (seems to be a missing method of datetime module):

    from datetime import datetime
    utcOffset_min = int(round((datetime.now() - datetime.utcnow()).total_seconds())) / 60   # round for taking time twice
    utcOffset_h = utcOffset_min / 60
    assert(utcOffset_min == utcOffset_h * 60)   # we do not handle 1/2 h timezone offsets
    
    print 'Local time offset is %i h to UTC.' % (utcOffset_h)
    

提交回复
热议问题