Improve parsing of date-only iso8601 strings
authorJens Georg <mail@jensge.org>
Tue, 24 Aug 2010 21:18:23 +0000 (00:18 +0300)
committerJens Georg <mail@jensge.org>
Thu, 26 Aug 2010 15:18:52 +0000 (18:18 +0300)
glib/gtimer.c
tests/testglib.c

index c1169a7..05fcbc5 100644 (file)
@@ -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 == ':')
     {
index 2453e16..7625928 100644 (file)
@@ -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",