How do you convert a naive datetime to DST-aware datetime in Python?

前端 未结 2 1201
情歌与酒
情歌与酒 2021-01-01 19:05

I\'m currently working on the backend for a calendaring system that returns naive Python datetimes. The way the front end works is the user creates various calendar events,

2条回答
  •  借酒劲吻你
    2021-01-01 19:56

    Pytz's localize function can do this: http://pytz.sourceforge.net/#localized-times-and-date-arithmetic

    from datetime import datetime
    import pytz    
    
    tz = pytz.timezone('US/Pacific')
    naive_dt = datetime(2020, 10, 5, 15, 0, 0) 
    utc_dt = tz.localize(naive_dt, is_dst=None).astimezone(pytz.utc)
    # -> 2020-10-05 22:00:00+00:00
    

提交回复
热议问题