typedef struct _GtkFormFieldDateTimePrivate GtkFormFieldDateTimePrivate;
struct _GtkFormFieldDateTimePrivate
{
- struct tm *default_value;
- struct tm *original_value;
+ GDateTime *default_value;
+ GDateTime *original_value;
gint type;
* gtk_form_field_datetime_get_value:
* @field: a #GtkFormField object.
*
- * Returns: a #GValue of type G_TYPE_POINTER that point to a struct tm.
+ * Returns: a #GValue of type G_TYPE_POINTER that point to a #GDateTime.
*/
GValue
*gtk_form_field_datetime_get_value (GtkFormField *field)
{
- struct tm *datetime;
+ GDateTime *gdatetime;
GValue *ret;
gchar *value;
GtkFormFieldDateTimePrivate *priv;
value = gtk_form_field_datetime_get_value_stringify (field);
- datetime = gtk_form_field_datetime_get_tm_from_str (value, priv->display_format);
- if (value != NULL && datetime != NULL)
+ gdatetime = gtk_form_field_datetime_get_gdatetime_from_str (value, priv->display_format);
+ if (value != NULL && gdatetime != NULL)
{
ret = g_malloc0 (sizeof (GValue));
g_value_init (ret, G_TYPE_POINTER);
- g_value_set_pointer (ret, datetime);
+ g_value_set_pointer (ret, gdatetime);
g_free (value);
}
if (value != NULL && format != NULL)
{
- struct tm *datetime;
+ GDateTime *gdatetime;
- datetime = gtk_form_field_datetime_get_tm_from_str (value, priv->display_format);
- if (datetime != NULL)
+ gdatetime = gtk_form_field_datetime_get_gdatetime_from_str (value, priv->display_format);
+ if (gdatetime != NULL)
{
gchar *dvalue;
- dvalue = gtk_form_field_datetime_get_str_from_tm (datetime, format);
+ dvalue = g_date_time_format (gdatetime, format);
if (dvalue != NULL)
{
ret = g_strconcat ("'", dvalue, "'", NULL);
* gtk_form_field_datetime_get_original_value:
* @field: a #GtkFormField object.
*
- * Returns: a #GValue of type G_TYPE_POINTER that point to a struct tm.
+ * Returns: a #GValue of type G_TYPE_POINTER that point to a #GDateTime.
*/
GValue
*gtk_form_field_datetime_get_original_value (GtkFormField *field)
ret = g_malloc0 (sizeof (GValue));
g_value_init (ret, G_TYPE_POINTER);
- g_value_set_pointer (ret, g_memdup (priv->original_value, sizeof (struct tm)));
+ g_value_set_pointer (ret, g_date_time_ref (priv->original_value));
return ret;
}
{
gchar *value;
- value = gtk_form_field_datetime_get_str_from_tm (priv->default_value, priv->display_format);
+ value = g_date_time_format (priv->default_value, priv->display_format);
if (value != NULL)
{
ret = gtk_form_field_datetime_set_value_stringify (field, value);
g_free (value);
}
+ else
+ {
+ ret = gtk_form_field_datetime_set_value_stringify (field, "");
+ }
}
else
{
ret = gtk_form_field_datetime_set_value_stringify (field, "");
}
- priv->original_value = g_memdup (priv->default_value, sizeof (struct tm));
+ priv->original_value = g_date_time_ref (priv->default_value);
return ret;
}
{
gboolean ret = FALSE;
GtkFormFieldDateTimePrivate *priv;
- struct tm *value;
+ GDateTime *value;
g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), FALSE);
priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (GTK_FORM_FIELD_DATETIME (field));
- value = gtk_form_field_datetime_get_tm_from_str (gtk_form_field_datetime_get_value_stringify (field),
+ value = gtk_form_field_datetime_get_gdatetime_from_str (gtk_form_field_datetime_get_value_stringify (field),
priv->display_format);
if (value != NULL && priv->original_value != NULL)
{
- ret = (value->tm_year != priv->original_value->tm_year
- || value->tm_mon != priv->original_value->tm_mon
- || value->tm_mday != priv->original_value->tm_mday);
+ ret = (g_date_time_get_year (value) != g_date_time_get_year (priv->original_value)
+ || g_date_time_get_month (value) != g_date_time_get_month (priv->original_value)
+ || g_date_time_get_day_of_month (value) != g_date_time_get_day_of_month (priv->original_value));
if (!ret && priv->type == GTK_FORM_FIELD_DATETIME_TYPE_DATETIME)
{
- ret = (value->tm_hour != priv->original_value->tm_hour
- || value->tm_min != priv->original_value->tm_min
- || value->tm_sec != priv->original_value->tm_sec);
+ ret = (g_date_time_get_hour (value) != g_date_time_get_hour (priv->original_value)
+ || g_date_time_get_minute (value) != g_date_time_get_minute (priv->original_value)
+ || g_date_time_get_second (value) != g_date_time_get_second (priv->original_value));
}
}
else if ((value == NULL || priv->original_value == NULL)
ret = TRUE;
}
- g_free (value);
+ g_date_time_unref (value);
return ret;
}
GtkFormFieldDateTimePrivate *priv;
+ GDateTime *gdatetime;
+
struct tm *tm_value;
gchar *min_value;
g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), FALSE);
- ret = TRUE;
-
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)
+ {
+ return FALSE;
+ }
+
+ ret = TRUE;
+
tm_value = gtk_form_field_datetime_get_tm_from_str (gtk_form_field_datetime_get_value_stringify (field), priv->display_format);
if (tm_value != NULL)
void
gtk_form_field_datetime_set_as_origin (GtkFormField *field)
{
- gboolean ret = FALSE;
GtkFormFieldDateTimePrivate *priv;
- struct tm *value;
+ GDateTime *value;
g_return_if_fail (IS_GTK_FORM_FIELD_DATETIME (field));
priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (GTK_FORM_FIELD_DATETIME (field));
- value = gtk_form_field_datetime_get_tm_from_str (gtk_form_field_datetime_get_value_stringify (field),
+ value = gtk_form_field_datetime_get_gdatetime_from_str (gtk_form_field_datetime_get_value_stringify (field),
priv->display_format);
- priv->original_value = g_memdup (value, sizeof (struct tm));
-
- if (priv->original_value != NULL && priv->type == GTK_FORM_FIELD_DATETIME_TYPE_DATE)
- {
- priv->original_value->tm_hour = 0;
- priv->original_value->tm_min = 0;
- priv->original_value->tm_sec = 0;
- }
+ priv->original_value = g_date_time_new_local (priv->type != GTK_FORM_FIELD_DATETIME_TYPE_TIME ? g_date_time_get_year (value) : 1,
+ priv->type != GTK_FORM_FIELD_DATETIME_TYPE_TIME ?g_date_time_get_month (value) : 1,
+ priv->type != GTK_FORM_FIELD_DATETIME_TYPE_TIME ?g_date_time_get_day_of_month (value) : 1,
+ priv->type != GTK_FORM_FIELD_DATETIME_TYPE_DATE ? g_date_time_get_hour (value): 0,
+ priv->type != GTK_FORM_FIELD_DATETIME_TYPE_DATE ? g_date_time_get_minute (value): 0,
+ priv->type != GTK_FORM_FIELD_DATETIME_TYPE_DATE ? g_date_time_get_seconds (value): 0.0);
- g_free (value);
+ g_date_time_unref (value);
}
/**
gboolean ret;
GtkFormFieldDateTimePrivate *priv;
gchar *field_name;
- struct tm *datetime;
+ GDateTime *datetime;
gchar *value;
g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), FALSE);
if (field_name != NULL && g_strcmp0 (g_strstrip (field_name), "") != 0)
{
- datetime = gdaex_data_model_get_field_value_tm_at (dm, row, field_name);
+ datetime = gdaex_data_model_get_field_value_gdatetime_at (dm, row, field_name);
- value = gtk_form_field_datetime_get_str_from_tm (datetime, priv->display_format);
+ value = g_date_time_format (datetime, priv->display_format);
- ret = gtk_form_field_datetime_set_value_stringify (field, value);
+ ret = gtk_form_field_datetime_set_value_stringify (field, value != NULL ? value : "");
- priv->original_value = g_memdup (datetime, sizeof (struct tm));
+ priv->original_value = g_date_time_ref (datetime);
- g_free (datetime);
+ g_date_time_unref (datetime);
g_free (value);
}
else
{
gboolean ret;
GtkFormFieldDateTimePrivate *priv;
- struct tm *datetime;
+ GDateTime *datetime;
gchar *value;
g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), FALSE);
priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (GTK_FORM_FIELD_DATETIME (field));
- datetime = (struct tm *)g_value_get_pointer (gvalue);
- value = gtk_form_field_datetime_get_str_from_tm (datetime, priv->display_format);
- ret = gtk_form_field_datetime_set_value_stringify (field, value);
- priv->original_value = g_memdup (datetime, sizeof (struct tm));
+ datetime = (GDateTime *)g_value_get_pointer (gvalue);
+ value = g_date_time_format (datetime, priv->display_format);
+ ret = gtk_form_field_datetime_set_value_stringify (field, value != NULL ? value : "");
+ priv->original_value = g_date_time_ref (datetime);
- g_free (datetime);
+ g_date_time_unref (datetime);
g_free (value);
return ret;
*gtk_form_field_datetime_get_tm_from_str (const gchar *str, const gchar *format)
{
struct tm *ret;
+
+ GDateTime *gdatetime;
+
+ ret = NULL;
+
+ gdatetime = gtk_form_field_datetime_get_gdatetime_from_str (str, format);
+
+ if (gdatetime != NULL)
+ {
+ ret = g_malloc0 (sizeof (struct tm));
+ ret->tm_year = g_date_time_get_year (gdatetime) - 1900;
+ ret->tm_mon = g_date_time_get_month (gdatetime) - 1;
+ ret->tm_mday = g_date_time_get_day_of_month (gdatetime);
+ ret->tm_hour = g_date_time_get_hour (gdatetime);
+ ret->tm_min = g_date_time_get_minute (gdatetime);
+ ret->tm_sec = g_date_time_get_second (gdatetime);
+ }
+
+ return ret;
+}
+
+/**
+ * gtk_form_field_datetime_get_gdatetime_from_str:
+ * @str: the #gchar to be parsed.
+ * @format:
+ *
+ */
+GDateTime
+*gtk_form_field_datetime_get_gdatetime_from_str (const gchar *str, const gchar *format)
+{
+ GDateTime *ret;
+
gchar *new_str;
gchar *new_format;
gchar **str_tokens;
guint len_formatv;
guint i;
guint i_to;
- GDate *date;
+
+ gint year;
+ gint month;
+ gint day;
+ gint hour;
+ gint minute;
+ gdouble seconds;
+
+ gboolean error;
g_return_val_if_fail (str != NULL, NULL);
return NULL;
}
- new_format = g_strstrip (g_strdup (format));
- if (g_strcmp0 (new_format, "") == 0)
+ if (format == NULL)
{
new_format = g_strdup ("%Y-%m-%d %H:%M:%S");
}
+ else
+ {
+ new_format = g_strstrip (g_strdup (format));
+ if (g_strcmp0 (new_format, "") == 0)
+ {
+ new_format = g_strdup ("%Y-%m-%d %H:%M:%S");
+ }
+ }
/* removes format identifiers to find delimiters */
delimiters = gtk_form_field_datetime_str_replace (new_format, "%Y", "");
g_free (delimiters);
g_free (new_format);
g_free (new_str);
+ g_strfreev (str_tokens);
return NULL;
}
+ year = 1;
+ month = 1;
+ day = 1;
+ hour = 0;
+ minute = 0;
+ seconds = 0.0;
+
+ error = FALSE;
+ ret = NULL;
+
len_strv = g_strv_length (str_tokens);
len_formatv = g_strv_length (format_tokens);
- ret = g_malloc0 (sizeof (struct tm));
- ret->tm_mday = 1;
- ret->tm_isdst = -1;
-
i_to = MIN (len_strv, len_formatv);
for (i = 0; i < i_to; i++)
{
if (g_strcmp0 (format_tokens[i], "%Y") == 0)
{
- ret->tm_year = strtol (str_tokens[i], NULL, 10) - 1900;
+ year = strtol (str_tokens[i], NULL, 10);
+ if (year < 1 || year > 9999)
+ {
+ error = TRUE;
+ break;
+ }
}
else if (g_strcmp0 (format_tokens[i], "%m") == 0)
{
- ret->tm_mon = strtol (str_tokens[i], NULL, 10) - 1;
+ month = strtol (str_tokens[i], NULL, 10);
+ if (month < 1 || month > 12)
+ {
+ error = TRUE;
+ break;
+ }
}
else if (g_strcmp0 (format_tokens[i], "%d") == 0)
{
- ret->tm_mday = strtol (str_tokens[i], NULL, 10);
+ day = strtol (str_tokens[i], NULL, 10);
+ if (day < 1 || day > 31)
+ {
+ error = TRUE;
+ break;
+ }
}
else if (g_strcmp0 (format_tokens[i], "%H") == 0)
{
- ret->tm_hour = strtol (str_tokens[i], NULL, 10);
+ hour = strtol (str_tokens[i], NULL, 10);
+ if (hour > 23)
+ {
+ error = TRUE;
+ break;
+ }
}
else if (g_strcmp0 (format_tokens[i], "%M") == 0)
{
- ret->tm_min = strtol (str_tokens[i], NULL, 10);
+ minute = strtol (str_tokens[i], NULL, 10);
+ if (minute > 59)
+ {
+ error = TRUE;
+ break;
+ }
}
else if (g_strcmp0 (format_tokens[i], "%S") == 0)
{
- ret->tm_sec = strtol (str_tokens[i], NULL, 10);
+ seconds = g_strtod (str_tokens[i], NULL);
+ if (seconds > 59.0)
+ {
+ error = TRUE;
+ break;
+ }
}
}
g_free (delimiters);
g_free (new_format);
g_free (new_str);
+ g_strfreev (str_tokens);
+ g_strfreev (format_tokens);
- /* check if it is valid */
- date = g_date_new_dmy (ret->tm_mday,
- ret->tm_mon + 1,
- ret->tm_year + 1900);
- if (date == NULL || !g_date_valid (date))
- {
- return NULL;
- }
-
- if (ret != NULL)
+ if (!error)
{
- mktime (ret);
+ ret = g_date_time_new_local (year,
+ month,
+ day,
+ hour,
+ minute,
+ seconds);
}
return ret;
return ret;
}
+/**
+ * gtk_form_field_datetime_get_gdatetime_from_tm:
+ * @datetime: a struct tm.
+ *
+ * Returns: a #GDateTime that represent the struct tm with time information.
+ */
+GDateTime
+*gtk_form_field_datetime_get_gdatetime_from_tm (struct tm *datetime)
+{
+ GDateTime *ret;
+
+ ret = NULL;
+
+ if (datetime != NULL)
+ {
+ ret = g_date_time_new_local (datetime->tm_mday,
+ datetime->tm_mon + 1,
+ datetime->tm_year + 1900,
+ datetime->tm_hour,
+ datetime->tm_min,
+ (gdouble)datetime->tm_sec);
+ }
+
+ return ret;
+}
+
/**
* gtk_form_field_datetime_get_now:
*
GParamSpec *pspec)
{
GtkFormFieldDateTime *field = (GtkFormFieldDateTime *)object;
-
GtkFormFieldDateTimePrivate *priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (field);
switch (property_id)
GParamSpec *pspec)
{
GtkFormFieldDateTime *field = (GtkFormFieldDateTime *)object;
-
GtkFormFieldDateTimePrivate *priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (field);
switch (property_id)
gchar *ret;
gchar *p;
+ if (string == NULL) return NULL;
+ if (origin == NULL || replace == NULL) return g_strdup (string);
+
p = g_strstr_len (string, -1, origin);
if (p == NULL)
GtkFormFieldDateTimePrivate *priv;
gchar *str_date;
- struct tm *tm_date;
+ GDateTime *gdatetime;
priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (field);
str_date = gtk_form_field_datetime_get_value_stringify (GTK_FORM_FIELD (field));
- tm_date = gtk_form_field_datetime_get_tm_from_str (str_date, priv->display_format);
+ gdatetime = gtk_form_field_datetime_get_gdatetime_from_str (str_date, priv->display_format);
- ret = (tm_date != NULL);
+ ret = (gdatetime != NULL);
g_free (str_date);
- g_free (tm_date);
+ g_date_time_unref (gdatetime);
return ret;
}