From 1902ae0a3733740a4a571be6fe409aa616a19cdb Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 4 Jul 2011 13:58:16 +0200 Subject: [PATCH] Correzioni e aggiunta della funzione Solipa::gdatetime_to_sql. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit NOTA BENE: c'รจ un problema di time zone nelle funzioni che utilizzato GDateTime. --- src/utils.c | 83 +++++++++++++++++++++++++++++++++++++++++------------ src/utils.h | 2 ++ 2 files changed, 67 insertions(+), 18 deletions(-) diff --git a/src/utils.c b/src/utils.c index 63cb7fa..91c0865 100644 --- a/src/utils.c +++ b/src/utils.c @@ -647,12 +647,12 @@ GDateTime ret = NULL; if (tm_data != NULL) { - ret = g_date_time_new_local (tm_data->tm_year + 1900, - tm_data->tm_mon + 1, - tm_data->tm_mday, - tm_data->tm_hour, - tm_data->tm_min, - tm_data->tm_sec); + ret = g_date_time_new_utc (tm_data->tm_year + 1900, + tm_data->tm_mon + 1, + tm_data->tm_mday, + tm_data->tm_hour, + tm_data->tm_min, + tm_data->tm_sec); } return ret; @@ -666,10 +666,11 @@ struct tm ret = g_new0 (struct tm, 1); 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_week (gdatetime); + 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); + mktime (ret); return ret; } @@ -686,14 +687,23 @@ const gchar { if (format == NULL) { - _format = g_strdup ("%Y-%m-%d %H:%M:%S"); + _format = g_strdup ("%F %R:%S"); } else { _format = g_strstrip (g_strdup (format)); - if (g_strcmp0 (_format, "") == 0) + if (g_strcmp0 (_format, "") == 0 + || g_strcmp0 (_format, "datetime") == 0) { - _format = g_strdup ("%Y-%m-%d %H:%M:%S"); + _format = g_strdup ("%F %R:%S"); + } + else if (g_strcmp0 (_format, "date") == 0) + { + _format = g_strdup ("%F"); + } + else if (g_strcmp0 (_format, "time") == 0) + { + _format = g_strdup ("%R:%S"); } } ret = g_strdup_printf ("'%s'", g_date_time_format (solipa_tm_to_gdatetime (tm_data), _format)); @@ -702,6 +712,43 @@ const gchar return ret; } +const gchar +*solipa_gdatetime_to_sql (GDateTime *gdatetime, const gchar *format) +{ + const gchar *ret; + gchar *_format; + + ret = g_strdup ("NULL"); + + if (gdatetime != NULL) + { + if (format == NULL) + { + _format = g_strdup ("%F %R:%S"); + } + else + { + _format = g_strstrip (g_strdup (format)); + if (g_strcmp0 (_format, "") == 0 + || g_strcmp0 (_format, "datetime") == 0) + { + _format = g_strdup ("%F %R:%S"); + } + else if (g_strcmp0 (_format, "date") == 0) + { + _format = g_strdup ("%F"); + } + else if (g_strcmp0 (_format, "time") == 0) + { + _format = g_strdup ("%R:%S"); + } + } + ret = g_strdup_printf ("'%s'", g_date_time_format (gdatetime, _format)); + } + + return ret; +} + GDateTime *solipa_get_gdatetime_from_string (const gchar *string, const gchar *format) { @@ -787,11 +834,11 @@ GDateTime { if (g_strcmp0 (format_tokens[i], "%Y") == 0) { - year = strtol (str_tokens[i], NULL, 10) - 1900; + year = strtol (str_tokens[i], NULL, 10); } else if (g_strcmp0 (format_tokens[i], "%m") == 0) { - month = strtol (str_tokens[i], NULL, 10) - 1; + month = strtol (str_tokens[i], NULL, 10); } else if (g_strcmp0 (format_tokens[i], "%d") == 0) { @@ -815,12 +862,12 @@ GDateTime g_free (new_format); g_free (new_str); - ret = g_date_time_new_local (year, - month, - day, - hour, - minute, - seconds); + ret = g_date_time_new_utc (year, + month, + day, + hour, + minute, + seconds); return ret; } diff --git a/src/utils.h b/src/utils.h index 0bf506d..4de2b13 100644 --- a/src/utils.h +++ b/src/utils.h @@ -60,7 +60,9 @@ GDate *solipa_get_today_gdate (void); GDateTime *solipa_tm_to_gdatetime (struct tm *tm_data); struct tm *solipa_gdatetime_to_tm (GDateTime *gdatetime); + const gchar *solipa_tm_to_sql (struct tm *tm_data, const gchar *format); +const gchar *solipa_gdatetime_to_sql (GDateTime *gdatetime, const gchar *format); GDateTime *solipa_get_gdatetime_from_string (const gchar *string, const gchar *format); -- 2.49.0