Skip to content

Commit 9113be8

Browse files
committed
feat: add live clock feature and enhance timestamp tools
- Introduced a live clock that updates the current epoch time every second, with an option to toggle its visibility. - Added functionality to copy the current epoch time to the clipboard and set the timestamp to the current time. - Enhanced the display of the current epoch time with formatted date representation. - Improved UI elements for better user interaction and experience.
1 parent 4fe58e6 commit 9113be8

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

src/pages/TimestampTools.tsx

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,25 @@ export default function TimestampTools() {
99
const [timestamp, setTimestamp] = useState(0) // Initialize with 0 to match server
1010
const [cronExpression, setCronExpression] = useState('0 0 * * *')
1111
const [cronDescription, setCronDescription] = useState('')
12+
const [currentEpoch, setCurrentEpoch] = useState(Math.floor(Date.now() / 1000))
13+
const [showLiveClock, setShowLiveClock] = useState(true)
1214

1315
// Set current timestamp after hydration to avoid mismatch
1416
useEffect(() => {
1517
setTimestamp(Math.floor(Date.now() / 1000))
1618
}, [])
1719

20+
// Update live epoch time every second
21+
useEffect(() => {
22+
if (!showLiveClock) return
23+
24+
const interval = setInterval(() => {
25+
setCurrentEpoch(Math.floor(Date.now() / 1000))
26+
}, 1000)
27+
28+
return () => clearInterval(interval)
29+
}, [showLiveClock])
30+
1831
const convertToDate = () => {
1932
const date = new Date(timestamp * 1000)
2033
return date.toLocaleString()
@@ -25,6 +38,28 @@ export default function TimestampTools() {
2538
toast.success('Set to current time')
2639
}
2740

41+
const copyToClipboard = async (text: string) => {
42+
try {
43+
await navigator.clipboard.writeText(text)
44+
toast.success('Copied to clipboard')
45+
} catch {
46+
toast.error('Copy failed')
47+
}
48+
}
49+
50+
const formatLiveDate = (epoch: number): string => {
51+
const date = new Date(epoch * 1000)
52+
return date.toLocaleString('en-US', {
53+
year: 'numeric',
54+
month: 'short',
55+
day: 'numeric',
56+
hour: '2-digit',
57+
minute: '2-digit',
58+
second: '2-digit',
59+
timeZoneName: 'short'
60+
})
61+
}
62+
2863
const parseCron = () => {
2964
try {
3065
const description = cronstrue.toString(cronExpression)
@@ -51,7 +86,57 @@ export default function TimestampTools() {
5186

5287
<div className="space-y-6">
5388
<div className="bg-white dark:bg-gray-800 p-6 rounded-lg border">
54-
<h2 className="text-xl font-semibold mb-4">Unix Timestamp</h2>
89+
<div className="flex items-center justify-between mb-4">
90+
<h2 className="text-xl font-semibold">Unix Timestamp</h2>
91+
<label className="flex items-center gap-2 cursor-pointer">
92+
<input
93+
type="checkbox"
94+
checked={showLiveClock}
95+
onChange={(e) => setShowLiveClock(e.target.checked)}
96+
className="w-4 h-4 rounded border-gray-300 dark:border-gray-600"
97+
/>
98+
<span className="text-sm text-gray-600 dark:text-gray-400">Show live clock</span>
99+
</label>
100+
</div>
101+
102+
{/* Live Clock Widget */}
103+
{showLiveClock && (
104+
<div className="mb-6 p-4 bg-gradient-to-r from-primary-50 to-primary-100 dark:from-primary-900/20 dark:to-primary-800/20 rounded-lg border border-primary-200 dark:border-primary-800">
105+
<div className="flex items-center justify-between flex-wrap gap-4">
106+
<div className="flex-1 min-w-[200px]">
107+
<div className="text-xs text-primary-700 dark:text-primary-300 font-medium mb-1">
108+
Current Epoch Time (Live)
109+
</div>
110+
<div className="flex items-baseline gap-3 flex-wrap">
111+
<span className="text-2xl font-mono font-bold text-primary-900 dark:text-primary-100">
112+
{currentEpoch.toLocaleString()}
113+
</span>
114+
<span className="text-sm text-primary-600 dark:text-primary-400">
115+
{formatLiveDate(currentEpoch)}
116+
</span>
117+
</div>
118+
</div>
119+
<div className="flex gap-2">
120+
<button
121+
onClick={() => copyToClipboard(currentEpoch.toString())}
122+
className="px-3 py-1.5 text-sm bg-primary-600 hover:bg-primary-700 text-white rounded-lg transition-colors"
123+
>
124+
📋 Copy Epoch
125+
</button>
126+
<button
127+
onClick={() => {
128+
setTimestamp(currentEpoch)
129+
toast.success('Timestamp set to current time')
130+
}}
131+
className="px-3 py-1.5 text-sm bg-white dark:bg-gray-700 hover:bg-gray-100 dark:hover:bg-gray-600 text-primary-700 dark:text-primary-300 border border-primary-300 dark:border-primary-700 rounded-lg transition-colors"
132+
>
133+
Use This
134+
</button>
135+
</div>
136+
</div>
137+
</div>
138+
)}
139+
55140
<p className="text-sm text-gray-600 dark:text-gray-400 mb-4">
56141
ℹ️ Unix timestamp is the number of seconds since January 1, 1970 (UTC). Commonly used in databases and APIs.
57142
</p>

0 commit comments

Comments
 (0)