| 1 |
from django.db.models import DecimalField, DurationField, Func |
| 2 |
|
| 3 |
|
| 4 |
class IntervalToSeconds(Func): |
| 5 |
function = "" |
| 6 |
template = """ |
| 7 |
EXTRACT(day from %(expressions)s) * 86400 + |
| 8 |
EXTRACT(hour from %(expressions)s) * 3600 + |
| 9 |
EXTRACT(minute from %(expressions)s) * 60 + |
| 10 |
EXTRACT(second from %(expressions)s) |
| 11 |
""" |
| 12 |
|
| 13 |
def __init__(self, expression, *, output_field=None, **extra): |
| 14 |
super().__init__( |
| 15 |
expression, output_field=output_field or DecimalField(), **extra |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
class SecondsToInterval(Func): |
| 20 |
function = "NUMTODSINTERVAL" |
| 21 |
template = "%(function)s(%(expressions)s, 'SECOND')" |
| 22 |
|
| 23 |
def __init__(self, expression, *, output_field=None, **extra): |
| 24 |
super().__init__( |
| 25 |
expression, output_field=output_field or DurationField(), **extra |
| 26 |
) |