]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Improve portability of timestamps in JSON
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Mon, 4 Dec 2023 06:03:14 +0000 (03:03 -0300)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Mon, 4 Dec 2023 06:10:26 +0000 (03:10 -0300)
It looks like %F, %T and %z are not portable conversion specification
characters for strptime() and strftime(). Therefore, change date format
from "%FT%T%z" to "%Y-%m-%dT%H:%M:%SZ".

This means the JSON now employs Zulu, which also fixes a unit test that
used to be hardcoded to my own timezone. Yaay.

Fixes #101.

src/json_util.c
test/cache/local_cache_test.c

index e69f2240df0d22b75781a9ab25fb2d162be849c9..870ecadd5b4354a33cad2f30f4be9ca418bb42ea 100644 (file)
@@ -5,6 +5,12 @@
 #include <time.h>
 #include "log.h"
 
+/*
+ * Careful with this; several of the conversion specification characters
+ * documented in the Linux man page are not actually portable.
+ */
+#define JSON_TS_FORMAT "%Y-%m-%dT%H:%M:%SZ"
+
 int
 json_get_str(json_t *parent, char const *name, char const **result)
 {
@@ -114,7 +120,7 @@ json_get_ts(json_t *parent, char const *name, time_t *result)
                return error;
 
        memset(&tm, 0, sizeof(tm));
-       consumed = strptime(str, "%FT%T%z", &tm);
+       consumed = strptime(str, JSON_TS_FORMAT, &tm);
        if (consumed == NULL || (*consumed) != 0)
                return pr_op_err("String '%s' does not appear to be a timestamp.",
                    str);
@@ -220,10 +226,10 @@ tt2json(time_t tt, json_t **result)
        struct tm tmbuffer, *tm;
 
        memset(&tmbuffer, 0, sizeof(tmbuffer));
-       tm = localtime_r(&tt, &tmbuffer);
+       tm = gmtime_r(&tt, &tmbuffer);
        if (tm == NULL)
                return errno;
-       if (strftime(str, sizeof(str) - 1, "%FT%T%z", tm) == 0)
+       if (strftime(str, sizeof(str) - 1, JSON_TS_FORMAT, tm) == 0)
                return ENOSPC;
 
        *result = json_string(str);
index 00751c63488e458d3632847c846bf6dd27f86fae..b9284bf59bffeae569eda72b5849a1151e7bff17 100644 (file)
@@ -733,13 +733,12 @@ START_TEST(test_metadata_json)
        /* printf("%s\n", str); */
        json_decref(json);
 
-       /* TODO (test) Time zones are hardcoded to CST */
        ck_assert_str_eq(
-           "[{\"url\":\"rsync://a.b.c/d\",\"attempt-timestamp\":\"1969-12-31T18:00:00-0600\",\"attempt-result\":0,\"success-timestamp\":\"1969-12-31T18:00:00-0600\"},"
-           "{\"url\":\"rsync://a.b.c/e\",\"attempt-timestamp\":\"1969-12-31T18:00:00-0600\",\"attempt-result\":1},"
-           "{\"url\":\"rsync://x.y.z/e\",\"attempt-timestamp\":\"1969-12-31T18:00:00-0600\",\"attempt-result\":0,\"success-timestamp\":\"1969-12-31T18:00:00-0600\"},"
-           "{\"url\":\"https://a/b\",\"attempt-timestamp\":\"1969-12-31T18:00:00-0600\",\"attempt-result\":1,\"success-timestamp\":\"1969-12-31T18:00:00-0600\"},"
-           "{\"url\":\"https://a/c\",\"attempt-timestamp\":\"1969-12-31T18:00:00-0600\",\"attempt-result\":0,\"success-timestamp\":\"1969-12-31T18:00:00-0600\"}]",
+           "[{\"url\":\"rsync://a.b.c/d\",\"attempt-timestamp\":\"1970-01-01T00:00:00Z\",\"attempt-result\":0,\"success-timestamp\":\"1970-01-01T00:00:00Z\"},"
+           "{\"url\":\"rsync://a.b.c/e\",\"attempt-timestamp\":\"1970-01-01T00:00:00Z\",\"attempt-result\":1},"
+           "{\"url\":\"rsync://x.y.z/e\",\"attempt-timestamp\":\"1970-01-01T00:00:00Z\",\"attempt-result\":0,\"success-timestamp\":\"1970-01-01T00:00:00Z\"},"
+           "{\"url\":\"https://a/b\",\"attempt-timestamp\":\"1970-01-01T00:00:00Z\",\"attempt-result\":1,\"success-timestamp\":\"1970-01-01T00:00:00Z\"},"
+           "{\"url\":\"https://a/c\",\"attempt-timestamp\":\"1970-01-01T00:00:00Z\",\"attempt-result\":0,\"success-timestamp\":\"1970-01-01T00:00:00Z\"}]",
            str);
        printf("%s", str);
        free(str);