Light from your own sun
Abstract
How our home page knows it is dark in London at five in November, without asking anyone where they are.
Our home page changes its light through the day. Until this week it did so on a fixed timetable: dusk at a quarter past seven, night by half past eight. In a London November that is wrong by three hours. These are notes on how we fixed it.
Where is the visitor?
We did not want to ask. A permission prompt is the wrong first impression for a page whose job is to put a visitor at ease. But the browser already knows something useful: the visitor's time zone.
Intl.DateTimeFormat().resolvedOptions().timeZone // "Europe/London"
A time zone is not a location, but it is close enough for light. We keep a short table of about seventy major zones, each placed at its main city. A zone we do not know falls back to a typical latitude for its region, and a longitude worked out from its clock.1
When does the sun rise?
With a latitude, a longitude and a date, sunrise and sunset follow from a few lines of trigonometry. We use the approximations published by NOAA, which are good to a minute or two.2 That is far more precise than we need: each change of light takes about an hour.
Hanging the day on the sun
The page has eight lights, from deep night to noon. Before, each sat at a fixed hour. Now each hangs on the visitor's own sun:
| Light | When |
|---|---|
| Before dawn | up to 90 minutes before sunrise |
| Dawn | a quarter of an hour after sunrise |
| Noon | solar noon |
| Dusk | a few minutes before sunset |
| Blue hour | up to an hour after sunset |
| Night | from two and a half hours after sunset |
In London this evening, night arrives at about five. In June it waits until after ten. In Melbourne the seasons run the other way, and the page follows without anyone telling it to.
Before the first frame
One detail mattered more than the arithmetic. The light has to be known before the browser draws anything, or the page flashes its daytime paper before turning dark. So the light is worked out by a small script that runs first, in the head of the page, before the painting or the words.
None of this is meant to look like a feature. It is meant to feel like nothing at all: the page simply looks like the sky outside your window.
Notes
- Travellers see the sun of whatever time zone their device is set to. That matches the clock on their screen, which is the right thing to match. Back to the text
- The US National Oceanic and Atmospheric Administration publishes the equations in the documentation for its solar calculator. Back to the text