Skip to content

Instantly share code, notes, and snippets.

@tildebyte
Created November 22, 2024 23:26
Show Gist options
  • Save tildebyte/c375b0f0124fe258663ebdd83392a566 to your computer and use it in GitHub Desktop.
Save tildebyte/c375b0f0124fe258663ebdd83392a566 to your computer and use it in GitHub Desktop.
lunar phase calc
# 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