npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zip-tz

v1.0.0

Published

Convert ZIP code to Time Zone

Downloads

83

Readme

zip-tz

npm package typescript MIT license test status

Stop. Before using this make sure you understand why it’s wrong to determine time zone from ZIP code.


zipTZ(string)

Given a ZIP code, get the most likely practical time zone in the Time Zone Database currently applicable to it. For the sake of bytes over-the-wire and lookup speed, this library does not enumerate all possible ZIP codes. Rather, it operates on ranges, and assumes the ZIP code is valid; non-existent ZIP codes may map to a time zone or null.

Rather than a more precise zone name, this library flattens the results to the largest canonical zone for that offset and DST behavior. The precise zones exist for historical reasons and are necessary in the Time Zone Database to compute past dates. For example, America/Kentucky/Monticello allows the database to describe the switch of several counties in Kentucky from Central to Eastern time in 2000. Since this library is only concerned with the current timezone configuration, it assigns those ZIP codes to the canonical zone for Eastern: America/New_York.

The set of supported time zones, including several outside of the US proper, since they also participate in the ZIP code system:

  • America/Adak
  • America/Anchorage
  • America/Chicago
  • America/Denver
  • America/Los_Angeles
  • America/New_York
  • America/Phoenix
  • America/Puerto_Rico
  • America/St_Thomas
  • Pacific/Chuuk
  • Pacific/Guam
  • Pacific/Honolulu
  • Pacific/Kosrae
  • Pacific/Kwajalein
  • Pacific/Majuro
  • Pacific/Pago_Pago
  • Pacific/Palau
  • Pacific/Pohnpei
  • Pacific/Saipan
  • Pacific/Wake

Why not use this?

ZIP codes are inherently not geographic areas, so while broadly useful it’s technically incorrect to geocode from a ZIP code alone. The county is generally the atom of time zones in the US, but there are exceptions. And even then, the routing of mail does not strictly follow the boundaries of states, counties, or even towns. As such, there are many examples where it’s impossible to assign a single time zone to a ZIP code, because there are multiple or zero that apply.

For example, 86502 has two time zones applicable because it partially serves areas within Navajo Nation (which does observe DST) in Arizona (which does not). For these scenarios, this library chooses the time zone that is subjectively "more correct", the one that is applicable to more of the area more of the time.

Usecases where the time zone is strictly important cannot be solved by only providing a ZIP code. And ultimately, time zones are a social convention, not a geographic quality. Ask your user what TZ they want to use!

Installation & Usage

npm install zip-tz

and include as a JavaScript or TypeScript module (types included):

import zipTZ from 'zip-tz';

…or a CommonJS module:

const zipTZ = require('zip-tz');

Then call with a ZIP code:

> zipTZ('10007')
'America/New_York'

Invalid ZIP codes will resolve, but malformed ZIP codes will throw:

> zipTZ('83005') // Not real
'America/Denver'
> zipTZ('88888') // Real but not geographic
'America/Denver'
> zipTZ('1000') // Incomplete
Uncaught Error: Invalid ZIP code format: 1000
> zipTZ('H3B 3A7') // Canadian
Uncaught Error: Invalid ZIP code format: H3B 3A7

Author

Alec Perkins

License

This package is licensed under the MIT License.

See ./LICENSE for more information.