-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdst_test.py
More file actions
36 lines (27 loc) · 1.18 KB
/
dst_test.py
File metadata and controls
36 lines (27 loc) · 1.18 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
from datetime import datetime, timedelta
import pytz
def get_period(hour):
if 4 <= hour <= 11:
return "morning"
if 12 <= hour <= 16:
return "afternoon"
if 17 <= hour <= 21:
return "evening"
return "night"
def run_dst(start_date, label):
print(f"\n--- {label} ---")
tz = pytz.timezone("US/Eastern")
start_dt = tz.localize(datetime.strptime(start_date, "%Y-%m-%d"))
# Generate 72 hours using UTC offsets to handle DST transitions correctly
times_utc = [start_dt.astimezone(pytz.utc) + timedelta(hours=i) for i in range(72)]
times_local = [t.astimezone(tz) for t in times_utc]
slices = [(4, 17), (17, 28), (28, 41), (41, 52)]
for start, end in slices:
subset = times_local[start:end]
periods = sorted(list(set(get_period(t.hour) for t in subset)))
times_str = f"{subset[0].strftime('%H:%M')} ({subset[0].strftime('%Z')}) to {subset[-1].strftime('%H:%M')} ({subset[-1].strftime('%Z')})"
print(
f"Slice [{start}:{end}]: {times_str} | Periods ({len(periods)}): {', '.join(periods)}"
)
run_dst("2024-03-10", "Spring Forward (2024-03-10)")
run_dst("2024-11-03", "Fall Back (2024-11-03)")