geo-tz

repository·master·Indexed 19 days ago

https://github.com/evansiroky/node-geo-tz

A Node.js module for looking up IANA timezone identifiers from GPS coordinates (latitude and longitude). It provides three data products: a default set alike since 1970, a comprehensive set for historical accuracy, and a 'same since now' set for current and future timekeeping. Features include configurable caching via setCache(), pre-caching for performance, and support for oceanic timezone lookups via Etc/GMT identifiers.

Tokens
3.2K
Snippets
16
Records
18
Agent score
66%

What's inside geo-tz

  1. Choose between different timezone data products

    master

    As of version 8, you can choose between three different data products based on your accuracy and file size requirements.

    Important for TypeScript users: You may need to use the dist entry points for compatibility.

    1. Alike Since 1970 (Default)

    Uses unioned timezones that have been alike since 1970. It returns the identifier with the highest population among similar timekeeping methods.

    • Use case: General purpose lookup where historical accuracy prior to 1970 is not required.
    • Import: require('geo-tz') or require('geo-tz/dist/find-1970')

    2. Comprehensive

    Contains all available timezone identifiers. This behaves like versions prior to v8, providing at least one unique identifier per country.

    • Use case: When you need historical accuracy (including years prior to 1970).
    • Tradeoff: Largest file size.
    • Import: require('geo-tz/all') or require('geo-tz/dist/find-all')

    3. Same since now

    Contains a unioned set of timezones that share the same timekeeping method into the future.

    • Use case: When you only care about current and future timekeeping.
    • Tradeoff: Smallest file size; may return identifiers based on current/future population trends.
    • Import: require('geo-tz/now') or require('geo-tz/dist/find-now')
    // Comprehensive example
    const { find } = require('geo-tz/all')
    find(12.826174, 45.036933) // ['Asia/Aden']
    
    // Same since now example
    const { find } = require('geo-tz/now')
    find(12.826174, 45.036933) // ['Europe/Moscow']
  2. Configure data path for bundlers using GEO_TZ_DATA_PATH

    master

    Because geo-tz reads data files from the data/ directory at runtime, standard bundlers may not include them automatically.

    To fix this:

    1. Explicitly copy the data/ directory to your bundle's output location.
    2. Set the GEO_TZ_DATA_PATH environment variable to point to that directory so the library knows where to find the files.
  3. Configure caching behavior with setCache(options)

    master

    By default, geo-tz lazy-loads lookup data into an unexpiring cache. Use setCache(options) to modify this behavior.

    Options:

    • preload (boolean): If true, attempts to cache all files at startup. This results in slower startup and higher memory usage.
    • store (Map-like object): Allows offloading the cache to a custom storage solution. The object must be compatible with the Map API.
    const { setCache } = require('geo-tz')
    
    // Preload all files
    setCache({ preload: true })
    
    // Use a custom Map-like storage
    let map = new Map();
    setCache({ store: map })
  4. Find timezones for a coordinate with find(lat, lon)

    master

    The find(lat, lon) method returns an array of IANA timezone identifiers found at the specified latitude and longitude.

    Note:

    • If a coordinate is exactly on a border, multiple timezones may be returned.
    • If the coordinate is at sea or in an uninhabited area, a timezone at sea (e.g., Etc/GMT*) will be returned.
    • The default data product returns timezones that have been alike since 1970.
    const { find } = require('geo-tz')
    
    find(47.650499, -122.350070)  // ['America/Los_Angeles']
    find(43.839319, 87.526148)  // ['Asia/Shanghai', 'Asia/Urumqi']
  5. Configure the data path via GEO_TZ_DATA_PATH

    master
    The library looks for its timezone data files in a specific directory. You can override the default data path by setting the GEO_TZ_DATA_PATH environment variable. If not set, it defaults to the ../data directory relative to the package installation.
  6. Find timezones as they existed in 1970

    master

    Use the find function to retrieve an array of timezone IDs (TZIDs) for a specific latitude and longitude, based on how timezones were defined in 1970.

    Note: This method uses the official timezone database but may exclude certain 'deprecated' zones that had different timekeeping methods prior to 1970.

    import { find } from 'node-geo-tz/find-1970';
    
    // lat must be between -90 and 90
    // lon must be between -180 and 180
    const timezones = find(40.7128, -74.0060);
    console.log(timezones); // e.g., ['America/New_York']
  7. Configure caching for 1970 timezone lookups

    master

    The setCache function allows you to configure how timezone data is cached to optimize lookup performance. It accepts a CacheOptions object.

    You can also use the GEO_TZ_DATA_PATH environment variable to specify a custom path to the timezone data files. If not provided, it defaults to the data directory relative to the package installation.

    import { setCache } from 'node-geo-tz/find-1970';
    
    // Example: setting cache options
    setCache({ preload: false });
  8. Find all timezone IDs at a coordinate using find()

    master

    Use find(lat, lon) to retrieve an array of all timezone IDs (TZIDs) that have ever existed at the specified GPS coordinates. This method is comprehensive and may include 'deprecated' zones that had different timekeeping methods prior to 1970.

    Parameters:

    • lat: Latitude (must be between -90 and 90).
    • lon: Longitude (must be between -180 and 180).

    Returns: An array of strings representing the timezone IDs.

    import { find } from 'node-geo-tz';
    
    const timezones = find(40.7128, -74.0060);
    console.log(timezones); // e.g., ['America/New_York']
  9. Speed up lookups with preCache()

    master

    Call preCache() to load all timezone features into memory immediately. This increases initial memory consumption but significantly speeds up subsequent find() lookups.

    import { preCache } from 'node-geo-tz';
    
    // Call this during application startup
    preCache();
  10. Pre-cache 1970 timezone data for faster lookups

    master

    Call preCache() to load all timezone features into memory. This is useful for applications that perform frequent lookups and want to avoid the overhead of reading from disk during runtime.

    import { preCache, find } from 'node-geo-tz/find-1970';
    
    // Load everything into memory first
    preCache();
    
    // Subsequent lookups will be faster
    const tz = find(34.0522, -118.2437);
  11. Find timezones at sea using getTimezoneAtSea()

    master

    The getTimezoneAtSea function returns an array of Etc/GMT* timezone identifiers corresponding to a specific longitude. This is useful for determining timezones when coordinates fall in oceanic regions where standard land-based timezone boundaries may not apply.

    Behavioral Notes:

    • If the longitude is exactly -180 or 180, the function returns both ['Etc/GMT+12', 'Etc/GMT-12'].
    • The function iterates through predefined oceanZones to find matching tzid values based on longitude boundaries.
    import { getTimezoneAtSea } from 'geo-tz';
    
    const tzs = getTimezoneAtSea(10.5);
    // Returns an array of matching Etc/GMT identifiers