[libgweather/ebassi/has-timezone] Add Location.has_timezone()




commit bb6658477dcd23f0bf98a859b36f594c503baa7a
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Fri Dec 31 17:10:01 2021 +0000

    Add Location.has_timezone()
    
    Avoids checking for NULL when calling get_timezone(), and pre-populates
    the cached time zone.

 libgweather/gweather-location.c | 29 +++++++++++++++++++++++++++++
 libgweather/gweather-location.h |  2 ++
 2 files changed, 31 insertions(+)
---
diff --git a/libgweather/gweather-location.c b/libgweather/gweather-location.c
index 5bdd5df0..0ad5adcf 100644
--- a/libgweather/gweather-location.c
+++ b/libgweather/gweather-location.c
@@ -1006,6 +1006,35 @@ gweather_location_get_timezone (GWeatherLocation *loc)
     return NULL;
 }
 
+/**
+ * gweather_location_has_timezone:
+ * @loc: the location
+ *
+ * Checks whether the location has a timezone.
+ *
+ * Returns: true if the location has a timezone; false otherwise
+ */
+gboolean
+gweather_location_has_timezone (GWeatherLocation *loc)
+{
+    g_return_val_if_fail (GWEATHER_IS_LOCATION (loc), FALSE);
+
+    if (loc->_timezone != NULL)
+        return TRUE;
+
+    g_autoptr (GWeatherLocation) s = NULL;
+
+    ITER_UP (loc, s) {
+        if (!IDX_VALID (s->tz_hint_idx))
+            continue;
+
+        loc->_timezone = timezone_ref_for_idx (s->db, s->tz_hint_idx);
+        return TRUE;
+    }
+
+    return FALSE;
+}
+
 /**
  * gweather_location_get_timezone_str:
  * @loc: a #GWeatherLocation
diff --git a/libgweather/gweather-location.h b/libgweather/gweather-location.h
index dedd6c87..4e33a108 100644
--- a/libgweather/gweather-location.h
+++ b/libgweather/gweather-location.h
@@ -141,6 +141,8 @@ GWeatherLocation *      gweather_location_detect_nearest_city_finish    (GAsyncR
 GWEATHER_AVAILABLE_IN_ALL
 const char *            gweather_location_get_country           (GWeatherLocation  *loc);
 GWEATHER_AVAILABLE_IN_ALL
+gboolean                gweather_location_has_timezone          (GWeatherLocation  *loc);
+GWEATHER_AVAILABLE_IN_ALL
 GTimeZone *             gweather_location_get_timezone          (GWeatherLocation  *loc);
 GWEATHER_AVAILABLE_IN_ALL
 const char *            gweather_location_get_timezone_str      (GWeatherLocation  *loc);


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