Blame SOURCES/0169-lib-introduce-parser-of-ISO-date-strings.patch

28bab8
From 9e28f84f001e3fb26ab84b62f1c69ae56d63c6f8 Mon Sep 17 00:00:00 2001
28bab8
From: Matej Habrnal <mhabrnal@redhat.com>
28bab8
Date: Fri, 12 Feb 2016 16:54:24 +0100
28bab8
Subject: [PATCH] lib: introduce parser of ISO date strings
28bab8
28bab8
Make sure we can convert data back and forth without losing information.
28bab8
The introduced function complements iso_date_string().
28bab8
28bab8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
28bab8
Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
28bab8
---
28bab8
 po/POTFILES.in                   |   1 +
28bab8
 src/include/internal_libreport.h |  11 ++++
28bab8
 src/lib/iso_date_string.c        |  27 +++++++++-
28bab8
 tests/Makefile.am                |   3 +-
28bab8
 tests/iso_date.at                | 106 +++++++++++++++++++++++++++++++++++++++
28bab8
 tests/testsuite.at               |   1 +
28bab8
 6 files changed, 147 insertions(+), 2 deletions(-)
28bab8
 create mode 100644 tests/iso_date.at
28bab8
28bab8
diff --git a/po/POTFILES.in b/po/POTFILES.in
28bab8
index 30c9cb5..e952711 100644
28bab8
--- a/po/POTFILES.in
28bab8
+++ b/po/POTFILES.in
28bab8
@@ -20,6 +20,7 @@ src/lib/create_dump_dir.c
28bab8
 src/lib/curl.c
28bab8
 src/lib/dump_dir.c
28bab8
 src/lib/event_config.c
28bab8
+src/lib/iso_date_string.c
28bab8
 src/lib/ureport.c
28bab8
 src/lib/make_descr.c
28bab8
 src/lib/parse_options.c
28bab8
diff --git a/src/include/internal_libreport.h b/src/include/internal_libreport.h
28bab8
index b632803..78a17ae 100644
28bab8
--- a/src/include/internal_libreport.h
28bab8
+++ b/src/include/internal_libreport.h
28bab8
@@ -644,6 +644,17 @@ char* get_environ(pid_t pid);
28bab8
 #define iso_date_string libreport_iso_date_string
28bab8
 char *iso_date_string(const time_t *pt);
28bab8
 #define LIBREPORT_ISO_DATE_STRING_SAMPLE "YYYY-MM-DD-hh:mm:ss"
28bab8
+#define LIBREPORT_ISO_DATE_STRING_FORMAT "%Y-%m-%d-%H:%M:%S"
28bab8
+
28bab8
+/* Parses date into integer UNIX time stamp
28bab8
+ *
28bab8
+ * @param date The parsed date string
28bab8
+ * @param pt Return value
28bab8
+ * @return 0 on success; otherwise non-0 number. -EINVAL if the parameter date
28bab8
+ * does not match LIBREPORT_ISO_DATE_STRING_FORMAT
28bab8
+ */
28bab8
+#define iso_date_string_parse libreport_iso_date_string_parse
28bab8
+int iso_date_string_parse(const char *date, time_t *pt);
28bab8
 
28bab8
 enum {
28bab8
     MAKEDESC_SHOW_FILES     = (1 << 0),
28bab8
diff --git a/src/lib/iso_date_string.c b/src/lib/iso_date_string.c
28bab8
index a7fb867..c0e567f 100644
28bab8
--- a/src/lib/iso_date_string.c
28bab8
+++ b/src/lib/iso_date_string.c
28bab8
@@ -33,7 +33,32 @@ char *iso_date_string(const time_t *pt)
28bab8
     if (ptm->tm_year+1900 < 0 || ptm->tm_year+1900 > 9999)
28bab8
         error_msg_and_die("Year=%d?? Aborting", ptm->tm_year+1900);
28bab8
 
28bab8
-    strftime(buf, sizeof(buf), "%Y-%m-%d-%H:%M:%S", ptm);
28bab8
+    strftime(buf, sizeof(buf), LIBREPORT_ISO_DATE_STRING_FORMAT, ptm);
28bab8
 
28bab8
     return buf;
28bab8
 }
28bab8
+
28bab8
+int iso_date_string_parse(const char *date, time_t *pt)
28bab8
+{
28bab8
+    struct tm local;
28bab8
+    const char *r = strptime(date, LIBREPORT_ISO_DATE_STRING_FORMAT, &local);
28bab8
+
28bab8
+    if (r == NULL)
28bab8
+    {
28bab8
+        log_warning(_("String doesn't seem to be a date: '%s'"), date);
28bab8
+        return -EINVAL;
28bab8
+    }
28bab8
+    if (*r != '\0')
28bab8
+    {
28bab8
+        log_warning(_("The date: '%s' has unrecognized suffix: '%s'"), date, r);
28bab8
+        return -EINVAL;
28bab8
+    }
28bab8
+    if (local.tm_year < 70)
28bab8
+    {
28bab8
+        log_warning(_("The date: '%s' is out of UNIX time stamp range"), date);
28bab8
+        return -EINVAL;
28bab8
+    }
28bab8
+
28bab8
+    *pt = mktime(&local);
28bab8
+    return 0;
28bab8
+}
28bab8
diff --git a/tests/Makefile.am b/tests/Makefile.am
28bab8
index 5ed7af6..f36ab57 100644
28bab8
--- a/tests/Makefile.am
28bab8
+++ b/tests/Makefile.am
28bab8
@@ -44,7 +44,8 @@ TESTSUITE_AT = \
28bab8
   string_list.at \
28bab8
   ureport.at \
28bab8
   dump_dir.at \
28bab8
-  global_config.at
28bab8
+  global_config.at \
28bab8
+  iso_date.at
28bab8
 
28bab8
 EXTRA_DIST += $(TESTSUITE_AT)
28bab8
 TESTSUITE = $(srcdir)/testsuite
28bab8
diff --git a/tests/iso_date.at b/tests/iso_date.at
28bab8
new file mode 100644
28bab8
index 0000000..789b46d
28bab8
--- /dev/null
28bab8
+++ b/tests/iso_date.at
28bab8
@@ -0,0 +1,106 @@
28bab8
+# -*- Autotest -*-
28bab8
+
28bab8
+AT_BANNER([ISO_date])
28bab8
+
28bab8
+## --------------- ##
28bab8
+## iso_date_string ##
28bab8
+## --------------- ##
28bab8
+
28bab8
+AT_TESTFUN([iso_date_string],
28bab8
+[[#include "internal_libreport.h"
28bab8
+#include <assert.h>
28bab8
+#include <string.h>
28bab8
+#include <stdio.h>
28bab8
+
28bab8
+bool string_cmp(const char *orig, const char *other)
28bab8
+{
28bab8
+    if (strcmp(orig, other) == 0)
28bab8
+        return true;
28bab8
+
28bab8
+    printf("'%s' != '%s'\n", orig, other);
28bab8
+    return false;
28bab8
+}
28bab8
+
28bab8
+int main(void)
28bab8
+{
28bab8
+    g_verbose=3;
28bab8
+
28bab8
+    setenv("TZ", "", 1);
28bab8
+    setenv("LC_ALL", "C", 1);
28bab8
+
28bab8
+    time_t local[3];
28bab8
+
28bab8
+    time(&local[0]);
28bab8
+    char *date = xstrdup(iso_date_string(NULL));
28bab8
+
28bab8
+    local[1] = local[0] + 1;
28bab8
+    local[2] = local[0] + 2;
28bab8
+    size_t i = 0;
28bab8
+    for (; ARRAY_SIZE(local); ++i)
28bab8
+    {
28bab8
+        if (string_cmp(date, iso_date_string(local + i)))
28bab8
+            break;
28bab8
+    }
28bab8
+    assert((i != ARRAY_SIZE(local)) || !"None of attempts hit result date");
28bab8
+    free(date);
28bab8
+
28bab8
+    time_t y2k = 946684800;
28bab8
+    assert(string_cmp("2000-01-01-00:00:00", iso_date_string(&y2k)));
28bab8
+
28bab8
+    return 0;
28bab8
+}
28bab8
+
28bab8
+]])
28bab8
+
28bab8
+## --------------------- ##
28bab8
+## iso_date_string_parse ##
28bab8
+## --------------------- ##
28bab8
+
28bab8
+AT_TESTFUN([parse_numbers],
28bab8
+[[#include "internal_libreport.h"
28bab8
+#include <assert.h>
28bab8
+#include <string.h>
28bab8
+#include <stdio.h>
28bab8
+
28bab8
+int main(void)
28bab8
+{
28bab8
+    g_verbose=3;
28bab8
+
28bab8
+    setenv("TZ", "", 1);
28bab8
+    setenv("LC_ALL", "C", 1);
28bab8
+
28bab8
+    {
28bab8
+        time_t result = 0;
28bab8
+        assert(iso_date_string_parse("", &result) == -EINVAL);
28bab8
+    }
28bab8
+
28bab8
+    {
28bab8
+        time_t result = 0;
28bab8
+        assert(iso_date_string_parse("foo", &result) == -EINVAL);
28bab8
+    }
28bab8
+
28bab8
+    {
28bab8
+        time_t result = 0;
28bab8
+        assert(iso_date_string_parse("1969-12-31-23:59:59", &result) == -EINVAL);
28bab8
+    }
28bab8
+
28bab8
+    {
28bab8
+        time_t result = 0;
28bab8
+        assert(iso_date_string_parse("1970-01-01-00:00:00", &result) == 0);
28bab8
+        assert(result == 0);
28bab8
+    }
28bab8
+
28bab8
+    {
28bab8
+        time_t result = 0;
28bab8
+        assert(iso_date_string_parse("2000-01-01-00:00:00", &result) == 0);
28bab8
+        assert(result == 946684800 || !"Y2k");
28bab8
+    }
28bab8
+
28bab8
+    {
28bab8
+        time_t result = 0;
28bab8
+        assert(iso_date_string_parse("2000-01-01-00:00:00fooo", &result) == -EINVAL);
28bab8
+    }
28bab8
+
28bab8
+    return 0;
28bab8
+}
28bab8
+]])
28bab8
diff --git a/tests/testsuite.at b/tests/testsuite.at
28bab8
index 91f0823..e5e2f72 100644
28bab8
--- a/tests/testsuite.at
28bab8
+++ b/tests/testsuite.at
28bab8
@@ -19,3 +19,4 @@ m4_include([string_list.at])
28bab8
 m4_include([ureport.at])
28bab8
 m4_include([dump_dir.at])
28bab8
 m4_include([global_config.at])
28bab8
+m4_include([iso_date.at])
28bab8
-- 
28bab8
1.8.3.1
28bab8