Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fc86070

Browse files
lorcapdpgeorge
authored andcommitted
python-stdlib/datetime: Add new implementation of datetime module.
This new module is a port of Python datetime providing classes for manipulating dates, times, and deltas. It completely replaces the existing unix-ffi version. Signed-off-by: Lorenzo Cappelletti <lorenzo.cappelletti@gmail.com>
1 parent 64b8817 commit fc86070

File tree

9 files changed

+3260
-6184
lines changed

9 files changed

+3260
-6184
lines changed

‎python-stdlib/datetime/datetime.py‎

Lines changed: 879 additions & 0 deletions
Large diffs are not rendered by default.

‎python-stdlib/datetime/localtz.patch‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+

‎python-stdlib/datetime/metadata.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
srctype = micropython-lib
2+
type = module
3+
version = 4.0.0
4+
author = Lorenzo Cappelletti

‎python-stdlib/datetime/setup.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
3+
# Remove current dir from sys.path, otherwise setuptools will peek up our
4+
# module instead of system's.
5+
sys.path.pop(0)
6+
from setuptools import setup
7+
8+
sys.path.append("..")
9+
import sdist_upip
10+
11+
setup(
12+
name="micropython-datetime",
13+
version="4.0.0",
14+
description="datetime module for MicroPython",
15+
long_description="This is a module reimplemented specifically for MicroPython standard library,\nwith efficient and lean design in mind. Note that this module is likely work\nin progress and likely supports just a subset of CPython's corresponding\nmodule. Please help with the development if you are interested in this\nmodule.",
16+
url="https://github.com/micropython/micropython-lib",
17+
author="micropython-lib Developers",
18+
author_email="micro-python@googlegroups.com",
19+
maintainer="micropython-lib Developers",
20+
maintainer_email="micro-python@googlegroups.com",
21+
license="MIT",
22+
cmdclass={"sdist": sdist_upip.sdist},
23+
py_modules=["datetime"],
24+
)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /