Skip to content

Commit 07683a8

Browse files
committed
Fixed pandas-dev/pandas#32174 moving to .timestamp() altogether in the s_from_dt() function. Added tests.
1 parent b575323 commit 07683a8

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

timeseria/tests/test_time.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
import datetime
3-
from ..time import dt, correct_dt_dst, dt_to_str, dt_from_str, change_tz
3+
from ..time import dt, correct_dt_dst, dt_to_str, dt_from_str, s_from_dt, change_tz, timezonize
4+
from pandas import Timestamp as PandasTimestamp
45

56

67
class TestTime(unittest.TestCase):
@@ -74,6 +75,15 @@ def test_dt(self):
7475
date_time = dt(3567,8,1,16,46, tzinfo='Europe/Rome')
7576
self.assertEqual(str(date_time), '3567-08-01 16:46:00+01:00')
7677

78+
79+
def test_s_from_dt(self):
80+
81+
date_time = dt(2001,12,1,16,46,10,6575, tzinfo='Europe/Rome')
82+
self.assertEqual(s_from_dt(date_time), 1007221570.006575)
83+
84+
date_time_pandas = PandasTimestamp(year=2001,month=12,day=1,hour=16,minute=46,second=10,microsecond=6575, tzinfo=timezonize('Europe/Rome'))
85+
self.assertEqual(s_from_dt(date_time_pandas), 1007221570.006575)
86+
7787

7888
def test_str_conversions(self):
7989

timeseria/time.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,12 @@ def s_from_dt(dt):
142142
"""Return the epoch seconds from a datetime object, with floating point for milliseconds/microseconds."""
143143
if not (isinstance(dt, datetime.datetime)):
144144
raise Exception('t_from_dt function called without datetime argument, got type "{}" instead.'.format(dt.__class__.__name__))
145-
microseconds_part = (dt.microsecond/1000000.0) if dt.microsecond else 0
146-
return ( calendar.timegm(dt.utctimetuple()) + microseconds_part)
145+
try:
146+
return dt.timestamp()
147+
except AttributeError:
148+
# Python 2.7
149+
microseconds_part = (dt.microsecond/1000000.0) if dt.microsecond else 0
150+
return (calendar.timegm(dt.utctimetuple()) + microseconds_part)
147151

148152

149153
def dt_from_str(string, timezone=None):

0 commit comments

Comments
 (0)