diff --git a/packages/labs/src/components/timezone-picker/timezoneMetadata.ts b/packages/labs/src/components/timezone-picker/timezoneMetadata.ts index 487431a374..4ebd245095 100644 --- a/packages/labs/src/components/timezone-picker/timezoneMetadata.ts +++ b/packages/labs/src/components/timezone-picker/timezoneMetadata.ts @@ -21,19 +21,26 @@ export function getTimezoneMetadata(timezone: string, date: Date, useManualCalc: return calculateMetadata(timezone, zone, timestamp); } -function getValidAbbreviation(abbreviation: string) { - return isValidAbbreviation(abbreviation) ? abbreviation : undefined; +/** + * Ignore abbreviations that are simply offsets, i.e. "+14" instead of "PST" + * @param abbreviation + */ +function getNonOffsetAbbreviation(abbreviation: string) { + return isNonOffsetAbbreviation(abbreviation) ? abbreviation : undefined; } -function isValidAbbreviation(abbreviation: string) { +function isNonOffsetAbbreviation(abbreviation: string) { return abbreviation != null && abbreviation.length > 0 && abbreviation[0] !== "-" && abbreviation[0] !== "+"; } +/** + * Use moment-timezone to parse the timestamp and provide timezone metadata + */ function getMetadataFromMoment(timezone: string, zone: moment.MomentZone, timestamp: number) { const zonedDate = moment.tz(timestamp, timezone); const offset = zonedDate.utcOffset(); const offsetAsString = zonedDate.format("Z"); - const abbreviation = getValidAbbreviation(zonedDate.zoneAbbr()); + const abbreviation = getNonOffsetAbbreviation(zonedDate.zoneAbbr()); return { abbreviation, offset, @@ -43,10 +50,14 @@ function getMetadataFromMoment(timezone: string, zone: moment.MomentZone, timest }; } +/** + * Manually determine timezone metadata by skipping the timestamp parsing and following + * http://momentjs.com/timezone/docs/#/data-formats/unpacked-format/ + */ function getMetadataFromMomentManual(timezone: string, zone: moment.MomentZone, timestamp: number) { const { abbrs, offsets, population, untils } = zone; const index = findOffsetIndex(timestamp, untils); - const abbreviation = getValidAbbreviation(abbrs[index]); + const abbreviation = getNonOffsetAbbreviation(abbrs[index]); const offset = offsets[index] * -1; const offsetAsString = getOffsetAsString(offset); return {