Find the first day of the week with Go-CLDR

Noam Tenne
2 min readJun 3, 2020
https://pixabay.com/photos/font-lead-set-book-printing-705667/

Locale is a PITA.

We’ve all been there — what’s the first day of the week? Is it Monday or Sunday? In some countries it’s Friday.

Apparently there are many opinions around this discussion.

Luckily enough we have an organization such as Unicode. The good people at Unicode supply us with CLDR —Common Locale Data Repository.
CLDR is a free and open source collection of structured files, mostly XML, containing many details of standardized locale information.
One piece of information which interests us in particular is firstDay within the supplemental data collection:

In the snippet above we can see the firstDay tag containing an ISO day code mapped to a list of ISO territory (country, effectively) codes.
For example, “mon” (Monday) maps to the territory “DK” (Denmark).
Also worth noting the territory “001”, which is the fallback, default territory.

Many languages and frameworks offer a calendar implemented based on these distinctions, but Go does not. Until now, that is.

Go-CLDR

Is a small library that I’ve built.

The library build process first grabs the public CLDR archive.

I was pleasantly surprised to discover that the golang.org/x/text std lib already contains a CLDR parser so parsing the payload was basically free.

Once the payload is parsed I use Go’s cool code generation and template capabilities to generate a lookup table the maps territories to week days.

The result?

Of course much more locale information can be grabbed from the repository and generated into the library, if needed.

--

--