mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-11-13 21:10:29 +00:00
* Init ingress session boarder / lookup * Add session to API * Add cokkie validate * Do it without event bus * Add logger * Fix validation * Add tests * Update tests * Mock json storage
69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
"""Tools file for Hass.io."""
|
|
from datetime import datetime, timedelta, timezone, tzinfo
|
|
import logging
|
|
import re
|
|
from typing import Any, Dict, Optional
|
|
|
|
import pytz
|
|
|
|
UTC = pytz.utc
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
# Copyright (c) Django Software Foundation and individual contributors.
|
|
# All rights reserved.
|
|
# https://github.com/django/django/blob/master/LICENSE
|
|
DATETIME_RE = re.compile(
|
|
r"(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})"
|
|
r"[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})"
|
|
r"(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?"
|
|
r"(?P<tzinfo>Z|[+-]\d{2}(?::?\d{2})?)?$"
|
|
)
|
|
|
|
|
|
# Copyright (c) Django Software Foundation and individual contributors.
|
|
# All rights reserved.
|
|
# https://github.com/django/django/blob/master/LICENSE
|
|
def parse_datetime(dt_str):
|
|
"""Parse a string and return a datetime.datetime.
|
|
|
|
This function supports time zone offsets. When the input contains one,
|
|
the output uses a timezone with a fixed offset from UTC.
|
|
Raises ValueError if the input is well formatted but not a valid datetime.
|
|
Returns None if the input isn't well formatted.
|
|
"""
|
|
match = DATETIME_RE.match(dt_str)
|
|
if not match:
|
|
return None
|
|
kws: Dict[str, Any] = match.groupdict()
|
|
if kws["microsecond"]:
|
|
kws["microsecond"] = kws["microsecond"].ljust(6, "0")
|
|
tzinfo_str = kws.pop("tzinfo")
|
|
|
|
tzinfo_val: Optional[tzinfo] = None
|
|
if tzinfo_str == "Z":
|
|
tzinfo_val = UTC
|
|
elif tzinfo_str is not None:
|
|
offset_mins = int(tzinfo_str[-2:]) if len(tzinfo_str) > 3 else 0
|
|
offset_hours = int(tzinfo_str[1:3])
|
|
offset = timedelta(hours=offset_hours, minutes=offset_mins)
|
|
if tzinfo_str[0] == "-":
|
|
offset = -offset
|
|
tzinfo_val = timezone(offset)
|
|
else:
|
|
tzinfo_val = None
|
|
kws = {k: int(v) for k, v in kws.items() if v is not None}
|
|
kws["tzinfo"] = tzinfo_val
|
|
return datetime(**kws)
|
|
|
|
|
|
def utcnow() -> datetime:
|
|
"""Return the current timestamp including timezone."""
|
|
return datetime.now(UTC)
|
|
|
|
|
|
def utc_from_timestamp(timestamp: float) -> datetime:
|
|
"""Return a UTC time from a timestamp."""
|
|
return UTC.localize(datetime.utcfromtimestamp(timestamp))
|