Changing timezone on an existing Django project

时间:2021-05-28 16:56:00

Like an idiot, I completely overlooked the timezone setting when I first built an application that collects datetime data.

就像一个白痴,当我第一次构建一个收集日期时间数据的应用程序时,我完全忽略了时区设置。

It wasn't an issue then because all I was doing was "time-since" style comparisons and ordering. Now I need to do full reports that show the actual datetime and of course, they're all stored at America/Chicago (the ridiculous Django default).

这不是一个问题,因为我所做的只是“时间 - 风格”比较和排序。现在我需要做完整的报告来显示实际的日期时间,当然,它们都存储在America / Chicago(荒谬的Django默认值)。

So yes. I've got a medium sized database full of these dates that are incorrect. I want to change settings.TIME_ZONE to 'UTC' but that doesn't help my existing data.

是的。我有一个中等大小的数据库,这些日期不正确。我想将settings.TIME_ZONE更改为“UTC”,但这对我现有的数据没有帮助。

What's the best (read: easiest, quickest) way to convert all that Model data en-masse?

什么是最好(阅读:最简单,最快捷)的方式来转换所有模型数据集体?

(All the data is from within the past two months, so there's thankfully no DST to convert)

(所有数据都来自过去两个月内,所以谢天谢地没有DST转换)

This project is currently on SQLite but I have another project on PostgreSQL with a similar problem that I might want to do the same on before DST kicks in... So ideally a DB-agnostic answer.

这个项目目前在SQLite上,但我在PostgreSQL上有另一个项目有类似的问题,我可能想在DST开始之前做同样的事情......所以理想情况下是一个与数据库无关的答案。

1 个解决方案

#1


3  

I would do a mass update to the database tables by adding or subtracting hours to/from the datetime fields.

我会通过在datetime字段中添加或减去小时来对数据库表进行批量更新。

Something like this works in SQL Server, and adds 2 hours to the date:

这样的东西在SQL Server中工作,并增加2小时的日期:

update tblName set date_field = dateadd("hh", 2, data_field)

#1


3  

I would do a mass update to the database tables by adding or subtracting hours to/from the datetime fields.

我会通过在datetime字段中添加或减去小时来对数据库表进行批量更新。

Something like this works in SQL Server, and adds 2 hours to the date:

这样的东西在SQL Server中工作,并增加2小时的日期:

update tblName set date_field = dateadd("hh", 2, data_field)