This repository was archived by the owner on Dec 20, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathgarclock.c
More file actions
356 lines (284 loc) · 11.6 KB
/
Copy pathgarclock.c
File metadata and controls
356 lines (284 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#ifdef _WIN32
#include <windows.h>
#warning "This software has barely been tested on windows. There is no guarantee that it will work."
#else
#include <sys/time.h>
#endif
const char ascii_zero[10][14] = { " 00000000 ", \
" 0000000000 ", \
" 000 000 ", \
" 00 00 ", \
" 00 00 ", \
" 00 00 ", \
" 00 00 ", \
" 000 000 ", \
" 0000000000 ", \
" 00000000 " };
const char ascii_one[10][14] = { " 1111 ", \
" 111111 ", \
" 1111 ", \
" 1111 ", \
" 1111 ", \
" 1111 ", \
" 1111 ", \
" 1111 ", \
" 11111111 ", \
" 1111111111 " };
const char ascii_two[10][14] = { " 22222 ", \
" 222222 ", \
" 222 222 ", \
" 222 222 ", \
" 2222 ", \
" 2222 ", \
" 2222 ", \
" 2222 ", \
" 2222222222 ", \
" 2222222222 " };
const char ascii_three[10][14] = { " 33333333 ", \
" 3333333333 ", \
" 333 333 ", \
" 333 ", \
" 33333333 ", \
" 33333333 ", \
" 333 ", \
" 333 333 ", \
" 3333333333 ", \
" 33333333 " };
const char ascii_four[10][14] = { " 444444 ", \
" 4444444 ", \
" 444 444 ", \
" 444 444 ", \
" 444 444 ", \
" 4444444444 ", \
" 4444444444 ", \
" 444 ", \
" 444 ", \
" 444 " };
const char ascii_five[10][14] = { " 5555555555 ", \
" 5555555555 ", \
" 555 ", \
" 555 ", \
" 55555555 ", \
" 5555555 ", \
" 555 ", \
" 555 555 ", \
" 5555555555 ", \
" 55555555 " };
const char ascii_six[10][14] = { " 66666666 ", \
" 6666666666 ", \
" 666 666 ", \
" 666 ", \
" 666666666 ", \
" 6666666666 ", \
" 66 66 ", \
" 66 66 ", \
" 6666666666 ", \
" 66666666 " };
const char ascii_seven[10][14] = { " 777777777 ", \
" 7777777777 ", \
" 777 ", \
" 777 ", \
" 777 ", \
" 777 ", \
" 777 ", \
" 777 ", \
" 777 ", \
" 777 " };
const char ascii_eight[10][14] = { " 88888888 ", \
" 8888888888 ", \
" 88 88 ", \
" 88 88 ", \
" 88888888 ", \
" 88888888 ", \
" 88 88 ", \
" 88 88 ", \
" 8888888888 ", \
" 88888888 " };
const char ascii_nine[10][14] = { " 99999999 ", \
" 9999999999 ", \
" 99 99 ", \
" 99 99 ", \
" 9999999999 ", \
" 999999999 ", \
" 999 ", \
" 999 999 ", \
" 9999999999 ", \
" 99999999 " };
const char ascii_colon[10][14] = { " :::::: ", \
" :::::: ", \
" :::::: ", \
" :::::: ", \
" ", \
" ", \
" :::::: ", \
" :::::: ", \
" :::::: ", \
" :::::: " };
const char ascii_slash[10][14] = { " ///", \
" /// ", \
" /// ", \
" /// ", \
" /// ", \
" /// ", \
" /// ", \
" /// ", \
" /// ", \
"/// " };
const char ascii_period[10][14] = { " ", \
" ", \
" ", \
" ", \
" ", \
" ", \
" ", \
" .... ", \
" .... ", \
" .... " };
const char ascii_space[10][14] = { " ", \
" ", \
" ", \
" ", \
" ", \
" ", \
" ", \
" ", \
" ", \
" " };
void reset_cursor(void);
void get_time_string(char *buffer, size_t buffer_size);
void get_date_string(char *buffer, size_t buffer_size);
void print_ascii_string(char *src_buffer, size_t src_buffer_size);
void handle_signal();
int main(void)
{
// Microshit wants us to explicitly enable ANSI functionality.
#ifdef _WIN32
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE) return 1;
DWORD dwMode = 0;
if (!GetConsoleMode(hOut, &dwMode)) return 1;
dwMode |= 0x0004; // ANSI flag
if (!SetConsoleMode(hOut, dwMode)) return 1;
signal(SIGINT, handle_signal);
#else
struct sigaction sa;
sa.sa_handler = handle_signal;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
// Register the signal handler using sigaction
sigaction(SIGINT, &sa, NULL);
#endif
setvbuf(stdout, NULL, _IOFBF, 1024); // disable auto-flush on newline
printf("\033[?25l"); // hide cursor
while (1)
{
reset_cursor();
char time_buffer[13] = {'\0'};
get_time_string(time_buffer, 13);
print_ascii_string(time_buffer, 12);
char date_buffer[11] = {'\0'};
get_date_string(date_buffer, 12);
print_ascii_string(date_buffer, 10);
fflush(stdout);
}
return 0;
}
inline void reset_cursor(void)
{
printf("\033[H");
}
void get_time_string(char *buffer, size_t buffer_size)
{
#ifdef _WIN32
SYSTEMTIME st;
GetLocalTime(&st);
// Format hh:mm:ss.mmm
snprintf(buffer, buffer_size, "%02d:%02d:%02d.%03d",
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
#else
struct timeval tv;
struct tm *tm_info;
char time_string[16];
gettimeofday(&tv, NULL);
tm_info = localtime(&tv.tv_sec);
strftime(time_string, sizeof(time_string), "%H:%M:%S", tm_info);
snprintf(buffer, buffer_size, "%s.%03ld", time_string, tv.tv_usec / 1000);
#endif
}
void get_date_string(char *buffer, size_t buffer_size)
{
time_t t = time(NULL);
struct tm *tm_info = localtime(&t);
// Format date as dd/mm/yyyy
strftime(buffer, buffer_size, "%d/%m/%Y", tm_info);
}
void print_ascii_string(char *src_buffer, size_t src_buffer_size)
{
for (int i = 0; i < 10; i++)
{
for (size_t j = 0; j < src_buffer_size; j++)
{
switch (src_buffer[j])
{
case '0':
printf("%s", ascii_zero[i]);
break;
case '1':
printf("%s", ascii_one[i]);
break;
case '2':
printf("%s", ascii_two[i]);
break;
case '3':
printf("%s", ascii_three[i]);
break;
case '4':
printf("%s", ascii_four[i]);
break;
case '5':
printf("%s", ascii_five[i]);
break;
case '6':
printf("%s", ascii_six[i]);
break;
case '7':
printf("%s", ascii_seven[i]);
break;
case '8':
printf("%s", ascii_eight[i]);
break;
case '9':
printf("%s", ascii_nine[i]);
break;
case ':':
printf("%s", ascii_colon[i]);
break;
case '/':
printf("%s", ascii_slash[i]);
break;
case '.':
printf("%s", ascii_period[i]);
break;
case ' ':
printf("%s", ascii_space[i]);
break;
default:
return;
}
}
printf("\n");
}
printf("\n");
}
void handle_signal()
{
reset_cursor();
printf("\033[2J\033[?25h"); // clear screen, enable cursor
fflush(stdout);
exit(0);
}