Every DB has its own method for extracting parts out of a datetime.
Here are a few:
-Conor
[1] http://www.postgresql.org/docs/8.4/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
[2] http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
[3] http://www.sqlite.org/lang_datefunc.html
The collection of functions that are truly "generic" is quite small right now. Date arithmetic and calendar functions in particular vary dramatically in their implementation across backends, and a comprehensive abstraction layer for them would be a large undertaking with a lot of controversial points.
So for this use case the most straight ahead approach to building up a library of date/calendar functions that you need is using the compiler extension, which is at http://www.sqlalchemy.org/docs/core/compiler.html .
For example, my current app has to do some simple date math across Postgresql and MS-SQL. So a function that calculates the length in days between two dates looks like:
from sqlalchemy.sql import expression
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.types import Integer
class date_delta(expression.FunctionElement):
"""Calculate the timedelta in days between two dates."""
type = Integer()
name = 'timedelta'
@compiles(date_delta, 'postgresql')
def _pg_date_delta(element, compiler, **kw):
return "(%s::date - %s::date)" % (
compiler.process(element.clauses.clauses[1]),
compiler.process(element.clauses.clauses[0]),
)
@compiles(date_delta, 'mssql')
def _ms_date_delta(element, compiler, **kw):
return "DATEDIFF(day, %s, %s)" % (
compiler.process(element.clauses.clauses[0], **kw),
compiler.process(element.clauses.clauses[1], **kw),
)
Where above, some inspection of the function object's ".clauses" member, called a ClauseList, has the actual two arguments inside of a list called "clauses". Some minimal source perusal of expression.py may be needed for more involved constructs but functions are pretty much in the above form.