Fix: use local timezone as default (#615)

* use local timezone as default

* refactor code

* fix time display
This commit is contained in:
an-lee
2024-05-17 10:23:39 +08:00
committed by GitHub
parent df68540fb1
commit 8cc030e6af
8 changed files with 38 additions and 24 deletions

View File

@@ -0,0 +1,26 @@
import dayjs from "dayjs";
import "dayjs/locale/en";
import "dayjs/locale/zh-cn";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
import localeData from "dayjs/plugin/localeData";
import localizedFormat from "dayjs/plugin/localizedFormat";
import relativeTime from "dayjs/plugin/relativeTime";
import duration from "dayjs/plugin/duration";
import i18next from "i18next";
dayjs.extend(localizedFormat);
dayjs.extend(duration);
dayjs.extend(relativeTime);
dayjs.extend(localeData);
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.locale(i18next.resolvedLanguage?.toLowerCase() || "en");
try {
dayjs.tz.setDefault(Intl.DateTimeFormat().resolvedOptions().timeZone);
} catch (e) {
dayjs.tz.guess();
}
export default dayjs;

View File

@@ -1,15 +1,8 @@
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
import dayjs from "dayjs";
import localizedFormat from "dayjs/plugin/localizedFormat";
import relativeTime from "dayjs/plugin/relativeTime";
import duration, { type DurationUnitType } from "dayjs/plugin/duration";
import "dayjs/locale/en";
import "dayjs/locale/zh-cn";
import dayjs from "@renderer/lib/dayjs";
import { type DurationUnitType } from "dayjs/plugin/duration";
import i18next, { t } from "i18next";
dayjs.extend(localizedFormat);
dayjs.extend(duration);
dayjs.extend(relativeTime);
import Chart from "chart.js/auto";
export function cn(...inputs: ClassValue[]) {
@@ -58,7 +51,7 @@ export function formatDateTime(date: Date) {
if (now.diff(then, "hour") === 0) {
return then.fromNow();
} else if (now.diff(then, "day") === 0) {
} else if (now.isSame(then, "day")) {
return then.format("HH:mm");
} else if (now.diff(then, "year") === 0) {
return then.format("MM/DD HH:mm");