From fadc3130a9b9b89d8512b11b1741b4a03bf49c55 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Fri, 9 Sep 2011 12:08:55 +0200 Subject: [PATCH] Migrated GtkFormFieldDateTime to GDateTime. closes #9 --- libgtkform/fielddatetime.c | 99 +++++++++++++++++--------------------- libgtkform/form.c | 2 +- test/test.xml | 1 + test/test_db.xml | 1 + 4 files changed, 47 insertions(+), 56 deletions(-) diff --git a/libgtkform/fielddatetime.c b/libgtkform/fielddatetime.c index 6cb3157..8821b57 100644 --- a/libgtkform/fielddatetime.c +++ b/libgtkform/fielddatetime.c @@ -325,11 +325,9 @@ gtk_form_field_datetime_clear (GtkFormField *field) gboolean gtk_form_field_datetime_is_empty (GtkFormField *field) { - gboolean ret; - g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), TRUE); - return !gtk_form_field_datetime_is_valid_datetime (GTK_FORM_FIELD_DATETIME (field)); + return (g_strcmp0 (gtk_form_field_datetime_get_value_stringify (GTK_FORM_FIELD (field)), "") == 0); } /** @@ -386,31 +384,32 @@ gtk_form_field_datetime_is_valid (GtkFormField *field) GtkFormFieldDateTimePrivate *priv; + gchar *str_value; GDateTime *gdatetime; - struct tm *tm_value; - gchar *min_value; - struct tm *tm_min_value; + GDateTime *gdt_min_value; gchar *max_value; - struct tm *tm_max_value; + GDateTime *gdt_max_value; + GDateTime *gdt_tmp; g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), FALSE); priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (GTK_FORM_FIELD_DATETIME (field)); - gdatetime = gtk_form_field_datetime_get_gdatetime_from_str (gtk_form_field_datetime_get_value_stringify (field), priv->display_format); - if (gdatetime == NULL) + ret = FALSE; + + str_value = gtk_form_field_datetime_get_value_stringify (field); + if (g_strcmp0 (str_value, "") == 0) { - return FALSE; + return TRUE; } - ret = TRUE; - - tm_value = gtk_form_field_datetime_get_tm_from_str (gtk_form_field_datetime_get_value_stringify (field), priv->display_format); + gdatetime = gtk_form_field_datetime_get_gdatetime_from_str (str_value, priv->display_format); - if (tm_value != NULL) + if (gdatetime != NULL) { + ret = TRUE; min_value = NULL; max_value = NULL; @@ -424,41 +423,36 @@ gtk_form_field_datetime_is_valid (GtkFormField *field) { if (g_strcasecmp (min_value, "now") == 0) { - time_t tt; - tt = time (NULL); - - tm_min_value = g_memdup (localtime (&tt), sizeof (struct tm)); + gdt_tmp = g_date_time_new_now_local (); if (priv->type == GTK_FORM_FIELD_DATETIME_TYPE_DATE) { - tm_min_value->tm_hour = 0; - tm_min_value->tm_min = 0; - tm_min_value->tm_sec = 0; + gdt_min_value = g_date_time_new_local (g_date_time_get_year (gdt_tmp), + g_date_time_get_month (gdt_tmp), + g_date_time_get_day_of_month (gdt_tmp), + 0, 0, 0.0); } else if (priv->type == GTK_FORM_FIELD_DATETIME_TYPE_TIME) { - tm_min_value->tm_year = 0; - tm_min_value->tm_mon = 0; - tm_min_value->tm_mday = 0; + gdt_min_value = g_date_time_new_local (1, 1, 1, + g_date_time_get_hour (gdt_tmp), + g_date_time_get_minute (gdt_tmp), + g_date_time_get_seconds (gdt_tmp)); } + gdt_min_value = g_date_time_ref (gdt_tmp); + g_date_time_unref (gdt_tmp); } else { - tm_min_value = gtk_form_field_datetime_get_tm_from_str (min_value, + gdt_min_value = gtk_form_field_datetime_get_gdatetime_from_str (min_value, gtk_form_field_datetime_get_str_format (GTK_FORM_FIELD_DATETIME (field))); } - if (tm_min_value != NULL) + if (gdt_min_value != NULL) { - time_t tt_min_value; - time_t tt_value; - - tt_min_value = mktime (tm_min_value); - tt_value = mktime (tm_value); - - ret = (difftime (tt_min_value, tt_value) <= 0); + ret = (g_date_time_compare (gdatetime, gdt_min_value) >= 0); - g_free (tm_min_value); + g_date_time_unref (gdt_min_value); } } @@ -468,45 +462,40 @@ gtk_form_field_datetime_is_valid (GtkFormField *field) { if (g_strcasecmp (max_value, "now") == 0) { - time_t tt; - tt = time (NULL); - - tm_max_value = g_memdup (localtime (&tt), sizeof (struct tm)); + gdt_tmp = g_date_time_new_now_local (); if (priv->type == GTK_FORM_FIELD_DATETIME_TYPE_DATE) { - tm_max_value->tm_hour = 0; - tm_max_value->tm_min = 0; - tm_max_value->tm_sec = 0; + gdt_max_value = g_date_time_new_local (g_date_time_get_year (gdt_tmp), + g_date_time_get_month (gdt_tmp), + g_date_time_get_day_of_month (gdt_tmp), + 0, 0, 0.0); } else if (priv->type == GTK_FORM_FIELD_DATETIME_TYPE_TIME) { - tm_max_value->tm_year = 0; - tm_max_value->tm_mon = 0; - tm_max_value->tm_mday = 0; + gdt_max_value = g_date_time_new_local (1, 1, 1, + g_date_time_get_hour (gdt_tmp), + g_date_time_get_minute (gdt_tmp), + g_date_time_get_seconds (gdt_tmp)); } + gdt_max_value = g_date_time_ref (gdt_tmp); + g_date_time_unref (gdt_tmp); } else { - tm_max_value = gtk_form_field_datetime_get_tm_from_str (max_value, + gdt_max_value = gtk_form_field_datetime_get_gdatetime_from_str (max_value, gtk_form_field_datetime_get_str_format (GTK_FORM_FIELD_DATETIME (field))); } - if (tm_max_value != NULL) + if (gdt_max_value != NULL) { - time_t tt_max_value; - time_t tt_value; - - tt_max_value = mktime (tm_max_value); - tt_value = mktime (tm_value); - - ret = (difftime (tt_max_value, tt_value) >= 0); + ret = (g_date_time_compare (gdatetime, gdt_max_value) <= 0); - g_free (tm_max_value); + g_date_time_unref (gdt_max_value); } } - g_free (tm_value); + g_date_time_unref (gdatetime); } return ret; diff --git a/libgtkform/form.c b/libgtkform/form.c index 5026aca..a635428 100644 --- a/libgtkform/form.c +++ b/libgtkform/form.c @@ -1568,7 +1568,7 @@ gtk_form_check (GtkForm *form, gboolean with_key, GtkFormWidget **form_widget, if (show_error_dialog) { gtk_form_show_check_error_dialog (fwidget, parent_window, - obl && is_empty ? _(" is obligatory") : _(" isn't valid")); + !is_valid ? _(" isn't valid") : _(" is obligatory")); } if (set_focus) diff --git a/test/test.xml b/test/test.xml index 1325c1a..199d95a 100644 --- a/test/test.xml +++ b/test/test.xml @@ -33,6 +33,7 @@ 12.45 + 2 diff --git a/test/test_db.xml b/test/test_db.xml index f50f034..b59c79c 100644 --- a/test/test_db.xml +++ b/test/test_db.xml @@ -45,6 +45,7 @@ t + 1900-01-01 now date %d/%m/%Y -- 2.49.0