Skip to content

Commit df458b7

Browse files
committed
Added fromisoformat() to date and datetime
1 parent 70a4e69 commit df458b7

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

adafruit_datetime.py

+42
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
# pylint: disable=too-many-lines
3030
import time as _time
3131
import math as _math
32+
import re as _re
3233
from micropython import const
3334

3435
__version__ = "0.0.0-auto.0"
@@ -657,6 +658,18 @@ def fromordinal(cls, ordinal):
657658
y, m, d = _ord2ymd(ordinal)
658659
return cls(y, m, d)
659660

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+
660673
@classmethod
661674
def today(cls):
662675
"""Return the current local date."""
@@ -1206,6 +1219,35 @@ def _fromtimestamp(cls, t, utc, tz):
12061219
def fromtimestamp(cls, timestamp, tz=None):
12071220
return cls._fromtimestamp(timestamp, tz is not None, tz)
12081221

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+
12091251
@classmethod
12101252
def now(cls, timezone=None):
12111253
"""Return the current local date and time."""

examples/datetime_simpletest.py

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# All rights reserved.
77
# SPDX-FileCopyrightText: 1991-1995 Stichting Mathematisch Centrum. All rights reserved.
88
# SPDX-FileCopyrightText: 2021 Brent Rubell for Adafruit Industries
9+
# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
910
# SPDX-License-Identifier: Python-2.0
1011

1112
# Example of working with a `datetime` object
@@ -28,3 +29,8 @@
2829
print(it)
2930

3031
print("Today is: ", dt.ctime())
32+
33+
iso_date_string = "2020-04-05T05:04:45.752301"
34+
print("Creating new datetime from ISO Date:", iso_date_string)
35+
isodate = datetime.fromisoformat(iso_date_string)
36+
print("Formatted back out as ISO Date: ", isodate.isoformat())

0 commit comments

Comments
 (0)