From 91adc4817866dcde11bc0547b2ee9ab672dbc2d8 Mon Sep 17 00:00:00 2001 From: default Date: Mon, 19 Sep 2022 21:24:55 +0200 Subject: [PATCH] Added time functions. --- snac.c | 20 +++++++++++++++++++- snac.h | 4 ++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/snac.c b/snac.c index 1957271..c163649 100644 --- a/snac.c +++ b/snac.c @@ -21,10 +21,28 @@ d_char *srv_baseurl = NULL; int dbglevel = 0; +d_char *xs_time(char *fmt, int local) +/* returns a d_char with a formated time */ +{ + time_t t = time(NULL); + struct tm tm; + char tmp[64]; + + if (local) + localtime_r(&t, &tm); + else + gmtime_r(&t, &tm); + + strftime(tmp, sizeof(tmp), fmt, &tm); + + return xs_str_new(tmp); +} + + void srv_log(d_char *str) /* logs a message */ { - char tm[16] = "00:00:00"; + xs *tm = xs_local_time("%H:%M:%S"); xs *msg = str; fprintf(stderr, "%s %s\n", tm, msg); diff --git a/snac.h b/snac.h index 609be40..d091a7b 100644 --- a/snac.h +++ b/snac.h @@ -7,6 +7,10 @@ extern d_char *srv_baseurl; extern int dbglevel; +d_char *xs_time(char *fmt, int local); +#define xs_local_time(fmt) xs_time(fmt, 1) +#define xs_utc_time(fmt) xs_time(fmt, 0) + void srv_log(d_char *str); int srv_open(char *basedir);