Commit cbe43273 authored by jbrockmendel's avatar jbrockmendel Committed by GitHub

Provide timedelta.total_seconds() implementation in datetime.pxd (GH-3616)

parent 74d9df82
......@@ -221,3 +221,14 @@ cdef inline int timedelta_seconds(object o):
# Get microseconds of timedelta
cdef inline int timedelta_microseconds(object o):
return (<PyDateTime_Delta*>o).microseconds
cdef inline double total_seconds(timedelta obj):
# Mirrors the "timedelta.total_seconds()" method.
# Note that this implementation is not guaranteed to give *exactly* the same
# result as the original method, due to potential differences in floating point rounding.
cdef:
double days, seconds, micros
days = <double>PyDateTime_DELTA_GET_DAYS(obj)
seconds = <double>PyDateTime_DELTA_GET_SECONDS(obj)
micros = <double>PyDateTime_DELTA_GET_MICROSECONDS(obj)
return days * 24 * 3600 + seconds + micros / 1_000_000
......@@ -4,7 +4,7 @@
#from datetime import time, date, datetime, timedelta, tzinfo
from cpython.datetime cimport import_datetime
from cpython.datetime cimport import_datetime, timedelta
from cpython.datetime cimport time_new, date_new, datetime_new, timedelta_new
from cpython.datetime cimport time_tzinfo, datetime_tzinfo
from cpython.datetime cimport time_hour, time_minute, time_second, time_microsecond
......@@ -12,6 +12,8 @@ from cpython.datetime cimport date_day, date_month, date_year
from cpython.datetime cimport datetime_day, datetime_month, datetime_year
from cpython.datetime cimport datetime_hour, datetime_minute, datetime_second, \
datetime_microsecond
from cpython.datetime cimport datetime, total_seconds
# These were added in Py3, make sure that their backport works.
from cpython.datetime cimport (
timedelta as timedelta_ext_type,
......@@ -194,3 +196,19 @@ def do_datetime_tzinfo2(int year, int month, int day,
r7 = (v2 == v)
r8 = (v3 == v1)
return r1, r2, r3, r4, r5, r6, r7, r8
def test_timedelta_total_seconds():
"""
>>> cytotal, pytotal = test_timedelta_total_seconds()
>>> assert cytotal == pytotal, (cytotal, pytotal)
>>> cytotal == pytotal
True
"""
cdef:
datetime now = py_datetime.datetime.now()
timedelta td = now - py_datetime.datetime(1970, 1, 1)
pytd = now - py_datetime.datetime(1970, 1, 1)
return total_seconds(td), pytd.total_seconds()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment