4

I would like to create a function that converts timezone aliases names into the canonical name for said timezone.

Status of timezones from Wikipedia

For example if I had Africa/Accra I would like to be able to look up that Africa/Abidjan is the canonical name of the timezone.

function getCanonicalName(name) {
  // TODO
}
getCanonicalName('Africa/Accra'); // => 'Africa/Abidjan'

Things I've Looked into on my own

I have tried using moment-timezone and luxon for parsing the zone name, but there doesn't seem to be a way to reverse engineer the canonical timezone as part of these libraries.

According to the moment-timezone docs, you can get the Canonical Name of a timezone from the "packed" data representation, but looking at the implementation of pack in their source code, it seems to just pass the source.name through.

luxon has a normalizeZone helper but that seems to just return a Zone instance from a wide range of input types. It does not, however, normalize the zone name. There is an isUniversal flag on luxon Zones, but this flag seems to be related to DST, not the status of the timezone.

Souperman
  • 5,057
  • 1
  • 14
  • 39
  • moment has the `guess()` API nowadays? [Link to Docs](http://momentjs.com/timezone/docs/#/using-timezones/guessing-user-timezone/) – Vishnu Oct 11 '21 at 06:39
  • Thank you, the `guess()` API would be sufficient to get the current user's timezone, but our service also needs to show administrators our user's timezone information which is stored on their user records. We allow them to store timezone aliases but we are hoping to normalize those into the canonical timezone so I don't think `guess` is sufficient. – Souperman Oct 11 '21 at 06:42

2 Answers2

3

This may not recognize that specific zone as an alias since it was updated in September 2021:

But in general:

import moment from 'moment';
import momenttz from 'moment-timezone';

// From moment-timezone, but not exported
function normalizeName (name) {
  return (name || '').toLowerCase().replace(/\//g, '_');
}

function getCanonicalName(name) {
  const normalizedName = normalizeName(name);

  // A canonical zone might exist
  if (moment.tz._zones[normalizedName]) {
    return moment.tz._names[normalizedName];
  }

  // If not, there'll be a link to a canonical name
  const canonicalZoneNormalized = moment.tz._links[normalizeName(name)];

  // And return the friendly name
  return moment.tz._names[canonicalZoneNormalized];
}

console.log(getCanonicalName('Pacific/Wallis')); // => Pacific/Tarawa
console.log(getCanonicalName('Pacific/Tarawa')); // => Pacific/Tarawa
Kevin Griffin
  • 14,084
  • 7
  • 28
  • 23
0

Given that timezone database is updated only once a year, I think my solution is low-maintenance. I'm following a very crude approach here. You can actually get the information displayed on the wikipedia page into a JSON first and then get a map containing all timezones and their canonical timezones by running this in the browser console:

const keys = [];
const timezones = [];

$("table:first thead tr th").each(function () {
    keys.push($(this).text().replace(/\n/g, ''));
});

$("table:first tbody tr").each(function () {
    const obj = {};
    let i = 0;

    $(this).children("td").each(function () {
        obj[keys[i]] = $(this).text().replace(/\n/g, '');
        i++;
    });

    timezones.push(obj);
});

const canonicalTimezones = Object.assign({}, ...timezones.map(x => ({ [x['TZ database name']]: (x['Type'] === 'Canonical' ? x['TZ database name'] : x['Notes'].substring(8)) })));

JSON.stringify(canonicalTimezones);

You can save the output in a file somewhere. And then you just need to parse it in your code and easily get the respective canonical timezone.

import canonicalTimezones from './canonicalTimezones.json';

function getCanonicalName(name) {
  return canonicalTimezones[name];
}
Ajay Raghav
  • 906
  • 9
  • 16