datetime and timezone conversion with pytz - mind blowing behaviour

后端 未结 1 523
臣服心动
臣服心动 2020-12-24 12:38

I\'m trying to convert timezone aware datetime object to UTC and then back to it\'s original timezone. I have a following snippet

t = datetime(
         


        
相关标签:
1条回答
  • 2020-12-24 13:26

    The documentation http://pytz.sourceforge.net/ states "Unfortunately using the tzinfo argument of the standard datetime constructors 'does not work' with pytz for many timezones." The code:

    t = datetime(
        2013, 5, 11, hour=11, minute=0,
        tzinfo=pytz.timezone('Europe/Warsaw')
    )
    

    doesn't work according to this, instead you should use the localize method:

    t = pytz.timezone('Europe/Warsaw').localize(
            datetime(2013, 5, 11, hour=11, minute=0))
    
    0 讨论(0)
提交回复
热议问题