|
| 1 | +localtz.patch |
| 2 | + |
| 3 | +The CPython's implementation of `datetime.fromtimestamp()`, |
| 4 | +`datetime.astimezone()` and `datetime.timestamp()` for naive datetime objects |
| 5 | +relay on proper management of DST (daylight saving time) by `time.localtime()` |
| 6 | +for the timezone of interest. In the Unix port of MicroPython, this is |
| 7 | +accomplished by properly setting the TZ environment variable, e.g. |
| 8 | +`os.putenv("TZ", "Europe/Rome")`. |
| 9 | + |
| 10 | +Because real boards often lack a supportive `time.localtime()`, the source code |
| 11 | +in `datetime.py` has been removed as to save precious resources. If your board |
| 12 | +provide a proper implementation, you can restore the support to naive datetime |
| 13 | +objects by applying this patch, e.g. `patch -p1 < localtz.patch`. |
| 14 | + |
| 15 | +--- a/datetime.py |
| 16 | ++++ b/datetime.py |
| 17 | +@@ -635,7 +635,10 @@ class datetime: |
| 18 | + else: |
| 19 | + us = 0 |
| 20 | + if tz is None: |
| 21 | +- raise NotImplementedError |
| 22 | ++ dt = cls(*_tmod.localtime(ts)[:6], microsecond=us, tzinfo=tz) |
| 23 | ++ s = (dt - datetime(*_tmod.localtime(ts - 86400)[:6]))._us // 1_000_000 - 86400 |
| 24 | ++ if s < 0 and dt == datetime(*_tmod.localtime(ts + s)[:6]): |
| 25 | ++ dt._fd = 1 |
| 26 | + else: |
| 27 | + dt = cls(*_tmod.gmtime(ts)[:6], microsecond=us, tzinfo=tz) |
| 28 | + dt = tz.fromutc(dt) |
| 29 | +@@ -812,13 +815,45 @@ class datetime: |
| 30 | + return self |
| 31 | + _tz = self._tz |
| 32 | + if _tz is None: |
| 33 | +- raise NotImplementedError |
| 34 | ++ ts = int(self._mktime()) |
| 35 | ++ os = datetime(*_tmod.localtime(ts)[:6]) - datetime(*_tmod.gmtime(ts)[:6]) |
| 36 | + else: |
| 37 | + os = _tz.utcoffset(self) |
| 38 | + utc = self - os |
| 39 | + utc = utc.replace(tzinfo=tz) |
| 40 | + return tz.fromutc(utc) |
| 41 | + |
| 42 | ++ def _mktime(self): |
| 43 | ++ def local(u): |
| 44 | ++ return (datetime(*_tmod.localtime(u)[:6]) - epoch)._us // 1_000_000 |
| 45 | ++ |
| 46 | ++ epoch = datetime.EPOCH.replace(tzinfo=None) |
| 47 | ++ t, us = divmod((self - epoch)._us, 1_000_000) |
| 48 | ++ ts = None |
| 49 | ++ |
| 50 | ++ a = local(t) - t |
| 51 | ++ u1 = t - a |
| 52 | ++ t1 = local(u1) |
| 53 | ++ if t1 == t: |
| 54 | ++ u2 = u1 + (86400 if self.fold else -86400) |
| 55 | ++ b = local(u2) - u2 |
| 56 | ++ if a == b: |
| 57 | ++ ts = u1 |
| 58 | ++ else: |
| 59 | ++ b = t1 - u1 |
| 60 | ++ if ts is None: |
| 61 | ++ u2 = t - b |
| 62 | ++ t2 = local(u2) |
| 63 | ++ if t2 == t: |
| 64 | ++ ts = u2 |
| 65 | ++ elif t1 == t: |
| 66 | ++ ts = u1 |
| 67 | ++ elif self.fold: |
| 68 | ++ ts = min(u1, u2) |
| 69 | ++ else: |
| 70 | ++ ts = max(u1, u2) |
| 71 | ++ return ts + us / 1_000_000 |
| 72 | ++ |
| 73 | + def utcoffset(self): |
| 74 | + return None if self._tz is None else self._tz.utcoffset(self) |
| 75 | + |
| 76 | +@@ -842,7 +877,7 @@ class datetime: |
| 77 | + |
| 78 | + def timestamp(self): |
| 79 | + if self._tz is None: |
| 80 | +- raise NotImplementedError |
| 81 | ++ return self._mktime() |
| 82 | + else: |
| 83 | + return (self - datetime.EPOCH).total_seconds() |
| 84 | + |
0 commit comments