Fix timezone handling, don’t display timedelta during full moon

This commit is contained in:
Thomas Renger 2024-11-01 16:29:18 +01:00
parent 8acc8a0af8
commit c70159e31b
2 changed files with 46 additions and 38 deletions

View file

@ -1,6 +1,7 @@
import datetime
import moon import moon
from mastodon import Mastodon from mastodon import Mastodon
from pytz import timezone
from datetime import datetime
# Set up Mastodon # Set up Mastodon
mastodon = Mastodon( mastodon = Mastodon(
@ -9,18 +10,14 @@ mastodon = Mastodon(
api_base_url = 'https://mastodon.wazongtest.de/' api_base_url = 'https://mastodon.wazongtest.de/'
) )
tz = timezone('Europe/Berlin')
now = datetime.datetime.now() now = datetime.now().astimezone(tz)
moon_text="Aktuelle Mondphase:\n" moon_text="Aktuelle Mondphase:\n"
moon_text+=moon.moon_phase_as_text(now) moon_text+=moon.moon_phase_as_text(now)
moon_text+="\n"
moon_text+=moon.till_next_full_as_text(now)
print(moon_text) print(moon_text)
mastodon.status_post(moon_text) mastodon.status_post(moon_text, visibility='unlisted')
me = mastodon.me() me = mastodon.me()
print(me['id'])
print(mastodon.account_statuses(me['id'])[0]) print(mastodon.account_statuses(me['id'])[0])

View file

@ -1,11 +1,13 @@
import datetime
import math import math
from pytz import timezone
from datetime import datetime
from datetime import timedelta
def next_full_moon(start): def next_full_moon(start):
# y2k = datetime.datetime(2000, 1, 1) # date of the known full moon
y2k = datetime.datetime(1999, 12, 22, 18, 31, 18) y2k = datetime(1999, 12, 22, 18, 31, 18).astimezone(timezone('Europe/Berlin'))
since2000 = (start - y2k) / datetime.timedelta(days=365.25) since2000 = (start - y2k) / timedelta(days=365.25)
rads = 3.14159265359/180 rads = 3.14159265359/180
phase = 0.5 # 0.5 equals full moon phase = 0.5 # 0.5 equals full moon
@ -63,28 +65,37 @@ def next_full_moon(start):
else: else:
jahr = c - 4715 jahr = c - 4715
return datetime.datetime(jahr, monat, tag, stunde, minute, sekunde) fmutc = datetime(jahr, monat, tag, stunde, minute, sekunde, tzinfo=timezone('UTC'))
return fmutc.astimezone(start.tzinfo)
def moon_phase_as_text(mday): def is_full_moon(mday: datetime):
nmoon = next_full_moon(mday) nmoon = next_full_moon(mday)
till_next_full=nmoon-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") return("🌕 Vollmond")
if (till_next_full < datetime.timedelta(days=6)): if (till_next_full < timedelta(days=6)):
return("🌔 Zunehmender Dreiviertelmond") return("🌔 Zunehmender Dreiviertelmond\n" + till_next_full_text)
if (till_next_full < datetime.timedelta(days=10)): if (till_next_full < timedelta(days=10)):
return("🌓 Zunehmender Halbmond") return("🌓 Zunehmender Halbmond\n" + till_next_full_text)
if (till_next_full < datetime.timedelta(days=14)): if (till_next_full < timedelta(days=14)):
return("🌒 Zunehmender Sichelmond") return("🌒 Zunehmender Sichelmond\n" + till_next_full_text)
if (till_next_full < datetime.timedelta(days=16)): if (till_next_full < timedelta(days=16)):
return("🌑 Neumond") return("🌑 Neumond\n" + till_next_full_text)
if (till_next_full < datetime.timedelta(days=20)): if (till_next_full < timedelta(days=20)):
return("🌘 Abnehmender Sichelmond") return("🌘 Abnehmender Sichelmond\n" + till_next_full_text)
if (till_next_full < datetime.timedelta(days=24)): if (till_next_full < timedelta(days=24)):
return("🌗 Abnehmender Halbmond") return("🌗 Abnehmender Halbmond\n" + till_next_full_text)
if (till_next_full < datetime.timedelta(days=28)): if (till_next_full < timedelta(days=28.5)):
return("🌖 Abnehmender Dreiviertelmond") return("🌖 Abnehmender Dreiviertelmond\n" + till_next_full_text)
return("🌕 Vollmond") return("🌕 Vollmond")
def strfdelta(tdelta, fmt): def strfdelta(tdelta, fmt):