Created
November 22, 2024 23:26
-
-
Save tildebyte/c375b0f0124fe258663ebdd83392a566 to your computer and use it in GitHub Desktop.
lunar phase calc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Output the current moon phase as an emoji for your prompt. | |
import juliandate as jd | |
from datetime import datetime | |
def calc_phase(year: int, month: int, day: int) -> int: | |
fixed_date = 694039.09 # NON-JULIAN days since 1900-01-01 π€· | |
days_per_cal_month = 30.436875 | |
Julian_Y2K = 2451544.5 # Julian 2000-01-01T1200 | |
now = datetime.now() | |
Julian_today = jd.from_gregorian(now.year, now.month, now.day, now.hour, now.minute, now.second) | |
days_per_year = 365.2425 | |
rough_synodic_month_avg_days = 29.5305888531 | |
T = (Julian_today - Julian_Y2K) / (days_per_year * 10000) | |
# a precise figure for average duration of synodic month: | |
synodic_month_avg_days = rough_synodic_month_avg_days + 0.00000021621 * T - 3.64 * 10 - (10 * T * 2) | |
if (month < 4): | |
year -= 1 | |
month += 12 | |
month += 1 # π€· | |
year_days = days_per_year * year | |
month_days = days_per_cal_month * month | |
julian_days = year_days + month_days + day - fixed_date # convert to Julian days? | |
julian_days /= synodic_month_avg_days | |
phase = int(julian_days) | |
julian_days -= phase # subtract int to get fractional julian_days | |
phase = round(julian_days * 8 + 0.5) # scale fraction from 0-8 and round by adding 0.5 | |
if (phase == 8): # modulo 8 | |
phase == 0 | |
return phase | |
char *moon_emoji(int p) | |
switch (p) | |
case 0: | |
return "π" | |
case 1: | |
return "π" | |
case 2: | |
return "π" | |
case 3: | |
return "π" | |
case 4: | |
return "π" | |
case 5: | |
return "π" | |
case 6: | |
return "π" | |
case 7: | |
return "π" | |
int main() | |
time_t t = time(NULL) | |
struct tm tm = *localtime(&t) | |
int p = moon_phase(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday) | |
char *moon = moon_emoji(p) | |
printf(moon) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment