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