Skip to content

Add new module python-stdlib/datetime #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
b197d22
python-stdlib/datetime: Add new module.
lorcap Sep 26, 2021
93ecef6
python-stdlib/datetime: Add @property timezone.
lorcap Oct 2, 2021
e4e6313
python-stdlib/datetime: Add EPOCH.
lorcap Oct 26, 2021
2d3bc85
python-stdlib/datetime: Optimize variable names.
lorcap Oct 26, 2021
3c25649
python-stdlib/datetime: Get rid of embarassing comment.
lorcap Oct 26, 2021
3446c80
python-stdlib/datetime: Optimize inline constant tuples.
lorcap Oct 26, 2021
a3f66a0
python-stdlib/datetime: Sort method order.
lorcap Nov 1, 2021
b0903dc
python-stdlib/datetime: Nanosecond resolution.
lorcap Nov 1, 2021
384d0f0
python-stdlib/datetime: Add more tests.
lorcap Nov 1, 2021
cff72a6
python-stdlib/datetime: Run /tools/codeformat.py.
lorcap Nov 1, 2021
cfe7ce2
python-stdlib/datetime: Fix tests.
lorcap Nov 6, 2021
86ae890
python-stdlib/datetime: Fix __add__(-day).
lorcap Nov 6, 2021
28185ed
python-stdlib/datetime: Fix datetime constructor.
lorcap Nov 6, 2021
84bfb1f
python-stdlib/datetime: Microsecond resolution.
lorcap Nov 12, 2021
c167999
python-stdlib/datetime: Forget timedelta limits.
lorcap Nov 12, 2021
86784f0
python-stdlib/datetime: Add new module.
lorcap Dec 1, 2021
1097841
python-stdlib/datetime: Add time and date tests.
lorcap Dec 4, 2021
e94bb27
python-stdlib/datetime: Fix __eq__ for datetime with tzinfo.
lorcap Dec 6, 2021
0847bf2
python-stdlib/datetime: Add __hash__ support.
lorcap Dec 6, 2021
224680c
python-stdlib/datetime: Add fromutc().
lorcap Dec 9, 2021
55c9abe
python-stdlib/datetime: More tests on astimezone().
lorcap Dec 9, 2021
76af846
python-stdlib/datetime: Optimize _sub().
lorcap Dec 10, 2021
9ea53dd
python-stdlib/datetime: Fix commit 1097841.
lorcap Dec 12, 2021
433a46c
python-stdlib/datetime: Add naive support to timestamp().
lorcap Dec 12, 2021
a2f746d
python-stdlib/datetime: Add Cet.fromutc().
lorcap Dec 13, 2021
fefc428
python-stdlib/datetime: Add fromtimestamp().
lorcap Dec 14, 2021
1b733a8
python-stdlib/datetime: Use @classmethod instead.
lorcap Dec 15, 2021
e32d05f
python-stdlib/datetime: Add support to fold.
lorcap Dec 15, 2021
6aec5e8
python-stdlib/datetime: Add datetime.fromordinal().
lorcap Dec 17, 2021
16d1107
python-stdlib/datetime: Add fold to timetz().
lorcap Dec 17, 2021
53a9682
python-stdlib/datetime: Add comparison ops to time.
lorcap Dec 19, 2021
d7329c3
python-stdlib/datetime: Add date.fromtimestamp().
lorcap Dec 20, 2021
c296186
python-stdlib/datetime: Add date.timetuple().
lorcap Dec 20, 2021
7cc43f3
python-stdlib/datetime: Fix date.__eq__().
lorcap Dec 20, 2021
33fcacc
python-stdlib/datetime: Add support to astimezone(tz=tz).
lorcap Dec 21, 2021
07dabe5
python-stdlib/datetime: Swap date() and time().
lorcap Dec 22, 2021
96d2bc1
python-stdlib/datetime: Shorter names for hidden methods.
lorcap Dec 22, 2021
b4ac923
python-stdlib/datetime: Decouple time and date from datetime.
lorcap Dec 22, 2021
49cd85d
python-stdlib/datetime: Remove naive support due to time.localtime().
lorcap Dec 23, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
879 changes: 879 additions & 0 deletions python-stdlib/datetime/datetime.py

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions python-stdlib/datetime/localtz.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
localtz.patch

The CPython's implementation of `datetime.fromtimestamp()`,
`datetime.astimezone()` and `datetime.timestamp()` for naive datetime objects
relay on proper management of DST (daylight saving time) by `time.localtime()`
for the timezone of interest. In the Unix port of MicroPython, this is
accomplished by properly setting the TZ environment variable, e.g.
`os.putenv("TZ", "Europe/Rome")`.

Because real boards often lack a supportive `time.localtime()`, the source code
in `datetime.py` has been removed as to save precious resources. If your board
provide a proper implementation, you can restore the support to naive datetime
objects by applying this patch, e.g. `patch -p1 < localtz.patch`.

--- a/datetime.py
+++ b/datetime.py
@@ -635,7 +635,10 @@ class datetime:
else:
us = 0
if tz is None:
- raise NotImplementedError
+ dt = cls(*_tmod.localtime(ts)[:6], microsecond=us, tzinfo=tz)
+ s = (dt - datetime(*_tmod.localtime(ts - 86400)[:6]))._us // 1_000_000 - 86400
+ if s < 0 and dt == datetime(*_tmod.localtime(ts + s)[:6]):
+ dt._fd = 1
else:
dt = cls(*_tmod.gmtime(ts)[:6], microsecond=us, tzinfo=tz)
dt = tz.fromutc(dt)
@@ -812,13 +815,45 @@ class datetime:
return self
_tz = self._tz
if _tz is None:
- raise NotImplementedError
+ ts = int(self._mktime())
+ os = datetime(*_tmod.localtime(ts)[:6]) - datetime(*_tmod.gmtime(ts)[:6])
else:
os = _tz.utcoffset(self)
utc = self - os
utc = utc.replace(tzinfo=tz)
return tz.fromutc(utc)

+ def _mktime(self):
+ def local(u):
+ return (datetime(*_tmod.localtime(u)[:6]) - epoch)._us // 1_000_000
+
+ epoch = datetime.EPOCH.replace(tzinfo=None)
+ t, us = divmod((self - epoch)._us, 1_000_000)
+ ts = None
+
+ a = local(t) - t
+ u1 = t - a
+ t1 = local(u1)
+ if t1 == t:
+ u2 = u1 + (86400 if self.fold else -86400)
+ b = local(u2) - u2
+ if a == b:
+ ts = u1
+ else:
+ b = t1 - u1
+ if ts is None:
+ u2 = t - b
+ t2 = local(u2)
+ if t2 == t:
+ ts = u2
+ elif t1 == t:
+ ts = u1
+ elif self.fold:
+ ts = min(u1, u2)
+ else:
+ ts = max(u1, u2)
+ return ts + us / 1_000_000
+
def utcoffset(self):
return None if self._tz is None else self._tz.utcoffset(self)

@@ -842,7 +877,7 @@ class datetime:

def timestamp(self):
if self._tz is None:
- raise NotImplementedError
+ return self._mktime()
else:
return (self - datetime.EPOCH).total_seconds()

4 changes: 4 additions & 0 deletions python-stdlib/datetime/metadata.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
srctype = cpython
type = module
version = 2.0.0
author = Lorenzo Cappelletti
24 changes: 24 additions & 0 deletions python-stdlib/datetime/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys

# Remove current dir from sys.path, otherwise setuptools will peek up our
# module instead of system's.
sys.path.pop(0)
from setuptools import setup

sys.path.append("..")
import sdist_upip

setup(
name="micropython-datetime",
version="2.0.0",
description="datetime module for MicroPython",
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.",
url="https://github.com/micropython/micropython-lib",
author="micropython-lib Developers",
author_email="micro-python@googlegroups.com",
maintainer="micropython-lib Developers",
maintainer_email="micro-python@googlegroups.com",
license="MIT",
cmdclass={"sdist": sdist_upip.sdist},
py_modules=["datetime"],
)
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy