tzaware_datetime
Need to use timezone-aware datetime values in the sqlalchemy ORM, but want to store the UTC values? Use tzaware_datetime.
Description
tzaware_datetime uses sqlalchemy's composite columns feature to store a UTC datetime value as well as the offset, in seconds, of the given datetime. This allows sqlalchemy to sort dates by a common timezone, and retains the "local" time.
Usage
See demo.py for a complete example.
- from tzaware_datetime import TZAwareDateTime
- Add three tzaware_datetime-specific columns when setting up the Table object (or call helper.append_columns(newtable, columnname))
- Add the composite column mapping to the mapper (or call helper.get_mapper_definition(newtable, columnname) when preparing the mapper)
- Add values to the new column by adding a TZAwareDateTime() object, with the realdate parameter set.
- Retrieve values by reading yourtable.tzawaredate.realdate (for the timezone-specific date), or yourtable.tzawaredate.utcdt for the UTC date.
Requirements
- Python: tested on 2.5
- sqlalchemy v0.5.6 or higher
- 3rd-party: dateutil v1.5 or higher (PyPI)