Skip to content

Commit af978ac

Browse files
committed
Add expression to select just the EPOCH part of a timestamp
1 parent 2215827 commit af978ac

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

psqlextra/expressions.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,32 @@ class Max(NonGroupableFunc):
112112

113113
def __init__(self, expression):
114114
super().__init__(expression, function='Max')
115+
116+
117+
class DateTimeEpochColumn(expressions.Col):
118+
"""Gets the date/time column as a UNIX epoch timestamp."""
119+
120+
contains_column_references = True
121+
122+
def as_sql(self, compiler, connection):
123+
"""Compiles this expression into SQL."""
124+
125+
sql, params = super().as_sql(compiler, connection)
126+
return 'EXTRACT(epoch FROM {})'.format(sql), params
127+
128+
def get_group_by_cols(self):
129+
return []
130+
131+
132+
class DateTimeEpoch(expressions.F):
133+
"""Gets the date/time column as a UNIX epoch timestamp."""
134+
135+
contains_aggregate = False
136+
137+
def resolve_expression(self, *args, **kwargs):
138+
original_expression = super().resolve_expression(*args, **kwargs)
139+
expression = DateTimeEpochColumn(
140+
original_expression.alias,
141+
original_expression.target,
142+
)
143+
return expression

0 commit comments

Comments
 (0)