2015-05-23 16:12:09 +08:00
|
|
|
from django.db.models import DecimalField, DurationField, Func
|
|
|
|
|
|
|
|
|
|
|
|
class IntervalToSeconds(Func):
|
|
|
|
function = ''
|
|
|
|
template = """
|
|
|
|
EXTRACT(day from %(expressions)s) * 86400 +
|
|
|
|
EXTRACT(hour from %(expressions)s) * 3600 +
|
|
|
|
EXTRACT(minute from %(expressions)s) * 60 +
|
|
|
|
EXTRACT(second from %(expressions)s)
|
|
|
|
"""
|
|
|
|
|
2017-02-02 00:41:56 +08:00
|
|
|
def __init__(self, expression, *, output_field=None, **extra):
|
|
|
|
super().__init__(expression, output_field=output_field or DecimalField(), **extra)
|
2015-05-23 16:12:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SecondsToInterval(Func):
|
|
|
|
function = 'NUMTODSINTERVAL'
|
|
|
|
template = "%(function)s(%(expressions)s, 'SECOND')"
|
|
|
|
|
2017-02-02 00:41:56 +08:00
|
|
|
def __init__(self, expression, *, output_field=None, **extra):
|
|
|
|
super().__init__(expression, output_field=output_field or DurationField(), **extra)
|