-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
76 lines (61 loc) · 1.82 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
void help(char *name) {
printf("Usage: %s [OPTIONS]\n", name);
printf("\n");
printf("Options:\n");
printf(" -t <timezone> Timezone offset in hours\n");
printf(" -f <format> Output format (default: @%%06.2f)\n");
printf(" -l Use local time instead of UTC\n");
printf("\n");
printf("Examples:\n");
printf(" %s\n", name);
printf(" %s -t 3\n", name);
printf(" %s -t -5 -f '%%06.2f'\n", name);
printf(" %s -l\n", name);
printf("\n");
printf("Report bugs to: crg@crg.eti.br\n");
}
int main(int argc, char *argv[]) {
time_t now = time(NULL);
struct tm *utc = gmtime(&now);
int t = 0;
char *format = NULL;
char buffer[80];
int opt;
while ((opt = getopt(argc, argv, "t:f:lh")) != -1) {
switch (opt) {
case 't':
t = atoi(optarg);
continue;
case 'f':
format = optarg;
continue;
case 'l':
utc = localtime(&now);
continue;
case 'h':
help(argv[0]);
return 0;
default:
help(argv[0]);
return 1;
}
}
if (t < -12 || t > 12) {
fprintf(stderr, "Invalid timezone\n");
return 1;
}
// Adjust hour with timezone and normalize to [0, 23]
int adjusted_hour = (utc->tm_hour + t + 24) % 24;
// Calculate the beat value (Swatch Internet Time)
int total_seconds_bmt = adjusted_hour * 3600 + utc->tm_min * 60 + utc->tm_sec;
float beat = total_seconds_bmt / 86.4; // 1 day = 1000 beats so 1 beat = 86.4 seconds
if (format == NULL) {
format = "@%06.2f\n";
}
printf(format, beat);
return 0;
}