Re: [hackers] [sbase][Patch] date: add date/time setting capability

From: Michael Forney <mforney_AT_mforney.org>
Date: Wed, 28 Dec 2016 21:38:51 -0800

On 12/28/16, John Vogel <jvogel4_AT_stny.rr.com> wrote:
> + t = time(NULL);
> + if (t == (time_t)-1)
> + eprintf("time failed: %s\n", strerror(errno));

eprintf will print the error message and a newline if the format
string ends in ':'.

So you can just do `eprintf("time:")`, which seems to be the
convention for sbase.

> +
> + if (!(now = tztime(&t)))
> + eprintf("%stime failed\n", tz);
> +
> if (argc) {
> - if (argc != 1 || argv[0][0] != '+')
> + if (argc != 1) {
> usage();
> - else
> + } else if (argv[0][0] == '+') {
> fmt = &argv[0][1];
> + } else {
> + struct tm date;
> + struct timespec ts;
> +
> + date.tm_mon = date_field(argv[0], 0) - 1;
> + date.tm_mday = date_field(argv[0], 2);
> + date.tm_hour = date_field(argv[0], 4);
> + date.tm_min = date_field(argv[0], 6);
> +
> + switch (strlen(argv[0])) {
> + case 8:
> + date.tm_year = now->tm_year;
> + break;
> + case 10:
> + date.tm_year = date_field(argv[0], 8);
> + if (date.tm_year < 69)
> + date.tm_year += 100;
> + break;
> + case 12:
> + date.tm_year = ((date_field(argv[0], 8) - 19) * 100) +
> date_field(argv[0], 10);
> + break;
> + default:
> + eprintf("invalid date format: %s\n", argv[0]);
> + }
> +
> + t = mktime(&date);
> + if (t == (time_t)-1)
> + eprintf("mktime failed: bad calender date/time: %s\n", argv[0]);
> +
> + ts.tv_sec = t;
> + ts.tv_nsec = 0;
> +
> + if (clock_settime(CLOCK_REALTIME, &ts) == -1)
> + eprintf("clock_settime failed: %s\n", strerror(errno));

Same here.

> +
> + return 0;
> + }
> }
> - if (!(now = tztime(&t)))
> - eprintf("%stime failed\n", tz);
>
> strftime(buf, sizeof(buf), fmt, now);
> puts(buf);
Received on Thu Dec 29 2016 - 06:38:51 CET

This archive was generated by hypermail 2.3.0 : Thu Dec 29 2016 - 06:48:18 CET