creating a timezone aware datetime object returns a wrong timezone

前端 未结 2 1818
-上瘾入骨i
-上瘾入骨i 2021-01-23 12:11

when I create a timezone aware datetime object for \'US/Eastern\' and print it out, It shows as if my time zone is -4:56 instead of -4:00<

2条回答
  •  一向
    一向 (楼主)
    2021-01-23 12:20

    Have a look at dateutil - you can safely construct the tz-aware datetime object using your originally intended method:

    import datetime
    import dateutil
    
    obj = datetime.datetime(2020, 7, 1, 9, 30, tzinfo=dateutil.tz.gettz('US/Eastern'))
    print(obj)
    # 2020-07-01 09:30:00-04:00
    

    In Python 3.9, there will be zoneinfo as part of the standard lib for that task.

提交回复
热议问题