]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
Correzioni a SolipaUtils::get_gdatetime_from_string.
authorAndrea Zagli <azagli@libero.it>
Sat, 27 Aug 2011 16:54:56 +0000 (18:54 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 27 Aug 2011 16:54:56 +0000 (18:54 +0200)
Non si teneva conto di date/tempo senza la data.

src/utils.c

index 78ee544d2eb8bee90df884ea7f899681d0c0cc27..c63b8b0464b198b376274dd999edf8bb97f3a8c3 100644 (file)
@@ -732,6 +732,7 @@ GDateTime
 *solipa_get_gdatetime_from_string (const gchar *string, const gchar *format)
 {
        GDateTime *ret;
+
        gchar *new_str;
        gchar *new_format;
        gchar **str_tokens;
@@ -749,6 +750,8 @@ GDateTime
        gint minute;
        gdouble seconds;
 
+       gboolean error;
+
        g_return_val_if_fail (string != NULL, NULL);
 
        new_str = g_strstrip (g_strdup (string));
@@ -808,32 +811,72 @@ GDateTime
        len_strv = g_strv_length (str_tokens);
        len_formatv = g_strv_length (format_tokens);
 
+       year = 1;
+       month = 1;
+       day = 1;
+       hour = 0;
+       minute = 0;
+       seconds = 0.0;
+
+       error = FALSE;
+       ret = NULL;
+
        i_to = MIN (len_strv, len_formatv);
        for (i = 0; i < i_to; i++)
                {
                        if (g_strcmp0 (format_tokens[i], "%Y") == 0)
                                {
                                        year = strtol (str_tokens[i], NULL, 10);
+                                       if (year < 1 || year > 9999)
+                                               {
+                                                       error = TRUE;
+                                                       break;
+                                               }
                                }
                        else if (g_strcmp0 (format_tokens[i], "%m") == 0)
                                {
                                        month = strtol (str_tokens[i], NULL, 10);
+                                       if (month < 1 || month > 12)
+                                               {
+                                                       error = TRUE;
+                                                       break;
+                                               }
                                }
                        else if (g_strcmp0 (format_tokens[i], "%d") == 0)
                                {
                                        day = strtol (str_tokens[i], NULL, 10);
+                                       if (day < 1 || day > 31)
+                                               {
+                                                       error = TRUE;
+                                                       break;
+                                               }
                                }
                        else if (g_strcmp0 (format_tokens[i], "%H") == 0)
                                {
                                        hour = strtol (str_tokens[i], NULL, 10);
+                                       if (hour > 23)
+                                               {
+                                                       error = TRUE;
+                                                       break;
+                                               }
                                }
                        else if (g_strcmp0 (format_tokens[i], "%M") == 0)
                                {
                                        minute = strtol (str_tokens[i], NULL, 10);
+                                       if (minute > 59)
+                                               {
+                                                       error = TRUE;
+                                                       break;
+                                               }
                                }
                        else if (g_strcmp0 (format_tokens[i], "%S") == 0)
                                {
                                        seconds = g_strtod (str_tokens[i], NULL);
+                                       if (seconds > 59.0)
+                                               {
+                                                       error = TRUE;
+                                                       break;
+                                               }
                                }
                }
 
@@ -841,12 +884,15 @@ GDateTime
        g_free (new_format);
        g_free (new_str);
 
-       ret = g_date_time_new_local (year,
-                                    month,
-                                    day,
-                                    hour,
-                                    minute,
-                                    seconds);
+       if (!error)
+               {
+                       ret = g_date_time_new_local (year,
+                                                    month,
+                                                    day,
+                                                    hour,
+                                                    minute,
+                                                    seconds);
+               }
 
        return ret;
 }