@@ -2,10 +2,27 @@ import dayjs from 'dayjs';
2
2
import React , { useState , useEffect } from 'react' ;
3
3
import { clipboard } from 'electron' ;
4
4
5
+ import relativeTime from 'dayjs/plugin/relativeTime' ;
6
+ import dayOfYear from 'dayjs/plugin/dayOfYear' ;
7
+ import weekOfYear from 'dayjs/plugin/weekOfYear' ;
8
+ import isLeapYear from 'dayjs/plugin/isLeapYear' ;
9
+ import weekday from 'dayjs/plugin/weekday' ;
10
+ import buddhistEra from 'dayjs/plugin/buddhistEra' ;
11
+
12
+ dayjs . extend ( relativeTime ) ;
13
+ dayjs . extend ( dayOfYear ) ;
14
+ dayjs . extend ( weekOfYear ) ;
15
+ dayjs . extend ( isLeapYear ) ;
16
+ dayjs . extend ( weekday ) ;
17
+ dayjs . extend ( buddhistEra ) ;
18
+
5
19
const UnixTimestampConverter = ( ) => {
6
20
const [ date , setDate ] = useState ( dayjs ( ) ) ;
7
21
const [ copied , setCopied ] = useState ( false ) ;
8
22
23
+ const [ epoch , setEpoch ] = useState ( dayjs ( ) . unix ( ) ) ;
24
+ const [ dateStr , setDateStr ] = useState ( dayjs ( ) . toISOString ( ) ) ;
25
+
9
26
useEffect ( ( ) => {
10
27
const timerID = setInterval ( ( ) => setDate ( dayjs ( ) ) , 1000 ) ;
11
28
@@ -14,30 +31,163 @@ const UnixTimestampConverter = () => {
14
31
} ;
15
32
} ) ;
16
33
34
+ const handleClipboard = ( ) => {
35
+ const cl = parseInt ( clipboard . readText ( ) , 10 ) || dayjs ( ) . unix ( ) ;
36
+ setEpoch ( cl ) ;
37
+ } ;
38
+
17
39
const handleCopy = ( ) => {
18
40
clipboard . write ( { text : `${ date . unix ( ) } ` } ) ;
19
41
setCopied ( true ) ;
20
42
setTimeout ( ( ) => setCopied ( false ) , 1_000 ) ;
21
43
} ;
22
44
45
+ const handleChangeEpoch = ( evt : { target : { value : string } } ) => {
46
+ const val = parseInt ( evt . target . value , 10 ) || 0 ;
47
+ setEpoch ( val ) ;
48
+ } ;
49
+
50
+ const handleChangeDate = ( evt : { target : { value : string } } ) => {
51
+ const val = evt . target . value ;
52
+ setDateStr ( val ) ;
53
+ } ;
54
+
55
+ const handleConvertFromDate = ( ) => {
56
+ if ( dayjs ( dateStr ) . isValid ( ) ) {
57
+ setEpoch ( dayjs ( dateStr ) . unix ( ) ) ;
58
+ }
59
+ } ;
60
+
61
+ useEffect ( ( ) => {
62
+ setDateStr ( dayjs . unix ( epoch ) . toISOString ( ) ) ;
63
+ } , [ epoch ] ) ;
64
+
23
65
return (
24
66
< div className = "min-h-full flex flex-col" >
25
- < div className = "flex justify-start mb-1" >
67
+ < section className = "mb-4 space-y-1 flex items-center space-x-2 border-b pb-4" >
68
+ < p > The current Unix epoch time is</ p >
69
+ < span className = "text-base bg-blue-200 px-2 py-1 rounded font-mono" >
70
+ { date . unix ( ) }
71
+ </ span >
26
72
< button
27
73
type = "button"
28
74
className = "btn"
29
75
onClick = { handleCopy }
30
76
disabled = { copied }
31
77
>
32
- { copied ? 'Copied' : ' Copy' }
78
+ Copy
33
79
</ button >
34
- </ div >
35
- < div className = "flex-1 min-h-full" >
36
- The current Unix epoch time is
37
- < span className = "text-base bg-blue-200 mx-2 px-2 py-1 rounded inline-flex items-center content-center font-mono" >
38
- { date . unix ( ) }
39
- </ span >
40
- </ div >
80
+ </ section >
81
+
82
+ < section className = "flex justify-start space-x-2 mb-4" >
83
+ < p > Input:</ p >
84
+ < button type = "button" className = "btn" onClick = { handleClipboard } >
85
+ Clipboard
86
+ </ button >
87
+ < button
88
+ type = "button"
89
+ className = "btn"
90
+ onClick = { ( ) => setEpoch ( dayjs ( ) . unix ( ) ) }
91
+ >
92
+ Now
93
+ </ button >
94
+ < button type = "button" className = "btn" onClick = { ( ) => setEpoch ( 0 ) } >
95
+ Epoch
96
+ </ button >
97
+ </ section >
98
+
99
+ < section className = "flex items-center justify-between space-x-4 mb-4 pb-4" >
100
+ < label htmlFor = "epoch" className = "flex-1" >
101
+ < span > Unix timestamp:</ span >
102
+ < input
103
+ value = { epoch }
104
+ onChange = { handleChangeEpoch }
105
+ type = "number"
106
+ id = "epoch"
107
+ className = "flex w-full p-1 rounded"
108
+ />
109
+ </ label >
110
+ < label htmlFor = "iso" className = "flex-1" >
111
+ < span > ISO date:</ span >
112
+ < input
113
+ value = { dateStr }
114
+ onChange = { handleChangeDate }
115
+ onBlur = { handleConvertFromDate }
116
+ type = "text"
117
+ id = "iso"
118
+ className = "flex w-full p-1 rounded"
119
+ />
120
+ </ label >
121
+ </ section >
122
+
123
+ < section className = "flex flex-col space-y-4 w-full p-4 pb-8 rounded-md bg-gray-200 border" >
124
+ < section className = "flex items-center justify-between space-x-4" >
125
+ < label htmlFor = "local" className = "flex-1" >
126
+ < span > Local:</ span >
127
+ < input
128
+ value = { dayjs . unix ( epoch ) . toDate ( ) . toLocaleString ( ) }
129
+ type = "text"
130
+ id = "local"
131
+ className = "flex w-full p-1 rounded"
132
+ readOnly
133
+ />
134
+ </ label >
135
+ < label htmlFor = "rel" className = "flex-1" >
136
+ < span > Relative:</ span >
137
+ < input
138
+ value = { dayjs . unix ( epoch ) . fromNow ( ) }
139
+ type = "text"
140
+ id = "rel"
141
+ className = "flex w-full p-1 rounded"
142
+ readOnly
143
+ />
144
+ </ label >
145
+ </ section >
146
+ < section className = "flex items-center justify-between space-x-4" >
147
+ < label htmlFor = "dayyear" className = "flex-1" >
148
+ < span > Day of year:</ span >
149
+ < input
150
+ value = { dayjs . unix ( epoch ) . dayOfYear ( ) }
151
+ type = "text"
152
+ id = "dayyear"
153
+ className = "flex w-full p-1 rounded"
154
+ readOnly
155
+ />
156
+ </ label >
157
+ < label htmlFor = "weekyear" className = "flex-1" >
158
+ < span > Week of year:</ span >
159
+ < input
160
+ value = { dayjs . unix ( epoch ) . week ( ) }
161
+ type = "text"
162
+ id = "weekyear"
163
+ className = "flex w-full p-1 rounded"
164
+ readOnly
165
+ />
166
+ </ label >
167
+ </ section >
168
+ < section className = "flex items-center justify-between space-x-4" >
169
+ < label htmlFor = "leap" className = "flex-1" >
170
+ < span > Is leap year:</ span >
171
+ < input
172
+ value = { `${ dayjs . unix ( epoch ) . isLeapYear ( ) } ` }
173
+ type = "text"
174
+ id = "leap"
175
+ className = "flex w-full p-1 rounded"
176
+ readOnly
177
+ />
178
+ </ label >
179
+ < label htmlFor = "be" className = "flex-1" >
180
+ < span > Buddhist Era (B.E.):</ span >
181
+ < input
182
+ value = { dayjs . unix ( epoch ) . format ( 'BBBB' ) }
183
+ type = "text"
184
+ id = "be"
185
+ className = "flex w-full p-1 rounded"
186
+ readOnly
187
+ />
188
+ </ label >
189
+ </ section >
190
+ </ section >
41
191
</ div >
42
192
) ;
43
193
} ;
0 commit comments