Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
How to get the canonical timezone name of a timezone alias?

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.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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
about 4 years ago · Juan Pablo Isaza Relatório

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];
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda