timezonecountry

package module
v0.0.0-...-db228bd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2024 License: MIT Imports: 3 Imported by: 0

README

go-timezone-country

This library converts timezones obtained from Intl.DateTimeFormat().resolvedOptions().timeZone to full country names.

Installation

go get -u github.com/medama-io/go-timezone-country

Usage

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
}

Contribution

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.

License

MIT

Acknowledgements

  • The idea behind this library was inspired by a blog post from Talha Awan

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TimezoneToCountry string

Functions

This section is empty.

Types

type TimezoneCountryMap

type TimezoneCountryMap map[string]string

TimezoneCountryMap is a map of all timezones to a country.

func NewTimezoneCountryMap

func NewTimezoneCountryMap() (TimezoneCountryMap, error)

NewTimezoneCountryMap creates a new TimezoneCountryMap using the embedded JSON.

func (TimezoneCountryMap) GetCountry

func (m TimezoneCountryMap) GetCountry(tz string) (string, error)

GetCountry returns the country name for a given timezone.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL