-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimezone_utils.py
More file actions
51 lines (43 loc) · 1.78 KB
/
timezone_utils.py
File metadata and controls
51 lines (43 loc) · 1.78 KB
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
"""
Central timezone utilities for TRACTools.
Handles CDT/CST conversion and ensures consistent timezone usage.
"""
from datetime import datetime
import zoneinfo
from typing import Optional
# Central US timezone (handles CDT/CST automatically)
CENTRAL_TZ = zoneinfo.ZoneInfo('America/Chicago')
def get_central_now() -> datetime:
"""Get current time in Central timezone (CDT/CST)."""
return datetime.now(CENTRAL_TZ)
def to_central(dt: datetime) -> datetime:
"""Convert any datetime to Central timezone."""
if dt.tzinfo is None:
# Assume naive datetime is already in Central time
return dt.replace(tzinfo=CENTRAL_TZ)
else:
# Convert from other timezone to Central
return dt.astimezone(CENTRAL_TZ)
def to_utc(dt: datetime) -> datetime:
"""Convert Central time to UTC for external libraries (like astropy)."""
if dt.tzinfo is None:
# Assume naive datetime is Central time
dt_central = dt.replace(tzinfo=CENTRAL_TZ)
else:
dt_central = dt
return dt_central.astimezone(zoneinfo.ZoneInfo('UTC'))
def central_to_naive(dt: datetime) -> datetime:
"""Convert Central timezone-aware datetime to naive datetime (for database storage)."""
if dt.tzinfo is None:
return dt # Already naive
central_dt = to_central(dt)
return central_dt.replace(tzinfo=None)
def naive_to_central(dt: datetime) -> datetime:
"""Convert naive datetime (assumed Central) to timezone-aware Central datetime."""
if dt.tzinfo is not None:
return to_central(dt) # Already timezone-aware
return dt.replace(tzinfo=CENTRAL_TZ)
def format_central_time(dt: datetime, fmt: str = '%Y-%m-%d %H:%M:%S %Z') -> str:
"""Format datetime in Central timezone."""
central_dt = to_central(dt)
return central_dt.strftime(fmt)