From 0dc5d456926257bd68702a4a1c989d132324d460 Mon Sep 17 00:00:00 2001 From: David Schleef Date: Mon, 21 Feb 2011 18:58:09 -0800 Subject: [PATCH] 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 e381b9d..e38ff9d 100644 --- a/glib/tests/gdatetime.c +++ b/glib/tests/gdatetime.c @@ -1023,11 +1023,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 */ @@ -1065,6 +1083,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 (); -- 2.7.4