Fix timezones

This commit is contained in:
Midgard 2021-02-25 12:33:43 +01:00
parent 7481793dd4
commit f5120dfbd2
Signed by: midgard
GPG key ID: 511C112F1331BBB4

View file

@ -11,7 +11,7 @@ CHANNEL = os.environ["SUNBOT_ANNOUNCE_CHANNEL"]
AUTHORIZATION = "Bearer " + os.environ["SUNBOT_ANNOUNCE_MATTERMOST_TOKEN"]
URL = os.environ["SUNBOT_ANNOUNCE_MATTERMOST_API_ENDPOINT"] + "/posts"
GHENT = astral.LocationInfo("Gent", "Belgium", "Europe/Brussels", 51.05, 3.72)
LOCATION = astral.LocationInfo("Gent", "Belgium", "Europe/Brussels", 51.05, 3.72)
def post(message):
payload = {
@ -36,29 +36,29 @@ def wait_until(t, now):
def announce_sunrise(today):
print("Announcing sunrise", flush=True)
now = datetime.datetime.now(GHENT.tzinfo)
post(":sunrise: De zon komt op in Gent!\nZonnemiddag om {} (binnen {})\nZonsondergang om {} (binnen {})".format(
now = LOCATION.tzinfo.localize(datetime.datetime.now())
post(":sunrise: De zon komt op in Gent!\nZonnemiddag om {:%H:%M %Z} (binnen {})\nZonsondergang om {:%H:%M %Z} (binnen {})".format(
tomorrow["noon"],
format_in(now - tomorrow["noon"]),
format_in(tomorrow["noon"] - now),
tomorrow["sunset"],
format_in(now - tomorrow["sunset"]),
format_in(tomorrow["sunset"] - now),
))
def announce_sunset(tomorrow):
print("Announcing sunset", flush=True)
now = datetime.datetime.now(GHENT.tzinfo)
post(":city_sunset: De zon gaat onder in Gent!\nZonsopkomst om {} (binnen {})".format(
now = LOCATION.tzinfo.localize(datetime.datetime.now())
post(":city_sunset: De zon gaat onder in Gent!\nZonsopkomst om {:%H:%M %Z} (binnen {})".format(
tomorrow["sunrise"],
format_in(now - tomorrow["sunrise"]),
format_in(tomorrow["sunrise"] - now),
))
if __name__ == "__main__":
print("Starting Sunbot", flush=True)
while True:
today = sun(GHENT.observer, date=datetime.date.today())
tomorrow = sun(GHENT.observer, date=datetime.date.today() + datetime.timedelta(days=1))
now = datetime.datetime.now(GHENT.tzinfo)
now = LOCATION.tzinfo.localize(datetime.datetime.now())
today = sun(LOCATION.observer, date=now.date())
tomorrow = sun(LOCATION.observer, date=now.date() + datetime.timedelta(days=1))
if now >= today["sunset"]:
wait_until(tomorrow["sunrise"], now)