更改现有 Django 项目的时区

发布于 2024-07-15 08:12:45 字数 463 浏览 6 评论 0原文

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

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

所以是的。 我有一个中等规模的数据库,其中充满了这些不正确的日期。 我想将 settings.TIME_ZONE 更改为 'UTC' 但这对我现有的数据没有帮助。

批量转换所有模型数据的最佳(阅读:最简单、最快)方法是什么?

(所有数据都来自过去两个月内,所以庆幸的是没有 DST 需要转换)

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

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).

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.

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)

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶舞 2024-07-22 08:12:45

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

类似这样的事情在 SQL Server 中有效,并为日期添加 2 小时:

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

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

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

update tblName set date_field = dateadd("hh", 2, data_field)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文