[glib/glib-2-28] Fix %z in g_date_time_format()



commit ca5d9d7a22e8e5ad706d1a16ea3513ef6c43a8b7
Author: David Schleef <ds schleef org>
Date:   Mon Feb 21 18:58:09 2011 -0800

    Fix %z in g_date_time_format()
    
    https://bugzilla.gnome.org/show_bug.cgi?id=642935

 glib/gdatetime.c       |    5 ++---
 glib/tests/gdatetime.c |   19 +++++++++++++++++++
 2 files changed, 21 insertions(+), 3 deletions(-)
---
diff --git a/glib/gdatetime.c b/glib/gdatetime.c
index f76c442..a8a2932 100644
--- a/glib/gdatetime.c
+++ b/glib/gdatetime.c
@@ -2385,10 +2385,9 @@ g_date_time_format (GDateTime *datetime,
                       gint64 offset = g_date_time_get_utc_offset (datetime)
                                     / USEC_PER_SECOND;
 
-                      g_string_append_printf (outstr, "%c%02d%02d",
-                                              offset >= 0 ? '+' : '-',
+                      g_string_append_printf (outstr, "%+03d%02d",
                                               (int) offset / 3600,
-                                              (int) offset / 60 % 60);
+                                              (int) abs(offset) / 60 % 60);
                     }
                   else
                     g_string_append (outstr, "+0000");
diff --git a/glib/tests/gdatetime.c b/glib/tests/gdatetime.c
index 619bb02..25913ba 100644
--- a/glib/tests/gdatetime.c
+++ b/glib/tests/gdatetime.c
@@ -998,11 +998,29 @@ test_all_dates (void)
   g_time_zone_unref (timezone);
 }
 
+static void
+test_z (void)
+{
+  GTimeZone *tz;
+  GDateTime *dt;
+
+  g_test_bug ("642935");
+
+  tz = g_time_zone_new ("-08:00");
+  dt = g_date_time_new (tz, 0, 0, 0, 0, 0, 0);
+  gchar *p = g_date_time_format (dt, "%z");
+  g_assert_cmpstr (p, ==, "-0800");
+  g_date_time_unref (dt);
+  g_free (p);
+}
+
+
 gint
 main (gint   argc,
       gchar *argv[])
 {
   g_test_init (&argc, &argv, NULL);
+  g_test_bug_base ("http://bugzilla.gnome.org/";);
 
   /* GDateTime Tests */
 
@@ -1039,6 +1057,7 @@ main (gint   argc,
   g_test_add_func ("/GDateTime/to_utc", test_GDateTime_to_utc);
   g_test_add_func ("/GDateTime/now_utc", test_GDateTime_now_utc);
   g_test_add_func ("/GDateTime/dst", test_GDateTime_dst);
+  g_test_add_func ("/GDateTime/test_z", test_z);
   g_test_add_func ("/GDateTime/test-all-dates", test_all_dates);
 
   return g_test_run ();



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