Sleep Cycle Calculator
Calculate optimal sleep and wake times based on 90-minute sleep cycles.
Frequently Asked Questions
Code Implementation
from datetime import datetime, timedelta
SLEEP_CYCLE_MINUTES = 90
FALL_ASLEEP_MINUTES = 14 # Average time to fall asleep
def wake_times_from_bedtime(bedtime: datetime, cycles: int = 5) -> list[datetime]:
"""Given a bedtime, return optimal wake-up times after N full 90-min cycles."""
fall_asleep = bedtime + timedelta(minutes=FALL_ASLEEP_MINUTES)
return [
fall_asleep + timedelta(minutes=SLEEP_CYCLE_MINUTES * i)
for i in range(1, cycles + 1)
]
def bedtimes_from_wake(wake_time: datetime, cycles: int = 5) -> list[datetime]:
"""Given a desired wake time, return bedtimes that allow N full 90-min cycles."""
return [
wake_time - timedelta(minutes=SLEEP_CYCLE_MINUTES * i + FALL_ASLEEP_MINUTES)
for i in range(1, cycles + 1)
]
# Example: bedtime at 11 PM
bedtime = datetime.now().replace(hour=23, minute=0, second=0, microsecond=0)
print(f"Bedtime: {bedtime.strftime('%I:%M %p')}")
print("Best wake times:")
for t in wake_times_from_bedtime(bedtime):
print(f" {t.strftime('%I:%M %p')} ({(t - bedtime).seconds // 60} min)")
# Example: must wake at 7 AM
wake = datetime.now().replace(hour=7, minute=0, second=0, microsecond=0)
print(f"\nWake time: {wake.strftime('%I:%M %p')}")
print("Best bedtimes:")
for t in bedtimes_from_wake(wake):
print(f" {t.strftime('%I:%M %p')}")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.