Sun
Meeus, Astronomical Algorithms · checked against USNO and Skyfield
What it is
Sunrise and sunset are when the top of the sun touches the horizon. The air bends its light, so the sun’s centre is then 0.833° below it. Twilight is the light after sunset and before sunrise, in three steps by how far the sun is below the horizon. Soldiers and sailors plan around them: nautical twilight is when the horizon is still visible.
- Some days have no sunrise. Inside the polar circles the sun can stay up or down all day, and the answer must say so rather than invent a time.
- Events are asked for in a window of time, 24 hours from a UTC start, so no time zone is needed.
- It builds on astronomical time: the Julian day and the conversion to azimuth and altitude.
Operations
| Operation | Gives |
|---|---|
events |
Rise, set, transit and twilight in a windowIn: latitudeInDegrees, longitudeInDegrees, startUtc, windowInHoursOut: riseUtc, setUtc, transitUtc, civilDawnUtc, civilDuskUtc, nauticalDawnUtc, nauticalDuskUtc, astronomicalDawnUtc, astronomicalDuskUtc, isAlwaysUp, isAlwaysDown |
position |
The sun's azimuth and altitude from a placeIn: latitudeInDegrees, longitudeInDegrees, instantUtcOut: azimuthInDegrees, altitudeInDegrees |
Fields
| Field | Meaning |
|---|---|
latitudeInDegrees | −90 to 90, north positive |
longitudeInDegrees | −180 to 180, east positive |
startUtc | The window's start, ISO 8601 with Z. Usually midnight UTC; a few cases start at a place's local midnight. |
windowInHours | The window's length: 24 in every case here. More than 0 and at most 48. |
instantUtc | An instant, ISO 8601 with Z, read as UT1 |
riseUtc | The first moment in the window the top of the sun reaches the horizon: its centre at −0.833°, geometric. null if it doesn't happen in the window. To the second, compared within 60 s: USNO gives the minute. |
setUtc | As rise, going down |
transitUtc | The first moment in the window the sun crosses the meridian, highest in the sky. Left out when the sun stays down all window, where USNO gives none. |
civilDawnUtc | Centre at −6°, rising. null if it doesn't happen in the window |
civilDuskUtc | Centre at −6°, setting |
nauticalDawnUtc | Centre at −12°, rising |
nauticalDuskUtc | Centre at −12°, setting |
astronomicalDawnUtc | Centre at −18°, rising |
astronomicalDuskUtc | Centre at −18°, setting |
isAlwaysUp | true when the sun is above −0.833° for the whole window |
isAlwaysDown | true when it's below −0.833° for the whole window |
azimuthInDegrees | 0 up to 360, clockwise from north. Left out near the zenith. |
altitudeInDegrees | −90 to 90, of the sun's centre, geometric: no refraction |
Edge cases
| Case | What it tests |
|---|---|
usno-events-vancouver-local-dayevents | A window from local midnight, 08:00Z. |
usno-events-tonga-local-dayevents | A window from local midnight across the date line. |
usno-events-alert-2026-06-21events | Midnight sun: no rise or set, always up, and a transit. |
usno-events-tromso-2026-12-21events | Polar night with civil twilight at midday. |
usno-events-helsinki-2026-06-21events | It never gets darker than civil twilight: nautical and astronomical dawn and dusk are null. |
usno-events-vancouver-2026-09-21events | A UTC day: the set (evening before, local time) comes before the rise. |
usno-events-mcmurdo-2026-06-21events | Polar night: always down, but nautical and astronomical twilight still come and go. |
usno-events-amundsen-scott-2026-12-21events | Near the south pole in December: always up. |
skyfield-position-below-horizonposition | USNO lists the sun only when it's up; below the horizon Skyfield answers. |
usno-position-midnight-sunposition | Midnight sun, low in the north. |
invalid-events-year-10000events | Instants run from year −9999 to 9999: outOfRange. |
invalid-position-year-overflowposition | Too many digits for most integers, but still a year: outOfRange. |
Their inputs and expected values are in vectors.json; search for the case's id.
For agents
Follow Meeus, Astronomical Algorithms: chapter 25 for the sun’s apparent right ascension and declination, and 15 for rise, set and transit, or search the altitude through the window. Find events in the window from startUtc for windowInHours, never in a local calendar day, and return the first of each. Use −0.833° for rise and set and −6°, −12° and −18° for twilight, all geometric. When the sun doesn’t cross a level in the window, return null for it, and set isAlwaysUp or isAlwaysDown. Pass every case in vectors.json within its tolerance.
To check by hand against the book, 2nd edition (1998): example 25.a (p. 165) for the sun’s position. It works in dynamical time and gives right ascension and declination with no observer, so it checks a step, not a case in this file. Example 15.a (p. 103) is Venus, at −0.5667°, not the sun: it checks the rise and set method only.
Test data
vectors.json holds 631 cases, published 2026-11-04. Earth model: WGS84 latitude and longitude; UT1 read from the UTC given. How to read it: Test data format.
| Operation | Cases |
|---|---|
events |
584580 published · 580 reference · 12 edge · 120 polar · 4 invalid |
position |
4727 published · 17 reference · 4 edge · 3 invalid |
Tags say where each value came from: what they mean.
| Field | Tolerance |
|---|---|
riseUtc | ± 60 s |
setUtc | ± 60 s |
transitUtc | ± 60 s |
civilDawnUtc | ± 60 s |
civilDuskUtc | ± 60 s |
nauticalDawnUtc | ± 60 s |
nauticalDuskUtc | ± 60 s |
astronomicalDawnUtc | ± 60 s |
astronomicalDuskUtc | ± 60 s |
azimuthInDegrees | ± 0.01 |
altitudeInDegrees | ± 0.01 |
Every other field is compared exactly.
Implementations
| Implementation | Status |
|---|---|
| gshaw/algorithms-swiftSwift | Passes |
| gshaw/algorithms-typescriptTypeScript | Passes |
Each implementation's CI grades it against this page's test data on every push, and again when the data changes. The code isn't public yet. See how implementations work.
Source
| Rise, set, transit and civil twilight | Astronomical Applications API 4.0.1: Complete Sun and Moon Data for One DayUS Naval Observatory. Every request and response is in /sun/sources/usno.jsonl · Public domain |
| Position | Astronomical Applications API 4.0.1: Celestial Navigation Data, Hc and ZnUS Naval Observatory. USNO lists the sun only when it's up; below the horizon, Skyfield answers · Public domain |
| Nautical and astronomical twilight | Skyfield 1.55, almanac.dark_twilight_day, on JPL's DE421Brandon Rhodes. USNO's API doesn't give these. Checked against USNO's rise, set and civil twilight on every case. Every call is in /sun/sources/skyfield.txt · MIT |
| Method | Astronomical Algorithms, 2nd edition, 1998, chapters 15 and 25Jean Meeus. The method to follow; no value in the file comes from it |