From ffb86ce7ff23cdf83a4d424fb0d330da6dd9daf5 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Tue, 21 Feb 2012 21:46:10 +0100 Subject: [PATCH] Use GDateTime instead of GDate if available GDate depends on broken msvcrt code. --- midori/sokoke.c | 31 --------------------- midori/sokoke.h | 4 --- panels/midori-history.c | 62 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 55 insertions(+), 42 deletions(-) diff --git a/midori/sokoke.c b/midori/sokoke.c index 3db12aaa..8cd7df7d 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -1029,37 +1029,6 @@ sokoke_time_t_to_julian (const time_t* timestamp) return julian; } -/** - * sokoke_days_between: - * @day1: a time_t timestamp value - * @day2: a time_t timestamp value - * - * Calculates the number of days between two timestamps. - * - * Return value: an integer. - **/ -gint -sokoke_days_between (const time_t* day1, - const time_t* day2) -{ - GDate* date1; - GDate* date2; - gint age; - - date1 = g_date_new (); - date2 = g_date_new (); - - g_date_set_time_t (date1, *day1); - g_date_set_time_t (date2, *day2); - - age = g_date_days_between (date1, date2); - - g_date_free (date1); - g_date_free (date2); - - return age; -} - /** * sokoke_set_config_dir: * @new_config_dir: an absolute path, or %NULL diff --git a/midori/sokoke.h b/midori/sokoke.h index ac8902f0..a92c6a77 100644 --- a/midori/sokoke.h +++ b/midori/sokoke.h @@ -121,10 +121,6 @@ sokoke_action_create_popup_menu_item (GtkAction* action); gint64 sokoke_time_t_to_julian (const time_t* timestamp); -gint -sokoke_days_between (const time_t* day1, - const time_t* day2); - const gchar* sokoke_set_config_dir (const gchar* new_config_dir); diff --git a/panels/midori-history.c b/panels/midori-history.c index ce635f6f..d2649507 100644 --- a/panels/midori-history.c +++ b/panels/midori-history.c @@ -116,18 +116,59 @@ midori_history_get_stock_id (MidoriViewable* viewable) return STOCK_HISTORY; } +#if !GLIB_CHECK_VERSION (2, 26, 0) +static gint +sokoke_days_between (const time_t* day1, + const time_t* day2) +{ + GDate* date1; + GDate* date2; + gint age; + + date1 = g_date_new (); + date2 = g_date_new (); + + g_date_set_time_t (date1, *day1); + g_date_set_time_t (date2, *day2); + + age = g_date_days_between (date1, date2); + + g_date_free (date1); + g_date_free (date2); + + return age; +} +#endif + static gchar* midori_history_format_date (KatzeItem *item) { - gint age; - gint64 day; - gchar token[50]; + gint64 day = katze_item_get_added (item); gchar* sdate; + gint age; + #if GLIB_CHECK_VERSION (2, 26, 0) + GDateTime* now = g_date_time_new_now_local (); + GDateTime* then = g_date_time_new_from_unix_local (day); + if (g_date_time_get_day_of_month (then) == g_date_time_get_day_of_month (now)) + sdate = g_strdup (_("Today")); + else if (g_date_time_get_day_of_year (then) == g_date_time_get_day_of_year (now) - 1) + sdate = g_strdup (_("Yesterday")); + else + { + age = g_date_time_get_day_of_year (now) - g_date_time_get_day_of_year (then); + if (age < 7) + sdate = g_strdup_printf (ngettext ("%d day ago", + "%d days ago", (gint)age), (gint)age); + else if (age == 7) + sdate = g_strdup (_("A week ago")); + else + sdate = g_date_time_format (then, "%x"); + } + #else + gchar token[50]; time_t current_time; current_time = time (NULL); - day = katze_item_get_added (item); - age = sokoke_days_between ((time_t*)&day, ¤t_time); /* A negative age is a date in the future, the clock is probably off */ @@ -147,6 +188,7 @@ midori_history_format_date (KatzeItem *item) sdate = g_strdup (_("Today")); else sdate = g_strdup (_("Yesterday")); + #endif return sdate; } @@ -412,9 +454,8 @@ midori_history_add_item_cb (KatzeArray* array, GtkTreeModel* model = gtk_tree_view_get_model (treeview); GtkTreeIter iter; KatzeItem* today; - time_t current_time; + time_t current_time = time (NULL); - current_time = time (NULL); if (gtk_tree_model_iter_children (model, &iter, NULL)) { gint64 day; @@ -423,7 +464,14 @@ midori_history_add_item_cb (KatzeArray* array, gtk_tree_model_get (model, &iter, 0, &today, -1); day = katze_item_get_added (today); + #if GLIB_CHECK_VERSION (2, 26, 0) + has_today = g_date_time_get_day_of_month ( + g_date_time_new_from_unix_local (day)) + == g_date_time_get_day_of_month ( + g_date_time_new_from_unix_local (current_time)); + #else has_today = sokoke_days_between ((time_t*)&day, ¤t_time) == 0; + #endif g_object_unref (today); if (has_today) {