[libgda] GdaHandler time correction



commit ff4ba60567b3effa5f90db02515c083cedd7cc24
Author: Vivien Malerba <malerba gnome-db org>
Date:   Thu Sep 26 21:47:10 2013 +0200

    GdaHandler time correction

 libgda/handlers/gda-handler-time.c |   96 ++----------------------------------
 tests/test-input-parsers.c         |    5 ++-
 2 files changed, 9 insertions(+), 92 deletions(-)
---
diff --git a/libgda/handlers/gda-handler-time.c b/libgda/handlers/gda-handler-time.c
index ef4235e..d18f97c 100644
--- a/libgda/handlers/gda-handler-time.c
+++ b/libgda/handlers/gda-handler-time.c
@@ -23,6 +23,7 @@
  */
 
 #include "gda-handler-time.h"
+#include <gda-util.h>
 #include <string.h>
 #include <ctype.h>
 #include <glib/gi18n-lib.h>
@@ -1208,97 +1209,10 @@ make_date (G_GNUC_UNUSED GdaHandlerTime *hdl, GDate *date, const gchar *value,
 static gboolean
 make_time (G_GNUC_UNUSED GdaHandlerTime *hdl, GdaTime *timegda, const gchar *value)
 {
-       const gchar *ptr;
-
-       memset (timegda, 0, sizeof (GdaTime));
-       timegda->timezone = GDA_TIMEZONE_INVALID;
-
-       if (!value)
-               return FALSE;
-
-       /* hour */
-       ptr = value;
-       if (!isdigit (*ptr))
-               return FALSE;
-
-       timegda->hour = *ptr - '0';
-       ptr++;
-       if (isdigit (*ptr)) {
-               timegda->hour = timegda->hour * 10 + (*ptr - '0');
-               ptr++;
-       }
-       if (timegda->hour >= 24)
-               return FALSE;
-
-       if (*ptr == ':')
-               ptr++;
-       else if (!isdigit (*ptr))
-               return FALSE;
-
-       /* minute */
-       timegda->minute = *ptr - '0';
-       ptr++;
-       if (isdigit (*ptr)) {
-               timegda->minute = timegda->minute * 10 + (*ptr - '0');
-               ptr++;
-       }
-       if (timegda->minute >= 60)
-               return FALSE;
-       if (*ptr == ':')
-               ptr++;
-       else if (!isdigit (*ptr))
-               return FALSE;
-
-       /* second */
-       timegda->second = *ptr - '0';
-       ptr++;
-       if (isdigit (*ptr)) {
-               timegda->second = timegda->second * 10 + (*ptr - '0');
-               ptr++;
-       }
-       if (timegda->second >= 60)
-               return FALSE;
-
-       /* fraction */
-       if (*ptr && (*ptr != '.') && (*ptr != '+') && (*ptr != '-'))
-               return FALSE;
-       if (*ptr == '.') {
-               ptr++;
-               if (!isdigit (*ptr))
-                       return FALSE;
-               while (isdigit (*ptr)) {
-                       unsigned long long lu;
-                       lu = timegda->fraction * 10 + (*ptr - '0');
-                       if (lu < G_MAXULONG)
-                               timegda->fraction = lu;
-                       else
-                               return FALSE;
-                       ptr++;
-               }
-       }
-       
-       /* timezone */
-       if ((*ptr == '-') || (*ptr == '+')) {
-               gint sign = (*ptr == '+') ? 1 : -1;
-               ptr++;
-               if (!isdigit (*ptr))
-                       return FALSE;
-               timegda->timezone = sign * (*ptr - '0');
-               ptr++;
-               if (isdigit (*ptr)) {
-                       timegda->timezone = timegda->minute * 10 + sign * (*ptr - '0');
-                       ptr++;
-               }
-               if (*ptr)
-                       return FALSE;
-               if ((timegda->timezone >= -24) && (timegda->timezone <= 24))
-                       timegda->timezone *= 60*60;
-               else {
-                       timegda->timezone = 0;
-                       return FALSE;
-               }
-       }
-       return TRUE;
+       if (gda_parse_iso8601_time (timegda, value))
+               return TRUE;
+       else
+               return gda_parse_formatted_time (timegda, value, 0);
 }
 
 
diff --git a/tests/test-input-parsers.c b/tests/test-input-parsers.c
index 7dec295..e0d4713 100644
--- a/tests/test-input-parsers.c
+++ b/tests/test-input-parsers.c
@@ -146,6 +146,9 @@ TestTime timedata[] = {
        {"12:1:2+11", TRUE, {12, 1, 2, 0, 11*60*60}},
        {"12:1:2.1234+11", TRUE, {12, 1, 2, 1234, 11*60*60}},
        {"12:1:2.12345678-3", TRUE, {12, 1, 2, 12345678, -3*60*60}},
+       {"12:1:2.12345678 UTC", TRUE, {12, 1, 2, 12345678, 0}},
+       {"12:1:2.12345678 CET", TRUE, {12, 1, 2, 12345678, 60*60}},
+       {"12:1:2.12345678 INVALID", FALSE, {12, 1, 2, 12345678, GDA_TIMEZONE_INVALID}},
 };
 
 static gboolean
@@ -340,7 +343,7 @@ test_time_handler (void)
                    (time.second != td.exp_time.second) ||
                    (time.fraction != td.exp_time.fraction) ||
                    (time.timezone != td.exp_time.timezone)) {
-                       g_print ("Wrong result forgda_data_handler_get_value_from_str (\"%s\", 
GDA_TYPE_TIME):\n"
+                       g_print ("Wrong result for gda_data_handler_get_value_from_str (\"%s\", 
GDA_TYPE_TIME):\n"
                                 "   exp: HH=%d MM=%d SS=%d FF=%ld TZ=%ld\n"
                                 "   got: HH=%d MM=%d SS=%d FF=%ld TZ=%ld\n",
                                 td.in_string, 


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