2019-12-27 23:07:37 +00:00
|
|
|
/* Calendar-clock test program
|
|
|
|
**
|
|
|
|
** 2018-Sep-25, chris@groessler.org
|
2019-12-31 00:53:39 +00:00
|
|
|
** 2019-Dec-30, Greg King
|
2019-12-27 23:07:37 +00:00
|
|
|
*/
|
2018-09-25 00:47:45 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#ifdef __CC65__
|
2019-12-27 23:07:37 +00:00
|
|
|
#include <conio.h>
|
|
|
|
#include <cc65.h>
|
|
|
|
#endif
|
2018-09-25 00:47:45 +00:00
|
|
|
|
2019-12-27 23:07:37 +00:00
|
|
|
static int print_time(void)
|
2018-09-25 00:47:45 +00:00
|
|
|
{
|
|
|
|
struct tm *cur_tm;
|
|
|
|
time_t cur_time = time(NULL);
|
2019-12-27 23:07:37 +00:00
|
|
|
|
2018-09-25 00:47:45 +00:00
|
|
|
if (cur_time == -1) {
|
|
|
|
printf("time() failed: %s\n", strerror(errno));
|
2019-12-27 23:07:37 +00:00
|
|
|
return 1;
|
2018-09-25 00:47:45 +00:00
|
|
|
}
|
|
|
|
cur_tm = localtime(&cur_time);
|
|
|
|
|
|
|
|
printf("time: %s\n", asctime(cur_tm));
|
|
|
|
// DEBUG:
|
2019-12-27 23:07:37 +00:00
|
|
|
printf("year=%d, mon=%d, mday=%d\nhour=%d, min=%d, sec=%d\n",
|
|
|
|
cur_tm->tm_year, cur_tm->tm_mon, cur_tm->tm_mday,
|
|
|
|
cur_tm->tm_hour, cur_tm->tm_min, cur_tm->tm_sec);
|
|
|
|
return 0;
|
2018-09-25 00:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2019-12-27 23:07:37 +00:00
|
|
|
char c;
|
2018-09-25 18:26:25 +00:00
|
|
|
int s;
|
|
|
|
struct tm cur_time;
|
|
|
|
struct timespec new_time;
|
2018-09-25 00:47:45 +00:00
|
|
|
|
|
|
|
#ifdef __CC65__
|
2019-12-27 23:07:37 +00:00
|
|
|
/* If DOS automatically will clear the screen after the program exits,
|
|
|
|
** then wait for a key-press.
|
|
|
|
*/
|
2018-09-25 12:35:52 +00:00
|
|
|
if (doesclrscrafterexit())
|
|
|
|
atexit((void (*)(void))cgetc);
|
2018-09-25 00:47:45 +00:00
|
|
|
#endif
|
|
|
|
|
2019-12-27 23:07:37 +00:00
|
|
|
if (argc == 1) {
|
|
|
|
return print_time();
|
2018-09-25 00:47:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 23:07:37 +00:00
|
|
|
if (argc != 2) {
|
2019-12-31 00:53:39 +00:00
|
|
|
#if defined(__APPLE2__)
|
|
|
|
printf("USAGE: CALL2051 [:REM YY-MM-DD-HH-MM-SS]\n");
|
|
|
|
#elif defined(__ATMOS__) || defined(__CBM__)
|
|
|
|
printf("Usage: run [:rem YY-MM-DD-HH-MM-SS]\n");
|
2019-12-27 23:07:37 +00:00
|
|
|
#else
|
|
|
|
printf("Usage: %s [YY-MM-DD-HH-MM-SS]\n", argv[0]);
|
|
|
|
#endif
|
2018-09-25 00:47:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-12-27 23:07:37 +00:00
|
|
|
memset(&cur_time, 0, sizeof cur_time);
|
|
|
|
s = sscanf(argv[1], "%d-%d-%d-%d-%d-%d",
|
|
|
|
&cur_time.tm_year, &cur_time.tm_mon, &cur_time.tm_mday,
|
|
|
|
&cur_time.tm_hour, &cur_time.tm_min, &cur_time.tm_sec);
|
|
|
|
if (s != 6 || cur_time.tm_year > 99 /* other input values aren't being verified */) {
|
|
|
|
printf("Invalid date-time format\n");
|
2018-09-25 00:47:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-12-27 23:07:37 +00:00
|
|
|
cur_time.tm_year += 100; /* assume 21st century */
|
2018-09-25 00:47:45 +00:00
|
|
|
--cur_time.tm_mon;
|
|
|
|
|
2019-12-27 23:07:37 +00:00
|
|
|
memset(&new_time, 0, sizeof new_time);
|
2018-09-25 00:47:45 +00:00
|
|
|
new_time.tv_sec = mktime(&cur_time);
|
|
|
|
|
2019-12-27 23:07:37 +00:00
|
|
|
printf("\nYou are about to set the time to\n--> %s\nContinue (y/n)? ", ctime(&new_time.tv_sec));
|
|
|
|
do {
|
2018-09-25 00:47:45 +00:00
|
|
|
#ifdef __CC65__
|
|
|
|
c = cgetc();
|
|
|
|
#else
|
|
|
|
c = getchar();
|
|
|
|
#endif
|
2019-12-27 23:07:37 +00:00
|
|
|
} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
|
2018-09-25 00:47:45 +00:00
|
|
|
printf("%c\n", c);
|
|
|
|
|
|
|
|
if (c == 'n' || c == 'N') {
|
2019-12-27 23:07:37 +00:00
|
|
|
printf("User abort\n");
|
2018-09-25 00:47:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = clock_settime(CLOCK_REALTIME, &new_time);
|
|
|
|
if (s) {
|
|
|
|
printf("clock_settime() failed: %s\n", strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
2019-12-27 23:07:37 +00:00
|
|
|
printf("Time set!\n\n");
|
|
|
|
|
2018-09-25 00:47:45 +00:00
|
|
|
//DEBUG test begin
|
2019-12-27 23:07:37 +00:00
|
|
|
return print_time();
|
2018-09-25 00:47:45 +00:00
|
|
|
//DEBUG test end
|
|
|
|
}
|
|
|
|
/* Local Variables: */
|
|
|
|
/* c-file-style: "cpg" */
|
|
|
|
/* c-basic-offset: 4 */
|
|
|
|
/* End: */
|