Comparing a time in UTC with a time in Eastern time using Python

前端 未结 3 1011
北荒
北荒 2020-12-11 19:32

I\'m trying to compare two times using the Python datetime module, but I can\'t seem to create a timezone-aware time object in UTC.



        
3条回答
  •  旧时难觅i
    2020-12-11 19:53

    I'm guessing that the problem is that UTC is considered to be not-any-timezone, or "offset-naive", perhaps? I'd recommend converting everything to UTC before doing any comparisons.

    You need to know timezones for inputs and outputs, obviously, but you should try to keep your internal representations all in UTC, and maybe just store the timezone of each user and convert when you need to. It will save a lot of headache in the long run.

    Also, you shouldn't do it this way. It's better to use

    timezone.localize(dt)
    

    as explained here: http://pytz.sourceforge.net/#localized-times-and-date-arithmetic

提交回复
热议问题