Skip to content

Commit 170ded0

Browse files
committed
Use DateTimeFormat timeZoneName shortOffset
Node.js version used (14) does not seem to implement longOffset. Use shortOffset instead, and massage the output to be the same as longOffset. Signed-off-by: Brendan Miller <6900229+bhmiller@users.noreply.github.com>
1 parent c551da0 commit 170ded0

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

python/ray/dashboard/client/src/common/timezone.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
const getCurrentUTCOffset = (timeZone: string) => {
22
try {
33
const date = new Date();
4+
// Ideally this would be longOffset, but it's not supported in used version of node
45
const formatter = new Intl.DateTimeFormat("en-US", {
56
timeZone,
6-
timeZoneName: "longOffset",
7+
timeZoneName: "shortOffset",
78
});
8-
// Extract just the GMT offset part from the formatted string
9+
// Extract the GMT offset part from the formatted string and ensure it has minutes
910
const formatted = formatter.format(date);
10-
const match = formatted.match(/GMT[+-]\d{2}:\d{2}/);
11-
return match ? match[0] : "";
11+
const match = formatted.match(/GMT[+-]\d{1,2}(?::\d{2})?/);
12+
if (match) {
13+
// Ensure the offset has minutes and hours are padded (e.g., convert GMT+5 to GMT+05:00)
14+
const offset = match[0];
15+
if (!offset.includes(":")) {
16+
return offset.replace(
17+
/([+-])(\d{1,2})$/,
18+
(_, sign, hours) => `${sign}${hours.padStart(2, "0")}:00`,
19+
);
20+
}
21+
return offset.replace(
22+
/([+-])(\d{1,2}):/,
23+
(_, sign, hours) => `${sign}${hours.padStart(2, "0")}:`,
24+
);
25+
}
26+
return "";
1227
} catch (e) {
1328
console.warn(`Error getting UTC offset for ${timeZone}:`, e);
1429
return "";

python/ray/dashboard/client/src/components/SearchComponent.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,11 @@ export const SearchTimezone = ({
122122
});
123123

124124
const browserTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
125-
126-
const browserOffset = (() => {
127-
const formatter = new Intl.DateTimeFormat("en-US", {
128-
timeZone: browserTimezone,
129-
timeZoneName: "longOffset",
130-
});
131-
const formatted = formatter.format(new Date());
132-
const match = formatted.match(/GMT[+-]\d{2}:?\d{2}/);
133-
return match ? match[0] : "";
134-
})();
135-
136-
if (browserOffset) {
125+
const browserTz = timezones.find((t) => t.value === browserTimezone);
126+
if (browserTz) {
137127
options.unshift({
138128
value: browserTimezone,
139-
utc: browserOffset,
129+
utc: browserTz.utc,
140130
group: "System",
141131
country: "Browser Time",
142132
});

0 commit comments

Comments
 (0)