[glib] Fix parsing of timezones



commit efc2cdbfc981754db361f49c30c8ee24ac0c769e
Author: Peter Kjellerstedt <pkj axis com>
Date:   Wed Apr 8 10:26:11 2009 -0400

    Fix parsing of timezones
    
    Make g_time_val_from_iso8601 handle timezones with minutes correctly;
    also accept comma as a fraction separator.  (#578369)
---
 glib/gtimer.c    |    6 +++---
 tests/testglib.c |   18 +++++++++++++++++-
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/glib/gtimer.c b/glib/gtimer.c
index 3221c1a..c52eeb7 100644
--- a/glib/gtimer.c
+++ b/glib/gtimer.c
@@ -367,7 +367,7 @@ g_time_val_from_iso8601 (const gchar *iso_date,
   time_->tv_sec = mktime_utc (&tm);
   time_->tv_usec = 0;
   
-  if (*iso_date == '.')
+  if (*iso_date == ',' || *iso_date == '.')
     {
       glong mul = 100000;
 
@@ -382,14 +382,14 @@ g_time_val_from_iso8601 (const gchar *iso_date,
     {
       gint sign = (*iso_date == '+') ? -1 : 1;
       
-      val = 60 * strtoul (iso_date + 1, (char **)&iso_date, 10);
+      val = strtoul (iso_date + 1, (char **)&iso_date, 10);
       
       if (*iso_date == ':')
 	val = 60 * val + strtoul (iso_date + 1, (char **)&iso_date, 10);
       else
         val = 60 * (val / 100) + (val % 100);
 
-      time_->tv_sec += (time_t) (val * sign);
+      time_->tv_sec += (time_t) (60 * val * sign);
     }
   else if (*iso_date++ != 'Z')
     return FALSE;
diff --git a/tests/testglib.c b/tests/testglib.c
index 8d3a59c..2eeea1e 100644
--- a/tests/testglib.c
+++ b/tests/testglib.c
@@ -1310,10 +1310,12 @@ various_string_tests (void)
 #define REF_STR_UTC       "1980-02-22T10:36:00Z"
 #define REF_STR_CEST      "1980-02-22T12:36:00+02:00"
 #define REF_STR_EST       "19800222T053600-0500"
+#define REF_STR_NST       "1980-02-22T07:06:00-03:30"
 #define REF_USEC_UTC      50000
 #define REF_STR_USEC_UTC  "1980-02-22T10:36:00.050000Z"
 #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_EST  "1980-02-22T05:36:00,05-05:00"
+#define REF_STR_USEC_NST  "19800222T070600,0500-0330"
 
   if (g_test_verbose())
     g_print ("checking g_time_val_from_iso8601...\n");
@@ -1342,6 +1344,13 @@ various_string_tests (void)
              date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
   g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
 
+  g_assert (g_time_val_from_iso8601 (REF_STR_NST, &date) != FALSE);
+  if (g_test_verbose())
+    g_print ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
+             date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
+             date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
+  g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
+
   ref_date.tv_usec = REF_USEC_UTC;
   g_assert (g_time_val_from_iso8601 (REF_STR_USEC_UTC, &date) != FALSE);
   if (g_test_verbose())
@@ -1364,6 +1373,13 @@ various_string_tests (void)
              date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
   g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
 
+  g_assert (g_time_val_from_iso8601 (REF_STR_USEC_NST, &date) != FALSE);
+  if (g_test_verbose())
+    g_print ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
+             date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
+             date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
+  g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
+
   if (g_test_verbose())
     g_print ("checking g_time_val_to_iso8601...\n");
   ref_date.tv_sec = REF_SEC_UTC;



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