Merge branch 'fork/ming4762/timezone-20251020'

This commit is contained in:
Jin Mao
2025-10-30 00:42:05 +08:00
24 changed files with 444 additions and 5 deletions

View File

@@ -1,4 +1,9 @@
import dayjs from 'dayjs';
import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';
dayjs.extend(utc);
dayjs.extend(timezone);
export function formatDate(time: number | string, format = 'YYYY-MM-DD') {
try {
@@ -6,7 +11,7 @@ export function formatDate(time: number | string, format = 'YYYY-MM-DD') {
if (!date.isValid()) {
throw new Error('Invalid date');
}
return date.format(format);
return date.tz().format(format);
} catch (error) {
console.error(`Error formatting date: ${error}`);
return time;
@@ -24,3 +29,19 @@ export function isDate(value: any): value is Date {
export function isDayjsObject(value: any): value is dayjs.Dayjs {
return dayjs.isDayjs(value);
}
/**
* 设置默认时区
* @param timezone
*/
export const setDefaultTimezone = (timezone?: string) => {
timezone ? dayjs.tz.setDefault(timezone) : dayjs.tz.setDefault();
};
/**
* 获取当前时区
* @returns 当前时区
*/
export const getTimezone = () => {
return dayjs.tz.guess();
};

View File

@@ -93,6 +93,15 @@ type PageTransitionType = 'fade' | 'fade-down' | 'fade-slide' | 'fade-up';
*/
type AuthPageLayoutType = 'panel-center' | 'panel-left' | 'panel-right';
/**
* 时区选项
*/
interface TimezoneOption {
label: string;
offset: number;
timezone: string;
}
export type {
AccessModeType,
AuthPageLayoutType,
@@ -108,4 +117,5 @@ export type {
PreferencesButtonPositionType,
TabsStyleType,
ThemeModeType,
TimezoneOption,
};

View File

@@ -133,6 +133,7 @@ exports[`defaultPreferences immutability test > should not modify the config obj
"refresh": true,
"sidebarToggle": true,
"themeToggle": true,
"timezone": true,
},
}
`;

View File

@@ -134,6 +134,7 @@ const defaultPreferences: Preferences = {
refresh: true,
sidebarToggle: true,
themeToggle: true,
timezone: true,
},
};

View File

@@ -1,4 +1,4 @@
import type { BuiltinThemeType } from '@vben-core/typings';
import type { BuiltinThemeType, TimezoneOption } from '@vben-core/typings';
interface BuiltinThemePreset {
color: string;
@@ -81,8 +81,39 @@ const BUILT_IN_THEME_PRESETS: BuiltinThemePreset[] = [
},
];
/**
* 时区选项
*/
const DEFAULT_TIME_ZONE_OPTIONS: TimezoneOption[] = [
{
offset: -5,
timezone: 'America/New_York',
label: 'America/New_York(GMT-5)',
},
{
offset: 0,
timezone: 'Europe/London',
label: 'Europe/London(GMT0)',
},
{
offset: 8,
timezone: 'Asia/Shanghai',
label: 'Asia/Shanghai(GMT+8)',
},
{
offset: 9,
timezone: 'Asia/Tokyo',
label: 'Asia/Tokyo(GMT+9)',
},
{
offset: 9,
timezone: 'Asia/Seoul',
label: 'Asia/Seoul(GMT+9)',
},
];
export const COLOR_PRESETS = [...BUILT_IN_THEME_PRESETS].slice(0, 7);
export { BUILT_IN_THEME_PRESETS };
export { BUILT_IN_THEME_PRESETS, DEFAULT_TIME_ZONE_OPTIONS };
export type { BuiltinThemePreset };

View File

@@ -275,6 +275,8 @@ interface WidgetPreferences {
sidebarToggle: boolean;
/** 是否显示主题切换部件 */
themeToggle: boolean;
/** 是否显示时区部件 */
timezone: boolean;
}
interface Preferences {