Skip to content
This repository

A fix to remove timezone information from dates before inserting in the DB. #16

Open
wants to merge 2 commits into from

3 participants

Adam Charnock Patrick Altman Jannis Leidel
Adam Charnock

A fix to remove timezone information from dates before inserting in the DB.

Some DBs do not support timezones on dates (notably, MySQL). We therefore have to remove the timezone information before storing the value. This is not too bad as we convert to the default timezone in get_db_prep_save(), and add the default timezone information back in get_db_prep_lookup().

I have tried to code this in a way that will be both backwards compatible (no connection parameter in Django < 1.3) and not hard-wired to MySQL specifically.

Of course, if the user changes the default timezone while there is data in the database, it will cause the dates in the database to skew. This is not ideal, but there is not much we can do about this apart from either forcing all dates to be stored in UTC, or putting a suitably large warning somewhere in the docs.

For my part, I wouldn't not mind forcing to UTC, but I am in the UK, so the impact of that for me is probably going to be less.

Adam Charnock A fix to remove timezone information from dates before inserting in t…
…he DB.


Some DBs do not support timezones on dates (notably, MySQL). We therefore have to remove the timezone information before storing the value. This is not too bad as we convert to the default timezone in `get_db_prep_save()`, and add the default timezone information back in `get_db_prep_lookup()`.

I have tried to code this in a way that will be both backwards compatible (no `connection` parameter in Django < 1.3) and not hard-wired to MySQL specifically.

Of course, if the user changes the default timezone while there is data in the database, it will cause the dates in the database to skew. This is not ideal, but there is not much we can do about this apart from either forcing all dates to be stored in UTC, or putting a suitably large warning somewhere in the docs.

For my part, I wouldn't not mind forcing to UTC, but I am in the UK, so the impact of that for me is probably going to be less.
5ce210f
Adam Charnock Correcting a (stupid) error in my previous commit. f20d4c0
Patrick Altman

This looks like a decent solution. I want Brian Rosner's approval though before I accept as he may have other ideas.

We all use postgresql so haven't experienced this problem before. :)

Jannis Leidel

I would argue that this is not needed anymore now that Django has full and transparent support for timezones. django-timezones probably needs to be updated to work in those cases, of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Sep 15, 2011
Adam Charnock A fix to remove timezone information from dates before inserting in t…
…he DB.


Some DBs do not support timezones on dates (notably, MySQL). We therefore have to remove the timezone information before storing the value. This is not too bad as we convert to the default timezone in `get_db_prep_save()`, and add the default timezone information back in `get_db_prep_lookup()`.

I have tried to code this in a way that will be both backwards compatible (no `connection` parameter in Django < 1.3) and not hard-wired to MySQL specifically.

Of course, if the user changes the default timezone while there is data in the database, it will cause the dates in the database to skew. This is not ideal, but there is not much we can do about this apart from either forcing all dates to be stored in UTC, or putting a suitably large warning somewhere in the docs.

For my part, I wouldn't not mind forcing to UTC, but I am in the UK, so the impact of that for me is probably going to be less.
5ce210f
Adam Charnock Correcting a (stupid) error in my previous commit. f20d4c0
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 10 additions and 0 deletions. Show diff stats Hide diff stats

  1. 10  timezones/fields.py
10  timezones/fields.py
@@ -93,6 +93,15 @@ def get_db_prep_save(self, value, connection=None):
93 93
                 value = default_tz.localize(value)
94 94
             else:
95 95
                 value = value.astimezone(default_tz)
  96
+        
  97
+        # Make sure the DB can handle timezone-aware dates (hint: MySQL cannot)
  98
+        if connection and getattr(connection, "ops") and getattr(connection.ops, "value_to_db_datetime"):
  99
+            try:
  100
+                connection.ops.value_to_db_datetime(value)
  101
+            except ValueError:
  102
+                # DB doesn't support timezones, so strip it off
  103
+                value = value.replace(tzinfo=None)
  104
+        
96 105
         return super(LocalizedDateTimeField, self).get_db_prep_save(value, connection=connection)
97 106
     
98 107
     def get_db_prep_lookup(self, lookup_type, value, connection=None, prepared=None):
@@ -104,6 +113,7 @@ def get_db_prep_lookup(self, lookup_type, value, connection=None, prepared=None)
104 113
             value = default_tz.localize(value)
105 114
         else:
106 115
             value = value.astimezone(default_tz)
  116
+        
107 117
         return super(LocalizedDateTimeField, self).get_db_prep_lookup(lookup_type, value, connection=connection, prepared=prepared)
108 118
 
109 119
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.