diff options
author | Christophe de Vienne <christophe@unlish.com> | 2014-11-27 15:29:48 +0100 |
---|---|---|
committer | Christophe de Vienne <christophe@unlish.com> | 2014-11-27 15:29:48 +0100 |
commit | adabb02f93efa946e245c68c34281216e07fabbc (patch) | |
tree | 6c0e7f9389b58d14a4f00264dcac9a6bbbd88c3e | |
parent | 36fa2febb554db2a11414a81e69da941a57c1ce4 (diff) | |
download | logilab-common-adabb02f93efa946e245c68c34281216e07fabbc.tar.gz |
[date] Fix utcdatetime
Dates with a tzinfo get messed up by utcdatetime.
The unittest in this patch depends on pytz.
Closes #280794
-rw-r--r-- | date.py | 2 | ||||
-rw-r--r-- | test/unittest_date.py | 23 |
2 files changed, 23 insertions, 2 deletions
@@ -314,7 +314,7 @@ def ustrftime(somedate, fmt='%Y-%m-%d'): def utcdatetime(dt): if dt.tzinfo is None: return dt - return datetime(*dt.utctimetuple()[:7]) + return (dt.replace(tzinfo=None) - dt.utcoffset()) def utctime(dt): if dt.tzinfo is None: diff --git a/test/unittest_date.py b/test/unittest_date.py index ba1522c..973951d 100644 --- a/test/unittest_date.py +++ b/test/unittest_date.py @@ -22,9 +22,10 @@ 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 + get_national_holidays, ustrftime, ticks2datetime, utcdatetime from datetime import date, datetime, timedelta +import pytz try: from mx.DateTime import Date as mxDate, DateTime as mxDateTime, \ @@ -145,6 +146,26 @@ class DateTC(TestCase): expected = [self.datecls(2006, 5, 6), self.datecls(2006, 6, 1), self.datecls(2006, 7, 1), self.datecls(2006, 8, 1)] self.assertListEqual(expected, r) + def test_utcdatetime(self): + if self.datetimecls is mxDateTime: + raise self.skipTest('standard datetime only test') + d = self.datetimecls(2014, 11, 26, 12, 0, 0, 57, tzinfo=pytz.utc) + d = utcdatetime(d) + self.assertEqual(d, self.datetimecls(2014, 11, 26, 12, 0, 0, 57)) + self.assertIsNone(d.tzinfo) + + d = pytz.timezone('Europe/Paris').localize( + self.datetimecls(2014, 11, 26, 12, 0, 0, 57)) + d = utcdatetime(d) + self.assertEqual(d, self.datetimecls(2014, 11, 26, 11, 0, 0, 57)) + self.assertIsNone(d.tzinfo) + + d = pytz.timezone('Europe/Paris').localize( + self.datetimecls(2014, 7, 26, 12, 0, 0, 57)) + d = utcdatetime(d) + self.assertEqual(d, self.datetimecls(2014, 7, 26, 10, 0, 0, 57)) + self.assertIsNone(d.tzinfo) + class MxDateTC(DateTC): datecls = mxDate |