* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include <stdlib.h>
#include <string.h>
#include <time.h>
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);
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;
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);
}
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,
}
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)