Skip to content
SQLAlchemy type to store aware datetime values
Branch: master
Clone or download
Latest commit 5a3d210 Jan 25, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
sqlalchemy_utc
tests Add utcnow function for generating server timestamps Jan 24, 2018
.coveragerc Convert from module to package and update test processing. Jan 24, 2018
.gitignore
.travis.yml Convert from module to package and update test processing. Jan 24, 2018
CHANGES.rst Release 0.10.0 Jan 25, 2018
LICENSE Initial commit Jun 21, 2016
README.rst
setup.cfg Initial commit Jun 21, 2016
setup.py
tox.ini

README.rst

SQLAlchemy-Utc

https://badge.fury.io/py/SQLAlchemy-Utc.svg? https://travis-ci.org/spoqa/sqlalchemy-utc.svg?branch=master https://codecov.io/github/spoqa/sqlalchemy-utc/coverage.svg?branch=master

This package provides a drop-in replacement of SQLAlchemy's built-in DateTime type with timezone=True option enabled. Although SQLAlchemy's built-in DateTime type provides timezone=True option, since some vendors like SQLite and MySQL don't provide timestamptz data type, the option doesn't make any effect on these vendors.

UtcDateTime type is equivalent to the built-in DateTime with timezone=True option enabled on vendors that support timestamptz e.g. PostgreSQL, but on SQLite or MySQL, it shifts all datetime.datetime values to UTC offset before store them, and returns always aware datetime.datetime values through result sets.

Long story short, UtcDateTime does:

  • take only aware datetime.datetime,
  • return only aware datetime.datetime,
  • never take or return naive datetime.datetime,
  • ensure timestamps in database always to be encoded in UTC, and
  • work as you'd expect.

A SQLAlchemy helper function, utcnow(), is provided as an alternative to func.now() for generating UtcDateTime values on the server. For example: Column('time', UtcDateTime(), default=utcnow()).

Written by Hong Minhee at Spoqa, and distributed under MIT license.

You can’t perform that action at this time.