countries-and-timezones

repository·dev·Indexed 42 days ago

https://github.com/manuelmhtr/countries-and-timezones

A minimalistic library for working with country and timezone data, updated with the IANA timezones database. Version 3.10.0 provides utilities to retrieve country objects by ISO 3166-1 IDs, timezone objects by IANA names, and mappings between countries and their respective timezones.

Tokens
6.5K
Snippets
32
Records
34
Agent score
51%

What's inside countries-and-timezones

  1. Use countries-and-timezones in the Browser

    dev

    You can include the library in your project via a <script> tag using a CDN. The library exports a global variable named ct.

    <!-- Latest version -->
    <script
      src="https://cdn.jsdelivr.net/gh/manuelmhtr/countries-and-timezones@latest/dist/index.min.js"
      type="text/javascript"
    ></script>
    
    <!-- Or specify a version -->
    <script
      src="https://cdn.jsdelivr.net/gh/manuelmhtr/countries-and-timezones@v3.8.0/dist/index.min.js"
      type="text/javascript"
    ></script>
    
    <script type="text/javascript">
      var data = ct.getCountry("MX");
      console.log(data);
    </script>
  2. Get all countries with .getAllCountries()

    dev

    Returns an object containing the data for all countries, keyed by their country ID.

    const ct = require("countries-and-timezones");
    
    const countries = ct.getAllCountries();
    console.log(countries);
    
    /*
    Prints:
    {
      AD: {
        id: 'AD',
        name: 'Andorra',
        timezones: [ 'Europe/Andorra' ]
      },
      ...
    }
    */
  3. Get a country by ID with .getCountry()

    dev

    Returns a country object referenced by its ISO 3166-1 id.

    const ct = require("countries-and-timezones");
    
    const country = ct.getCountry("DE");
    console.log(country);
    
    /*
    Prints:
    {
      id: 'DE',
      name: 'Germany',
      timezones: [ 'Europe/Berlin', 'Europe/Zurich' ]
    }
    */
  4. Get the most relevant country for a timezone with .getCountryForTimezone()

    dev

    Returns the single most relevant country object (based on geographical location) that uses the provided timezone name.

    const ct = require("countries-and-timezones");
    
    const timezone = ct.getCountryForTimezone("Europe/Zurich");
    console.log(timezone);
    
    /*
    Prints:
    {
      "id": "CH",
      "name": "Switzerland",
      "timezones": [ "Europe/Zurich" ]
    }
    */
  5. Get all timezones with .getAllTimezones()

    dev

    Returns an object containing the data for all timezones, keyed by their timezone name.

    const ct = require("countries-and-timezones");
    
    const timezones = ct.getAllTimezones();
    console.log(timezones);
    
    /*
    Prints:
    {
      "Africa/Abidjan": {
        "name": "Africa/Abidjan",
        "countries": [ "CI", "BF", ... ],
        "utcOffset": 0,
        "utcOffsetStr": "+00:00",
        "dstOffset": 0,
        "dstOffsetStr": "+00:00",
        "aliasOf": null
      },
      ...
    }
    */
  6. Get a timezone by name with .getTimezone()

    dev

    Returns a timezone object referenced by its name from the IANA/tz database.

    const ct = require("countries-and-timezones");
    
    const timezone = ct.getTimezone("America/Los_Angeles");
    console.log(timezone);
    
    /*
    Prints:
    {
      name: 'America/Los_Angeles',
      countries: [ 'US' ],
      utcOffset: -480,
      utcOffsetStr: '-08:00',
      dstOffset: -420,
      dstOffsetStr: '-07:00',
      aliasOf: null
    }
    */
  7. Get timezones for a country with .getTimezonesForCountry()

    dev

    Returns an array of all timezone objects used within a specific country, identified by its id.

    const ct = require("countries-and-timezones");
    
    const timezones = ct.getTimezonesForCountry("MX");
    console.log(timezones);
    
    /*
    Prints:
    [
      {
        "name": "America/Bahia_Banderas",
        "countries": [ "MX" ],
        "utcOffset": -360,
        "utcOffsetStr": "-06:00",
        "dstOffset": -300,
        "dstOffsetStr": "-05:00",
        "aliasOf": null
      },
      ...
    ]
    */
  8. Get countries for a timezone with .getCountriesForTimezone()

    dev

    Returns a list of country objects that use a specific timezone name. Note: When a timezone is shared by multiple countries, the first element in the returned array is considered more relevant due to its geographical location.

    const ct = require("countries-and-timezones");
    
    const timezone = ct.getCountriesForTimezone("Europe/Zurich");
    console.log(timezone);
    
    /*
    Prints:
    [
      {
        "id": "CH",
        "name": "Switzerland",
        "timezones": [ "Europe/Zurich" ]
      },
      ...
    ]
    */
  9. Configure XO linting rules

    dev

    The project uses xo for linting. Configuration is defined in xo.config.ts using the FlatXoConfig type. You can specify files to ignore and override ESLint/XO rules.

    Key configuration options include:

    • ignores: An array of file patterns to skip during linting.
    • prettier: Boolean to enable Prettier integration.
    • space: Boolean to enable space-based formatting.
    • rules: An object containing specific rule overrides for @typescript-eslint, import-x, n, and unicorn plugins.
    import type { FlatXoConfig } from "xo";
    
    const xoConfig: FlatXoConfig = [
      {
        ignores: ["vite.config.ts"],
      },
      {
        prettier: true,
        space: true,
        rules: {
          "@typescript-eslint/ban-types": "off",
          "import-x/extensions": ["error", "never"],
          "unicorn/prevent-abbreviations": [
            "error",
            {
              allowList: {
                dst: true,
              },
            },
          ],
        },
      },
    ];
    
    export default xoConfig;