[nautilus/wip/antoniof/nobody-changes-timezone-that-fast] general: Rate limit local time zone reads




commit 455faecb9172803056583bb9ad8e9d6d7c9897e7
Author: António Fernandes <antoniof gnome org>
Date:   Fri Sep 18 00:21:06 2020 +0100

    general: Rate limit local time zone reads

 src/nautilus-batch-rename-utilities.c | 16 ++++++++++------
 src/nautilus-file.c                   | 23 +++++++++++++----------
 src/nautilus-search-hit.c             |  4 +++-
 src/nautilus-ui-utilities.c           | 30 ++++++++++++++++++++++++++++++
 src/nautilus-ui-utilities.h           |  2 ++
 5 files changed, 58 insertions(+), 17 deletions(-)
---
diff --git a/src/nautilus-batch-rename-utilities.c b/src/nautilus-batch-rename-utilities.c
index 0412a172e..4019e363f 100644
--- a/src/nautilus-batch-rename-utilities.c
+++ b/src/nautilus-batch-rename-utilities.c
@@ -20,6 +20,7 @@
 #include "nautilus-batch-rename-utilities.h"
 #include "nautilus-file.h"
 #include "nautilus-tracker-utilities.h"
+#include "nautilus-ui-utilities.h"
 
 #include <glib.h>
 #include <gtk/gtk.h>
@@ -971,17 +972,20 @@ on_cursor_callback (GObject      *object,
             {
                 if (metadata_type == CREATION_DATE)
                 {
+                    g_autoptr (GTimeZone) time_zone = nautilus_get_time_zone ();
+
                     /* Add the sort order to the order hash table */
                     g_hash_table_insert (query_data->date_order_hash_table,
                                          g_strdup (tracker_sparql_cursor_get_string (cursor, 0, NULL)),
                                          GINT_TO_POINTER (g_hash_table_size 
(query_data->date_order_hash_table)));
 
-                    date_time = g_date_time_new_local (atoi (year),
-                                                       atoi (month),
-                                                       atoi (day),
-                                                       atoi (hours),
-                                                       atoi (minutes),
-                                                       atoi (seconds));
+                    date_time = g_date_time_new (time_zone,
+                                                 atoi (year),
+                                                 atoi (month),
+                                                 atoi (day),
+                                                 atoi (hours),
+                                                 atoi (minutes),
+                                                 atoi (seconds));
 
                     file_metadata->metadata[metadata_type] = format_date_time (date_time);
                 }
diff --git a/src/nautilus-file.c b/src/nautilus-file.c
index 6fcaea89b..7cfbfbccd 100644
--- a/src/nautilus-file.c
+++ b/src/nautilus-file.c
@@ -5454,18 +5454,21 @@ nautilus_file_get_date_as_string (NautilusFile       *file,
     file_date_time = g_date_time_new_from_unix_local (file_time_raw);
     if (date_format != NAUTILUS_DATE_FORMAT_FULL)
     {
+        g_autoptr (GTimeZone) time_zone = nautilus_get_time_zone ();
         GDateTime *file_date;
 
-        now = g_date_time_new_now_local ();
-        today_midnight = g_date_time_new_local (g_date_time_get_year (now),
-                                                g_date_time_get_month (now),
-                                                g_date_time_get_day_of_month (now),
-                                                0, 0, 0);
-
-        file_date = g_date_time_new_local (g_date_time_get_year (file_date_time),
-                                           g_date_time_get_month (file_date_time),
-                                           g_date_time_get_day_of_month (file_date_time),
-                                           0, 0, 0);
+        now = g_date_time_new_now (time_zone);
+        today_midnight = g_date_time_new (time_zone,
+                                          g_date_time_get_year (now),
+                                          g_date_time_get_month (now),
+                                          g_date_time_get_day_of_month (now),
+                                          0, 0, 0);
+
+        file_date = g_date_time_new (time_zone,
+                                     g_date_time_get_year (file_date_time),
+                                     g_date_time_get_month (file_date_time),
+                                     g_date_time_get_day_of_month (file_date_time),
+                                     0, 0, 0);
 
         days_ago = g_date_time_difference (today_midnight, file_date) / G_TIME_SPAN_DAY;
 
diff --git a/src/nautilus-search-hit.c b/src/nautilus-search-hit.c
index 4eb2e39d1..c541c2ab6 100644
--- a/src/nautilus-search-hit.c
+++ b/src/nautilus-search-hit.c
@@ -26,6 +26,7 @@
 #include "nautilus-query.h"
 #define DEBUG_FLAG NAUTILUS_DEBUG_SEARCH_HIT
 #include "nautilus-debug.h"
+#include "nautilus-ui-utilities.h"
 
 struct _NautilusSearchHit
 {
@@ -58,6 +59,7 @@ void
 nautilus_search_hit_compute_scores (NautilusSearchHit *hit,
                                     NautilusQuery     *query)
 {
+    g_autoptr (GTimeZone) time_zone = nautilus_get_time_zone ();
     GDateTime *now;
     GFile *query_location;
     GFile *hit_location;
@@ -94,7 +96,7 @@ nautilus_search_hit_compute_scores (NautilusSearchHit *hit,
     }
     g_object_unref (hit_location);
 
-    now = g_date_time_new_now_local ();
+    now = g_date_time_new_now (time_zone);
     if (hit->modification_time != NULL)
     {
         m_diff = g_date_time_difference (now, hit->modification_time);
diff --git a/src/nautilus-ui-utilities.c b/src/nautilus-ui-utilities.c
index 195f765f9..670ebc33a 100644
--- a/src/nautilus-ui-utilities.c
+++ b/src/nautilus-ui-utilities.c
@@ -268,6 +268,36 @@ get_text_for_date_range (GPtrArray *date_range,
     return label;
 }
 
+#define TIME_ZONE_RATE_LIMIT 2000000
+
+GTimeZone *
+nautilus_get_time_zone (void)
+{
+    static GTimeZone *cached_time_zone = NULL;
+    static gint64 last_read_time = 0;
+    gint64 current_time;
+
+    current_time = g_get_monotonic_time ();
+
+    if (g_once_init_enter (&last_read_time))
+    {
+        cached_time_zone = g_time_zone_new_local ();
+        g_once_init_leave (&last_read_time, current_time);
+    }
+
+    if (current_time - last_read_time > TIME_ZONE_RATE_LIMIT)
+    {
+        GTimeZone *old_time_zone = cached_time_zone;
+
+        last_read_time = current_time;
+        cached_time_zone = g_time_zone_new_local ();
+
+        g_time_zone_unref (old_time_zone);
+    }
+
+    return g_time_zone_ref (cached_time_zone);
+}
+
 GtkDialog *
 show_dialog (const gchar    *primary_text,
              const gchar    *secondary_text,
diff --git a/src/nautilus-ui-utilities.h b/src/nautilus-ui-utilities.h
index 1136e1de6..e4021b8b9 100644
--- a/src/nautilus-ui-utilities.h
+++ b/src/nautilus-ui-utilities.h
@@ -37,6 +37,8 @@ gboolean    nautilus_file_date_in_between           (guint64            file_uni
 gchar     * get_text_for_date_range                 (GPtrArray         *date_range,
                                                      gboolean           prefix_with_since);
 
+GTimeZone * nautilus_get_time_zone                  (void);
+
 GtkDialog * show_dialog                             (const gchar       *primary_text,
                                                      const gchar       *secondary_text,
                                                      GtkWindow         *parent,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]