简体中文、英文、多语言框架、时区、夏令时、公制/英制和区域配置
This commit is contained in:
112
src/lib/dayjs.ts
Normal file
112
src/lib/dayjs.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import dayjs from 'dayjs';
|
||||
import utc from 'dayjs/plugin/utc';
|
||||
import timezone from 'dayjs/plugin/timezone';
|
||||
import 'dayjs/locale/zh-cn';
|
||||
import 'dayjs/locale/en';
|
||||
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
|
||||
export default dayjs;
|
||||
|
||||
/** 在指定时区格式化时间,自动处理 DST */
|
||||
export function formatInTz(date: Date | string | number, tz: string, format: string): string {
|
||||
try {
|
||||
return dayjs(date).tz(tz).format(format);
|
||||
} catch {
|
||||
return dayjs(date).format(format);
|
||||
}
|
||||
}
|
||||
|
||||
/** 当前时间在指定时区的格式化结果 */
|
||||
export function nowInTz(tz: string, format: string): string {
|
||||
return formatInTz(new Date(), tz, format);
|
||||
}
|
||||
|
||||
/** 获取时区偏移标签,如 UTC+08:00 */
|
||||
export function getTimezoneOffsetLabel(tz: string): string {
|
||||
try {
|
||||
const offset = dayjs().tz(tz).utcOffset();
|
||||
const sign = offset >= 0 ? '+' : '-';
|
||||
const abs = Math.abs(offset);
|
||||
const h = Math.floor(abs / 60).toString().padStart(2, '0');
|
||||
const m = (abs % 60).toString().padStart(2, '0');
|
||||
return `UTC${sign}${h}:${m}`;
|
||||
} catch {
|
||||
return 'UTC+00:00';
|
||||
}
|
||||
}
|
||||
|
||||
/** 判断指定时区当前是否处于夏令时 */
|
||||
export function isDST(tz: string): boolean {
|
||||
try {
|
||||
const now = dayjs().tz(tz);
|
||||
// 取本年1月(北半球冬季)与本年7月(北半球夏季)的偏移量
|
||||
const year = now.year();
|
||||
const janOffset = dayjs.tz(dayjs(`${year}-01-15T12:00:00Z`).toISOString(), tz).utcOffset();
|
||||
const julOffset = dayjs.tz(dayjs(`${year}-07-15T12:00:00Z`).toISOString(), tz).utcOffset();
|
||||
const currentOffset = now.utcOffset();
|
||||
// DST 时区夏季偏移量会大于冬季偏移量;当前偏移等于夏季偏移则为 DST
|
||||
const summerOffset = Math.max(janOffset, julOffset);
|
||||
const winterOffset = Math.min(janOffset, julOffset);
|
||||
if (summerOffset === winterOffset) return false; // 无 DST 的时区
|
||||
return currentOffset === summerOffset;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export interface TimezoneOption {
|
||||
value: string;
|
||||
label: string;
|
||||
}
|
||||
|
||||
/** 常用 IANA 时区列表 */
|
||||
export const TIMEZONES: TimezoneOption[] = [
|
||||
// 亚洲
|
||||
{ value: 'Asia/Shanghai', label: '上海 (Shanghai)' },
|
||||
{ value: 'Asia/Hong_Kong', label: '香港 (Hong Kong)' },
|
||||
{ value: 'Asia/Tokyo', label: '东京 (Tokyo)' },
|
||||
{ value: 'Asia/Seoul', label: '首尔 (Seoul)' },
|
||||
{ value: 'Asia/Singapore', label: '新加坡 (Singapore)' },
|
||||
{ value: 'Asia/Bangkok', label: '曼谷 (Bangkok)' },
|
||||
{ value: 'Asia/Kolkata', label: '加尔各答 (Kolkata)' },
|
||||
{ value: 'Asia/Dubai', label: '迪拜 (Dubai)' },
|
||||
{ value: 'Asia/Taipei', label: '台北 (Taipei)' },
|
||||
// 美洲
|
||||
{ value: 'America/New_York', label: '纽约 (New York)' },
|
||||
{ value: 'America/Chicago', label: '芝加哥 (Chicago)' },
|
||||
{ value: 'America/Denver', label: '丹佛 (Denver)' },
|
||||
{ value: 'America/Los_Angeles', label: '洛杉矶 (Los Angeles)' },
|
||||
{ value: 'America/Anchorage', label: '安克雷奇 (Anchorage)' },
|
||||
{ value: 'America/Toronto', label: '多伦多 (Toronto)' },
|
||||
{ value: 'America/Vancouver', label: '温哥华 (Vancouver)' },
|
||||
{ value: 'America/Mexico_City', label: '墨西哥城 (Mexico City)' },
|
||||
{ value: 'America/Sao_Paulo', label: '圣保罗 (São Paulo)' },
|
||||
{ value: 'America/Argentina/Buenos_Aires', label: '布宜诺斯艾利斯 (Buenos Aires)' },
|
||||
// 欧洲
|
||||
{ value: 'Europe/London', label: '伦敦 (London)' },
|
||||
{ value: 'Europe/Paris', label: '巴黎 (Paris)' },
|
||||
{ value: 'Europe/Berlin', label: '柏林 (Berlin)' },
|
||||
{ value: 'Europe/Madrid', label: '马德里 (Madrid)' },
|
||||
{ value: 'Europe/Rome', label: '罗马 (Rome)' },
|
||||
{ value: 'Europe/Amsterdam', label: '阿姆斯特丹 (Amsterdam)' },
|
||||
{ value: 'Europe/Moscow', label: '莫斯科 (Moscow)' },
|
||||
{ value: 'Europe/Istanbul', label: '伊斯坦布尔 (Istanbul)' },
|
||||
// 大洋洲
|
||||
{ value: 'Australia/Sydney', label: '悉尼 (Sydney)' },
|
||||
{ value: 'Australia/Perth', label: '珀斯 (Perth)' },
|
||||
// 非洲
|
||||
{ value: 'Africa/Cairo', label: '开罗 (Cairo)' },
|
||||
{ value: 'Africa/Johannesburg', label: '约翰内斯堡 (Johannesburg)' },
|
||||
// UTC
|
||||
{ value: 'UTC', label: 'UTC' },
|
||||
];
|
||||
|
||||
/** 获取时区下拉选项,带 UTC 偏移标签 */
|
||||
export function getTimezoneOptions(): TimezoneOption[] {
|
||||
return TIMEZONES.map((tz) => ({
|
||||
value: tz.value,
|
||||
label: `${tz.label} (${getTimezoneOffsetLabel(tz.value)})`,
|
||||
}));
|
||||
}
|
||||
104
src/lib/units.ts
Normal file
104
src/lib/units.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
import { useSelector } from 'react-redux';
|
||||
import type { RootState } from '../store';
|
||||
import type { UnitSystem } from '../store/preferencesSlice';
|
||||
|
||||
/** 摄氏度转华氏度 */
|
||||
export function celsiusToFahrenheit(c: number): number {
|
||||
return (c * 9) / 5 + 32;
|
||||
}
|
||||
|
||||
/** 华氏度转摄氏度 */
|
||||
export function fahrenheitToCelsius(f: number): number {
|
||||
return ((f - 32) * 5) / 9;
|
||||
}
|
||||
|
||||
/** 公里转英里 */
|
||||
export function kmToMile(km: number): number {
|
||||
return km * 0.621371;
|
||||
}
|
||||
|
||||
/** 英里转公里 */
|
||||
export function mileToKm(mi: number): number {
|
||||
return mi / 0.621371;
|
||||
}
|
||||
|
||||
/** km/h 转 mph */
|
||||
export function kmhToMph(kmh: number): number {
|
||||
return kmh * 0.621371;
|
||||
}
|
||||
|
||||
export interface FormattedValue {
|
||||
value: number;
|
||||
unit: string;
|
||||
label: string;
|
||||
}
|
||||
|
||||
/** 根据单位制格式化温度(输入为摄氏度原始值) */
|
||||
export function formatTemperature(
|
||||
celsius: number,
|
||||
unitSystem: UnitSystem,
|
||||
digits = 1
|
||||
): FormattedValue {
|
||||
if (unitSystem === 'imperial') {
|
||||
return {
|
||||
value: Number(celsiusToFahrenheit(celsius).toFixed(digits)),
|
||||
unit: '°F',
|
||||
label: `${celsiusToFahrenheit(celsius).toFixed(digits)}°F`,
|
||||
};
|
||||
}
|
||||
return {
|
||||
value: Number(celsius.toFixed(digits)),
|
||||
unit: '℃',
|
||||
label: `${celsius.toFixed(digits)}℃`,
|
||||
};
|
||||
}
|
||||
|
||||
/** 根据单位制格式化距离(输入为公里原始值) */
|
||||
export function formatDistance(km: number, unitSystem: UnitSystem, digits = 1): FormattedValue {
|
||||
if (unitSystem === 'imperial') {
|
||||
return {
|
||||
value: Number(kmToMile(km).toFixed(digits)),
|
||||
unit: 'mi',
|
||||
label: `${kmToMile(km).toFixed(digits)} mi`,
|
||||
};
|
||||
}
|
||||
return {
|
||||
value: Number(km.toFixed(digits)),
|
||||
unit: 'km',
|
||||
label: `${km.toFixed(digits)} km`,
|
||||
};
|
||||
}
|
||||
|
||||
/** 根据单位制格式化速度(输入为 km/h 原始值) */
|
||||
export function formatSpeed(kmh: number, unitSystem: UnitSystem, digits = 1): FormattedValue {
|
||||
if (unitSystem === 'imperial') {
|
||||
return {
|
||||
value: Number(kmhToMph(kmh).toFixed(digits)),
|
||||
unit: 'mph',
|
||||
label: `${kmhToMph(kmh).toFixed(digits)} mph`,
|
||||
};
|
||||
}
|
||||
return {
|
||||
value: Number(kmh.toFixed(digits)),
|
||||
unit: 'km/h',
|
||||
label: `${kmh.toFixed(digits)} km/h`,
|
||||
};
|
||||
}
|
||||
|
||||
export interface UnitFormatter {
|
||||
unitSystem: UnitSystem;
|
||||
formatTemperature: (celsius: number, digits?: number) => FormattedValue;
|
||||
formatDistance: (km: number, digits?: number) => FormattedValue;
|
||||
formatSpeed: (kmh: number, digits?: number) => FormattedValue;
|
||||
}
|
||||
|
||||
/** React hook: 从 preferencesSlice 读取 unitSystem,返回绑定好的格式化函数 */
|
||||
export function useUnitFormatter(): UnitFormatter {
|
||||
const unitSystem = useSelector((state: RootState) => state.preferences.unitSystem);
|
||||
return {
|
||||
unitSystem,
|
||||
formatTemperature: (celsius: number, digits = 1) => formatTemperature(celsius, unitSystem, digits),
|
||||
formatDistance: (km: number, digits = 1) => formatDistance(km, unitSystem, digits),
|
||||
formatSpeed: (kmh: number, digits = 1) => formatSpeed(kmh, unitSystem, digits),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user