How to make an unaware datetime timezone aware in python

前端 未结 12 2315
一向
一向 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 09:05

    In the format of unutbu's answer; I made a utility module that handles things like this, with more intuitive syntax. Can be installed with pip.

    import datetime
    import saturn
    
    unaware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0)
    now_aware = saturn.fix_naive(unaware)
    
    now_aware_madrid = saturn.fix_naive(unaware, 'Europe/Madrid')
    

提交回复
热议问题