Skip to content

fix(theme): take sun times from the local calendar day, not the UTC one - #3237

Merged
bbedward merged 2 commits into
duxlabs-dev:masterfrom
Mario-Mohar:fix/suncalc-local-reference-day
Aug 31, 2026
Merged

bbedward merged 2 commits into
duxlabs-dev:masterfrom
Mario-Mohar:fix/suncalc-local-reference-day

Conversation

@Mario-Mohar

Copy link
Copy Markdown
Contributor

Description

CalculateSunTimesWithTwilight picked its reference day from date.UTC(). The event seconds it computes are in local mean solar time and lonOffset converts 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:

  • the polar branches of CalculateSunTimes build their day from date.Year()/Month()/Day() in date.Location()
  • thememode.startOfNextDay uses next.Location()
  • the gamma scheduler keys its calcDay cache on now.Location() (wayland/manager.go:487) while feeding the same now into a UTC-day calculation

Only 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, 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; schedulerLoop clamps 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 SunTimes drive 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, 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 — for them date.Date() and date.UTC().Date() are the same.

Reproduced and verified against the reporter's table:

case before after
Kunming 07:30 CST sunrise 08-25 07:01, sunset 08-25 19:23, isLight=false, next 08-26 07:01 (past) sunrise 08-26 07:01, sunset 08-26 19:22, isLight=true, next 08-26 19:22
Kunming 08:05 CST sunrise 08-26 07:01, isLight=true unchanged
Los Angeles 18:00 PDT sunrise 08-27 06:36, isLight=false sunrise 08-26 06:35, isLight=true, next 08-26 19:12

quickshell/Common/suncalc.js is the unmodified upstream suncalc library, works in absolute Julian time and is only used by WeatherService for sun position — it does not have this bug and is untouched.

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that changes existing behavior)
  • Refactor / internal cleanup
  • Documentation
  • Other

Related issues

Fixes #3179

Screenshots / video

Not applicable — no visual change; the observable effect is which mode is reported and when.

Checklist

  • My code follows the conventions in CONTRIBUTING.md
  • I have tested my changes locally
  • New user-facing strings are wrapped in I18n.tr() with translator context, reusing existing terms where possible — no new strings in this PR
  • Go changes: ran make fmt, added/updated tests, make test passes, and go mod tidy is clean
  • QML changes: ran make lint-qml with no new warnings — no QML touched
  • I have opened a corresponding pull request in dlx-docs to document any new behaviors — not applicable, this restores the documented behaviour rather than changing it

Tests

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 disagree
  • TestSunTimesStableAcrossUTCMidnight — the times for one local day do not move when UTC midnight passes
  • TestSunTimesUnchangedForUTCCallers — a UTC-located date is unaffected

core/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 that nextTransition is never in the past
  • TestComputeLocationScheduleStableAcrossUTCMidnight — neither the mode nor nextTransition changes at UTC midnight

Both new files use time.FixedZone rather than time.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 past nextTransition), .../west_before_sunset_after_utc_midnight and TestComputeLocationScheduleStableAcrossUTCMidnight all fail with exactly the reported symptoms. go test ./... across core is otherwise green.

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.
@bbedward

Copy link
Copy Markdown
Member

/claude review

Comment thread core/internal/server/wayland/suncalc.go Outdated
Comment on lines +110 to +111
year, month, day := date.Date()
dayStart := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
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

claude Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Claude review

Core fix is right and the tests are well built; one residual edge case where it regresses behaviour.

  • Reference day is taken unconditionally from the caller's calendar day, so UTC+13/+14 zones west of the antimeridian (Apia, Kiritimati, Tongatapu, Chatham) now get every sun event a day late and report dark all day, core/internal/server/wayland/suncalc.go:110
    Checked: the arithmetic by hand for Kunming, Los Angeles, London and the dateline zones, every caller of CalculateSunTimes(WithTwilight) in thememode and wayland, and the two new test files for compile/name conflicts and whether their assertions hold. Model: claude-opus-5.

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.
@Mario-Mohar

Copy link
Copy Markdown
Contributor Author

Good catch — confirmed and fixed in 476baea4.

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:

antimeridian_west_longitude_east_offset
  sunrise is 2026-08-27T06:48:59+13:00, want it on the local day 2026-08-26
antimeridian_utc_plus_14
  sunrise is 2026-08-27T06:38:38+14:00, want it on the local day 2026-08-26
antimeridian_quarter_hour_offset
  sunrise is 2026-08-27T07:27:41+12:45, want it on the local day 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: dateOrbitAngle was still reading the civil day. For the antimeridian zones that put declination and the equation of time on a different day from the events derived from them, so it now takes the same reference day. No effect anywhere else.

Tests at both levels, and both fail without the change: three zones added to TestSunTimesUseLocalCalendarDay, and two cases in TestComputeLocationScheduleAcrossUTCOffsets covering the symptom itself — midday light, night dark. The night case passes either way and is there as the control.

Full core suite: 48 packages green, gofmt/vet/build clean.

@bbedward

Copy link
Copy Markdown
Member

/claude review

@claude

claude Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Claude review

No issues found. The antimeridian regression flagged in the previous review is genuinely fixed by 476baea4.

Checked: the new reference-day derivation by hand (localNoon.UTC().Add(-lonOffset).Date() yields the mean-solar-time date, which is exactly the day dayStart + solarSecs + lonOffset is anchored on) for Kunming UTC+8, Los Angeles UTC-7, London/UTC, and the dateline zones Apia UTC+13, Kiritimati UTC+14 and Chatham UTC+12:45 — all four events land inside the caller's local day in every case, and the result is unchanged for zones where the solar and civil dates agree; moving dateOrbitAngle onto dayStart is date-only so the dropped time-of-day is irrelevant; every caller (thememode.computeLocationSchedule including its startOfNextDay branch, wayland.recalcSchedule whose calcDay cache is keyed on the local civil day, and tomorrowDawn) stays consistent with the new anchoring, and nextTransition is strictly ahead of now in each branch so schedulerLoop cannot busy-loop; the two new test files for name collisions, Config field names and zero-value Manager safety, plus that their assertions actually hold at the computed times; quickshell/Common/suncalc.js and ThemeAutoService.qml are untouched. Model: claude-opus-5.

@bbedward
bbedward merged commit 8875095 into duxlabs-dev:master Aug 31, 2026
3 checks passed
@Mario-Mohar
Mario-Mohar deleted the fix/suncalc-local-reference-day branch September 2, 2026 13:21
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.

theme.auto/gamma location mode: sun times use the UTC calendar day, so light/dark is wrong for hours per day away from UTC±0

2 participants