Skip to content

fix: isDaytime returns wrong value near UTC midnight - #79

Merged
ceeK merged 1 commit into
mainfrom
chowell/fix-isdaytime-utc-midnight
Aug 30, 2026
Merged

ceeK merged 1 commit into
mainfrom
chowell/fix-isdaytime-utc-midnight

Conversation

@ceeK

@ceeK ceeK commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

TL;DR

The sun can be up while isDaytime says false. Happens every evening west of Greenwich, every morning east of it. Two forks independently patched this. Fixed here + regression tests.

The bug, in one example

  • Minneapolis, June 15, 20:30 local. Sunset is at 21:02. Sun is up. ☀️
  • But 20:30 CDT = 01:30 UTC on June 16 — UTC has already flipped to tomorrow.
  • isDaytime only checks the sunrise→sunset window of the UTC calendar day (June 16: sunrise 10:26 UTC → sunset 02:02 UTC June 17).
  • 01:30 is before that window → returns false. Wrong. 🌑

Mirror image in Tokyo: 08:30 JST is still yesterday in UTC → morning sun reads as night.

The fix

The correct window always exists — it just belongs to a neighbouring UTC day. So:

  1. Check the stored sunrise/sunset window first (the common case, zero extra work).
  2. Miss? Compute the windows for date − 24h and date + 24h and check those.
  3. A neighbouring day with no sunrise/sunset (polar circles) is skipped, not force-unwrapped — this is where Fix for incorrect results if local day is "tomorrow" in UTC #48's version would crash.

Boundary semantics unchanged: >= sunrise is day, < sunset flips to night. Polar day/night behaviour unchanged (that's #59, separate).

Tests

4 new tests pin both sides of the boundary in both directions:

Case Before After
Minneapolis 20:30 CDT (sun up) ❌ false ✅ true
Minneapolis 21:30 CDT (sun down) ✅ false ✅ false
Tokyo 08:30 JST (sun up) ❌ false ✅ true
Tokyo 02:30 JST (sun down) ✅ false ✅ false

swift test: 14/14 pass (all existing tests untouched and green).

Related

Closes the same defect reported via #44/#45/#48 (this version is nil-safe and adds no API). Root-cause option (anchoring calculate() to the solar day instead of the UTC day) is a behaviour-breaking change — worth considering for a major version, not this PR.

isDaytime compared date against the sunrise/sunset window anchored to
date's UTC calendar day. When the local solar day straddles UTC midnight
(evenings west of Greenwich, mornings east of it), the window containing
date belongs to the previous or next UTC day, so the sun could be up
while isDaytime returned false.

Check the stored window first, then fall back to the windows computed
for the adjacent UTC days, skipping days with no sunrise/sunset near the
polar circles. Boundary semantics (>= sunrise, < sunset) are unchanged.

Adds regression tests pinning both sides of the boundary in each
direction (Minneapolis evening, Tokyo morning).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PYPBCzQiXBvadC3y6Ae6FH
@ceeK
ceeK marked this pull request as ready for review August 30, 2026 22:11
@ceeK
ceeK merged commit df27c8e into main Aug 30, 2026
7 checks passed
@ceeK
ceeK deleted the chowell/fix-isdaytime-utc-midnight branch August 30, 2026 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant