Let the timezone picker be searched by country

Finding a zone meant already knowing its name, which is the wrong way round
when the thing you know is the country -- Ghana's zone is named after Accra,
and nothing in the picker said so.

Zones now carry the countries they cover, drawn from tzdata's own tables:
zone1970.tab and zone.tab for the zone-to-country-code rows, iso3166.tab for
the names. The picker already matched on description as well as label, so
putting the countries there makes them searchable and shows them in the list.

Those tables only name about 420 of the ~600 zones; the rest are
backward-compatibility aliases like America/Buenos_Aires and US/Pacific, which
tzdata ships as hard links to their canonical zone. Matching an alias to its
canonical name by inode lifts coverage to 553, and the 45 left are the Etc/GMT
pseudo-zones, which have no country by definition.

The inherited countries are the union of every zone sharing the inode, not the
first one found: those siblings are one clock under several countries' names,
and each table names a different subset, so first-match made Africa/Asmera
findable as Ethiopia but not as Eritrea.

Joining is done in ClockModel against a fixture, so it does not depend on the
host's tzdata version; the shell side only reads the files.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MhxtL7WzL5XmuTd8GHbcTY
This commit is contained in:
Harvey
2026-09-04 20:13:11 +02:00
co-authored by Claude Opus 5
parent 3d8bb1b1b3
commit 460217d059
5 changed files with 192 additions and 18 deletions
+71
View File
@@ -249,4 +249,75 @@ test("an unresolved clock keeps the width of the format it asked for", function
assert.strictEqual(bar.right[1].time, "--:--:--")
})
// A miniature stand-in for the tzdata files, so these tests do not depend on
// the host's tzdata version. Africa/Asmera is a hard link to Africa/Nairobi
// (inode 100) and carries no country row of its own.
var CATALOG = [
"@ISO",
"# a comment that must be skipped",
"ZA\tSouth Africa",
"LS\tLesotho",
"ER\tEritrea",
"ET\tEthiopia",
"CH\tSwitzerland",
"DE\tGermany",
"@ZONE",
"ZA,LS\t-2615+02800\tAfrica/Johannesburg",
"CH,DE\t+4723+00832\tEurope/Zurich\tBusingen",
"ER\t+1520+03853\tAfrica/Asmara",
"ET\t+0901+03842\tAfrica/Nairobi",
"@INODE",
"100\tAfrica/Nairobi",
"100\tAfrica/Asmara",
"100\tAfrica/Asmera",
"200\tAfrica/Johannesburg",
"300\tEtc/GMT+5",
"@LIST",
"Africa/Johannesburg",
"Europe/Zurich",
"Africa/Asmera",
"Etc/GMT+5"
].join("\n")
test("the catalogue tags zones with their countries", function () {
var cat = M.parseZoneCatalog(CATALOG)
var by = {}
cat.forEach(function (c) { by[c.zone] = c.countries })
assert.strictEqual(by["Africa/Johannesburg"], "South Africa, Lesotho", "all codes on the row")
assert.strictEqual(by["Europe/Zurich"], "Switzerland, Germany")
})
test("a legacy alias inherits countries from its hard-link siblings", function () {
var by = {}
M.parseZoneCatalog(CATALOG).forEach(function (c) { by[c.zone] = c.countries })
// Asmera has no row of its own; it shares inode 100 with Asmara and Nairobi,
// so it must be findable by BOTH of their countries, not just the first.
assert.ok(by["Africa/Asmera"].indexOf("Eritrea") !== -1, "Eritrea, via Asmara")
assert.ok(by["Africa/Asmera"].indexOf("Ethiopia") !== -1, "Ethiopia, via Nairobi")
})
test("zones with no country are listed, not dropped", function () {
var cat = M.parseZoneCatalog(CATALOG)
var etc = cat.filter(function (c) { return c.zone === "Etc/GMT+5" })[0]
assert.ok(etc, "still offered in the picker")
assert.strictEqual(etc.countries, "", "just has nothing to match a country search")
})
test("the catalogue is sorted and only contains listed zones", function () {
var cat = M.parseZoneCatalog(CATALOG)
var names = cat.map(function (c) { return c.zone })
assert.deepStrictEqual(names, names.slice().sort(), "sorted by zone")
assert.strictEqual(names.indexOf("Africa/Nairobi"), -1,
"Nairobi supplies country data but was not in @LIST, so it is not offered")
assert.strictEqual(names.length, 4)
})
test("a malformed or empty catalogue yields an empty list, not a crash", function () {
assert.deepStrictEqual(M.parseZoneCatalog(""), [])
assert.deepStrictEqual(M.parseZoneCatalog(null), [])
assert.deepStrictEqual(M.parseZoneCatalog("garbage\nwith no sections"), [])
assert.deepStrictEqual(M.parseZoneCatalog("@LIST\nAfrica/Cairo"),
[{ zone: "Africa/Cairo", countries: "" }], "a list with no country data still works")
})
console.log(passed + " passed" + (process.exitCode ? ", some FAILED" : ""))