status-time/time.c
author Stiletto <blasux@blasux.ru>
Sat, 17 Aug 2019 15:25:10 +0400
changeset 17 5b6294779007
parent 10 442bed4a072e
permissions -rw-r--r--
[status-time] Support NOT changing timezone

#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <sys/time.h>
#include <errno.h>
#include <time.h>

#include "config.h"
#ifdef LOCALE
#include <locale.h>
#endif

int
main(void)
{
    char buf[129];
    time_t tim;
    struct tm *timtm;
#ifdef LOCALE
    setlocale(LC_TIME, LOCALE);
#endif

#ifdef TIMEZONE
    setenv("TZ", TIMEZONE, 1);
#endif

    while (1) {
        bzero(buf, sizeof(buf));
	tim = time(NULL);
	timtm = localtime(&tim);
	if (timtm == NULL) {
		perror("localtime");
		exit(1);
	}

	if (!strftime(buf, sizeof(buf)-1, TIMEFORMAT, timtm)) {
		fprintf(stderr, "strftime == 0\n");
		exit(1);
	}
	printf("%s\n", buf);
	fflush(stdout);
	sleep(UPDATE_PERIOD);
    }
}