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

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