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

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