How to make an unaware datetime timezone aware in python

前端 未结 12 2070
一向
一向 2020-11-22 08:39

What I need to do

I have a timezone-unaware datetime object, to which I need to add a time zone in order to be able to compare it with other timezon

12条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 08:59

    I wrote this at Nov 22 2011 , is Pyhton 2 only , never checked if it work on Python 3

    I had use from dt_aware to dt_unaware

    dt_unaware = dt_aware.replace(tzinfo=None)
    

    and dt_unware to dt_aware

    from pytz import timezone
    localtz = timezone('Europe/Lisbon')
    dt_aware = localtz.localize(dt_unware)
    

    but answer before is also a good solution.

提交回复
热议问题