Created
February 26, 2025 22:31
-
-
Save sesh/aee286cc1105ec05655963904b6b203e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from zoneinfo import ZoneInfo | |
from datetime import datetime, timedelta, date, time | |
import sys | |
def describe_change(old_list, new_list, names_list): | |
diffs = [] | |
for old, new, name in zip(old_list, new_list, names_list): | |
old_tz, old_dst = old | |
new_tz, new_dst = new | |
if old_tz != new_tz: | |
if old_dst.total_seconds() > 0 and new_dst.total_seconds() == 0: | |
diffs.append(f"{name} stops observing DST") | |
elif old_dst.total_seconds() == 0 and new_dst.total_seconds() > 0: | |
diffs.append(f"{name} switches to DST") | |
else: | |
# non-dst timezone change (rare for our timezones!) | |
diffs.append(f"{name} changes: {old_tz - new_tz}") | |
print("Changes:") | |
for d in diffs: | |
print(f" - {d}") | |
def parse_args(args): | |
result = { | |
a.split("=")[0]: int(a.split("=")[1]) | |
if "=" in a and a.split("=")[1].isnumeric() | |
else a.split("=")[1] | |
if "=" in a | |
else True | |
for a in args | |
if "--" in a | |
} | |
result["[]"] = [a for a in args if not a.startswith('--')] | |
return result | |
def print_offsets(offsets): | |
printable = [] | |
for offset, dst in offsets: | |
printable.append((str(offset), dst.total_seconds() > 0)) | |
return str(printable) | |
def teezee(timezones, start_date, start_hour=5): | |
current_timezone = timezones[0] | |
time_of_day = datetime.combine(start_date, time(hour=start_hour, minute=0)) | |
offsets_at_start = [(tz.utcoffset(time_of_day), tz.dst(time_of_day)) for tz in timezones] | |
result = {} | |
for hours in range(24): | |
result[time_of_day.strftime("%H:%M")] = [] | |
for tz in timezones[1:]: | |
result[time_of_day.strftime("%H:%M")].append(time_of_day.astimezone(tz).strftime("%H:%M")) | |
time_of_day = time_of_day + timedelta(hours=1) | |
for k, v in result.items(): | |
times = [k] | |
times.extend(v) | |
print(",".join(times)) | |
morning = datetime.combine(start_date, time(hour=8, minute=0, second=0).replace(tzinfo=current_timezone)) | |
for day in range(365): | |
morning = morning + timedelta(days=1) | |
if offsets_at_start != [(tz.utcoffset(morning), tz.dst(morning)) for tz in timezones]: | |
print(f"Valid until {morning.date()}") | |
print() | |
describe_change(offsets_at_start, [(tz.utcoffset(morning), tz.dst(morning)) for tz in timezones], timezones) | |
break | |
return morning.date() | |
if __name__ == "__main__": | |
timezones = [ | |
ZoneInfo("Australia/Melbourne"), | |
ZoneInfo("America/Los_Angeles"), | |
] | |
args = parse_args(sys.argv[1:]) | |
if args["[]"]: | |
print([int(x) for x in args["[]"][0].split("-")]) | |
d = date(*[int(x) for x in args["[]"][0].split("-")]) | |
else: | |
d = date.today() | |
while True: | |
print(",".join(["Melbourne", "San Francisco"])) | |
d = teezee(timezones, d) | |
i = input("---") | |
if i != "": | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment