Skip to content

Instantly share code, notes, and snippets.

@alexjyong
Created February 26, 2024 21:43
Show Gist options
  • Save alexjyong/8931ceff0f6209b088d48e7f57a8bfca to your computer and use it in GitHub Desktop.
Save alexjyong/8931ceff0f6209b088d48e7f57a8bfca to your computer and use it in GitHub Desktop.
Script to calculate mars and moon time at a given location.
from datetime import datetime, timezone
import pytz
# Constants for Martian and Lunar day lengths in seconds
MARS_DAY_LENGTH = 88775.244
MOON_DAY_LENGTH = 2551442.8 # Average lunar day
# Function to get Earth time in a specific timezone
def get_earth_time(timezone):
tz = pytz.timezone(timezone)
return datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S')
# Function to get Mars time at a specific longitude
def get_mars_time(longitude):
now_utc = datetime.now(timezone.utc)
millis_since_midnight = (now_utc - now_utc.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() * 1000
mars_sol = (millis_since_midnight / (MARS_DAY_LENGTH * 1000)) * 360
local_mars_time_degrees = (mars_sol + longitude) % 360
# Convert degrees to hours, minutes, and seconds
total_seconds = (local_mars_time_degrees / 360) * MARS_DAY_LENGTH
hours = int(total_seconds // 3600)
minutes = int((total_seconds % 3600) // 60)
seconds = int(total_seconds % 60)
print(f'Mars Sol: {local_mars_time_degrees:.2f}°')
return f'Mars Time at longitude {longitude}: {hours:02d}:{minutes:02d}:{seconds:02d}'
# Function to get Moon time at a specific landmark
def get_moon_time(landmark_longitude):
millis_since_new_moon = (datetime.now(timezone.utc) - datetime(1970, 1, 7, 20, 35, 0, tzinfo=timezone.utc)).total_seconds() * 1000
moon_phase = (millis_since_new_moon / MOON_DAY_LENGTH) * 360
local_moon_time_degrees = (moon_phase + landmark_longitude) % 360
# Convert degrees to hours, minutes, and seconds
total_seconds = (local_moon_time_degrees / 360) * MOON_DAY_LENGTH
hours = int(total_seconds // 3600)
minutes = int((total_seconds % 3600) // 60)
seconds = int(total_seconds % 60)
print (f'Moon Phase: {local_moon_time_degrees:.2f}°')
return f'Moon Time at longitude {landmark_longitude}: {hours:02d}:{minutes:02d}:{seconds:02d}'
# Example usage
print(get_earth_time('America/New_York')) # Earth time in New York
print(get_mars_time(0)) # Mars time at Mars Prime Meridian
print(get_moon_time(0)) # Moon time at lunar prime meridian
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment