| Server IP : 129.212.197.240 / Your IP : 216.73.216.19 Web Server : Apache/2.4.29 (Ubuntu) System : Linux snapshots-s-1vcpu-1gb-nyc1-261112-2000 4.15.0-197-generic #208-Ubuntu SMP Tue Nov 1 17:23:37 UTC 2022 x86_64 User : www-data ( 33) PHP Version : 7.2.24-0ubuntu0.18.04.15 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /snap/certbot/current/lib/python3.12/site-packages/pyrfc3339/ |
Upload File : |
import re
from datetime import UTC, datetime
from .utils import datetime_utcoffset
def parse(timestamp: str, utc: bool = False, produce_naive: bool = False) -> datetime:
"""
Parse an :RFC:`3339`-formatted timestamp and return a :class:`datetime.datetime`.
If the timestamp is presented in UTC, then the :attr:`~datetime.datetime.tzinfo` attribute of the
returned `datetime` will be set to :attr:`datetime.timezone.utc`.
>>> parse('2009-01-01T10:01:02Z')
datetime.datetime(2009, 1, 1, 10, 1, 2, tzinfo=datetime.timezone.utc)
Otherwise, a :class:`datetime.timezone` instance is created with the appropriate offset, and
the :attr:`~datetime.datetime.tzinfo` attribute of the returned :class:`~datetime.datetime` is set to that value.
>>> parse('2009-01-01T14:01:02-04:00')
datetime.datetime(2009, 1, 1, 14, 1, 2, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000)))
However, if :meth:`parse()` is called with :python:`utc=True`, then the returned
:class:`~datetime.datetime` will be normalized to UTC (and its :attr:`~datetime.datetime.tzinfo` attribute set to
:attr:`~datetime.timezone.utc`), regardless of the input timezone.
>>> parse('2009-01-01T06:01:02-04:00', utc=True)
datetime.datetime(2009, 1, 1, 10, 1, 2, tzinfo=datetime.timezone.utc)
As parsing is delegated to :meth:`datetime.datetime.fromisoformat()`, certain
timestamps which do not strictly adhere to :RFC:`3339` are nonetheless accepted.
>>> parse('2009-01-01T06:01:02')
datetime.datetime(2009, 1, 1, 6, 1, 2)
Exceptions will, however, be thrown for blatantly invalid input:
>>> parse('2009-01-01T25:01:02Z') # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError: hour must be in 0..23
:param str timestamp: the :RFC:`3339` timestamp to be parsed
:param bool utc: :const:`True` to normalize the timestamp to UTC; :const:`False` otherwise. Defaults to :const:`False`.
:param bool produce_naive: :const:`True` if the produced :class:`~datetime.datetime` instance should
not have a timezone attached (that is, be 'naive'); :const:`False` otherwise.
Defaults to :const:`False`.
:return: the parsed timestamp
:rtype: datetime.datetime
"""
# Python does not recognize "Z" as an alias for "+00:00", so we perform the
# substitution here.
timestamp = re.sub("Z$", "+00:00", timestamp, flags=re.IGNORECASE)
dt_out = datetime.fromisoformat(timestamp)
if utc:
dt_out = dt_out.astimezone(UTC)
if produce_naive:
if datetime_utcoffset(dt_out) == 0:
dt_out = dt_out.replace(tzinfo=None)
else:
raise ValueError("cannot produce a naive datetime from a local timestamp")
return dt_out