2021-09-12 12:56:39 -04:00
|
|
|
import dayjs from "dayjs";
|
|
|
|
import timezone from "dayjs/plugin/timezone";
|
|
|
|
import utc from "dayjs/plugin/utc";
|
2021-09-12 07:27:41 -04:00
|
|
|
import timezones from "timezones-list";
|
2021-09-29 03:14:34 -04:00
|
|
|
import { localeDirection, currentLocale } from "./i18n";
|
2021-09-12 07:27:41 -04:00
|
|
|
|
2021-09-19 07:04:51 -04:00
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(timezone);
|
2021-09-12 07:27:41 -04:00
|
|
|
|
|
|
|
function getTimezoneOffset(timeZone) {
|
|
|
|
const now = new Date();
|
|
|
|
const tzString = now.toLocaleString("en-US", {
|
|
|
|
timeZone,
|
|
|
|
});
|
|
|
|
const localString = now.toLocaleString("en-US");
|
|
|
|
const diff = (Date.parse(localString) - Date.parse(tzString)) / 3600000;
|
|
|
|
const offset = diff + now.getTimezoneOffset() / 60;
|
|
|
|
return -offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function timezoneList() {
|
|
|
|
let result = [];
|
|
|
|
|
|
|
|
for (let timezone of timezones) {
|
|
|
|
try {
|
|
|
|
let display = dayjs().tz(timezone.tzCode).format("Z");
|
|
|
|
|
|
|
|
result.push({
|
|
|
|
name: `(UTC${display}) ${timezone.tzCode}`,
|
|
|
|
value: timezone.tzCode,
|
|
|
|
time: getTimezoneOffset(timezone.tzCode),
|
2021-09-19 07:04:51 -04:00
|
|
|
});
|
2021-09-12 07:27:41 -04:00
|
|
|
} catch (e) {
|
2021-09-28 01:29:32 -04:00
|
|
|
// Skipping not supported timezone.tzCode by dayjs
|
2021-09-12 07:27:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result.sort((a, b) => {
|
|
|
|
if (a.time > b.time) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b.time > a.time) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2021-09-19 07:04:51 -04:00
|
|
|
});
|
2021-09-12 07:27:41 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2021-09-29 03:14:34 -04:00
|
|
|
|
|
|
|
export function setPageLocale() {
|
2021-10-26 06:58:04 -04:00
|
|
|
const html = document.documentElement
|
2021-09-29 03:14:34 -04:00
|
|
|
html.setAttribute('lang', currentLocale() )
|
|
|
|
html.setAttribute('dir', localeDirection() )
|
2021-10-26 06:58:04 -04:00
|
|
|
}
|