PanderMusubi / lunar-phase-calendar

Roling ICS calendar with lunar phases for two years in advance
Apache License 2.0
38 stars 5 forks source link

Bugfix for skipped moon phases #5

Closed azoellner closed 9 months ago

azoellner commented 10 months ago

Sometimes, a moon phase is skipped. This is caused by the phase float values of consecutive days skipping the according integer.

For example, the full moon on 2023-09-30 is missing. The phases and codes for the dates around that event are day phase code
2023-09-28 12.866777777777779 3
2023-09-29 13.955666666666668 3
2023-09-30 15.044555555555556 5
2023-10-01 16.133444444444443 5

Note that here the int(phase) == 14 is skipped, hence code == 4 is missing.

This can be fixed by doing a code correction whenever the difference between today's and yesterday's code modulo 8 is larger than 1.

Suggested fix:

diff --git a/generate.py b/generate.py
index daee891..16bddc0 100755
--- a/generate.py
+++ b/generate.py
@@ -56,14 +56,18 @@ def day_to_moon_phase_and_accurate_code(day):
     phase_today = phase(day)
     code_today = moon_phase_to_inacurate_code(phase_today)

-    if code_today % 2 != 0:
-        return phase_today, code_today
-
     phase_yesterday = phase(day - timedelta(days=1))
     code_yesterday = moon_phase_to_inacurate_code(phase_yesterday)

+    if (code_today - code_yesterday) % 8 > 1:
+        # skipped one code, hence do correction
+        return phase_today, (code_today - 1) % 8
+
+    if code_today % 2 != 0:
+        return phase_today, code_today
+
     if code_today == code_yesterday:
-        return phase_today, code_today + 1
+        return phase_today, (code_today + 1) % 8

     return phase_today, code_today

P.S. Thank you for those nice calendars.

PanderMusubi commented 9 months ago

Thanks, fixed in https://github.com/PanderMusubi/lunar-phase-calendar/commit/c20a69a85e8e95676555e0b712370837232e66b8#diff-801d46835a8aee4eba56246cd21be7ae8628884a17b2648ec6afc8786e319af8