Changing timezone on an existing Django project
Asked Answered
M

1

5

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.

Molluscoid answered 27/3, 2009 at 13:48 Comment(3)
What database are you using, btw?Skyscraper
SQLite but it's entirely possible I'll want to do the same thing on another project that I used Postgres for... Something DB-agnostic would be ideal =)Molluscoid
SQLite stores everything as text. Some databases, however, store datetime in UTC internally. Oracle, for example, translates the UTC in the database to localtime. You don't have to update anything to change the displayed localtime.Drawn
G
3

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)
Gobbler answered 27/3, 2009 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.