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
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])

View file

@ -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):