Skip to content

Commit

Permalink
fix(input-time-zone): fix handling of unknown and cityless time zones…
Browse files Browse the repository at this point in the history
… from offset display mode (#7947)

**Related Issue:** #7944

## Summary

This updates the component to remove cityless time zone entries in the
`offset` mode and for unknown entries (either from browser differences
or updated values) their city part will be displayed as is.

**Note**: due to time constraints, testing will be tackled in #7951.
  • Loading branch information
jcfranco authored Oct 5, 2023
1 parent 557d658 commit 75e0302
Showing 1 changed file with 73 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,48 @@ import { InputTimeZoneMessages } from "./assets/input-time-zone/t9n";

const hourToMinutes = 60;

const timeZoneNameBlockList = [
"CET",
"CST6CDT",
"EET",
"EST",
"EST5EDT",
"Etc/GMT+1",
"Etc/GMT+10",
"Etc/GMT+11",
"Etc/GMT+12",
"Etc/GMT+2",
"Etc/GMT+3",
"Etc/GMT+4",
"Etc/GMT+5",
"Etc/GMT+6",
"Etc/GMT+7",
"Etc/GMT+8",
"Etc/GMT+9",
"Etc/GMT-1",
"Etc/GMT-10",
"Etc/GMT-11",
"Etc/GMT-12",
"Etc/GMT-13",
"Etc/GMT-14",
"Etc/GMT-2",
"Etc/GMT-3",
"Etc/GMT-4",
"Etc/GMT-5",
"Etc/GMT-6",
"Etc/GMT-7",
"Etc/GMT-8",
"Etc/GMT-9",
"Factory",
"HST",
"MET",
"MST",
"MST7MDT",
"PST8PDT",
"UTC",
"WET",
];

function timeZoneOffsetToDecimal(shortOffsetTimeZoneName: string): string {
const minusSign = "−";
const hyphen = "-";
Expand Down Expand Up @@ -71,16 +113,42 @@ export async function createTimeZoneItems(

const listFormatter = new Intl.ListFormat(locale, { style: "long", type: "conjunction" });

// we remove blocked entries from tzs and adjust label indices accordingly
timeZoneGroups.forEach((group) => {
const indexOffsets: number[] = [];
let removedSoFar = 0;

group.tzs = group.tzs.filter((tz) => {
if (timeZoneNameBlockList.includes(tz)) {
removedSoFar++;
return false;
}

indexOffsets.push(removedSoFar);
return true;
});

group.labelTzIndices = group.labelTzIndices
.map((index) => index - (indexOffsets[index] || 0))
.filter((index) => index < group.tzs.length);
});

return timeZoneGroups
.map<TimeZoneItem<number>>(({ labelTzIndices, tzs }) => {
const groupRepTz = tzs[0];
const decimalOffset = timeZoneOffsetToDecimal(getTimeZoneShortOffset(groupRepTz, locale, referenceDateInMs));
const value = toOffsetValue(groupRepTz, referenceDateInMs);
const label = createTimeZoneOffsetLabel(
messages,
decimalOffset,
listFormatter.format(labelTzIndices.map((index: number) => messages[tzs[index]]))
);
const tzLabels = labelTzIndices.map((index: number) => {
const timeZone = tzs[index];
const timeZoneLabel = messages[timeZone];
return (
timeZoneLabel ||
// get city token
timeZone.split("/").pop()
);
});

const label = createTimeZoneOffsetLabel(messages, decimalOffset, listFormatter.format(tzLabels));

return {
label,
Expand Down

0 comments on commit 75e0302

Please sign in to comment.