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.

Bands below the horizon: civil twilight to 6 degrees, nautical to 12, astronomical to 18, then night
  • 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

OperationGives
events Rise, set, transit and twilight in a windowIn: latitudeInDegrees, longitudeInDegrees, startUtc, windowInHours
Out: riseUtc, setUtc, transitUtc, civilDawnUtc, civilDuskUtc, nauticalDawnUtc, nauticalDuskUtc, astronomicalDawnUtc, astronomicalDuskUtc, isAlwaysUp, isAlwaysDown
position The sun's azimuth and altitude from a placeIn: latitudeInDegrees, longitudeInDegrees, instantUtc
Out: azimuthInDegrees, altitudeInDegrees

Fields

FieldMeaning
latitudeInDegrees−90 to 90, north positive
longitudeInDegrees−180 to 180, east positive
startUtcThe window's start, ISO 8601 with Z. Usually midnight UTC; a few cases start at a place's local midnight.
windowInHoursThe window's length: 24 in every case here. More than 0 and at most 48.
instantUtcAn instant, ISO 8601 with Z, read as UT1
riseUtcThe 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.
setUtcAs rise, going down
transitUtcThe 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.
civilDawnUtcCentre at −6°, rising. null if it doesn't happen in the window
civilDuskUtcCentre at −6°, setting
nauticalDawnUtcCentre at −12°, rising
nauticalDuskUtcCentre at −12°, setting
astronomicalDawnUtcCentre at −18°, rising
astronomicalDuskUtcCentre at −18°, setting
isAlwaysUptrue when the sun is above −0.833° for the whole window
isAlwaysDowntrue when it's below −0.833° for the whole window
azimuthInDegrees0 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

CaseWhat it tests
usno-events-vancouver-local-dayeventsA window from local midnight, 08:00Z.
usno-events-tonga-local-dayeventsA window from local midnight across the date line.
usno-events-alert-2026-06-21eventsMidnight sun: no rise or set, always up, and a transit.
usno-events-tromso-2026-12-21eventsPolar night with civil twilight at midday.
usno-events-helsinki-2026-06-21eventsIt never gets darker than civil twilight: nautical and astronomical dawn and dusk are null.
usno-events-vancouver-2026-09-21eventsA UTC day: the set (evening before, local time) comes before the rise.
usno-events-mcmurdo-2026-06-21eventsPolar night: always down, but nautical and astronomical twilight still come and go.
usno-events-amundsen-scott-2026-12-21eventsNear the south pole in December: always up.
skyfield-position-below-horizonpositionUSNO lists the sun only when it's up; below the horizon Skyfield answers.
usno-position-midnight-sunpositionMidnight sun, low in the north.
invalid-events-year-10000eventsInstants run from year −9999 to 9999: outOfRange.
invalid-position-year-overflowpositionToo 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.

OperationCases
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.

FieldTolerance
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

ImplementationStatus
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 twilightAstronomical 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
PositionAstronomical 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 twilightSkyfield 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
MethodAstronomical Algorithms, 2nd edition, 1998, chapters 15 and 25Jean Meeus. The method to follow; no value in the file comes from it