Files
magpie/internal/gui/syshourcycle.go
T
yetone 50ce30d56e gui: times follow the system's 12 or 24 hour clock (iXerol on X)
An en-US Mac set to 24-Hour Time showed Routing's request times in 12
hours: no webview hands the page the system's clock, and navigator.language
carries only the region. boot.js now says it as hourCycle ("h23" or "h12",
left out when the system names none, and for a browser tab, which keeps its
own): macOS from AppleICUForce24HourTime / AppleICUForce12HourTime, Windows
from Control Panel\International sTimeFormat, Linux from gsettings
clock-format. intlLang() adds -u-hc-<cycle> to its tag, so every formatter
that takes it follows, and the places that forced 24 hours (hour12: false,
hourCycle h23) spread hourClock() instead: 24 unless the system says 12.
Read once per 30s, so a change in System Settings shows at the next load.

Run on mote: new hc-clock.test.cjs (en-US on a 24h system, en-GB on a 12h
one, a silent system), chromium and webkit; Go tests for the three readers
and boot.js; gofmt; go vet darwin, windows and linux nogui; internal/gui
tests; GUI files of gui-*, i18n-*, panel-*, quota-*, reset-*, routing-*,
usage-* in chromium and webkit. TestCoreScriptBudget was over by 77 bytes
and passes with two app.js comments cut, no raise.
Broken by hand: hc-clock goes red (2 of 3) without the intlLang suffix.
Not tried: a real Windows or Linux session's setting (the readers are
unit-tested on their formats), a live Mac.
2026-11-11 04:13:38 -08:00

82 lines
2.0 KiB
Go

package gui
import (
"strings"
"sync"
"time"
)
// The clock the system is set to show, 12-hour or 24-hour, which no
// webview hands the page: macOS's "24-Hour Time" switch is not in
// navigator.language, so an en-US Mac set to 24 hours showed 12 (iXerol on
// X). boot.js tells the page as "h23" or "h12", or leaves it out when the
// system says nothing beyond its region's own, and the page asks its
// formatters for it (intlLang in i18n.js).
// hourCycleCache holds the answer for a while: the page asks at every load,
// and macOS's costs two processes.
var hourCycleCache struct {
sync.Mutex
at time.Time
val string
}
// systemHourCycle is "h23", "h12", or "" when the system names none.
var systemHourCycle = cachedHourCycle
func cachedHourCycle() string {
c := &hourCycleCache
c.Lock()
defer c.Unlock()
if !c.at.IsZero() && time.Since(c.at) < 30*time.Second {
return c.val
}
c.val, c.at = readHourCycle(), time.Now()
return c.val
}
// forcedHourCycle is macOS's pair of switches (AppleICUForce24HourTime and
// AppleICUForce12HourTime, as `defaults read -g` prints them: "1", or a
// failure when the key is not there). The 24-hour one wins, as ICU reads
// them.
func forcedHourCycle(force24, force12 string) string {
switch {
case strings.TrimSpace(force24) == "1":
return "h23"
case strings.TrimSpace(force12) == "1":
return "h12"
}
return ""
}
// windowsHourCycle is the user's long time format (Control Panel →
// International, sTimeFormat): "HH:mm:ss" is 24 hours, "h:mm:ss tt" 12.
// Letters inside quotes are literal text, not a pattern.
func windowsHourCycle(format string) string {
var in bool
for _, r := range format {
switch {
case r == '\'':
in = !in
case in:
case r == 'H':
return "h23"
case r == 'h':
return "h12"
}
}
return ""
}
// gnomeHourCycle is gsettings' org.gnome.desktop.interface clock-format:
// '24h' or '12h'.
func gnomeHourCycle(out string) string {
switch strings.Trim(strings.TrimSpace(out), "'\"") {
case "24h":
return "h23"
case "12h":
return "h12"
}
return ""
}