Changing timezone on an existing Django project

2019-06-23 01: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).

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.

1条回答
爷、活的狠高调
2楼-- · 2019-06-23 01:34

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)
查看更多
登录 后发表回答