mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-06 20:51:48 +02:00
🐛 Better error handling for browser time zone support (#1240)
This commit is contained in:
parent
7e2ffd8d2b
commit
7bc978b87a
1 changed files with 8 additions and 4 deletions
|
@ -1,5 +1,4 @@
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import soft from "timezone-soft";
|
|
||||||
|
|
||||||
import { supportedTimeZones } from "@/utils/supported-time-zones";
|
import { supportedTimeZones } from "@/utils/supported-time-zones";
|
||||||
|
|
||||||
|
@ -20,8 +19,8 @@ export function parseIanaTimezone(timezone: string): {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getBrowserTimeZone() {
|
export function getBrowserTimeZone() {
|
||||||
const res = soft(Intl.DateTimeFormat().resolvedOptions().timeZone)[0];
|
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||||
return resolveGeographicTimeZone(res.iana);
|
return resolveGeographicTimeZone(timeZone);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function resolveGeographicTimeZone(timezone: string) {
|
export function resolveGeographicTimeZone(timezone: string) {
|
||||||
|
@ -29,7 +28,12 @@ export function resolveGeographicTimeZone(timezone: string) {
|
||||||
|
|
||||||
if (!tz) {
|
if (!tz) {
|
||||||
// find nearest timezone with the same offset
|
// find nearest timezone with the same offset
|
||||||
const offset = dayjs().tz(timezone).utcOffset();
|
let offset = 0;
|
||||||
|
try {
|
||||||
|
offset = dayjs().tz(timezone).utcOffset();
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`Failed to resolve timezone ${timezone}`);
|
||||||
|
}
|
||||||
return supportedTimeZones.find((tz) => {
|
return supportedTimeZones.find((tz) => {
|
||||||
return dayjs().tz(tz, true).utcOffset() === offset;
|
return dayjs().tz(tz, true).utcOffset() === offset;
|
||||||
})!;
|
})!;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue