From: Scott Worley Date: Mon, 4 Sep 2023 00:50:46 +0000 (-0700) Subject: Encode time in time-logger's format X-Git-Tag: v1.0.0~16 X-Git-Url: http://git.scottworley.com/tl-append/commitdiff_plain/bab98a3a230bcc2161254e5796ec97e1391b4907?hp=d522116b5fdc09e3f83fe4f88515dec52a3e0462 Encode time in time-logger's format This is not a good format. It looses timezone information during daylight savings time changes -- it cannot distinguish between the first 01:30 and the second 01:30 during the fall transition. --- diff --git a/common.c b/common.c index ffa4b5a..1bc1102 100644 --- a/common.c +++ b/common.c @@ -1,3 +1,6 @@ +#define _POSIX_C_SOURCE 2 +#include "common.h" + #include #include @@ -13,3 +16,13 @@ void die_err(const char *message) { perror(message); exit(1); } + +const char *encode_time(time_t t) { + struct tm tm; + localtime_r(&t, &tm); + const size_t size = 20; + char *out = (char *)malloc(size); + if (strftime(out, size, "%Y %m %d %H %M %S", &tm) != size - 1) + die("Couldn't format time"); + return out; +} diff --git a/common.h b/common.h index e442b2c..8d40a78 100644 --- a/common.h +++ b/common.h @@ -1,5 +1,9 @@ +#include + extern const char *FILENAME; void die(const char *message); void die_err(const char *message); + +const char *encode_time(time_t t); diff --git a/tl-append-test.c b/tl-append-test.c index 6d037cb..7e8455d 100644 --- a/tl-append-test.c +++ b/tl-append-test.c @@ -1,4 +1,7 @@ #define _POSIX_C_SOURCE 2 +#define _XOPEN_SOURCE +#include +#include #include #include #include @@ -68,6 +71,45 @@ static void verify_log_contents(ex_t exps[]) { die_err("Error closing log file"); } +static void test_encode_time() { + /* localtime_r to set tm's timezone */ + time_t now_time = time(NULL); + struct tm tm; + if (localtime_r(&now_time, &tm) == NULL) + die_err("Can't unpack current time?"); + + const char *strptime_result = strptime("2011-12-13 14:15:16", "%F %T", &tm); + if (strptime_result == NULL || *strptime_result != '\0') + die("Couldn't parse time?"); + time_t tt = mktime(&tm); + if (tt == (time_t)-1) + die_err("Can't pack time?"); + + const char *encoded = encode_time(tt); + /* Loose check to allow for daylight savings time changes between the current + * time and the target time. :( */ + assert(encoded[0] == '2'); + assert(encoded[1] == '0'); + assert(encoded[2] == '1'); + assert(encoded[3] == '1'); + assert(encoded[4] == ' '); + assert(encoded[5] == '1'); + assert(encoded[6] == '2'); + assert(encoded[7] == ' '); + assert(encoded[8] == '1'); + assert(encoded[9] == '3'); + assert(encoded[10] == ' '); + assert(encoded[11] == '1'); + assert(isdigit(encoded[12])); + assert(encoded[13] == ' '); + assert(isdigit(encoded[14])); + assert(isdigit(encoded[15])); + assert(encoded[16] == ' '); + assert(encoded[17] == '1'); + assert(encoded[18] == '6'); + assert(encoded[19] == '\0'); +} + static void write_and_read_line() { remove_logfile(); ex_t e = write_to_tl_append("foo\n"); @@ -82,6 +124,7 @@ static void write_and_read_two_lines() { } int main() { + test_encode_time(); write_and_read_line(); write_and_read_two_lines(); }