dmfilipenko / timezones.json

Full list of timezones

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Differences between name and offset properties

hiramdeveloper opened this issue · comments

For example in the (UTC-06:00) Guadalajara, Mexico City, Monterrey case, those properties has differents values, I mean has the next values below:

name: (UTC-06:00) Guadalajara, Mexico City, Monterrey
offset: -5

The correct offset property value should be: -6 as we can see in the name property.

Could you help me adding the correct values please?

commented

Yep! I thought I was going crazy.... spent 3 days figuring out why I was always off by one hour when I had a use case between calculating delta between Pacific Standard Time and Central Standard Time... He has the offset for CST set to -5 but it's supposed to be -6 and PST is correct at -8...

This is because @dmfilipenko didn't take daylight savings into account, it seems. It looks like this is a snapshot of time zones at some arbitrary point in time.

Oh man, my job has this file committed to the codebase, I'm so glad I found this github project because I was chasing my tail trying to figure out the issue I was seeing in my app's UI.

Finally, I discovered that EST has this same issue, which ultimately led me here.

If you must continue using this file in your project (we are for now), I think it is cleaner to parse the offset out of the text property rather than rely on the numerical offset property provided in the JSON data.

I am using this code snippet to map the timezones in this JSON by their UTC offset according to their text property.

Note that these UTC offsets are mapped by minutes, not hours.

import {groupBy} from 'lodash';
const UTCOffsetRegex: RegExp = /\(UTC(.*?)\)/;
const timezoneJson = [ ... ];

interface Timezone {
  value: string;
  abbr: string;
  offset: number;
  isdst: boolean;
  text: string;
  utc?: string[];
}

static get tzByOffsetMins(): {[key: number]: Timezone[]} {
	const tzOffsetMap: {[key: number]: Timezone[]} = groupBy(timezoneJson, (timezone: Timezone): number => {
		const offset: string = UTCOffsetRegex.exec(timezone.text)[1];
		const [hours, mins]: string[] = offset.split(':');
		const hoursAsMins: number = Math.abs(Number(hours) * 60);
		const offsetMultiplier: number = hours.startsWith('-') ? -1 : 1;
		const totalMins: number = hoursAsMins + Number(mins);
		const offsetInt: number = totalMins * offsetMultiplier;
		return offsetInt;
	});
	return tzOffsetMap;
}

I've notices several of these differences, where the text and offset values differ. For example these two:

  {
    value: 'Central Standard Time',
    abbr: 'CDT',
    offset: -5,
    isdst: true,
    text: '(UTC-06:00) Central Time (US & Canada)',
    utc: [ ... ],
  },
 {
    value: 'Eastern Standard Time',
    abbr: 'EDT',
    offset: -4,
    isdst: true,
    text: '(UTC-05:00) Eastern Time (US & Canada)',
    utc: [ ... ],
  },

Anyone got any insight into this?

Edit: I made some cross checking and created this gist if anyone is interested (https://gist.github.com/adrielTosi/7a08cc60dc3f00e68733bb9b65b7adeb). I don't claim it's 100% correct, but the values to offset and text are the same.