From c70159e31bf2f0b67e3df43e0d633dfaf7a317ab Mon Sep 17 00:00:00 2001 From: Thomas Renger Date: Fri, 1 Nov 2024 16:29:18 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20timezone=20handling,=20don=E2=80=99t=20di?= =?UTF-8?q?splay=20timedelta=20during=20full=20moon?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/bot.py | 13 ++++------ bot/moon.py | 71 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 08a4df6..d25ae9e 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -1,6 +1,7 @@ -import datetime import moon from mastodon import Mastodon +from pytz import timezone +from datetime import datetime # Set up Mastodon mastodon = Mastodon( @@ -9,18 +10,14 @@ mastodon = Mastodon( api_base_url = 'https://mastodon.wazongtest.de/' ) - -now = datetime.datetime.now() +tz = timezone('Europe/Berlin') +now = datetime.now().astimezone(tz) moon_text="Aktuelle Mondphase:\n" moon_text+=moon.moon_phase_as_text(now) -moon_text+="\n" -moon_text+=moon.till_next_full_as_text(now) print(moon_text) -mastodon.status_post(moon_text) +mastodon.status_post(moon_text, visibility='unlisted') me = mastodon.me() -print(me['id']) - print(mastodon.account_statuses(me['id'])[0]) \ No newline at end of file diff --git a/bot/moon.py b/bot/moon.py index 77578ab..d566ca6 100644 --- a/bot/moon.py +++ b/bot/moon.py @@ -1,14 +1,16 @@ -import datetime import math +from pytz import timezone +from datetime import datetime +from datetime import timedelta def next_full_moon(start): - # y2k = datetime.datetime(2000, 1, 1) - y2k = datetime.datetime(1999, 12, 22, 18, 31, 18) - since2000 = (start - y2k) / datetime.timedelta(days=365.25) + # date of the known full moon + y2k = datetime(1999, 12, 22, 18, 31, 18).astimezone(timezone('Europe/Berlin')) + since2000 = (start - y2k) / timedelta(days=365.25) rads = 3.14159265359/180 - phase=0.5 # 0.5 equals full moon + phase = 0.5 # 0.5 equals full moon # Anzahl der Mondphasen seit 2000 k = math.floor((since2000)*12.36853087)+phase @@ -34,10 +36,10 @@ def next_full_moon(start): z = math.floor(JDE + 0.5) f = (JDE + 0.5) - math.floor(JDE + 0.5) if (z < 2299161): - a = z + a = z else: - g = math.floor((z - 1867216.25) / 36524.25) - a = z + 1 + g - math.floor(g / 4) + g = math.floor((z - 1867216.25) / 36524.25) + a = z + 1 + g - math.floor(g / 4) b = a + 1524 c = math.floor((b - 122.1) / 365.25) d = math.floor(365.25 * c) @@ -54,37 +56,46 @@ def next_full_moon(start): tag = math.floor(tag_temp) if (e < 14): - monat = e -1 + monat = e -1 else: - monat = e - 13 + monat = e - 13 if (monat > 2): - jahr = c - 4716 + jahr = c - 4716 else: - jahr = c - 4715 + jahr = c - 4715 + + fmutc = datetime(jahr, monat, tag, stunde, minute, sekunde, tzinfo=timezone('UTC')) - return datetime.datetime(jahr, monat, tag, stunde, minute, sekunde) + return fmutc.astimezone(start.tzinfo) - -def moon_phase_as_text(mday): +def is_full_moon(mday: datetime): nmoon = next_full_moon(mday) till_next_full=nmoon-mday - if (till_next_full < datetime.timedelta(days=1)): + if (till_next_full < timedelta(days=1) or till_next_full < timedelta(days=28)): + return True + return False + +def moon_phase_as_text(mday: datetime): + nmoon = next_full_moon(mday) + till_next_full = nmoon - mday + till_next_full_text = strfdelta(till_next_full, "{days} Tage {hours} Stunden bis Vollmond") + if (till_next_full < timedelta(days=1)): return("🌕 Vollmond") - if (till_next_full < datetime.timedelta(days=6)): - return("🌔 Zunehmender Dreiviertelmond") - if (till_next_full < datetime.timedelta(days=10)): - return("🌓 Zunehmender Halbmond") - if (till_next_full < datetime.timedelta(days=14)): - return("🌒 Zunehmender Sichelmond") - if (till_next_full < datetime.timedelta(days=16)): - return("🌑 Neumond") - if (till_next_full < datetime.timedelta(days=20)): - return("🌘 Abnehmender Sichelmond") - if (till_next_full < datetime.timedelta(days=24)): - return("🌗 Abnehmender Halbmond") - if (till_next_full < datetime.timedelta(days=28)): - return("🌖 Abnehmender Dreiviertelmond") + if (till_next_full < timedelta(days=6)): + return("🌔 Zunehmender Dreiviertelmond\n" + till_next_full_text) + if (till_next_full < timedelta(days=10)): + return("🌓 Zunehmender Halbmond\n" + till_next_full_text) + if (till_next_full < timedelta(days=14)): + return("🌒 Zunehmender Sichelmond\n" + till_next_full_text) + if (till_next_full < timedelta(days=16)): + return("🌑 Neumond\n" + till_next_full_text) + if (till_next_full < timedelta(days=20)): + return("🌘 Abnehmender Sichelmond\n" + till_next_full_text) + if (till_next_full < timedelta(days=24)): + return("🌗 Abnehmender Halbmond\n" + till_next_full_text) + if (till_next_full < timedelta(days=28.5)): + return("🌖 Abnehmender Dreiviertelmond\n" + till_next_full_text) return("🌕 Vollmond") def strfdelta(tdelta, fmt):