Skip to main content

2026-11-29 · 5 min read

21 tools later: search, favorites, and timezone math with nothing but Intl

DataAccessibility

Translated from the Chinese original: read it in Chinese

The site's tools started at 17: yen price comparison, METAR translator, CSP reader, election map, and so on. As each was added it got entrances (the top-right menu, Ctrl+K, the footer), but once there were many, the reaction from a user was: "many of your features are hard to find."

This time I did three things at once: added 4 new tools, made the old ones easier to find, and made sure the new tools' calculations are correct. What follows is what I actually did and actually verified.

1. "Easy to find": not one more entrance, but fewer lines to read

/tools used to be a 17-line list. There were already plenty of entrances; what was missing was a way to find things within the list. I added four things:

  • A search box: it reuses the matchItems from Ctrl+K (multiple words are ANDed, and titles that start with the term rank first). The string matched is "Chinese name + English name + description", so typing "tatami" (the Japanese floor-mat unit, written with a single Chinese character) finds the "tsubo / tatami / ㎡ converter", and typing "electricity" finds "server electricity cost", as long as the description really contains those words. When adding a tool, the description (desc) is its search keywords, so write in the words people will type.
  • Category filter: it reuses the four categories already in the top-right menu (travel, Japan, coding, data), with the count shown on each button.
  • Favorites: a star on the right of every row, stored in the browser's localStorage (key tool-favs), with no account and no upload. Favorites float to the top of the list.
  • "Similar tools" at the bottom of a tool page: after viewing a tool, it lists up to 4 from the same category, newest first.

Reading favorites with useSyncExternalStore

Favorites are in localStorage, which the server cannot read. Reading it in a component with useEffect and then calling setState makes the first screen flash, and the React Compiler lint complains. The site already has a standard way (the "learned it" list of the N5 kanji cards works like this):

const raw = useSyncExternalStore(subscribe, readRaw, () => "");
const favs = new Set(raw.split(" ").filter(Boolean));

The third argument is the server snapshot: always treated as "no favorites", and after hydration it switches to the real value, with no mismatch. When writing, it notifies subscribers itself, and multiple tabs in the same browser stay in sync through the storage event.

2. Flight time difference: only Intl, and daylight saving comes out right

One of the new tools is the "flight time converter": enter the local departure time plus the flight duration to get the local arrival time and whether it is the next day; or enter the local departure and arrival times and work out the flight duration.

The instinct is to install date-fns-tz or luxon. But browsers already include the IANA time zone database, and Intl.DateTimeFormat can tell you "the wall-clock time in a given zone at a given instant"; what is missing is only the reverse: given a wall-clock time, find which instant it is.

The approach is first to write "at a given instant, how many minutes ahead of UTC is this zone":

export function offsetMinutes(ms: number, tz: string): number {
  const p = Object.fromEntries(
    new Intl.DateTimeFormat("en-US", {
      timeZone: tz, hourCycle: "h23",
      year: "numeric", month: "numeric", day: "numeric",
      hour: "numeric", minute: "numeric", second: "numeric",
    }).formatToParts(new Date(ms)).map((x) => [x.type, Number(x.value)]),
  );
  // Put the "wall-clock time" back together as if it were UTC, subtract the real instant, and the difference is the offset
  return Math.round((Date.UTC(p.year, p.month - 1, p.day, p.hour, p.minute, p.second) - Math.floor(ms / 1000) * 1000) / 60000);
}

Then use it to convert wall-clock time back to an instant:

const guess = Date.UTC(y, mo - 1, d, h, mi);              // first assume this is UTC
const first = guess - offsetMinutes(guess, tz) * 60000;    // correct once with the offset at that instant
const second = guess - offsetMinutes(first, tz) * 60000;   // the offset may differ across a DST change, so correct again

It corrects twice because if the first guessed instant and the real instant straddle a daylight-saving switch, the offsets differ.

How I verified it: numbers pinned in unit tests

For this kind of thing, "looks right" does not count, so the tests pin several values I could confirm independently:

ScenarioResult
Taipei winter offsetMinutes480 (+8)
Tokyo540 (+9)
Los Angeles January / July−480 / −420 (1 hour of daylight saving)
Departing Kaohsiung 10:00, flying 2 h 50 min to JapanSame day 13:50, time difference +1
Departing Taipei 23:30, flying 11 hours to Los AngelesSame day 18:30 (crossing the date line, not the next day)

All 510 unit tests in the project pass, and the type check and eslint have no errors.

What I did not verify: the edge case of daylight saving's "hour that does not exist on the day of the switch" (clocks moving forward in spring). I wrote no test for it, and the tool's output for that moment is not guaranteed to be right. Most flights do not land exactly in that hour, but it is a known gap.

3. Another small tool tripped over: the year the era changes has two answers

The "Japanese era, Minguo and Gregorian year converter" converts only the year, not the month and day. That creates an unavoidable fact: 2019 is both Heisei 31 and Reiwa 1, and 1989 is both Showa 64 and Heisei 1. So the function returns an array rather than a single value:

erasOfYear(2019) // [{ name: "Reiwa", year: 1 }, { name: "Heisei", year: 31 }]

When the interface gets two results, it shows them side by side with a slash and adds a line, "the era changed partway through this year", rather than forcing a choice. For precision down to month and day, you need the start dates (Reiwa 2019-05-01, Heisei 1989-01-08).

4. A <dl> that the a11y scan blocked

On the timestamp converter page, I wrote each row like this:

<dl>
  <div>
    <dt>Taipei</dt>
    <dd>2023-11-15 06:13:20</dd>
    <button>Copy</button>   <!-- the problem is here -->
  </div>
</dl>

Visually there is no problem, but the project's axe scan (a11y-sitemap) reported definition-list (serious) in both the light and dark themes. Under <dl>, each group may contain only dt and dd, and a button cannot sit directly in the <div> beside them. Moving the button inside the dd fixed it:

<dd>
  <span>2023-11-15 06:13:20</span>
  <button>Copy</button>
</dd>

This kind of mistake cannot be seen by looking at the screen with your own eyes: the scan blocks it before going live, which is much cheaper than a screen-reader user discovering it afterwards.

Summary

  • Once there are many tools, "entrances" are not the gap; search, filtering and favorites within the list are. Write the description text as keywords.
  • Time-zone conversion does not necessarily need a library: reverse the offset with Intl.DateTimeFormat.formatToParts and correct twice, and daylight saving comes out right, but you have to test the edges yourself; I tested common scenarios and not "the hour that does not exist".
  • Read localStorage things with useSyncExternalStore and give the server snapshot an empty value, and there are no hydration problems.
  • Leave semantic errors in new interfaces to the a11y scan, which really did block one this time.

Type to search projects and posts too