Django DateTimeZoneField

In a scheduling project I’ve been working on, I’ve come across the need to ingest and present a lot of datetime information. The input will include many different timezones and may not reflect the user’s timezone. Thus the user must specify the timezone in the datetime stamp field. You’d think Django would handle this out of the box, but it really doesn’t. My solution is to have the user explicitly specify the timezone in the datetime field via a neat widget, and have the server side coerce that data into UTC before storing it. As long as your django instance has timezone settings configured, this will let you store a naïve datetime that is guaranteed to be UTC. There are benefits to this – datetime arithmetic becomes simple and the presentation layer can handle converting to other timezones.

My solution is to create a new field class that inherits from forms.DateTimeField, and simply override the way it initially converts the string value to a datetime stamp. I use the dateutil parser to give the datetime string a bit of wiggle room , though in most cases the javascript widget will produce the input. The parsed datetime object will be timezone aware, but is then converted to UTC before django handles form validation / input cleaning. I didn’t need to, but you could also explicitly remove the tzinfo at this stage.

Define a new field type in your forms.py:

forms.py - field definition

Python

1

2

3

4

5

6

7

classDateTimeZoneField(forms.DateTimeField):

defto_python(self,value):

"Respect the TZ input and immediately convert the value to UTC"

ifnotvalue:

returnNone

dtz=dateutil.parser.parse(value).astimezone(pytz.utc)

returndtz

When defining your form / modelform, explicitly specify the field and widget, i.e: