Skip to content

Add methods to calculate the next full moon and next new moon #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/astral/moon.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,47 @@ def phase(date: Optional[datetime.date] = None) -> float:
if moon >= 28.0:
moon -= 28.0
return moon


def next_full_moon(date: Optional[datetime.date] = None) -> datetime.date:
"""Calculates the next full moon date after the specified date

Args:
date: Start date to calculate the next full moon from. Dates are always in the UTC timezone.
If not specified then today's date is used.

Returns:
The date of the next full moon.
"""

if date is None:
date = today()

end = date + datetime.timedelta(days=31)
for i in range((end - today).days):
day = date + datetime.timedelta(days=i)
calculated_phase = round(phase(date=day, rtype=float), 0)
if calculated_phase == 14:
return day


def next_new_moon(date: Optional[datetime.date] = None) -> datetime.date:
"""Calculates the next new moon date after the specified date

Args:
date: Start date to calculate the next new moon from. Dates are always in the UTC timezone.
If not specified then today's date is used.

Returns:
The date of the next new moon.
"""

if date is None:
date = today()

end = date + datetime.timedelta(days=31)
for i in range((end - today).days):
day = date + datetime.timedelta(days=i)
calculated_phase = round(phase(date=day, rtype=float), 0)
if calculated_phase == 0:
return day