[libgweather] Introduce gweather_info_is_daytime() for computing sun visibility



commit ca75aadfd11b372653d7ecb336512a79dd79cec7
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Sun Dec 2 20:38:21 2012 +0100

    Introduce gweather_info_is_daytime() for computing sun visibility
    
    Finding out if the sun is visible at a certain point of space and time
    requires care if the point is within the polar circles. GWeather already
    has code to deal with that, so let's just expose it.

 libgweather/gweather-weather.h |    2 +
 libgweather/test_sun_moon.c    |    1 +
 libgweather/weather.c          |   47 +++++++++++++++++++++++++++++----------
 3 files changed, 38 insertions(+), 12 deletions(-)
---
diff --git a/libgweather/gweather-weather.h b/libgweather/gweather-weather.h
index a5f9d1f..b5b473f 100644
--- a/libgweather/gweather-weather.h
+++ b/libgweather/gweather-weather.h
@@ -119,6 +119,8 @@ gchar *			 gweather_info_get_weather_summary (GWeatherInfo *info);
 const gchar *		 gweather_info_get_icon_name	   (GWeatherInfo *info);
 gint			 gweather_info_next_sun_event	   (GWeatherInfo *info);
 
+gboolean                 gweather_info_is_daytime          (GWeatherInfo *info);
+
 /* values retrieving functions */
 
 typedef enum { /*< underscore_name=gweather_wind_direction >*/
diff --git a/libgweather/test_sun_moon.c b/libgweather/test_sun_moon.c
index f384298..f39f62a 100644
--- a/libgweather/test_sun_moon.c
+++ b/libgweather/test_sun_moon.c
@@ -68,6 +68,7 @@ main (int argc, char **argv)
 	    fabs(latitude), (latitude >= 0. ? 'N' : 'S'),
 	    fabs(longitude), (longitude >= 0. ? 'E' : 'W'),
 	    asctime(gmtime(&priv->current_time)));
+    printf("daytime:   %s\n", gweather_info_is_daytime(info) ? "yes" : "no");
     printf("sunrise:   %s",
 	   (priv->sunriseValid ? ctime(&priv->sunrise) : "(invalid)\n"));
     printf("sunset:    %s",
diff --git a/libgweather/weather.c b/libgweather/weather.c
index 98585dd..a9bf4ef 100644
--- a/libgweather/weather.c
+++ b/libgweather/weather.c
@@ -1018,13 +1018,46 @@ gweather_info_get_weather_summary (GWeatherInfo *info)
     return out;
 }
 
+/**
+ * gweather_info_is_daytime:
+ * @info: a #GWeatherInfo
+ *
+ * Returns whether it is daytime (that is, if the sun is visible)
+ * or not at the location and the point of time referred by @info.
+ * This is mostly equivalent to comparing the return value
+ * of gweather_info_get_value_sunrise() and
+ * gweather_info_get_value_sunset(), but it accounts also
+ * for midnight sun and polar night, for locations within
+ * the Artic and Antartic circles.
+ */
+gboolean
+gweather_info_is_daytime (GWeatherInfo *info)
+{
+    GWeatherInfoPrivate *priv;
+    time_t current_time;
+
+    g_return_val_if_fail (GWEATHER_IS_INFO (info), FALSE);
+
+    priv = info->priv;
+
+    _gweather_info_ensure_sun (info);
+
+    if (priv->polarNight)
+	return FALSE;
+    if (priv->midnightSun)
+	return TRUE;
+
+    current_time = priv->current_time;
+    return ( !priv->sunriseValid || (current_time >= priv->sunrise) ) &&
+	( !priv->sunsetValid || (current_time < priv->sunset) );
+}
+
 const gchar *
 gweather_info_get_icon_name (GWeatherInfo *info)
 {
     GWeatherInfoPrivate *priv;
     GWeatherConditions   cond;
     GWeatherSky          sky;
-    time_t               current_time;
     gboolean             daytime;
     gchar*               icon;
     static gchar         icon_buffer[32];
@@ -1086,17 +1119,7 @@ gweather_info_get_icon_name (GWeatherInfo *info)
         }
     }
 
-    if (priv->midnightSun ||
-	(!priv->sunriseValid && !priv->sunsetValid))
-	daytime = TRUE;
-    else if (priv->polarNight)
-	daytime = FALSE;
-    else {
-	current_time = priv->current_time;
-	daytime =
-	    ( !priv->sunriseValid || (current_time >= priv->sunrise) ) &&
-	    ( !priv->sunsetValid || (current_time < priv->sunset) );
-    }
+    daytime = gweather_info_is_daytime (info);
 
     switch (sky) {
     case GWEATHER_SKY_INVALID:



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