[gnome-settings-daemon/gnome-3-24] color: Potentially fix sunrise calcs for fractional timezone offsets



commit 8bfac913d0b824ddfe210645697e8db5d8a955ac
Author: Philip Withnall <withnall endlessm com>
Date:   Mon Mar 27 12:29:10 2017 +0100

    color: Potentially fix sunrise calcs for fractional timezone offsets
    
    If a timezone is offset by a non-integer number of hours, the value of
    tz_offset would be truncated. Fix that by casting to double first.
    
    Add a unit test for it.
    
    Coverity ID: 1418246
    
    https://bugzilla.gnome.org/show_bug.cgi?id=780587

 plugins/color/gcm-self-test.c          |   22 ++++++++++++++++++++++
 plugins/color/gsd-night-light-common.c |    2 +-
 2 files changed, 23 insertions(+), 1 deletions(-)
---
diff --git a/plugins/color/gcm-self-test.c b/plugins/color/gcm-self-test.c
index 8827ca4..83c06c5 100644
--- a/plugins/color/gcm-self-test.c
+++ b/plugins/color/gcm-self-test.c
@@ -232,6 +232,27 @@ gcm_test_sunset_sunrise (void)
 }
 
 static void
+gcm_test_sunset_sunrise_fractional_timezone (void)
+{
+        gdouble sunrise;
+        gdouble sunrise_actual = 7.6 + 1.5;
+        gdouble sunset;
+        gdouble sunset_actual = 16.8 + 1.5;
+        g_autoptr(GTimeZone) tz = NULL;
+        g_autoptr(GDateTime) dt = NULL;
+
+        tz = g_time_zone_new ("+01:30");
+        dt = g_date_time_new (tz, 2007, 2, 1, 0, 0, 0);
+
+        /* get for our made up timezone, today */
+        gsd_night_light_get_sunrise_sunset (dt, 51.5, -0.1278, &sunrise, &sunset);
+        g_assert_cmpfloat (sunrise, <, sunrise_actual + 0.1);
+        g_assert_cmpfloat (sunrise, >, sunrise_actual - 0.1);
+        g_assert_cmpfloat (sunset, <, sunset_actual + 0.1);
+        g_assert_cmpfloat (sunset, >, sunset_actual - 0.1);
+}
+
+static void
 gcm_test_frac_day (void)
 {
         g_autoptr(GDateTime) dt = g_date_time_new_utc (2007, 2, 1, 12, 59, 59);
@@ -267,6 +288,7 @@ main (int argc, char **argv)
 
         g_test_add_func ("/color/edid", gcm_test_edid_func);
         g_test_add_func ("/color/sunset-sunrise", gcm_test_sunset_sunrise);
+        g_test_add_func ("/color/sunset-sunrise/fractional-timezone", 
gcm_test_sunset_sunrise_fractional_timezone);
         g_test_add_func ("/color/fractional-day", gcm_test_frac_day);
         g_test_add_func ("/color/night-light", gcm_test_night_light);
 
diff --git a/plugins/color/gsd-night-light-common.c b/plugins/color/gsd-night-light-common.c
index 9f786e9..fe82bbf 100644
--- a/plugins/color/gsd-night-light-common.c
+++ b/plugins/color/gsd-night-light-common.c
@@ -57,7 +57,7 @@ gsd_night_light_get_sunrise_sunset (GDateTime *dt,
         g_return_val_if_fail (pos_lat <= 90.f && pos_lat >= -90.f, FALSE);
         g_return_val_if_fail (pos_long <= 180.f && pos_long >= -180.f, FALSE);
 
-        gdouble tz_offset =  g_date_time_get_utc_offset (dt) / G_USEC_PER_SEC / 60 / 60; // B5
+        gdouble tz_offset = (gdouble) g_date_time_get_utc_offset (dt) / G_USEC_PER_SEC / 60 / 60; // B5
         gdouble date_as_number = ts / G_USEC_PER_SEC / 24 / 60 / 60 + 2;  // B7
         gdouble time_past_local_midnight = 0;  // E2, unused in this calculation
         gdouble julian_day = date_as_number + 2415018.5 +


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