[glib] Improve parsing of date-only iso8601 strings



commit db7ddcc19529a101b820347e8cca419fb37f6a02
Author: Jens Georg <mail jensge org>
Date:   Wed Aug 25 00:18:23 2010 +0300

    Improve parsing of date-only iso8601 strings

 glib/gtimer.c    |   15 +++++++++++++--
 tests/testglib.c |    4 ++++
 2 files changed, 17 insertions(+), 2 deletions(-)
---
diff --git a/glib/gtimer.c b/glib/gtimer.c
index c1169a7..05fcbc5 100644
--- a/glib/gtimer.c
+++ b/glib/gtimer.c
@@ -429,9 +429,20 @@ g_time_val_from_iso8601 (const gchar *iso_date,
       tm.tm_year = val / 10000 - 1900;
     }
 
-  if (*iso_date++ != 'T')
+  if (*iso_date != 'T')
+    {
+      /* Date only */
+      if (*iso_date == '\0')
+        return TRUE;
+      return FALSE;
+    }
+
+  *iso_date++;
+
+  /* If there is a 'T' then there has to be a time */
+  if (!g_ascii_isdigit (*iso_date))
     return FALSE;
-  
+
   val = strtoul (iso_date, (char **)&iso_date, 10);
   if (*iso_date == ':')
     {
diff --git a/tests/testglib.c b/tests/testglib.c
index 2453e16..7625928 100644
--- a/tests/testglib.c
+++ b/tests/testglib.c
@@ -1283,6 +1283,7 @@ various_string_tests (void)
 
 #define REF_INVALID1      "Wed Dec 19 17:20:20 GMT 2007"
 #define REF_INVALID2      "1980-02-22T10:36:00Zulu"
+#define REF_INVALID3      "1980-02-22T"
 #define REF_SEC_UTC       320063760
 #define REF_STR_UTC       "1980-02-22T10:36:00Z"
 #define REF_STR_LOCAL     "1980-02-22T13:36:00"
@@ -1294,6 +1295,7 @@ various_string_tests (void)
 #define REF_STR_USEC_CEST "19800222T123600.050000000+0200"
 #define REF_STR_USEC_EST  "1980-02-22T05:36:00,05-05:00"
 #define REF_STR_USEC_NST  "19800222T070600,0500-0330"
+#define REF_STR_DATE_ONLY "1980-02-22"
 
   if (g_test_verbose())
     g_print ("checking g_time_val_from_iso8601...\n");
@@ -1301,6 +1303,8 @@ various_string_tests (void)
   ref_date.tv_usec = 0;
   g_assert (g_time_val_from_iso8601 (REF_INVALID1, &date) == FALSE);
   g_assert (g_time_val_from_iso8601 (REF_INVALID2, &date) == FALSE);
+  g_assert (g_time_val_from_iso8601 (REF_INVALID3, &date) == FALSE);
+  g_assert (g_time_val_from_iso8601 (REF_STR_DATE_ONLY, &date) != FALSE);
   g_assert (g_time_val_from_iso8601 (REF_STR_UTC, &date) != FALSE);
   if (g_test_verbose())
     g_print ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]