diff --git a/src/App.css b/src/App.css index 9317408..8548cc1 100644 --- a/src/App.css +++ b/src/App.css @@ -133,6 +133,23 @@ overflow: hidden; } +.digital-clock-footer { + display: flex; + flex-direction: row; + align-items: baseline; + justify-content: flex-end; + margin-top: 4px; +} + +.digital-clock-timezone { + font-family: Arial, sans-serif; + font-weight: 600; + color: #fff; + text-shadow: 0 0 10px rgba(255, 255, 255, 0.5); + line-height: 1; + white-space: nowrap; +} + .style-button { padding: 10px 20px; font-size: 14px; diff --git a/src/App.tsx b/src/App.tsx index 15221fc..bd1f1df 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -23,10 +23,19 @@ function formatDate(date: Date): DateInfo { } } -function DigitalClock({ time, size, period, is24Hour, onToggleFormat, dateInfo }: { time: string; size: number; period?: string; is24Hour: boolean; onToggleFormat: () => void; dateInfo: DateInfo }) { +function getTimezoneInfo(): string { + // 获取本地时区名称,如 "Asia/Shanghai" + const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone + // 获取时区偏移,如 "GMT+8" + const offset = new Date().toLocaleTimeString('en-US', { timeZoneName: 'short' }).split(' ').pop() || '' + return `${timeZone} ${offset}` +} + +function DigitalClock({ time, size, period, is24Hour, onToggleFormat, dateInfo, timeZone }: { time: string; size: number; period?: string; is24Hour: boolean; onToggleFormat: () => void; dateInfo: DateInfo; timeZone: string }) { const fontSize = Math.floor(size * 0.95) const periodSize = Math.floor(size * 0.20) const dateSize = Math.floor(size * 0.20) + const timeZoneSize = Math.floor(size * 0.20) return (
@@ -63,6 +72,14 @@ function DigitalClock({ time, size, period, is24Hour, onToggleFormat, dateInfo } > {time}
+
+ + {timeZone} + +
) @@ -70,7 +87,12 @@ function DigitalClock({ time, size, period, is24Hour, onToggleFormat, dateInfo } function App() { const [time, setTime] = useState(new Date()) - const [clockStyle, setClockStyle] = useState('digital') + const [clockStyle, setClockStyle] = useState(() => { + // 从 localStorage 读取,默认数码管 ('digital') + const saved = localStorage.getItem('clock-style') + const validStyles: ClockStyle[] = ['digital', 'flip', 'roll'] + return saved && validStyles.includes(saved as ClockStyle) ? (saved as ClockStyle) : 'digital' + }) const [clockSize, setClockSize] = useState(120) const [is24Hour, setIs24Hour] = useState(() => { // 从 localStorage 读取,默认 24 小时制 (true) @@ -83,6 +105,12 @@ function App() { return saved === 'up' ? 'up' : 'down' }) + // 切换样式时保存到 localStorage + const handleSetClockStyle = (style: ClockStyle) => { + setClockStyle(style) + localStorage.setItem('clock-style', style) + } + // 切换格式时保存到 localStorage const toggleFormat = () => { const newValue = !is24Hour @@ -141,13 +169,14 @@ function App() { const { timeString, period } = formatTime() const dateInfo = formatDate(time) + const timeZone = getTimezoneInfo() const renderClock = () => { switch (clockStyle) { case 'flip': - return + return case 'roll': - return + return case 'digital': default: return ( @@ -158,6 +187,7 @@ function App() { is24Hour={is24Hour} onToggleFormat={toggleFormat} dateInfo={dateInfo} + timeZone={timeZone} /> ) } @@ -168,19 +198,19 @@ function App() {
diff --git a/src/components/FlipClock.css b/src/components/FlipClock.css index d9d0123..8bd2b4d 100644 --- a/src/components/FlipClock.css +++ b/src/components/FlipClock.css @@ -83,6 +83,23 @@ white-space: nowrap; } +.flip-clock-footer { + display: flex; + flex-direction: row; + align-items: baseline; + justify-content: flex-end; + margin-top: 4px; +} + +.flip-clock-timezone { + font-family: Arial, sans-serif; + font-weight: 600; + color: #fff; + text-shadow: 0 0 10px rgba(255, 255, 255, 0.5); + line-height: 1; + white-space: nowrap; +} + .flip-clock-digit-group { display: flex; flex-direction: row; diff --git a/src/components/FlipClock.tsx b/src/components/FlipClock.tsx index 24a90b5..a4826cc 100644 --- a/src/components/FlipClock.tsx +++ b/src/components/FlipClock.tsx @@ -13,15 +13,17 @@ interface FlipClockProps { period?: string; is24Hour: boolean; onToggleFormat: () => void; + timeZone: string; } -export function FlipClock({ time, size, dateInfo, period, is24Hour, onToggleFormat }: FlipClockProps) { +export function FlipClock({ time, size, dateInfo, period, is24Hour, onToggleFormat, timeZone }: FlipClockProps) { // Parse time - it's already in 12h format when period is provided const [hoursStr, minutes, seconds] = time.split(':'); const displayHours = hoursStr; const periodSize = size * 0.20; const dateSize = size * 0.20; + const timeZoneSize = size * 0.20; return (
@@ -91,6 +93,14 @@ export function FlipClock({ time, size, dateInfo, period, is24Hour, onToggleForm
+
+ + {timeZone} + +
); diff --git a/src/components/RollClock.css b/src/components/RollClock.css index 7773d26..6a6b01d 100644 --- a/src/components/RollClock.css +++ b/src/components/RollClock.css @@ -89,6 +89,23 @@ white-space: nowrap; } +.roll-clock-footer { + display: flex; + flex-direction: row; + align-items: baseline; + justify-content: flex-end; + margin-top: 4px; +} + +.roll-clock-timezone { + font-family: Arial, sans-serif; + font-weight: 600; + color: #fff; + text-shadow: 0 0 10px rgba(255, 255, 255, 0.5); + line-height: 1; + white-space: nowrap; +} + .roll-clock-digit-group { display: flex; flex-direction: row; diff --git a/src/components/RollClock.tsx b/src/components/RollClock.tsx index ec16f23..cc780e7 100644 --- a/src/components/RollClock.tsx +++ b/src/components/RollClock.tsx @@ -15,9 +15,10 @@ interface RollClockProps { onToggleFormat: () => void; direction: 'down' | 'up'; onToggleDirection: () => void; + timeZone: string; } -export function RollClock({ time, size, dateInfo, period, is24Hour, onToggleFormat, direction, onToggleDirection }: RollClockProps) { +export function RollClock({ time, size, dateInfo, period, is24Hour, onToggleFormat, direction, onToggleDirection, timeZone }: RollClockProps) { // Parse time - it's already in 12h format when period is provided const [hoursStr, minutes, seconds] = time.split(':'); const displayHours = hoursStr; @@ -25,6 +26,7 @@ export function RollClock({ time, size, dateInfo, period, is24Hour, onToggleForm const buttonSize = size * 0.25; const periodSize = size * 0.20; const dateSize = size * 0.20; + const timeZoneSize = size * 0.20; return (
@@ -95,6 +97,14 @@ export function RollClock({ time, size, dateInfo, period, is24Hour, onToggleForm
+
+ + {timeZone} + +
{/* Direction toggle button */}