[gnome-desktop] Fix animated background's starttime hour parsing



commit 30bfd9d6dc5805f8be6776640dc5f4e7fc296de9
Author: Robert Marcano <robert marcanoonline com>
Date:   Wed Apr 7 13:48:47 2021 -0400

    Fix animated background's starttime hour parsing
    
    The incorrect time had an offset of one hour.

 libgnome-desktop/gnome-bg-slide-show.c |   5 +-
 tests/bg-slide-show.c                  | 101 +++++++++++++++++++++++++++++++++
 tests/meson.build                      |   4 +-
 tests/starttime.xml                    |  21 +++++++
 4 files changed, 129 insertions(+), 2 deletions(-)
---
diff --git a/libgnome-desktop/gnome-bg-slide-show.c b/libgnome-desktop/gnome-bg-slide-show.c
index e5a60fa5..ee3a5d54 100644
--- a/libgnome-desktop/gnome-bg-slide-show.c
+++ b/libgnome-desktop/gnome-bg-slide-show.c
@@ -384,7 +384,7 @@ handle_text (GMarkupParseContext *context,
                 self->priv->start_tm.tm_mday = parse_int (text);
         }
         else if (stack_is (self, "hour", "starttime", "background", NULL)) {
-                self->priv->start_tm.tm_hour = parse_int (text) - 1;
+                self->priv->start_tm.tm_hour = parse_int (text);
         }
         else if (stack_is (self, "minute", "starttime", "background", NULL)) {
                 self->priv->start_tm.tm_min = parse_int (text);
@@ -680,6 +680,9 @@ parse_file_contents (GnomeBGSlideShow  *self,
         if (!failed) {
                 guint queue_length;
 
+                /* Per the API of mktime, use a negative value on tm_isdst, in order to
+                 * use the system databases to get the active timezone DST status.  */
+                self->priv->start_tm.tm_isdst = -1;
                 t = mktime (&self->priv->start_tm);
 
                 self->priv->start_time = (double)t;
diff --git a/tests/bg-slide-show.c b/tests/bg-slide-show.c
new file mode 100644
index 00000000..12ab690e
--- /dev/null
+++ b/tests/bg-slide-show.c
@@ -0,0 +1,101 @@
+/* -*- mode: C; c-file-style: "linux"; indent-tabs-mode: t -*-
+ *
+ * Copyright (C) 2021 Robert Marcano <robert marcanoonline com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include <glib.h>
+#define GNOME_DESKTOP_USE_UNSTABLE_API
+#include <libgnome-desktop/gnome-bg-slide-show.h>
+
+void
+test_starttime (void)
+{
+       GTimeZone *tz;
+       GDateTime *reference_time, *slide_show_time;
+       GnomeBGSlideShow *slide_show;
+       gboolean loaded;
+
+       tz = g_time_zone_new_local ();
+       /* Load reference time to be compared with the one on the XML file */
+       reference_time = g_date_time_new_from_iso8601 ("2021-04-02T13:14:15", tz);
+       g_assert (reference_time != NULL);
+
+       g_time_zone_unref (tz);
+
+       /* Parsing XML file */
+       slide_show = gnome_bg_slide_show_new ("starttime.xml");
+       loaded = gnome_bg_slide_show_load (slide_show, NULL);
+       g_assert (loaded);
+
+       /* Test if the slide show start time is the same that the reference time */
+       slide_show_time = g_date_time_new_from_unix_local (gnome_bg_slide_show_get_start_time (slide_show));
+       g_assert (g_date_time_compare (reference_time, slide_show_time) == 0);
+
+       g_date_time_unref (reference_time);
+       g_date_time_unref (slide_show_time);
+       g_object_unref (slide_show);
+}
+
+static void
+test_starttime_subprocess (const char *tz_str)
+{
+       if (g_test_subprocess ()) {
+               g_setenv ("TZ", tz_str, TRUE);
+               tzset ();
+               test_starttime ();
+               return;
+       }
+
+       g_test_trap_subprocess (NULL, 0, 0);
+       g_test_trap_assert_passed ();
+}
+
+static void
+test_starttime_no_dst (void)
+{
+       test_starttime_subprocess ("C");
+}
+
+static void
+test_starttime_dst (void)
+{
+       test_starttime_subprocess ("US/Eastern");
+}
+
+int
+main (int   argc,
+      char *argv[])
+{
+       const char *basedir;
+
+       g_test_init (&argc, &argv, NULL);
+
+       basedir = g_getenv ("G_TEST_SRCDIR");
+       if (basedir == NULL)
+               basedir = INSTALLED_TEST_DIR;
+
+       /* We need to ensure the process' current working directory
+        * is the same as the test data in order to load the test
+        * slide show easily.
+        */
+       chdir (basedir);
+
+       g_test_add_func ("/bg-slide-show/starttime-no-dst", test_starttime_no_dst);
+       g_test_add_func ("/bg-slide-show/starttime-dst", test_starttime_dst);
+
+       return g_test_run ();
+}
diff --git a/tests/meson.build b/tests/meson.build
index 8333a810..3b4c0666 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -12,6 +12,7 @@ test_env = [
 ]
 
 test_programs = [
+  'bg-slide-show',
   'wall-clock',
   'wallclock-reftest',
   'languages'
@@ -52,7 +53,8 @@ if get_option('installed_tests')
     'he_IL.utf8.ref.ui',
     'he_IL.utf8.ui',
     'ja_JP.utf8.ref.ui',
-    'ja_JP.utf8.ui'
+    'ja_JP.utf8.ui',
+    'starttime.xml'
   )
   install_data(test_data, install_dir: test_execdir)
   install_data(test_metas, install_dir: test_metadir)
diff --git a/tests/starttime.xml b/tests/starttime.xml
new file mode 100644
index 00000000..7e7d9693
--- /dev/null
+++ b/tests/starttime.xml
@@ -0,0 +1,21 @@
+<background>
+  <starttime>
+    <year>2021</year>
+    <month>4</month>
+    <day>2</day>
+    <hour>13</hour>
+    <minute>14</minute>
+    <second>15</second>
+  </starttime>
+
+<static>
+<duration>60.0</duration>
+<file>start.png</file>
+</static>
+
+<static>
+<duration>60.0</duration>
+<file>end.png</file>
+</static>
+
+</background>


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