From: Andrea Zagli Date: Sat, 1 Aug 2009 16:34:04 +0000 (+0200) Subject: Replaced strptime with a custom function, because strptime is not implemented on... X-Git-Tag: 0.1.0~31 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=6fa9f1b3d2febf69b2cde145fca1b6af6db28e98;p=libgtkform Replaced strptime with a custom function, because strptime is not implemented on windows. --- diff --git a/docs/reference/libgtkform-decl-list.txt b/docs/reference/libgtkform-decl-list.txt index db4ff8b..5247330 100644 --- a/docs/reference/libgtkform-decl-list.txt +++ b/docs/reference/libgtkform-decl-list.txt @@ -111,6 +111,7 @@ gtk_form_field_datetime_get_value_sql gtk_form_field_datetime_clear gtk_form_field_datetime_is_empty gtk_form_field_datetime_set_from_datamodel +gtk_form_field_datetime_get_tm_from_str
diff --git a/docs/reference/libgtkform-decl.txt b/docs/reference/libgtkform-decl.txt index f31c5d9..61fc341 100644 --- a/docs/reference/libgtkform-decl.txt +++ b/docs/reference/libgtkform-decl.txt @@ -483,6 +483,11 @@ GtkFormField *field gboolean GtkFormField *field, GdaDataModel *dm, gint row + +gtk_form_field_datetime_get_tm_from_str +struct tm * +const gchar *str + TYPE_GTK_FORM_WIDGET_LABEL #define TYPE_GTK_FORM_WIDGET_LABEL (gtk_form_widget_label_get_type ()) diff --git a/docs/reference/libgtkform-undocumented.txt b/docs/reference/libgtkform-undocumented.txt index 7271ade..c6d0933 100644 --- a/docs/reference/libgtkform-undocumented.txt +++ b/docs/reference/libgtkform-undocumented.txt @@ -1,7 +1,7 @@ 6% symbol docs coverage. 17 symbols documented. 2 symbols incomplete. -251 not documented. +252 not documented. GTK_FORM @@ -125,6 +125,7 @@ gtk_form_field_boolean_set_from_datamodel gtk_form_field_boolean_str_to_boolean (value) gtk_form_field_clear gtk_form_field_datetime_clear +gtk_form_field_datetime_get_tm_from_str gtk_form_field_datetime_get_type gtk_form_field_datetime_get_value gtk_form_field_datetime_get_value_sql diff --git a/docs/reference/libgtkform-unused.txt b/docs/reference/libgtkform-unused.txt index 3e6a29a..d79e4ad 100644 --- a/docs/reference/libgtkform-unused.txt +++ b/docs/reference/libgtkform-unused.txt @@ -14,6 +14,7 @@ GtkFormWidgetLabelClass GtkFormWidgetSpinClass GtkFormWidgetTextviewClass gtk_form_field_boolean_str_to_boolean +gtk_form_field_datetime_get_tm_from_str gtk_form_get_gtkbuilder gtk_form_get_key gtk_form_get_table diff --git a/src/fielddatetime.c b/src/fielddatetime.c index b167549..e9a1da4 100644 --- a/src/fielddatetime.c +++ b/src/fielddatetime.c @@ -16,6 +16,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include #include #include @@ -139,7 +140,7 @@ const gchar const GValue *gtk_form_field_datetime_get_value (GtkFormField *field) { - struct tm datetime; + struct tm *datetime; GValue *ret = NULL; g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), NULL); @@ -148,11 +149,12 @@ const GValue const gchar *value = gtk_form_field_datetime_get_value_stringify (field); - if (value != NULL && strptime (value, format, &datetime) != '\0') + datetime = gtk_form_field_datetime_get_tm_from_str (value); + if (value != NULL && datetime != NULL) { ret = g_malloc0 (sizeof (GValue)); g_value_init (ret, G_TYPE_POINTER); - g_value_set_pointer (ret, &datetime); + g_value_set_pointer (ret, datetime); } return (const GValue *)ret; @@ -178,15 +180,16 @@ const gchar if (value != NULL) { - struct tm datetime; + struct tm *datetime; - if (strptime (value, format, &datetime) != '\0') + datetime = gtk_form_field_datetime_get_tm_from_str (value); + if (datetime != NULL) { char *buf; buf = g_malloc (20); - if (strftime (buf, 20, format, &datetime) != 0) + if (strftime (buf, 20, format, datetime) != 0) { ret = g_strconcat ("'", buf, "'", NULL); } @@ -269,6 +272,67 @@ gtk_form_field_datetime_set_from_datamodel (GtkFormField *field, GdaDataModel *d return ret; } +/** + * gtk_form_field_datetime_get_tm_from_str: + * @str: the #gchar to be parsed. + * + */ +struct tm +*gtk_form_field_datetime_get_tm_from_str (const gchar *str) +{ + + /* TO DO + * check if it is a valid date/time + */ + + struct tm *tm; + gchar *new_str; + + g_return_val_if_fail (str != NULL, NULL); + + new_str = g_strstrip (g_strdup (str)); + + if (strcmp (new_str, "") == 0) + { + return NULL; + } + + tm = g_malloc0 (sizeof (struct tm)); + tm->tm_mday = 1; + switch (strlen (str)) + { + case 8: + /* only time */ + tm->tm_hour = strtol (g_strndup (new_str, 2), NULL, 10); + tm->tm_min = strtol (g_strndup (new_str + 3, 2), NULL, 10); + tm->tm_sec = strtol (g_strndup (new_str + 6, 2), NULL, 10); + break; + + case 10: + /* only date */ + tm->tm_year = strtol (g_strndup (new_str, 4), NULL, 10) - 1900; + tm->tm_mon = strtol (g_strndup (new_str + 5, 2), NULL, 10) - 1; + tm->tm_mday = strtol (g_strndup (new_str + 8, 2), NULL, 10); + break; + + case 19: + /* date & time */ + tm->tm_year = strtol (g_strndup (new_str, 4), NULL, 10) - 1900; + tm->tm_mon = strtol (g_strndup (new_str + 5, 2), NULL, 10) - 1; + tm->tm_mday = strtol (g_strndup (new_str + 8, 2), NULL, 10); + tm->tm_hour = strtol (g_strndup (new_str + 11, 2), NULL, 10); + tm->tm_min = strtol (g_strndup (new_str + 14, 2), NULL, 10); + tm->tm_sec = strtol (g_strndup (new_str + 17, 2), NULL, 10); + break; + + default: + tm = NULL; + break; + } + + return tm; +} + /* PRIVATE */ static void gtk_form_field_datetime_set_property (GObject *object, diff --git a/src/fielddatetime.h b/src/fielddatetime.h index 3d85e7b..c39d956 100644 --- a/src/fielddatetime.h +++ b/src/fielddatetime.h @@ -69,6 +69,8 @@ gboolean gtk_form_field_datetime_is_empty (GtkFormField *field); gboolean gtk_form_field_datetime_set_from_datamodel (GtkFormField *field, GdaDataModel *dm, gint row); +struct tm *gtk_form_field_datetime_get_tm_from_str (const gchar *str); + G_END_DECLS diff --git a/src/form.c b/src/form.c index 8885636..db5034f 100644 --- a/src/form.c +++ b/src/form.c @@ -318,27 +318,11 @@ GtkForm } else { - datetime = g_malloc (sizeof (struct tm)); - if (strptime (prop, "%F %T", datetime) != NULL) + datetime = gtk_form_field_datetime_get_tm_from_str (prop); + if (datetime != NULL) { g_object_set (G_OBJECT (field), "default", datetime, NULL); } - else - { - datetime = g_malloc0 (sizeof (struct tm)); - if (strptime (prop, "%F", datetime) != NULL) - { - g_object_set (G_OBJECT (field), "default", datetime, NULL); - } - else - { - datetime = g_malloc0 (sizeof (struct tm)); - if (strptime (prop, "%T", datetime) != NULL) - { - g_object_set (G_OBJECT (field), "default", datetime, NULL); - } - } - } } } else if (strcmp (type, "float") == 0) diff --git a/test/test.xml b/test/test.xml index 757b853..40c1dca 100644 --- a/test/test.xml +++ b/test/test.xml @@ -59,7 +59,7 @@ and t a b - 13:20:44 + 2009-10-15 10:20:44