Repository navigation
fix(theme): take sun times from the local calendar day, not the UTC one - #3237
Conversation
CalculateSunTimesWithTwilight read its reference day off date.UTC(). The event seconds it computes are local mean solar time and lonOffset converts them to UTC, so the instant of a local day's sunrise lands on the previous UTC day east of Greenwich and a local sunset on the next one west of it. Whenever the local and UTC calendars disagree, the function therefore returned a neighbouring day's times. Everything around it already works in local calendar days: the polar branches of CalculateSunTimes build their day from date.Year/Month/Day, thememode's startOfNextDay uses next.Location(), and the gamma scheduler keys its calcDay cache on now.Location(). Only the reference day inside the calculation disagreed. Effect at UTC+8 (Kunming, 25.04N 102.72E): between local sunrise and 08:00 local, computeLocationSchedule got yesterday's sunrise and sunset, both already past, fell through to the next-day branch and got today's sunrise, also past. It reported dark with nextTransition in the past, which schedulerLoop clamps to a one-second wait, so it re-evaluated every second until 08:00 local and then flipped to light on its own. West of UTC the mirror image: at UTC-7 the mode went dark from 17:00 local, when the UTC date rolls over, until the real sunset. The same SunTimes drive the gamma/night-light scheduler, so night light shifted by a day the same way. Fix: derive the reference day from date's own calendar day, keeping dayStart anchored at UTC midnight so the lonOffset arithmetic is unchanged, and take the orbit angle from the same day. Callers whose date is already in UTC are unaffected. Tests: suncalc_refday_test.go covers the reference day east and west of UTC, stability across UTC midnight, and that UTC callers do not move. manager_location_test.go is new to the thememode package and covers computeLocationSchedule at both offsets, including that nextTransition is never in the past.
|
/claude review |
| year, month, day := date.Date() | ||
| dayStart := time.Date(year, month, day, 0, 0, 0, 0, time.UTC) |
There was a problem hiding this comment.
Edge case, but a regression: zones on the far side of the antimeridian now get every event a day late.
With this anchor the events land at localMidnight(D) + solarSecs + delta, where delta = zoneOffset - lon/15. That is fine while |delta| < 12h (true almost everywhere), but for UTC+13/+14 zones west of the antimeridian delta ≈ +24.5h: Apia (UTC+13, lon -171.8), Kiritimati (UTC+14, lon -157.4), Tongatapu, Chatham. There every event lands on D+1, so computeLocationSchedule takes the now.Before(times.Sunrise) branch all day and reports dark around the clock. Before this change those zones were correct for the hours where the UTC date was still D-1 (i.e. local morning), so this makes them worse, not better.
Picking the reference day from delta instead of using D unconditionally covers both; it is a no-op wherever |delta| < 12h, so the cases in the new tests are unaffected:
| year, month, day := date.Date() | |
| dayStart := time.Date(year, month, day, 0, 0, 0, 0, time.UTC) | |
| year, month, day := date.Date() | |
| _, zoneOffset := date.Zone() | |
| dayShift := int(math.Floor((12 + float64(zoneOffset)/3600 - lon/15) / 24)) | |
| dayStart := time.Date(year, month, day-dayShift, 0, 0, 0, 0, time.UTC) |
Claude reviewCore fix is right and the tests are well built; one residual edge case where it regresses behaviour.
|
Taking the reference day straight off the caller's calendar day is right for nearly every zone, but not for one shifted a full day away from its own meridian. Apia, Kiritimati, Tongatapu and the Chathams all sit on west longitudes while keeping a UTC+12:45..+14 offset, so their mean solar time and their civil time are a day apart. Every sun event landed on the following local day there, and the location schedule reported dark around the clock. Anchor on the caller's local noon and convert that to mean solar time to pick the reference day. For every other zone this resolves to the same date as before, so Kunming, Los Angeles and London are unchanged to the second. dateOrbitAngle now takes the same reference day, so declination and the equation of time no longer belong to a different day than the events computed from them. Covered at both levels: three antimeridian zones in the suncalc test, and two cases in the location schedule test for the symptom itself. Both fail without this change.
|
Good catch — confirmed and fixed in The reference day was taken straight off the caller's calendar day, which is right for nearly every zone but wrong for one shifted a full day from its own meridian. Apia, Kiritimati, Tongatapu and the Chathams sit on west longitudes while keeping a UTC+12:45..+14 offset, so their mean solar time and their civil time are about 24 hours apart and the solar day is a different date. Reproduced for 2026-08-26: Every event a day late, so the location schedule reported dark around the clock — exactly as described. The fix anchors on the caller's local noon and converts that to mean solar time to pick the reference day: year, month, day := date.Date()
localNoon := time.Date(year, month, day, 12, 0, 0, 0, date.Location())
refYear, refMonth, refDay := localNoon.UTC().Add(-lonOffset).Date()
dayStart := time.Date(refYear, refMonth, refDay, 0, 0, 0, 0, time.UTC)Where the solar and civil day are the same date — everywhere except these zones — this resolves to exactly what the previous line produced, so Kunming, Los Angeles and London are unchanged to the second. One thing beyond the report: Tests at both levels, and both fail without the change: three zones added to Full |
|
/claude review |
Claude reviewNo issues found. The antimeridian regression flagged in the previous review is genuinely fixed by Checked: the new reference-day derivation by hand ( |
Description
CalculateSunTimesWithTwilightpicked its reference day fromdate.UTC(). The event seconds it computes are in local mean solar time andlonOffsetconverts them to UTC, so the instant of a given local day's sunrise lands on the previous UTC day for eastern longitudes and a local sunset on the next UTC day for western ones. Whenever the local and UTC calendars disagree — which is several hours of every day for any offset away from UTC±0 — the function returned a neighbouring day's times.Everything around the calculation already speaks in local calendar days:
CalculateSunTimesbuild their day fromdate.Year()/Month()/Day()indate.Location()thememode.startOfNextDayusesnext.Location()calcDaycache onnow.Location()(wayland/manager.go:487) while feeding the samenowinto a UTC-day calculationOnly the reference day inside the calculation disagreed.
Effect at UTC+8 (Kunming, 25.04N 102.72E), as reported: between local sunrise and 08:00 local,
computeLocationSchedulegot yesterday's sunrise and sunset — both already past — fell through to the next-day branch and got today's sunrise, also past. It reported dark withnextTransitionin the past;schedulerLoopclamps a non-positive wait to one second, so it re-evaluated every second until 08:00 local and then flipped to light on its own. West of UTC the mirror image: at UTC-7 the mode went dark from 17:00 local, when the UTC date rolls over, until the real sunset.The same
SunTimesdrive the gamma/night-light scheduler, so night light shifted by a day in the same way.Fix: derive the reference day from
date's own calendar day, keepingdayStartanchored at UTC midnight so thelonOffsetarithmetic is unchanged, and take the orbit angle from the same day. Callers whosedateis already in UTC are unaffected — for themdate.Date()anddate.UTC().Date()are the same.Reproduced and verified against the reporter's table:
08-25 07:01, sunset08-25 19:23,isLight=false, next08-26 07:01(past)08-26 07:01, sunset08-26 19:22,isLight=true, next08-26 19:2208-26 07:01,isLight=true08-27 06:36,isLight=false08-26 06:35,isLight=true, next08-26 19:12quickshell/Common/suncalc.jsis the unmodified upstream suncalc library, works in absolute Julian time and is only used byWeatherServicefor sun position — it does not have this bug and is untouched.Type of change
Related issues
Fixes #3179
Screenshots / video
Not applicable — no visual change; the observable effect is which mode is reported and when.
Checklist
I18n.tr()with translator context, reusing existing terms where possible — no new strings in this PRmake fmt, added/updated tests,make testpasses, andgo mod tidyis cleanmake lint-qmlwith no new warnings — no QML touchedTests
core/internal/server/wayland/suncalc_refday_test.go(new):TestSunTimesUseLocalCalendarDay— all four events land on the caller's local day, east and west of UTC, at hours where the two calendars disagreeTestSunTimesStableAcrossUTCMidnight— the times for one local day do not move when UTC midnight passesTestSunTimesUnchangedForUTCCallers— a UTC-located date is unaffectedcore/internal/server/thememode/manager_location_test.go(new, first test file in that package):TestComputeLocationScheduleAcrossUTCOffsets— the reported mode at five points east of, west of and at UTC, and thatnextTransitionis never in the pastTestComputeLocationScheduleStableAcrossUTCMidnight— neither the mode nornextTransitionchanges at UTC midnightBoth new files use
time.FixedZonerather thantime.LoadLocation, so they need no tzdata and carry no DST ambiguity. Counter-check: with the fix reverted,TestSunTimesUseLocalCalendarDay,TestSunTimesStableAcrossUTCMidnight,TestComputeLocationScheduleAcrossUTCOffsets/east_after_sunrise_before_utc_midnight(both the mode and the pastnextTransition),.../west_before_sunset_after_utc_midnightandTestComputeLocationScheduleStableAcrossUTCMidnightall fail with exactly the reported symptoms.go test ./...acrosscoreis otherwise green.