attribute 'tzinfo' of 'datetime.datetime' objects is not writable

后端 未结 3 592
一生所求
一生所求 2021-02-05 02:01

How do I set the timezone of a datetime instance that just came out of the datastore?

When it first comes out it is in UTC. I want to change it to EST.

I\'m try

3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-05 02:54

    If you're receiving a datetime that's in EST, but doesn't have its tzinfo field set, use dt.replace(tzinfo=tz) to assign a tzinfo without modifying the time. (Your database should be doing this for you.)

    If you're receiving a datetime that's in UDT, and you want it in EST, then you need astimezone. http://docs.python.org/library/datetime.html#datetime.datetime.astimezone

    In the vast majority of cases, your database should be storing and returning data in UDT, and you shouldn't need to use replace (except possibly to assign a UDT tzinfo).

提交回复
热议问题