|
29 | 29 | # pylint: disable=too-many-lines
|
30 | 30 | import time as _time
|
31 | 31 | import math as _math
|
| 32 | +import re as _re |
32 | 33 | from micropython import const
|
33 | 34 |
|
34 | 35 | __version__ = "0.0.0-auto.0"
|
@@ -657,6 +658,18 @@ def fromordinal(cls, ordinal):
|
657 | 658 | y, m, d = _ord2ymd(ordinal)
|
658 | 659 | return cls(y, m, d)
|
659 | 660 |
|
| 661 | + @classmethod |
| 662 | + def fromisoformat(cls, date_string): |
| 663 | + """Return a date object constructed from an ISO date format.""" |
| 664 | + match = _re.match( |
| 665 | + r"([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])", date_string |
| 666 | + ) |
| 667 | + if match: |
| 668 | + y, m, d = int(match.group(1)), int(match.group(2)), int(match.group(3)) |
| 669 | + return cls(y, m, d) |
| 670 | + else: |
| 671 | + raise ValueError("Not a valid ISO Date") |
| 672 | + |
660 | 673 | @classmethod
|
661 | 674 | def today(cls):
|
662 | 675 | """Return the current local date."""
|
@@ -1206,6 +1219,35 @@ def _fromtimestamp(cls, t, utc, tz):
|
1206 | 1219 | def fromtimestamp(cls, timestamp, tz=None):
|
1207 | 1220 | return cls._fromtimestamp(timestamp, tz is not None, tz)
|
1208 | 1221 |
|
| 1222 | + @classmethod |
| 1223 | + def fromisoformat(cls, date_string, tz=None): |
| 1224 | + """Return a date object constructed from an ISO date format. |
| 1225 | + YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]] |
| 1226 | + """ |
| 1227 | + match = _re.match( |
| 1228 | + r"([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])(T([0-9][0-9]))?(:([0-9][0-9]))?(:([0-9][0-9]))?(\.[0-9][0-9][0-9][0-9][0-9][0-9])?", |
| 1229 | + date_string, |
| 1230 | + ) |
| 1231 | + if match: |
| 1232 | + y, m, d = int(match.group(1)), int(match.group(2)), int(match.group(3)) |
| 1233 | + hh = int(match.group(5)) if match.group(5) else 0 |
| 1234 | + mm = int(match.group(5)) if match.group(7) else 0 |
| 1235 | + ss = int(match.group(9)) if match.group(9) else 0 |
| 1236 | + us = round((float("0" + match.group(10)) if match.group(10) else 0) * 1e6) |
| 1237 | + result = cls( |
| 1238 | + y, |
| 1239 | + m, |
| 1240 | + d, |
| 1241 | + hh, |
| 1242 | + mm, |
| 1243 | + ss, |
| 1244 | + us, |
| 1245 | + tz, |
| 1246 | + ) |
| 1247 | + return result |
| 1248 | + else: |
| 1249 | + raise ValueError("Not a valid ISO Date") |
| 1250 | + |
1209 | 1251 | @classmethod
|
1210 | 1252 | def now(cls, timezone=None):
|
1211 | 1253 | """Return the current local date and time."""
|
|
0 commit comments