|
| 1 | +import datetime |
| 2 | +from collections.abc import Callable |
| 3 | +from functools import wraps |
| 4 | +from typing import overload |
| 5 | + |
| 6 | +from dateutil.relativedelta import relativedelta |
| 7 | + |
| 8 | + |
| 9 | +def _wrapper[T, **P](func: Callable[P, T]) -> Callable[P, T]: |
| 10 | + @wraps(func) |
| 11 | + def wrapper(*args: P.args, **kwargs: P.kwargs) -> T: |
| 12 | + kwargs["tzinfo"] = datetime.UTC |
| 13 | + return func(*args, **kwargs) |
| 14 | + |
| 15 | + return wrapper |
| 16 | + |
| 17 | + |
| 18 | +datetime_utc = _wrapper(datetime.datetime) |
| 19 | +""" |
| 20 | +Return a datetime object but with UTC information. |
| 21 | +""" |
| 22 | + |
| 23 | + |
| 24 | +def from_timestamp(ts: float) -> datetime.datetime: |
| 25 | + """Return a UTC datetime object from a timestamp. |
| 26 | +
|
| 27 | + :return: datetime object |
| 28 | + """ |
| 29 | + return datetime.datetime.fromtimestamp(ts, tz=datetime.UTC) |
| 30 | + |
| 31 | + |
| 32 | +def utc_now() -> datetime.datetime: |
| 33 | + """Get the current time in UTC. |
| 34 | +
|
| 35 | + :return: datetime object |
| 36 | + """ |
| 37 | + return datetime.datetime.now(tz=datetime.UTC) |
| 38 | + |
| 39 | + |
| 40 | +@overload |
| 41 | +def to_utc(dt: datetime.datetime) -> datetime.datetime: ... |
| 42 | + |
| 43 | + |
| 44 | +@overload |
| 45 | +def to_utc(dt: datetime.datetime | None) -> datetime.datetime | None: ... |
| 46 | + |
| 47 | + |
| 48 | +def to_utc(dt: datetime.datetime | None) -> datetime.datetime | None: |
| 49 | + """This converts a naive datetime object that represents UTC into |
| 50 | + an aware datetime object. |
| 51 | +
|
| 52 | + :return: datetime object, or None if `dt` was None. |
| 53 | + """ |
| 54 | + if dt is None: |
| 55 | + return None |
| 56 | + if dt.tzinfo is None: |
| 57 | + return dt.replace(tzinfo=datetime.UTC) |
| 58 | + if dt.tzinfo == datetime.UTC: |
| 59 | + # Already UTC. |
| 60 | + return dt |
| 61 | + return dt.astimezone(datetime.UTC) |
| 62 | + |
| 63 | + |
| 64 | +def strptime_utc(date_string: str, format: str) -> datetime.datetime: |
| 65 | + """Parse a string that describes a time but includes no timezone, |
| 66 | + into a timezone-aware datetime object set to UTC. |
| 67 | +
|
| 68 | + :raise ValueError: If `format` expects timezone information to be |
| 69 | + present in `date_string`. |
| 70 | + """ |
| 71 | + if "%Z" in format or "%z" in format: |
| 72 | + raise ValueError(f"Cannot use strptime_utc with timezone-aware format {format}") |
| 73 | + return to_utc(datetime.datetime.strptime(date_string, format)) |
| 74 | + |
| 75 | + |
| 76 | +def previous_months(number_of_months: int) -> tuple[datetime.date, datetime.date]: |
| 77 | + """Calculate date boundaries for matching the specified previous number of months. |
| 78 | +
|
| 79 | + :param number_of_months: The number of months in the interval. |
| 80 | + :returns: Date interval boundaries, consisting of a 2-tuple of |
| 81 | + `start` and `until` dates. |
| 82 | +
|
| 83 | + These boundaries should be used such that matching dates are on the |
| 84 | + half-closed/half-open interval `[start, until)` (i.e., start <= match < until). |
| 85 | + Only dates/datetimes greater than or equal to `start` and less than |
| 86 | + (NOT less than or equal to) `until` should be considered as matching. |
| 87 | +
|
| 88 | + `start` will be the first day of the designated month. |
| 89 | + `until` will be the first day of the current month. |
| 90 | + """ |
| 91 | + now = utc_now() |
| 92 | + start = now - relativedelta(months=number_of_months) |
| 93 | + start = start.replace(day=1) |
| 94 | + until = now.replace(day=1) |
| 95 | + return start.date(), until.date() |
| 96 | + |
| 97 | + |
| 98 | +def minute_timestamp(dt: datetime.datetime) -> datetime.datetime: |
| 99 | + """Minute resolution timestamp by truncating the seconds from a datetime object. |
| 100 | +
|
| 101 | + :param dt: datetime object with seconds resolution |
| 102 | + :return: datetime object with minute resolution |
| 103 | + """ |
| 104 | + return datetime.datetime(dt.year, dt.month, dt.day, dt.hour, dt.minute) |
0 commit comments