#NEWLINE# def truncate_date( engine , interval , field ) : #NEWLINE# #INDENT# '\n    Returns function which truncates UNIX timestamp to interval\n    >>> truncate_date(\'mysql\', \'minute\', \'clock\')\n    "DATE_FORMAT(`clock`, \'%%Y-%%m-%%d %%H:%%i\')"\n    ' #NEWLINE# FUNCTIONS_FOR_ENGINES = { #NEWLINE# #INDENT# 'mysql' : { #NEWLINE# #INDENT# 'minute' : "FROM_UNIXTIME(`{}`, '%%Y-%%m-%%d %%H:%%i')" , #NEWLINE# 'hour' : "FROM_UNIXTIME(`{}`, '%%Y-%%m-%%d %%H:00')" , #NEWLINE# 'day' : "FROM_UNIXTIME(`{}`, '%%Y-%%m-%%d')" , #NEWLINE# 'week' : "DATE_FORMAT(DATE_SUB(FROM_UNIXTIME(`{}`), INTERVAL(WEEKDAY(FROM_UNIXTIME(`{}`))) DAY), '%%Y-%%m-%%d')" , #NEWLINE# 'month' : "FROM_UNIXTIME(`{}`, '%%Y-%%m-01')" , #NEWLINE# 'year' : "FROM_UNIXTIME(`{}`, '%%Y-01-01')" , #NEWLINE# #UNINDENT# } , #NEWLINE# 'postgresql' : { #NEWLINE# #INDENT# 'minute' : "DATE_TRUNC('minute', TO_TIMESTAMP({}))" , #NEWLINE# 'hour' : "DATE_TRUNC('hour', TO_TIMESTAMP({}))" , #NEWLINE# 'day' : "DATE_TRUNC('day', TO_TIMESTAMP({}))" , #NEWLINE# 'week' : "DATE_TRUNC('week', TO_TIMESTAMP({}))" , #NEWLINE# 'month' : "DATE_TRUNC('month', TO_TIMESTAMP({}))" , #NEWLINE# 'year' : "DATE_TRUNC('year', TO_TIMESTAMP({}))" , #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } #NEWLINE# if ( engine not in FUNCTIONS_FOR_ENGINES ) : #NEWLINE# #INDENT# raise NotSupportedError ( ( 'Database engine %s is not supported' % engine ) ) #NEWLINE# #UNINDENT# if ( interval not in FUNCTIONS_FOR_ENGINES [ engine ] ) : #NEWLINE# #INDENT# raise NotSupportedError ( ( 'Interval %s is not supported by database' % interval ) ) #NEWLINE# #UNINDENT# template = template [ engine ] [ interval ] #NEWLINE# return template . replace ( '{}' , field )