Last active
August 29, 2015 14:18
-
-
Save j00bar/b201148db49c876d91da to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| In [1]: import pytz | |
| In [2]: from datetime import datetime, date, time, timedelta | |
| In [3]: now = datetime.utcnow().replace(tzinfo=pytz.UTC) | |
| In [4]: now | |
| Out[4]: datetime.datetime(2015, 4, 3, 18, 17, 45, 509236, tzinfo=<UTC>) | |
| In [5]: localnow = now.astimezone(pytz.timezone('US/Eastern')) | |
| In [6]: localnow | |
| Out[6]: datetime.datetime(2015, 4, 3, 14, 17, 45, 509236, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>) | |
| In [7]: normalizednow = pytz.timezone('US/Eastern').normalize(localnow) | |
| In [8]: normalizednow | |
| Out[8]: datetime.datetime(2015, 4, 3, 14, 17, 45, 509236, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>) | |
| In [9]: today_midnight = datetime.combine(date.today(), time(0, tzinfo=pytz.timezone('US/Eastern'))) | |
| In [10]: today_midnight_in_utc = today_midnight.astimezone(pytz.UTC) | |
| In [11]: today_midnight_in_utc | |
| Out[11]: datetime.datetime(2015, 4, 3, 5, 0, tzinfo=<UTC>) | |
| In [12]: today_midnight | |
| Out[12]: datetime.datetime(2015, 4, 3, 0, 0, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>) | |
| In [13]: today_midnight.dst() | |
| Out[13]: datetime.timedelta(0) | |
| In [14]: localnow.dst() | |
| Out[14]: datetime.timedelta(0, 3600) | |
| In [15]: better_today_midnight = pytz.timezone('US/Eastern').localize(datetime.combine(date.today(), time())) | |
| In [16]: better_today_midnight_utc = better_today_midnight.astimezone(pytz.UTC) | |
| In [17]: better_today_midnight | |
| Out[17]: datetime.datetime(2015, 4, 3, 0, 0, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>) | |
| In [18]: better_today_midnight_utc | |
| Out[18]: datetime.datetime(2015, 4, 3, 4, 0, tzinfo=<UTC>) | |
| In [19]: spring_forward = datetime(2015, 3, 8, 2, 0, 0) | |
| In [20]: fall_back = datetime(2015, 11, 1, 2, 0, 0) | |
| In [21]: pytz.timezone('US/Eastern').localize(spring_forward + timedelta(seconds=60), is_dst=None) | |
| --------------------------------------------------------------------------- | |
| NonExistentTimeError Traceback (most recent call last) | |
| <ipython-input-21-b255774bc7b0> in <module>() | |
| ----> 1 pytz.timezone('US/Eastern').localize(spring_forward + timedelta(seconds=60), is_dst=None) | |
| /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz/tzinfo.pyc in localize(self, dt, is_dst) | |
| 325 # If we refuse to guess, raise an exception. | |
| 326 if is_dst is None: | |
| --> 327 raise NonExistentTimeError(dt) | |
| 328 | |
| In [22]: pytz.timezone('US/Eastern').localize(fall_back - timedelta(seconds=60), is_dst=None) | |
| --------------------------------------------------------------------------- | |
| AmbiguousTimeError Traceback (most recent call last) | |
| <ipython-input-22-92196f229dd3> in <module>() | |
| ----> 1 pytz.timezone('US/Eastern').localize(fall_back - timedelta(seconds=60), is_dst=None) | |
| /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz/tzinfo.pyc in localize(self, dt, is_dst) | |
| 347 # ambiguous case | |
| 348 if is_dst is None: | |
| --> 349 raise AmbiguousTimeError(dt) | |
| 350 | |
| 351 # Filter out the possiblilities that don't match the requested | |
| AmbiguousTimeError: 2015-11-01 01:59:00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment