[glib: 1/2] gtimezone: Cache timezones based on the identifier they were created by




commit 851241f19a3fd9ec693b3dd8f37a84c7f970984a
Author: Philip Withnall <pwithnall endlessos org>
Date:   Wed Sep 23 11:23:51 2020 +0100

    gtimezone: Cache timezones based on the identifier they were created by
    
    Rather than invalidating the cache by comparing `TZ` to the cached
    timezone identifier, key entirely off the value of `TZ` (and a cached
    copy of it).
    
    This fixes the timezone cache being constantly invalidated if `TZ` is
    `NULL` (which will always differ from the identifier of the default
    local timezone which is constructed from `g_time_zone_new (NULL)`.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Fixes: #2204

 glib/gtimezone.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
---
diff --git a/glib/gtimezone.c b/glib/gtimezone.c
index 8e0621e54..ef67ec50b 100644
--- a/glib/gtimezone.c
+++ b/glib/gtimezone.c
@@ -196,6 +196,7 @@ struct _GTimeZone
 G_LOCK_DEFINE_STATIC (time_zones);
 static GHashTable/*<string?, GTimeZone>*/ *time_zones;
 G_LOCK_DEFINE_STATIC (tz_local);
+static gchar *tzenv_cached = NULL;
 static GTimeZone *tz_local = NULL;
 
 #define MIN_TZYEAR 1916 /* Daylight Savings started in WWI */
@@ -1842,11 +1843,17 @@ g_time_zone_new_local (void)
   G_LOCK (tz_local);
 
   /* Is time zone changed and must be flushed? */
-  if (tz_local && g_strcmp0 (g_time_zone_get_identifier (tz_local), tzenv))
-    g_clear_pointer (&tz_local, g_time_zone_unref);
+  if (tz_local && g_strcmp0 (tzenv, tzenv_cached) != 0)
+    {
+      g_clear_pointer (&tz_local, g_time_zone_unref);
+      g_clear_pointer (&tzenv_cached, g_free);
+    }
 
   if (tz_local == NULL)
-    tz_local = g_time_zone_new (tzenv);
+    {
+      tz_local = g_time_zone_new (tzenv);
+      tzenv_cached = g_strdup (tzenv);
+    }
 
   tz = g_time_zone_ref (tz_local);
 


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