Fix regression with datetime2ticks vs date object as argument

we should not get a traceback in this case. Closes #6060938

authorSylvain Thénault <sylvain.thenault@logilab.fr>
changeset63de39be70f2
branchdefault
phasepublic
hiddenno
parent revision#00a2bdf4d576 [spec] Only require python-egenix-mx-base on EL6
child revision#7ebacaa38380 [registry] Stop using load_module_from_name
files modified by this revision
logilab/common/date.py
test/unittest_date.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1463502298 -7200
# Tue May 17 18:24:58 2016 +0200
# Node ID 63de39be70f20f18345cf0e13b154e935a99fa62
# Parent 00a2bdf4d576e751c13cddbc7d98f96e2243d264
Fix regression with datetime2ticks vs date object as argument

we should not get a traceback in this case. Closes #6060938

diff --git a/logilab/common/date.py b/logilab/common/date.py
@@ -235,11 +235,11 @@
1          return somedate
2      assert isinstance(somedate, (date, DateTimeType)), repr(somedate)
3      return datetime(somedate.year, somedate.month, somedate.day)
4 
5  def datetime2ticks(somedate):
6 -    return timegm(somedate.timetuple()) * 1000 + int(somedate.microsecond / 1000)
7 +    return timegm(somedate.timetuple()) * 1000 + int(getattr(somedate, 'microsecond', 0) / 1000)
8 
9  def ticks2datetime(ticks):
10      miliseconds, microseconds = divmod(ticks, 1000)
11      try:
12          return datetime.fromtimestamp(miliseconds)
diff --git a/test/unittest_date.py b/test/unittest_date.py
@@ -172,10 +172,15 @@
13          timestamp = timegm(d.timetuple())
14          self.assertEqual(datetime2ticks(d), timestamp * 1000)
15          d = d.replace(microsecond=123456)
16          self.assertEqual(datetime2ticks(d), timestamp * 1000 + 123)
17 
18 +    def test_datetime2ticks_date_argument(self):
19 +        d = date(2014, 11, 26)
20 +        timestamp = timegm(d.timetuple())
21 +        self.assertEqual(datetime2ticks(d), timestamp * 1000)
22 +
23 
24  class MxDateTC(DateTC):
25      datecls = mxDate
26      datetimecls = mxDateTime
27      timedeltacls = RelativeDateTime