]> git.sur5r.net Git - cc65/blob - testcode/lib/clock-test.c
Fixed _textcolor definition.
[cc65] / testcode / lib / clock-test.c
1 /* Clock test program
2  *
3  * 25-Sep-2018, chris@groessler.org
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <time.h>
10 #include <errno.h>
11
12 #ifdef __CC65__
13 #include <conio.h>
14 #include <cc65.h>
15 #endif /* #ifdef __CC65__ */
16
17 static void print_time(void)
18 {
19     struct tm *cur_tm;
20     time_t cur_time = time(NULL);
21     if (cur_time == -1) {
22         printf("time() failed: %s\n", strerror(errno));
23         return;
24     }
25     cur_tm = localtime(&cur_time);
26
27     printf("time: %s\n", asctime(cur_tm));
28     // DEBUG:
29     printf("mday=%d mon=%d year=%d\nhour=%d min=%d sec=%d\n", cur_tm->tm_mday, cur_tm->tm_mon, cur_tm->tm_year, cur_tm->tm_hour, cur_tm->tm_min, cur_tm->tm_sec);
30 }
31
32 int main(int argc, char **argv)
33 {
34     char c = 0;
35     int s;
36     struct tm cur_time;
37     struct timespec new_time;
38
39 #ifdef __CC65__
40     /* if DOS will automatically clear the screen after the program exits, wait for a keypress... */
41     if (doesclrscrafterexit())
42         atexit((void (*)(void))cgetc);
43 #endif
44
45     if (argc <= 1) {
46         print_time();
47         return 0;
48     }
49
50     if (argc != 3 || strcasecmp(*(argv + 1), "set")) {
51         printf("usage: CLOCKTST [set DD-MM-YY-HH-MM-SS]\n");
52         return 1;
53     }
54
55     memset(&cur_time, 0, sizeof(cur_time));
56     s = sscanf(*(argv + 2), "%d-%d-%d-%d-%d-%d", &cur_time.tm_mday, &cur_time.tm_mon, &cur_time.tm_year, &cur_time.tm_hour, &cur_time.tm_min, &cur_time.tm_sec);
57     if (s != 6 || cur_time.tm_year > 99 /* other input values aren't being verified... */) {
58         printf("invalid time/date format\n");
59         return 1;
60     }
61     --cur_time.tm_mon;
62     if (cur_time.tm_year < 79)
63         cur_time.tm_year += 100;  /* adjust century */
64
65     memset(&new_time, 0, sizeof(new_time));
66     new_time.tv_sec = mktime(&cur_time);
67
68     printf("\nyou are about to set the time to\n-->  %s\n\nContinue (y/n)?", ctime(&new_time.tv_sec));
69
70     while (c != 'y' && c != 'Y' && c != 'n' && c != 'N') {
71 #ifdef __CC65__
72         c = cgetc();
73 #else
74         c = getchar();
75 #endif
76     }
77     printf("%c\n", c);
78
79     if (c == 'n' || c == 'N') {
80         printf("user abort\n");
81         return 0;
82     }
83
84     s = clock_settime(CLOCK_REALTIME, &new_time);
85     if (s) {
86         printf("clock_settime() failed: %s\n", strerror(errno));
87         return 1;
88     }
89     printf("time set!\n");
90     //DEBUG test begin
91     print_time();
92     //DEBUG test end
93     return 0;
94 }
95 /* Local Variables: */
96 /* c-file-style: "cpg" */
97 /* c-basic-offset: 4 */
98 /* End: */