This library converts timezones obtained from Intl.DateTimeFormat().resolvedOptions().timeZone
to full country names.
go get -u github.com/medama-io/go-timezone-country
package main
import (
"fmt"
tz "github.com/medama-io/go-timezone-country"
)
func main() {
// Load the timezone to country name map during startup.
timezoneNameMap, err := tz.NewTimezoneCountryMap()
if err != nil {
panic(err)
}
// Obtain the country name from the timezone.
countryName, err := timezoneNameMap.GetCountryName("Europe/London")
if err != nil {
panic(err)
}
fmt.Println(countryName) // United Kingdom
}
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
The data is updated from time to time using the scripts/update.sh
script. In the case of timezone differences according to the IANA standard, we print the discrepancies while running the script and maintain the old timezone mappings in the data/missing.json
file. This data can be corrobated using the unicode-org/cldr-json repository.
It's also worth tracking the TC39 Canonical Timezone proposal, which is currently in Stage 3. This may be a future option to map timezones to countries natively and more accurately. The proposal can be found in the tc39/proposal-canonical-tz.
MIT
- The idea behind this library was inspired by a blog post from Talha Awan