in src/components/TimezoneProvider/index.tsx [15:66]
function makeTZData() {
const timezoneData = Object.entries(spacetime().timezones).filter(
(key) => !key[0].includes('etc') && !key[0].includes('utc'),
);
// [string, string] = [value, label]
const tzs: [string, string][] = [];
timezoneData.forEach(([key, value], index) => {
const parseHour = (value: number) => {
if (value < -9.5 || value > 9.5) {
return value > 9 ? `+${Math.floor(value)}` : value;
} else {
return value < 0 ? `-0${Math.floor(value * -1)}` : `+0${Math.floor(value)}`;
}
};
// offsets end section can be 00, 15, 30, or 45
const offset = `${parseHour(spacetime().goto(key).timezone().current.offset)}:${
Number.isInteger(value.offset) ? '00' : `${60 * (value.offset - Math.floor(value.offset))}`
}`;
let region = key.split('/')[0];
region = `${region[0].toUpperCase()}${region.slice(1)}`
.replace('Us', 'US')
.replace('Uk', 'UK')
.replace('Gb', 'GB')
.replace('Uct', 'UCT')
.replace('Nz', 'NZ')
.replace('Gmt', 'GMT')
.replace('Prc', 'PRC')
.replace('Roc', 'ROC')
.replace('Rok', 'ROK')
.replace('eire', 'Eire');
let city = '';
if (key.includes('/')) {
city = key.split('/')[1].replace(/_/g, ' ');
city = `${city[0].toUpperCase()}${city.slice(1)}`.replace(/([\s-])(\w)/g, (_, p1, p2) => p1 + p2.toUpperCase());
// we need to add index to differentiate the offsets from one another
tzs.push([`${offset}|${index}`, `(GMT${offset}) ${region}/${city}`]);
} else {
// we need to add index to differentiate the offsets from one another
tzs.push([`${offset}|${index}`, `(GMT${offset}) ${region}`]);
}
});
const getOffset = (a: string) => parseFloat(a.split('|')[0]);
tzs.sort((a, b) =>
getOffset(a[0]) === getOffset(b[0]) ? a[1].localeCompare(b[1]) : getOffset(a[0]) - getOffset(b[0]),
);
return tzs;
}