Skip to content

Timezone Support

jmah8 edited this page Jul 29, 2022 · 1 revision

If your Django app had DateTimeFields before setting USE_TZ=True you will need to manually migrate DATETIME2 columns to DATETIMEOFFSET and convert local time to UTC. Since this driver uses DATETIME2 as the underlying column type for DateTimeField when USE_TZ=False you wouldn't be able to take advantage of timezones without migrating. One way of doing this is to run

ALTER TABLE TABLENAME
   ALTER COLUMN DATETIMEFIELD DATETIMEOFFSET;

UPDATE TABLENAME
   SET DATETIMEFIELD = TODATETIMEOFFSET(DATETIMEFIELD, XXX) AT TIME ZONE 'UTC'

replacing TABLENAME with the table name of the table that has the DATETIME2 column to migrate, replacing DATETIMEFIELD with the column name of the DATETIME2 field and the XXX with an integer of the time zone offset in minutes or a string of hours and minutes in the format {+|-}TZH:THM. The range is +14 to -14 (in hours). This script would need to be run on all DateTimeField so that all DATETIME2 columns are converted into DATETIMEOFFSET.

For next steps in migrating to USE_TZ = True follow Django's documentation on migrating a project started before timezone support was added

Clone this wiki locally