SQLAlchemy DateTime timezone

前端 未结 4 1155
情话喂你
情话喂你 2021-01-31 07:54

SQLAlchemy\'s DateTime type allows for a timezone=True argument to save a non-naive datetime object to the database, and to return it as such. Is there

4条回答
  •  眼角桃花
    2021-01-31 08:25

    a solution is given in this question’s answer:

    you can circumvent that by storing all (date)time objects in your database in UTC, and converting the resulting naïve datetime objects to aware ones on retrieval.

    the only drawback is that you lose timezone information, but it’s probably a good idea to store your datetime objects in utc, anyway.

    if you care about the timezone information, i’d store it separately, and only convert the utc to local time in the last possible instance (e.g. right before displaying)

    or maybe you don’t need to care after all, and can use local timezone information from the machine you run your progam on, or the user’s browser if it’s a webapp.

提交回复
热议问题