Created
October 5, 2012 08:55
-
-
Save zed/3838828 to your computer and use it in GitHub Desktop.
can't get correct time during DST using dateutil (pytz works)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# based on https://launchpadlibrarian.net/94884626/testUtcToDst.patch | |
# from https://bugs.launchpad.net/dateutil/+bug/944123 | |
import unittest | |
from datetime import datetime, timedelta | |
import dateutil.tz | |
import dateutil.zoneinfo | |
import pytz | |
tzname = "America/Toronto" | |
fmt = '%Y-%m-%d %H:%M:%S %Z%z' | |
before_utc_naive_dt = datetime(2011, 11, 6, 5, 30) | |
before_str = "2011-11-06 01:30:00 EDT-0400" | |
after_utc_naive_dt = before_utc_naive_dt + timedelta(hours=1) | |
after_str = "2011-11-06 01:30:00 EST-0500" | |
class TestPytzVsDateutil(unittest.TestCase): | |
def _test(self, totz): | |
self.assertEqual(totz(before_utc_naive_dt).strftime(fmt), before_str) | |
self.assertEqual(totz(after_utc_naive_dt).strftime(fmt), after_str) | |
def _test_dateutil(self, gettz): | |
def totz(naive_utc_dt, utc=gettz("UTC"), tz=gettz("America/Toronto")): | |
return naive_utc_dt.replace(tzinfo=utc).astimezone(tz) | |
self._test(totz) | |
def test_pytz(self): | |
def totz(naive_utc_dt, utc=pytz.utc, tz=pytz.timezone(tzname)): | |
return tz.normalize( | |
naive_utc_dt.replace(tzinfo=utc).astimezone(tz)) | |
self._test(totz) | |
def test_dateutil_tz(self): | |
self._test_dateutil(dateutil.tz.gettz) | |
def test_dateutil_zoneinfo(self): | |
self._test_dateutil(dateutil.zoneinfo.gettz) | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment