Skip to content

Instantly share code, notes, and snippets.

@inklesspen
Created June 24, 2015 00:05
Show Gist options
  • Save inklesspen/90b554c864b99340747e to your computer and use it in GitHub Desktop.
Save inklesspen/90b554c864b99340747e to your computer and use it in GitHub Desktop.
from sqlalchemy import DateTime
from sqlalchemy.types import TypeDecorator
class AwareDateTime(TypeDecorator):
"""
A DateTime type which can only store tz-aware DateTimes
"""
impl = DateTime(timezone=True)
def process_bind_param(self, value, dialect):
if isinstance(value, datetime.datetime) and value.tzinfo is None:
raise ValueError("{!r} must be TZ-aware".format(value))
return value
def __repr__(self):
return 'AwareDateTime()'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment