[2025-04-14] Handle timezone correctly with zoneinfo
This commit is contained in:
parent
5489c69d04
commit
7a9a9a5eb2
@ -2,6 +2,7 @@
|
|||||||
<div style="visibility: hidden">
|
<div style="visibility: hidden">
|
||||||
<form id="form" method="POST" action="/">
|
<form id="form" method="POST" action="/">
|
||||||
<input name="timestamp" id="timestamp" type="number"></input>
|
<input name="timestamp" id="timestamp" type="number"></input>
|
||||||
|
<input name="timezone" id="timezone" type="text"></input>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</html>
|
</html>
|
||||||
@ -9,10 +10,14 @@
|
|||||||
window.onload = function(e){
|
window.onload = function(e){
|
||||||
var d = new Date();
|
var d = new Date();
|
||||||
var timestamp = Math.round(d.getTime() / 1000);
|
var timestamp = Math.round(d.getTime() / 1000);
|
||||||
input = document.getElementById("timestamp")
|
var timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||||
input.value = timestamp;
|
ts = document.getElementById("timestamp")
|
||||||
|
ts.value = timestamp;
|
||||||
|
tz = document.getElementById("timezone")
|
||||||
|
tz.value = timezone;
|
||||||
form = document.getElementById("form")
|
form = document.getElementById("form")
|
||||||
form.submit();
|
form.submit();
|
||||||
console.log(timestamp);
|
console.log(timestamp);
|
||||||
|
console.log(timezone);
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import math
|
import math
|
||||||
|
from zoneinfo import ZoneInfo
|
||||||
|
|
||||||
from flask import Flask, render_template, url_for, request
|
from flask import Flask, render_template, url_for, request
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@ -174,12 +175,12 @@ def getHomoSapiens(yearPercent):
|
|||||||
else:
|
else:
|
||||||
return f"Homo Sapiens have been around for ~{-math.floor(yearsUntilHumans)} years"
|
return f"Homo Sapiens have been around for ~{-math.floor(yearsUntilHumans)} years"
|
||||||
|
|
||||||
def getTemplate(timestamp):
|
def getTemplate(timestamp,timezone):
|
||||||
now = datetime.datetime.fromtimestamp(timestamp)
|
now = datetime.datetime.fromtimestamp(timestamp).astimezone(ZoneInfo(timezone))
|
||||||
nowTime = now.strftime("%-I:%M%p")
|
nowTime = now.strftime("%-I:%M%p")
|
||||||
nowDate = now.strftime("%A") + " " + now.strftime("%-d") + " " + now.strftime("%B")
|
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)
|
totalDays = datetime.datetime(day=1,month=1,year=now.year+1) - datetime.datetime(day=1,month=1,year=now.year)
|
||||||
|
|
||||||
yearPercent = delta/totalDays
|
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
|
return now.year, nowTime, nowDate, round(yearPercent*100,2), footballMinute, workDay, workTime, marathonMile, abbeyRoadSong, hobbitChapter, lat, long, homoSapiens
|
||||||
|
|
||||||
def index(timestamp):
|
def index(timestamp,timezone):
|
||||||
year, nowTime, nowDate, yearPercent, footballMinute, workDay, workTime, marathonMile, abbeyRoadSong, hobbitChapter, lat, long, homoSapiens = getTemplate(timestamp)
|
year, nowTime, nowDate, yearPercent, footballMinute, workDay, workTime, marathonMile, abbeyRoadSong, hobbitChapter, lat, long, homoSapiens = getTemplate(timestamp,timezone)
|
||||||
cssPath = url_for("static", filename="style.css")
|
cssPath = url_for("static", filename="style.css")
|
||||||
return render_template("index.html",\
|
return render_template("index.html",\
|
||||||
cssPath=cssPath,\
|
cssPath=cssPath,\
|
||||||
@ -216,7 +217,8 @@ def index(timestamp):
|
|||||||
def init():
|
def init():
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
timestamp = int(request.values.get('timestamp'))
|
timestamp = int(request.values.get('timestamp'))
|
||||||
return index(timestamp)
|
timezone = request.values.get('timezone')
|
||||||
|
return index(timestamp,timezone)
|
||||||
else:
|
else:
|
||||||
return render_template("init.html")
|
return render_template("init.html")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user