From 7a9a9a5eb2e40ab4634f8bd50d870457ad6f1a02 Mon Sep 17 00:00:00 2001 From: Andrew Conlin Date: Mon, 14 Apr 2025 18:20:48 +0900 Subject: [PATCH] [2025-04-14] Handle timezone correctly with `zoneinfo` --- build/templates/init.html | 9 +++++++-- build/year-progress.py | 14 ++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/build/templates/init.html b/build/templates/init.html index 3944df9..2d43182 100644 --- a/build/templates/init.html +++ b/build/templates/init.html @@ -2,6 +2,7 @@
+
@@ -9,10 +10,14 @@ window.onload = function(e){ var d = new Date(); var timestamp = Math.round(d.getTime() / 1000); - input = document.getElementById("timestamp") - input.value = timestamp; + var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; + ts = document.getElementById("timestamp") + ts.value = timestamp; + tz = document.getElementById("timezone") + tz.value = timezone; form = document.getElementById("form") form.submit(); console.log(timestamp); + console.log(timezone); } diff --git a/build/year-progress.py b/build/year-progress.py index 06648fb..876619d 100644 --- a/build/year-progress.py +++ b/build/year-progress.py @@ -1,5 +1,6 @@ import datetime import math +from zoneinfo import ZoneInfo from flask import Flask, render_template, url_for, request app = Flask(__name__) @@ -174,12 +175,12 @@ def getHomoSapiens(yearPercent): else: return f"Homo Sapiens have been around for ~{-math.floor(yearsUntilHumans)} years" -def getTemplate(timestamp): - now = datetime.datetime.fromtimestamp(timestamp) +def getTemplate(timestamp,timezone): + now = datetime.datetime.fromtimestamp(timestamp).astimezone(ZoneInfo(timezone)) nowTime = now.strftime("%-I:%M%p") nowDate = now.strftime("%A") + " " + now.strftime("%-d") + " " + now.strftime("%B") - delta = now - datetime.datetime(day=1,month=1,year=now.year) + delta = now - datetime.datetime(day=1,month=1,year=now.year).astimezone(now.tzinfo) totalDays = datetime.datetime(day=1,month=1,year=now.year+1) - datetime.datetime(day=1,month=1,year=now.year) yearPercent = delta/totalDays @@ -193,8 +194,8 @@ def getTemplate(timestamp): return now.year, nowTime, nowDate, round(yearPercent*100,2), footballMinute, workDay, workTime, marathonMile, abbeyRoadSong, hobbitChapter, lat, long, homoSapiens -def index(timestamp): - year, nowTime, nowDate, yearPercent, footballMinute, workDay, workTime, marathonMile, abbeyRoadSong, hobbitChapter, lat, long, homoSapiens = getTemplate(timestamp) +def index(timestamp,timezone): + year, nowTime, nowDate, yearPercent, footballMinute, workDay, workTime, marathonMile, abbeyRoadSong, hobbitChapter, lat, long, homoSapiens = getTemplate(timestamp,timezone) cssPath = url_for("static", filename="style.css") return render_template("index.html",\ cssPath=cssPath,\ @@ -216,7 +217,8 @@ def index(timestamp): def init(): if request.method == "POST": timestamp = int(request.values.get('timestamp')) - return index(timestamp) + timezone = request.values.get('timezone') + return index(timestamp,timezone) else: return render_template("init.html")