|
1 | 1 | from datetime import datetime, timezone |
2 | 2 | from enum import Enum |
3 | | -import re |
4 | 3 |
|
5 | 4 | STRING_SEPARATOR = " | " |
6 | 5 |
|
@@ -51,63 +50,3 @@ def bbox_from_list(raw_bbox_list: list): |
51 | 50 | ) |
52 | 51 |
|
53 | 52 | return InlineList(list_bbox_val) |
54 | | - |
55 | | - |
56 | | -def to_iso8601(dt: datetime) -> str: |
57 | | - """ |
58 | | - Convert datetime to UTC ISO 8601 string, for both naive and aware datetimes. |
59 | | - """ |
60 | | - if dt.tzinfo is None: |
61 | | - # Treat naive datetime as UTC |
62 | | - dt = dt.replace(tzinfo=timezone.utc) |
63 | | - else: |
64 | | - # Convert to UTC |
65 | | - dt = dt.astimezone(timezone.utc) |
66 | | - |
67 | | - return dt.strftime("%Y-%m-%dT%H:%M:%SZ") |
68 | | - |
69 | | - |
70 | | -def datetime_to_string(data: datetime): |
71 | | - # normalize to UTC and format with Z |
72 | | - if data.tzinfo is None: |
73 | | - data = data.replace(tzinfo=timezone.utc) |
74 | | - else: |
75 | | - data = data.astimezone(timezone.utc) |
76 | | - return data.strftime("%Y-%m-%dT%H:%M:%SZ") |
77 | | - |
78 | | - |
79 | | -def datetime_from_string(value: str) -> datetime | None: |
80 | | - """ |
81 | | - Parse common ISO8601 datetime strings and return a timezone-aware datetime. |
82 | | - Accepts: |
83 | | - - 2025-12-17T12:34:56Z |
84 | | - - 2025-12-17T12:34:56+02:00 |
85 | | - - 2025-12-17T12:34:56+0200 |
86 | | - If no timezone is present, returns a UTC-aware datetime (assumption). |
87 | | - Returns None if parsing fails. |
88 | | - """ |
89 | | - if not isinstance(value, str): |
90 | | - return None |
91 | | - |
92 | | - s = value.strip() |
93 | | - # quick normalization: trailing Z -> +00:00 |
94 | | - if s.endswith("Z"): |
95 | | - s = s[:-1] + "+00:00" |
96 | | - |
97 | | - # normalize +0200 -> +02:00 |
98 | | - s = re.sub(r"([+-]\d{2})(\d{2})$", r"\1:\2", s) |
99 | | - |
100 | | - # Try stdlib first (requires offset with colon to return aware dt) |
101 | | - try: |
102 | | - dt = datetime.fromisoformat(s) |
103 | | - except Exception: |
104 | | - dt = None |
105 | | - |
106 | | - if dt is None: |
107 | | - return None |
108 | | - |
109 | | - # If dt is naive, assume UTC |
110 | | - if dt.tzinfo is None: |
111 | | - dt = dt.replace(tzinfo=timezone.utc) |
112 | | - |
113 | | - return dt |
0 commit comments