Added fromisoformat() to date and datetime
This commit is contained in:
parent
70a4e69632
commit
df458b7257
2 changed files with 48 additions and 0 deletions
|
|
@ -29,6 +29,7 @@ Implementation Notes
|
|||
# pylint: disable=too-many-lines
|
||||
import time as _time
|
||||
import math as _math
|
||||
import re as _re
|
||||
from micropython import const
|
||||
|
||||
__version__ = "0.0.0-auto.0"
|
||||
|
|
@ -657,6 +658,18 @@ class date:
|
|||
y, m, d = _ord2ymd(ordinal)
|
||||
return cls(y, m, d)
|
||||
|
||||
@classmethod
|
||||
def fromisoformat(cls, date_string):
|
||||
"""Return a date object constructed from an ISO date format."""
|
||||
match = _re.match(
|
||||
r"([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])", date_string
|
||||
)
|
||||
if match:
|
||||
y, m, d = int(match.group(1)), int(match.group(2)), int(match.group(3))
|
||||
return cls(y, m, d)
|
||||
else:
|
||||
raise ValueError("Not a valid ISO Date")
|
||||
|
||||
@classmethod
|
||||
def today(cls):
|
||||
"""Return the current local date."""
|
||||
|
|
@ -1206,6 +1219,35 @@ class datetime(date):
|
|||
def fromtimestamp(cls, timestamp, tz=None):
|
||||
return cls._fromtimestamp(timestamp, tz is not None, tz)
|
||||
|
||||
@classmethod
|
||||
def fromisoformat(cls, date_string, tz=None):
|
||||
"""Return a date object constructed from an ISO date format.
|
||||
YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]]
|
||||
"""
|
||||
match = _re.match(
|
||||
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])?",
|
||||
date_string,
|
||||
)
|
||||
if match:
|
||||
y, m, d = int(match.group(1)), int(match.group(2)), int(match.group(3))
|
||||
hh = int(match.group(5)) if match.group(5) else 0
|
||||
mm = int(match.group(5)) if match.group(7) else 0
|
||||
ss = int(match.group(9)) if match.group(9) else 0
|
||||
us = round((float("0" + match.group(10)) if match.group(10) else 0) * 1e6)
|
||||
result = cls(
|
||||
y,
|
||||
m,
|
||||
d,
|
||||
hh,
|
||||
mm,
|
||||
ss,
|
||||
us,
|
||||
tz,
|
||||
)
|
||||
return result
|
||||
else:
|
||||
raise ValueError("Not a valid ISO Date")
|
||||
|
||||
@classmethod
|
||||
def now(cls, timezone=None):
|
||||
"""Return the current local date and time."""
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
# All rights reserved.
|
||||
# SPDX-FileCopyrightText: 1991-1995 Stichting Mathematisch Centrum. All rights reserved.
|
||||
# SPDX-FileCopyrightText: 2021 Brent Rubell for Adafruit Industries
|
||||
# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
|
||||
# SPDX-License-Identifier: Python-2.0
|
||||
|
||||
# Example of working with a `datetime` object
|
||||
|
|
@ -28,3 +29,8 @@ for it in tt:
|
|||
print(it)
|
||||
|
||||
print("Today is: ", dt.ctime())
|
||||
|
||||
iso_date_string = "2020-04-05T05:04:45.752301"
|
||||
print("Creating new datetime from ISO Date:", iso_date_string)
|
||||
isodate = datetime.fromisoformat(iso_date_string)
|
||||
print("Formatted back out as ISO Date: ", isodate.isoformat())
|
||||
|
|
|
|||
Loading…
Reference in a new issue