skycal

    A pure MoonBit astronomical calculations library: Julian dates, sunrise/sunset, solar position, twilight, golden hour and moon phases.

    astronomy
    sunrise
    sunset
    moon
    julian-date
    calendar
    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    1 hour ago
    Downloads
    2

    No dependencies

    #skycal

    CI

    A pure MoonBit astronomy & calendar library — Julian dates, sunrise/sunset, solar & lunar positions, twilight windows, moon phases, 24 solar terms, planetary positions and calendar conversions, with deterministic, fully testable algorithms.

    中文说明见下方 中文简介。

    #Features

    • Julian Date conversions — Gregorian ↔ JD/MJD round trips, exact at anchor epochs (J2000.0 = 2451545.0, MJD 0 = 1858-11-17)
    • Solar position — altitude and azimuth at any instant from any location (NOAA solar position algorithm)
    • Sunrise / sunset / solar noon — for a calendar day and geographic position, with polar day/night reported explicitly as None; an iterative variant targets ±0.5 min
    • Twilight windows — civil (-6°), nautical (-12°) and astronomical (-18°)
    • Golden & blue hours — photography-oriented event windows (sun between -6° and +6°)
    • Annual daylight statistics — total daylight hours, longest/shortest day, polar day/night day counts, monthly daylight table
    • Moon phases — illuminated fraction, phase angle, moon age and the conventional 8-phase enum (Meeus truncated series)
    • Moon position & rise/set — ecliptic longitude/latitude/distance, topocentric altitude/azimuth with parallax + refraction, moonrise/moonset/transit times
    • Sidereal time & coordinate transforms — GMST, local sidereal time, equatorial ↔ horizontal conversions, Bennett atmospheric refraction
    • 24 solar terms (节气) — solved by bisection on the sun's apparent longitude, ±1 min typical
    • Planetary positions — Mercury / Venus / Mars / Jupiter / Saturn geocentric RA/dec/distance (JPL mean elements, 1800-2050)
    • Calendar conversions — Julian calendar ↔ JD, Julian Day Number, Unix timestamps

    #Installation

    moon add jiujitong/skycal

    #Usage

    // Sunrise/sunset for Beijing on the 2026 summer solstice
    let pos = @skycal.make_geo(39.9042, 116.4074)
    match @skycal.sun_times(2026, 6, 21, pos) {
    Some(times) => {
    println(@skycal.jd_to_local_string(times.get_sunrise(), 8.0)) // 2026-06-21 04:46
    println(@skycal.format_minutes(times.daylight_minutes())) // 15h 00m
    }
    None => println("polar day or night")
    }

    // Moon phase right now
    let moon = @skycal.moon_phase(@skycal.julian_date(2026, 11, 23, 12, 0, 0))
    println(moon.get_phase().name()) // e.g. "Waxing Gibbous"
    println(moon.get_illumination()) // 0..1

    // Moonrise / moonset / transit (10-min scan + interpolation)
    match @skycal.moon_times(2026, 11, 23, pos) {
    Some(mt) => println(@skycal.jd_to_local_string(mt.get_transit(), 8.0))
    None => ()
    }

    // 24 solar terms for a year
    for term in @skycal.solar_terms(2026) {
    println(term.get_index()) // 0 = 春分 (March equinox), 23 = 惊蛰
    }

    // Planetary position (Jupiter tonight)
    let jup = @skycal.planet_altaz(@skycal.jupiter(), jd, pos)
    println(jup.0) // altitude, degrees

    // Golden hour (photography): sun between -6° and +6°
    match @skycal.golden_hours(2026, 6, 21, pos) {
    Some(g) => println(@skycal.jd_to_local_string(g.get_evening_start(), 8.0))
    None => ()
    }

    All instants are represented as Julian Dates (Double, UTC). Use jd_to_datetime / jd_to_local_string to convert to calendar values. See the generated interface in pkg.generated.mbti for the full API.

    #CLI demo

    moon run cmd/main # worked example (Beijing, 2026-06-21) moon run cmd/main -- 35.6762 139.6503 9 2026 12 21 # Tokyo winter solstice

    #Accuracy

    QuantityAccuracyNotes
    Solar declination / altitude±0.03°NOAA low-precision equations, valid 1900-2100
    Sunrise / sunset±2 minvs USNO published tables (see tests)
    Sunrise / sunset (precise)±0.5 miniterative variant, event-time solar parameters
    Equation of time±0.1 min
    Moon illumination~1%Meeus truncated series
    Moon phase timing~1 hour
    Moon ecliptic position~0.3° / ~0.01° / ~500 kmlongitude / latitude / distance (Meeus dominant terms)
    Moonrise / moonset±5 min10-min scan + linear interpolation
    24 solar terms±1 minapparent-longitude bisection
    Planet geocentric position~0.5°JPL mean elements + light-time, 1800-2050
    GMST~0.1 sMeeus 12.4 polynomial

    #Verification methodology

    Tests combine four independent layers (65 tests, all deterministic):

    1. Anchor epochs — J2000.0 and MJD 0 are exact by definition; leap-century rules are checked directly.
    2. Published reference tables — Beijing / New York / Tokyo solstice sunrise/sunset values are compared against USNO/NOAA published data with a ±2 min tolerance (e.g. Beijing 2026-06-21: 04:46 / 12:16 / 19:46 CST, daylight 15h00m).
    3. Physical invariants — declination bounded by ±23.44° all year, equation of time within ±16.5 min, equatorial equinox daylight ≈ 12 h, rise < noon < set, polar day/night reported as None, illumination within [0, 1].
    4. Determinism — repeated evaluations are bit-identical; no randomness anywhere.

    #Scope (explicitly NOT supported)

    • High-precision (arcsecond) ephemerides (VSOP87-class) — planetary positions here use JPL mean orbital elements (~0.5° accuracy, valid 1800-2050)
    • Solar/lunar eclipse prediction — out of scope
    • Chinese traditional calendar (农历/八字) — see justinwongcn/tyme4mb on mooncakes.io
    • Asteroid / comet tracking — out of scope
    • Timezones are left to the caller: all results are UTC Julian Dates; use jd_to_local_string for display. For |UTC offset| ≥ 12h, day boundaries may shift by one day.

    #Reference sources

    • Solar equations: General Solar Position Calculations, NOAA Global Monitoring Division (public domain), and the NOAA Solar Calculator methodology
    • Lunar phase: Jean Meeus, Astronomical Algorithms, 2nd ed., chapters 47-49 (mean elements + dominant periodic corrections)
    • All MoonBit code in this repository is written from scratch; no third-party code is included. The algorithms above are standard published science.

    #Development

    moon check --deny-warn # zero warnings policy moon test # 65 deterministic tests moon fmt && moon info # format + refresh interface moon run cmd/main # CLI demo

    #License

    MIT — see LICENSE.

    #中文简介

    skycal 是一个纯 MoonBit 的天文历法计算库:儒略日转换、日出日落(含 ±0.5 分钟高精度迭代法)、太阳/月亮位置(高度角/方位角,含视差与大气折射)、民用/航海/天文晨昏蒙影、摄影黄金时刻、月相(照度、月龄、八相)、月出月落月中天、恒星时与坐标转换、24 节气(黄经二分求解)、五大行星地心位置(JPL 平均轨道要素)、儒略历/JDN/Unix 时间戳转换、年日照统计(总时长/最长最短日/极昼极夜天数)。全部数值确定性、可复现,65 个测试与 USNO/NOAA 公开数据交叉验证(±2 分钟)。

    适用场景:摄影计划(黄金时刻)、太阳能面板选址、农业物联网(嵌入式设备按纬度计算日照)、观星计划(恒星时/月亮/行星)、日程应用、潮汐感知。安装 moon add jiujitong/skycal,示例:

    let pos = @skycal.make_geo(39.9042, 116.4074) // 北京
    match @skycal.sun_times(2026, 6, 21, pos) {
    Some(t) => println(@skycal.jd_to_local_string(t.get_sunrise(), 8.0)) // 2026-06-21 04:46
    None => println("polar day or night")
    }

    明确不做:角秒级星历(VSOP87)、日月食、农历八字(请用 tyme4mb)、小行星彗星。参考来源:NOAA 太阳位置计算文档、Meeus《Astronomical Algorithms》、JPL 平均轨道要素表。所有 MoonBit 代码为本仓库原创。

    GeoPos

    pub struct GeoPos {
    lat : Double
    lon : Double
    }

    GoldenHours

    pub struct GoldenHours {
    morning_start : Double
    morning_end : Double
    evening_start : Double
    evening_end : Double
    }

    GoldenHours::get_evening_end

    fn GoldenHours::get_evening_end(self : GoldenHours) -> Double

    GoldenHours::get_evening_start

    fn GoldenHours::get_evening_start(self : GoldenHours) -> Double

    GoldenHours::get_morning_end

    fn GoldenHours::get_morning_end(self : GoldenHours) -> Double

    GoldenHours::get_morning_start

    fn GoldenHours::get_morning_start(self : GoldenHours) -> Double

    MoonPhase

    pub struct MoonPhase {
    elongation : Double
    phase_angle : Double
    illumination : Double
    age_days : Double
    phase : Phase
    }

    MoonPhase::get_age_days

    fn MoonPhase::get_age_days(self : MoonPhase) -> Double

    MoonPhase::get_elongation

    fn MoonPhase::get_elongation(self : MoonPhase) -> Double

    MoonPhase::get_illumination

    fn MoonPhase::get_illumination(self : MoonPhase) -> Double

    MoonPhase::get_phase

    fn MoonPhase::get_phase(self : MoonPhase) -> Phase

    MoonPhase::get_phase_angle

    fn MoonPhase::get_phase_angle(self : MoonPhase) -> Double

    MoonPosition

    pub struct MoonPosition {
    longitude : Double
    latitude : Double
    distance : Double
    right_ascension : Double
    declination : Double
    }

    MoonPosition::get_declination

    fn MoonPosition::get_declination(self : MoonPosition) -> Double

    MoonPosition::get_distance

    fn MoonPosition::get_distance(self : MoonPosition) -> Double

    MoonPosition::get_latitude

    fn MoonPosition::get_latitude(self : MoonPosition) -> Double

    MoonPosition::get_longitude

    fn MoonPosition::get_longitude(self : MoonPosition) -> Double

    MoonPosition::get_right_ascension

    fn MoonPosition::get_right_ascension(self : MoonPosition) -> Double

    MoonTimes

    pub struct MoonTimes {
    moonrise : Double?
    moonset : Double?
    transit : Double
    }

    MoonTimes::get_moonrise

    fn MoonTimes::get_moonrise(self : MoonTimes) -> Double?

    MoonTimes::get_moonset

    fn MoonTimes::get_moonset(self : MoonTimes) -> Double?

    MoonTimes::get_transit

    fn MoonTimes::get_transit(self : MoonTimes) -> Double

    Phase

    pub enum Phase {
    NewMoon
    WaxingCrescent
    FirstQuarter
    WaxingGibbous
    FullMoon
    WaningGibbous
    LastQuarter
    WaningCrescent
    }

    Phase::name

    fn Phase::name(self : Phase) -> String

    Planet

    pub enum Planet {
    Mercury
    Venus
    Mars
    Jupiter
    Saturn
    }

    PlanetPosition

    pub struct PlanetPosition {
    right_ascension : Double
    declination : Double
    distance : Double
    longitude : Double
    }

    PlanetPosition::get_declination

    fn PlanetPosition::get_declination(self : PlanetPosition) -> Double

    PlanetPosition::get_distance

    fn PlanetPosition::get_distance(self : PlanetPosition) -> Double

    PlanetPosition::get_longitude

    fn PlanetPosition::get_longitude(self : PlanetPosition) -> Double

    PlanetPosition::get_right_ascension

    fn PlanetPosition::get_right_ascension(self : PlanetPosition) -> Double

    SolarTerm

    pub struct SolarTerm {
    index : Int
    jd : Double
    }

    SolarTerm::get_index

    fn SolarTerm::get_index(self : SolarTerm) -> Int

    SolarTerm::get_jd

    fn SolarTerm::get_jd(self : SolarTerm) -> Double

    SolarTerm::name

    fn SolarTerm::name(self : SolarTerm) -> String

    SunPosition

    pub struct SunPosition {
    altitude : Double
    azimuth : Double
    declination : Double
    right_ascension : Double
    }

    SunPosition::get_altitude

    fn SunPosition::get_altitude(self : SunPosition) -> Double

    SunPosition::get_azimuth

    fn SunPosition::get_azimuth(self : SunPosition) -> Double

    SunPosition::get_declination

    fn SunPosition::get_declination(self : SunPosition) -> Double

    SunPosition::get_right_ascension

    fn SunPosition::get_right_ascension(self : SunPosition) -> Double

    SunTimes

    pub struct SunTimes {
    sunrise : Double
    sunset : Double
    solar_noon : Double
    }

    SunTimes::daylight_minutes

    fn SunTimes::daylight_minutes(self : SunTimes) -> Double

    SunTimes::get_solar_noon

    fn SunTimes::get_solar_noon(self : SunTimes) -> Double

    SunTimes::get_sunrise

    fn SunTimes::get_sunrise(self : SunTimes) -> Double

    SunTimes::get_sunset

    fn SunTimes::get_sunset(self : SunTimes) -> Double

    YearDaylight

    pub struct YearDaylight {
    total_hours : Double
    longest_day : Double
    longest_date : (Int, Int)
    shortest_day : Double
    shortest_date : (Int, Int)
    polar_night_days : Int
    polar_day_days : Int
    }

    YearDaylight::get_longest_date

    fn YearDaylight::get_longest_date(self : YearDaylight) -> (Int, Int)

    YearDaylight::get_longest_day

    fn YearDaylight::get_longest_day(self : YearDaylight) -> Double

    YearDaylight::get_polar_day_days

    fn YearDaylight::get_polar_day_days(self : YearDaylight) -> Int

    YearDaylight::get_polar_night_days

    fn YearDaylight::get_polar_night_days(self : YearDaylight) -> Int

    YearDaylight::get_shortest_date

    fn YearDaylight::get_shortest_date(self : YearDaylight) -> (Int, Int)

    YearDaylight::get_shortest_day

    fn YearDaylight::get_shortest_day(self : YearDaylight) -> Double

    YearDaylight::get_total_hours

    fn YearDaylight::get_total_hours(self : YearDaylight) -> Double

    SOLAR_TERM_COUNT

    let SOLAR_TERM_COUNT : Int

    SOLAR_TERM_DEG

    let SOLAR_TERM_DEG : Double

    SYNODIC_MONTH

    let SYNODIC_MONTH : Double

    UNIX_EPOCH_JD

    let UNIX_EPOCH_JD : Double

    acos_deg

    fn acos_deg(x : Double) -> Double

    asin_deg

    fn asin_deg(x : Double) -> Double

    astronomical_twilight

    fn astronomical_twilight(year : Int, month : Int, day : Int, pos : GeoPos) -> (Double, Double)?

    atan2_deg

    fn atan2_deg(y : Double, x : Double) -> Double

    civil_twilight

    fn civil_twilight(year : Int, month : Int, day : Int, pos : GeoPos) -> (Double, Double)?

    cos_deg

    fn cos_deg(deg : Double) -> Double

    daylight_table

    fn daylight_table(year : Int, pos : GeoPos) -> Array[(Int, Double?)]

    days_in_month

    fn days_in_month(year : Int, month : Int) -> Int

    deg2rad

    fn deg2rad(deg : Double) -> Double

    ecliptic_to_equatorial

    fn ecliptic_to_equatorial(lon : Double, lat : Double, jd : Double) -> (Double, Double)

    equatorial_to_horizontal

    fn equatorial_to_horizontal(ra : Double, dec : Double, jd : Double, pos : GeoPos) -> (Double, Double)

    format_lat

    fn format_lat(lat : Double) -> String

    format_lon

    fn format_lon(lon : Double) -> String

    format_minutes

    fn format_minutes(minutes : Double) -> String

    from_mjd

    fn from_mjd(mjd : Double) -> Double

    from_unix_seconds

    fn from_unix_seconds(seconds : Double) -> (Int, Int, Int, Int, Int, Int)

    gmst

    fn gmst(jd : Double) -> Double

    golden_hours

    fn golden_hours(year : Int, month : Int, day : Int, pos : GeoPos) -> GoldenHours?

    horizontal_to_equatorial

    fn horizontal_to_equatorial(alt : Double, az : Double, jd : Double, pos : GeoPos) -> (Double, Double)

    jd_to_datetime

    fn jd_to_datetime(jd : Double) -> (Int, Int, Int, Int, Int, Int)

    jd_to_jdn

    fn jd_to_jdn(jd : Double) -> Int

    jd_to_julian_calendar

    fn jd_to_julian_calendar(jd : Double) -> (Int, Int, Int)

    jd_to_local_string

    fn jd_to_local_string(jd : Double, tz_offset_hours : Double) -> String

    jd_to_string

    fn jd_to_string(jd : Double) -> String

    jd_to_unix_seconds

    fn jd_to_unix_seconds(jd : Double) -> Double

    jdn_to_jd

    fn jdn_to_jd(jdn : Int) -> Double

    julian_calendar_to_jd

    fn julian_calendar_to_jd(year : Int, month : Int, day : Int) -> Double

    julian_centuries

    fn julian_centuries(jd : Double) -> Double

    julian_date

    fn julian_date(year : Int, month : Int, day : Int, hour : Int, minute : Int, second : Int) -> Double

    jupiter

    fn jupiter() -> Planet

    local_sidereal_time

    fn local_sidereal_time(jd : Double, lon : Double) -> Double

    make_geo

    fn make_geo(lat : Double, lon : Double) -> GeoPos

    mars

    fn mars() -> Planet

    mercury

    fn mercury() -> Planet

    moon_altaz

    fn moon_altaz(jd : Double, pos : GeoPos) -> (Double, Double)

    moon_phase

    fn moon_phase(jd : Double) -> MoonPhase

    moon_position

    fn moon_position(jd : Double) -> MoonPosition

    moon_times

    fn moon_times(year : Int, month : Int, day : Int, pos : GeoPos) -> MoonTimes?

    nautical_twilight

    fn nautical_twilight(year : Int, month : Int, day : Int, pos : GeoPos) -> (Double, Double)?

    new_moon_jd

    fn new_moon_jd(k : Int) -> Double

    normalize_deg

    fn normalize_deg(deg : Double) -> Double

    obliquity

    fn obliquity(jd : Double) -> Double

    pad2

    fn pad2(n : Int) -> String

    planet_altaz

    fn planet_altaz(p : Planet, jd : Double, pos : GeoPos) -> (Double, Double)

    planet_position

    fn planet_position(p : Planet, jd : Double) -> PlanetPosition

    rad2deg

    fn rad2deg(rad : Double) -> Double

    refract

    fn refract(geometric_alt : Double) -> Double

    saturn

    fn saturn() -> Planet

    sin_deg

    fn sin_deg(deg : Double) -> Double

    solar_apparent_longitude

    fn solar_apparent_longitude(jd : Double) -> Double

    solar_term_name

    fn solar_term_name(k : Int) -> String

    solar_terms

    fn solar_terms(year : Int) -> Array[SolarTerm]

    sun_position

    fn sun_position(jd : Double, pos : GeoPos) -> SunPosition

    sun_rise_set_precise

    fn sun_rise_set_precise(year : Int, month : Int, day : Int, pos : GeoPos) -> (Double, Double)?

    sun_times

    fn sun_times(year : Int, month : Int, day : Int, pos : GeoPos) -> SunTimes?

    tan_deg

    fn tan_deg(deg : Double) -> Double

    to_mjd

    fn to_mjd(jd : Double) -> Double

    to_unix_seconds

    fn to_unix_seconds(year : Int, month : Int, day : Int, hour : Int, minute : Int, second : Int) -> Double

    unix_seconds_to_jd

    fn unix_seconds_to_jd(seconds : Double) -> Double

    venus

    fn venus() -> Planet

    wrap_deg

    fn wrap_deg(deg : Double) -> Double

    year_daylight

    fn year_daylight(year : Int, pos : GeoPos) -> YearDaylight