]> spindle.queued.net Git - midori/commitdiff
Use GDateTime instead of GDate if available
authorChristian Dywan <christian@twotoasts.de>
Tue, 21 Feb 2012 20:46:10 +0000 (21:46 +0100)
committerChristian Dywan <christian@twotoasts.de>
Tue, 21 Feb 2012 20:47:07 +0000 (21:47 +0100)
GDate depends on broken msvcrt code.

midori/sokoke.c
midori/sokoke.h
panels/midori-history.c

index 3db12aaaa8284613d53fba246ad4f960c66163c6..8cd7df7dc5315a860e88972cae5e5f054b4e519c 100644 (file)
@@ -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
index ac8902f0cee1495cb6be387e1e16e4094abb6a65..a92c6a775c83ec561fe9c929cb9fa38f77e11d0e 100644 (file)
@@ -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);
 
index ce635f6fd2df1b45966ffba086a3ed62a2db4e7a..d2649507373d2e00bc51e6ad69131592c3a9f30c 100644 (file)
@@ -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)
 {
+    gint64 day = katze_item_get_added (item);
+    gchar* sdate;
     gint age;
-    gint64 day;
+    #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];
-    gchar* sdate;
     time_t current_time;
 
     current_time = time (NULL);
-    day = katze_item_get_added (item);
-
     age = sokoke_days_between ((time_t*)&day, &current_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, &current_time) == 0;
+        #endif
         g_object_unref (today);
         if (has_today)
         {