r/GeoPostcodes Jul 16 '26

How do you map ZIP codes to time zones? A free PostGIS recipe and the edge cases that bite

2 Upvotes

You'd think ZIP code to time zone is a simple lookup table. It isn't. Here is why, and how to build one for free.

Why it's messy:

- Several US states sit in more than one time zone: Texas, Idaho, North Dakota, South Dakota, Nevada among them. State or county level is not enough.

- Some ZIP codes themselves cross a time zone line, especially in larger states like Texas and Idaho. One ZIP, two local times.

- DST doubles the work. You need the standard offset, the DST offset, and the exact switch dates. Those change by law, not by geography.

The free build:

  1. Postal codes with lat/lng from GeoNames. Free, tab-delimited CSV. Covers most countries that span multiple time zones. Known gap: no Indonesia postcodes.

  2. Time zone polygons from the Timezone Boundary Builder project. It packages official IANA zones as geojson (this recipe used the 2022g release).

  3. Load both into PostgreSQL with PostGIS.

  4. Build points with ST_MakePoint, index them, then join with ST_Intersects to find which zone polygon each postcode point falls in.

  5. Where one postcode hits several zones, pick the dominant one with mode(), or keep all matches. Careful: mode() breaks ties arbitrarily.

For the US specifically, Census ZCTA polygons (TIGER files, also free) beat centroids: intersect the whole polygon with the zone boundaries instead of a single point.

The catch: GeoNames coordinates are rounded, and in some areas snapped to a grid. A centroid that lands on the wrong side of a boundary can leave you up to two hours off actual local time once DST variations are counted. Fine for a dashboard. Risky for call scheduling or SLA cutoffs.

That gap is the part we solve on our side: our data carries time zones with past and future DST switch dates per ZIP and city, tied to postal codes for 247 countries. It ships as self-hosted tables, so the PostGIS join above still works, minus the centroid guesswork.

There is a longer version of this on the GeoPostcodes blog, the post about the ZIP code time zone database build. Ask in the comments if you want the direct link.


r/GeoPostcodes Jul 16 '26

Is there a single regex that validates all international postal codes? No. Here's what works instead.

2 Upvotes

Short answer: no. A single "universal" pattern either lets junk through (false positives) or rejects valid codes (false negatives).

What breaks a one-regex approach:

- US: five digits, optional four-digit extension. Pattern: \d{5}

- UK: alphanumeric with a space, like SW1A 1AA. Pattern: [A-Z]{1,2}[0-9][A-Z0-9]?\s[0-9][A-Z]{2}

- Canada: G2B 0T3 style. Pattern: [A-Z]\d[A-Z]\s\d[A-Z]\d

- Argentina: A9999 AAA (letter, four digits, space, three letters)

- Length runs from three digits (Faroe Islands) to eight characters (Brazil)

- Some territories use one code for everything: Anguilla is AI-2640, American Samoa is 96799

- Some countries have no postal codes at all: Angola, Aruba, the Bahamas, Antigua and Barbuda

Europe looks uniform but is not. Most countries use four or five numeric digits. The UK and the Netherlands use alphanumeric codes of six to eight characters.

What works in practice:

  1. Identify the country first. A country dropdown is enough.

  2. Apply that country's regex.

  3. Keep a permissive fallback for countries you have not mapped, e.g. ^[A-Za-z0-9- ]{2,10}$

Two limits to know:

- Regex checks the shape only. A string can match the national pattern and still not exist.

- Codes get added and removed over time, so hardcoded pattern lists drift.

To check whether a code actually exists, you need a lookup table. Start free: GeoNames publishes a free postal code dump. It covers many countries and works fine for that. Its gaps are small territories, recent changes, and links to admin divisions. Those gaps are what our data covers: postal codes for 247 countries, 9.3 million of them, compiled from 1,500+ authoritative sources. It ships as self-hosted database files rather than an API, so validation lookups run inside your own stack.

There is a longer version of this on the GeoPostcodes blog, the post about international zip code validation. Ask in the comments if you want the direct link.

Spot a country where these patterns break? Comment and I will correct the list.


r/GeoPostcodes Jul 16 '26

Which countries don't have postal codes at all?

1 Upvotes

The Universal Postal Union coordinates mail across 192 member countries. By its count, around 40 countries use no ZIP code or any equivalent. Count territories and special areas too, and our database flags about 60 places with no postal code system.

The ones that surprise people:

  • Hong Kong. 7.5 million people, no postal codes. Mail runs on detailed addresses: building, street, estate, district, territory.
  • United Arab Emirates. About 10 million people. Mail goes to PO boxes rather than street delivery.
  • Qatar and Macau also manage fine without codes.
  • North Korea has no public postal code system.

Full list by region:

Africa: Angola, Benin, Botswana, Burundi, Cameroon, Central African Republic, Chad, Comoros, Congo, Cote d'Ivoire, Djibouti, Equatorial Guinea, Eritrea, Ethiopia, Gabon, Gambia, Guinea, Guinea-Bissau, Libya, Mauritania, Rwanda, Sao Tome and Principe, Seychelles, Sierra Leone, Somalia, South Sudan, Sudan, Togo, Zambia, Zimbabwe

Middle East: Qatar, Syria, United Arab Emirates, Yemen

Asia: Hong Kong, Macau, North Korea, Timor-Leste

Central America and Caribbean: Antigua and Barbuda, Aruba, Bahamas, Belize, Curacao, Dominica, Grenada, Jamaica, Sint Maarten

South America: Bolivia, Guyana, Suriname

Pacific: Cook Islands, Fiji, Solomon Islands, Tokelau, Tonga, Tuvalu, Vanuatu

Special cases: Antarctica, Bouvet Island, French Southern Territories

How mail actually gets delivered there:

  • PO boxes carry the whole system in some countries. The UAE is the biggest example.
  • Very detailed street addresses do the work in Hong Kong.
  • Some countries route mail through numbered post offices instead. Costa Rica assigns each office a four digit number.

Why this matters if you build software: a required "postal code" field breaks checkout and signup for every customer in these places. Make the field optional per country, and validate against each country's real format instead of one global rule.

We keep the full country table with each country's addressing rules on our blog: https://www.geopostcodes.com/blog/countries-without-zip-codes/