|
| 1 | +import datetime as dt |
| 2 | + |
| 3 | +# A class capturing the platform's idea of local time. |
| 4 | +# (May result in wrong values on historical times in |
| 5 | +# timezones where UTC offset and/or the DST rules had |
| 6 | +# changed in the past.) |
| 7 | +import time |
| 8 | + |
| 9 | +ZERO = dt.timedelta(0) |
| 10 | +HOUR = dt.timedelta(hours=1) |
| 11 | +SECOND = dt.timedelta(seconds=1) |
| 12 | + |
| 13 | +STDOFFSET = dt.timedelta(seconds=-time.timezone) |
| 14 | +if time.daylight: |
| 15 | + DSTOFFSET = dt.timedelta(seconds=-time.altzone) |
| 16 | +else: |
| 17 | + DSTOFFSET = STDOFFSET |
| 18 | + |
| 19 | +DSTDIFF = DSTOFFSET - STDOFFSET |
| 20 | + |
| 21 | + |
| 22 | +class LocalTimezone(dt.tzinfo): |
| 23 | + |
| 24 | + def fromutc(self, when): |
| 25 | + assert when.tzinfo is self |
| 26 | + stamp = (when - dt.datetime(1970, 1, 1, tzinfo=self)) // SECOND |
| 27 | + args = time.localtime(stamp)[:6] |
| 28 | + dst_diff = DSTDIFF // SECOND |
| 29 | + # Detect fold |
| 30 | + fold = (args == time.localtime(stamp - dst_diff)) |
| 31 | + return dt.datetime(*args, microsecond=when.microsecond, |
| 32 | + tzinfo=self, fold=fold) |
| 33 | + |
| 34 | + def utcoffset(self, when): |
| 35 | + if self._isdst(when): |
| 36 | + return DSTOFFSET |
| 37 | + else: |
| 38 | + return STDOFFSET |
| 39 | + |
| 40 | + def dst(self, when): |
| 41 | + if self._isdst(when): |
| 42 | + return DSTDIFF |
| 43 | + else: |
| 44 | + return ZERO |
| 45 | + |
| 46 | + def tzname(self, when): |
| 47 | + return time.tzname[self._isdst(when)] |
| 48 | + |
| 49 | + def _isdst(self, when): |
| 50 | + tt = (when.year, when.month, when.day, |
| 51 | + when.hour, when.minute, when.second, |
| 52 | + when.weekday(), 0, 0) |
| 53 | + stamp = time.mktime(tt) |
| 54 | + tt = time.localtime(stamp) |
| 55 | + return tt.tm_isdst > 0 |
| 56 | + |
| 57 | + |
| 58 | +Local = LocalTimezone() |
| 59 | + |
| 60 | + |
| 61 | +# A complete implementation of current DST rules for major US time zones. |
| 62 | + |
| 63 | +def first_sunday_on_or_after(when): |
| 64 | + days_to_go = 6 - when.weekday() |
| 65 | + if days_to_go: |
| 66 | + when += dt.timedelta(days_to_go) |
| 67 | + return when |
| 68 | + |
| 69 | + |
| 70 | +# US DST Rules |
| 71 | +# |
| 72 | +# This is a simplified (i.e., wrong for a few cases) set of rules for US |
| 73 | +# DST start and end times. For a complete and up-to-date set of DST rules |
| 74 | +# and timezone definitions, visit the Olson Database (or try pytz): |
| 75 | +# http://www.twinsun.com/tz/tz-link.htm |
| 76 | +# https://sourceforge.net/projects/pytz/ (might not be up-to-date) |
| 77 | +# |
| 78 | +# In the US, since 2007, DST starts at 2am (standard time) on the second |
| 79 | +# Sunday in March, which is the first Sunday on or after Mar 8. |
| 80 | +DSTSTART_2007 = dt.datetime(1, 3, 8, 2) |
| 81 | +# and ends at 2am (DST time) on the first Sunday of Nov. |
| 82 | +DSTEND_2007 = dt.datetime(1, 11, 1, 2) |
| 83 | +# From 1987 to 2006, DST used to start at 2am (standard time) on the first |
| 84 | +# Sunday in April and to end at 2am (DST time) on the last |
| 85 | +# Sunday of October, which is the first Sunday on or after Oct 25. |
| 86 | +DSTSTART_1987_2006 = dt.datetime(1, 4, 1, 2) |
| 87 | +DSTEND_1987_2006 = dt.datetime(1, 10, 25, 2) |
| 88 | +# From 1967 to 1986, DST used to start at 2am (standard time) on the last |
| 89 | +# Sunday in April (the one on or after April 24) and to end at 2am (DST time) |
| 90 | +# on the last Sunday of October, which is the first Sunday |
| 91 | +# on or after Oct 25. |
| 92 | +DSTSTART_1967_1986 = dt.datetime(1, 4, 24, 2) |
| 93 | +DSTEND_1967_1986 = DSTEND_1987_2006 |
| 94 | + |
| 95 | + |
| 96 | +def us_dst_range(year): |
| 97 | + # Find start and end times for US DST. For years before 1967, return |
| 98 | + # start = end for no DST. |
| 99 | + if 2006 < year: |
| 100 | + dststart, dstend = DSTSTART_2007, DSTEND_2007 |
| 101 | + elif 1986 < year < 2007: |
| 102 | + dststart, dstend = DSTSTART_1987_2006, DSTEND_1987_2006 |
| 103 | + elif 1966 < year < 1987: |
| 104 | + dststart, dstend = DSTSTART_1967_1986, DSTEND_1967_1986 |
| 105 | + else: |
| 106 | + return (dt.datetime(year, 1, 1), ) * 2 |
| 107 | + |
| 108 | + start = first_sunday_on_or_after(dststart.replace(year=year)) |
| 109 | + end = first_sunday_on_or_after(dstend.replace(year=year)) |
| 110 | + return start, end |
| 111 | + |
| 112 | + |
| 113 | +class USTimeZone(dt.tzinfo): |
| 114 | + |
| 115 | + def __init__(self, hours, reprname, stdname, dstname): |
| 116 | + self.stdoffset = dt.timedelta(hours=hours) |
| 117 | + self.reprname = reprname |
| 118 | + self.stdname = stdname |
| 119 | + self.dstname = dstname |
| 120 | + |
| 121 | + def __repr__(self): |
| 122 | + return self.reprname |
| 123 | + |
| 124 | + def tzname(self, when): |
| 125 | + if self.dst(when): |
| 126 | + return self.dstname |
| 127 | + else: |
| 128 | + return self.stdname |
| 129 | + |
| 130 | + def utcoffset(self, when): |
| 131 | + return self.stdoffset + self.dst(when) |
| 132 | + |
| 133 | + def dst(self, when): |
| 134 | + if when is None or when.tzinfo is None: |
| 135 | + # An exception may be sensible here, in one or both cases. |
| 136 | + # It depends on how you want to treat them. The default |
| 137 | + # fromutc() implementation (called by the default astimezone() |
| 138 | + # implementation) passes a datetime with when.tzinfo is self. |
| 139 | + return ZERO |
| 140 | + assert when.tzinfo is self |
| 141 | + start, end = us_dst_range(when.year) |
| 142 | + # Can't compare naive to aware objects, so strip the timezone from |
| 143 | + # when first. |
| 144 | + when = when.replace(tzinfo=None) |
| 145 | + if start + HOUR <= when < end - HOUR: |
| 146 | + # DST is in effect. |
| 147 | + return HOUR |
| 148 | + if end - HOUR <= when < end: |
| 149 | + # Fold (an ambiguous hour): use when.fold to disambiguate. |
| 150 | + return ZERO if when.fold else HOUR |
| 151 | + if start <= when < start + HOUR: |
| 152 | + # Gap (a non-existent hour): reverse the fold rule. |
| 153 | + return HOUR if when.fold else ZERO |
| 154 | + # DST is off. |
| 155 | + return ZERO |
| 156 | + |
| 157 | + def fromutc(self, when): |
| 158 | + assert when.tzinfo is self |
| 159 | + start, end = us_dst_range(when.year) |
| 160 | + start = start.replace(tzinfo=self) |
| 161 | + end = end.replace(tzinfo=self) |
| 162 | + std_time = when + self.stdoffset |
| 163 | + dst_time = std_time + HOUR |
| 164 | + if end <= dst_time < end + HOUR: |
| 165 | + # Repeated hour |
| 166 | + return std_time.replace(fold=1) |
| 167 | + if std_time < start or dst_time >= end: |
| 168 | + # Standard time |
| 169 | + return std_time |
| 170 | + if start <= std_time < end - HOUR: |
| 171 | + # Daylight saving time |
| 172 | + return dst_time |
| 173 | + |
| 174 | + |
| 175 | +Eastern = USTimeZone(-5, "Eastern", "EST", "EDT") |
| 176 | +Central = USTimeZone(-6, "Central", "CST", "CDT") |
| 177 | +Mountain = USTimeZone(-7, "Mountain", "MST", "MDT") |
| 178 | +Pacific = USTimeZone(-8, "Pacific", "PST", "PDT") |
0 commit comments