Spaces:
Running

File size: 3,440 Bytes
25834e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""

Date/time parsing and conversion utilities — framework-agnostic.



Consolidates three near-identical ``_parse_datetime`` copies found in

builders/stats.py, builders/query.py, and api/v1/keys.py into a single

canonical implementation.

"""

from __future__ import annotations

import math
from datetime import datetime, timezone
from typing import Any


def parse_datetime(value: Any) -> datetime | None:
    """Parse a date/time value into a timezone-aware UTC datetime.



    Accepts:

    - ``None`` → ``None``

    - ``int`` / ``float`` → treated as Unix epoch seconds

    - ``str`` ending in ``"Z"`` → converted to ``+00:00`` before parsing

    - Any ISO 8601 string (``datetime.fromisoformat``)



    Naive datetimes (no ``tzinfo``) are assumed to be UTC.



    Returns:

        A timezone-aware ``datetime`` in UTC, or ``None`` if *value* is ``None``

        or cannot be parsed.

    """
    if value is None:
        return None
    try:
        if isinstance(value, int | float):
            return datetime.fromtimestamp(int(value), tz=timezone.utc)
        raw = str(value)
        if raw.endswith("Z"):
            raw = raw[:-1] + "+00:00"
        dt = datetime.fromisoformat(raw)
        if dt.tzinfo is None:
            dt = dt.replace(tzinfo=timezone.utc)
        return dt.astimezone(timezone.utc)
    except (ValueError, OSError, OverflowError):
        return None


def to_unix_timestamp(dt: datetime | None, default: int | None = None) -> int | None:
    """Convert a datetime to a Unix timestamp integer.



    Naive datetimes are assumed to be UTC.  Returns *default* if *dt* is ``None``.



    Args:

        dt: The datetime to convert, or ``None``.

        default: Value to return when *dt* is ``None`` (defaults to ``None``).



    Returns:

        Unix timestamp as an integer, or *default* if *dt* is ``None``.

    """
    if dt is None:
        return default
    if dt.tzinfo is None:
        dt = dt.replace(tzinfo=timezone.utc)
    return int(dt.timestamp())


def to_unix_timestamp_ceil(dt: datetime | None) -> int | None:
    """Like ``to_unix_timestamp`` but rounds a fractional second UP.



    For a moment something starts: truncating would let it read as begun

    up to 999 ms early; rounding up errs on the side of not yet.

    """
    if dt is None:
        return None
    if dt.tzinfo is None:
        dt = dt.replace(tzinfo=timezone.utc)
    return math.ceil(dt.timestamp())


def as_aware_utc(dt: Any) -> datetime | None:
    """Normalize a stored datetime for comparison — Mongo returns naive

    UTC datetimes by default (the client is not ``tz_aware``)."""
    if not isinstance(dt, datetime):
        return None
    if dt.tzinfo is None:
        return dt.replace(tzinfo=timezone.utc)
    return dt.astimezone(timezone.utc)


def convert_to_gmt(expiration_time: str) -> datetime | None:
    """Parse an ISO 8601 string and convert to UTC.



    Returns ``None`` for naive (timezone-unaware) strings because an

    expiration time without a timezone is ambiguous.



    Args:

        expiration_time: ISO 8601 datetime string (with or without tzinfo).



    Returns:

        UTC-aware ``datetime``, or ``None`` if the input is timezone-naive.

    """
    dt = datetime.fromisoformat(expiration_time)
    if dt.tzinfo is None:
        return None
    return dt.astimezone(timezone.utc)