I’m working on a small CherryPy web service that among other things saves timestamps to a database. The timestamp is in RFC 3339 format (like
2007-07-31T16:05:00.000-05:00), and I needed to store the timezone.
Luckily mxDateTime and SQLObject’s DateTimeCol both support full dates with times and time zone. Unfortunately its not immediately obvious from SQLObject’s lackluster documentation how to use mxDateTime instead of Python’s built-in datetime.
A little searching brought me to a mailing list post about how to use mxDateTime by default in SQLObject. (I don’t know why the sample code includes the conditional as I would think you’d want your code to outright fail if you’re unable to use the datetime library you expect.)
So my model’s code looks something like this:
from sqlobject import * from sqlobject import col col.default_datetime_implementation = MXDATETIME_IMPLEMENTATION class Foo(SQLObject): timestamp = DateTimeCol(default=DateTimeCol.now)
Then my parsing code looks something like this:
import model from mx import DateTime timestamp = '2007-07-31T16:05:00.000-05:00' bar = model.Foo(timestamp=DateTime.DateTimeFrom(timestamp)) print 'UTC Timestamp:', bar.timestamp print 'Local Timestamp:', bar.timestamp.localtime()
Basically once you use the magic line
col.default_datetime_implementation = MXDATETIME_IMPLEMENTATION, everything Just Works.