summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Cristau <julien.cristau@logilab.fr>2016-03-10 17:38:55 +0100
committerJulien Cristau <julien.cristau@logilab.fr>2016-03-10 17:38:55 +0100
commitf301e5d41e13cdba72eabeb2cd984d2f62e5553c (patch)
tree1023f043b99eec57b89125f6f3ec044545e4f757
parent8b22ff15cecfeddd8b919f5136cf30fa0304ba27 (diff)
downloadlogilab-common-f301e5d41e13cdba72eabeb2cd984d2f62e5553c.tar.gz
[date] take milliseconds into account in datetime2ticks
-rw-r--r--logilab/common/date.py2
-rw-r--r--test/unittest_date.py14
2 files changed, 12 insertions, 4 deletions
diff --git a/logilab/common/date.py b/logilab/common/date.py
index a093a8a..37e371b 100644
--- a/logilab/common/date.py
+++ b/logilab/common/date.py
@@ -237,7 +237,7 @@ def todatetime(somedate):
return datetime(somedate.year, somedate.month, somedate.day)
def datetime2ticks(somedate):
- return timegm(somedate.timetuple()) * 1000
+ return timegm(somedate.timetuple()) * 1000 + int(somedate.microsecond / 1000)
def ticks2datetime(ticks):
miliseconds, microseconds = divmod(ticks, 1000)
diff --git a/test/unittest_date.py b/test/unittest_date.py
index 973951d..22302b0 100644
--- a/test/unittest_date.py
+++ b/test/unittest_date.py
@@ -20,11 +20,12 @@ Unittests for date helpers
"""
from logilab.common.testlib import TestCase, unittest_main, tag
-from logilab.common.date import date_range, endOfMonth
-from logilab.common.date import add_days_worked, nb_open_days, \
- get_national_holidays, ustrftime, ticks2datetime, utcdatetime
+from logilab.common.date import (date_range, endOfMonth, add_days_worked,
+ nb_open_days, get_national_holidays, ustrftime, ticks2datetime,
+ utcdatetime, datetime2ticks)
from datetime import date, datetime, timedelta
+from calendar import timegm
import pytz
try:
@@ -166,6 +167,13 @@ class DateTC(TestCase):
self.assertEqual(d, self.datetimecls(2014, 7, 26, 10, 0, 0, 57))
self.assertIsNone(d.tzinfo)
+ def test_datetime2ticks(self):
+ d = datetime(2014, 11, 26, 12, 0, 0, 57, tzinfo=pytz.utc)
+ timestamp = timegm(d.timetuple())
+ self.assertEqual(datetime2ticks(d), timestamp * 1000)
+ d = d.replace(microsecond=123456)
+ self.assertEqual(datetime2ticks(d), timestamp * 1000 + 123)
+
class MxDateTC(DateTC):
datecls = mxDate