ruby-changes:23785
From: nobu <ko1@a...>
Date: Tue, 29 May 2012 17:28:31 +0900 (JST)
Subject: [ruby-changes:23785] nobu:r35836 (trunk): strftime.c: triple colons modifier
nobu 2012-05-29 17:28:13 +0900 (Tue, 29 May 2012) New Revision: 35836 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=35836 Log: strftime.c: triple colons modifier partially borrowed from ext/date. * strftime.c (rb_strftime_with_timespec): support GNU extension triple colons modifier. [EXPERIMENTAL] Modified files: trunk/ChangeLog trunk/strftime.c trunk/test/ruby/test_time.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 35835) +++ ChangeLog (revision 35836) @@ -1,5 +1,8 @@ -Tue May 29 17:28:15 2012 Nobuyoshi Nakada <nobu@r...> +Tue May 29 17:28:20 2012 Nobuyoshi Nakada <nobu@r...> + * strftime.c (rb_strftime_with_timespec): support GNU extension triple + colons modifier. [EXPERIMENTAL] + * strftime.c (rb_strftime_with_timespec): check conversion with locale modifier. Index: strftime.c =================================================================== --- strftime.c (revision 35835) +++ strftime.c (revision 35836) @@ -460,6 +460,18 @@ #ifdef MAILHEADER_EXT case 'z': /* time zone offset east of GMT e.g. -0600 */ + if (gmt) { + off = 0; + } + else { + off = NUM2LONG(rb_funcall(vtm->utc_offset, rb_intern("round"), 0)); + } + if (off < 0) { + off = -off; + sign = -1; + } else { + sign = +1; + } switch (colons) { case 0: /* %z -> +hhmm */ precision = precision <= 5 ? 2 : precision-3; @@ -476,22 +488,25 @@ NEEDS(precision + 7); break; + case 3: /* %:::z -> +hh[:mm[:ss]] */ + if (off % 3600 == 0) { + precision = precision <= 3 ? 2 : precision-1; + NEEDS(precision + 3); + } + else if (off % 60 == 0) { + precision = precision <= 6 ? 2 : precision-4; + NEEDS(precision + 4); + } + else { + precision = precision <= 9 ? 2 : precision-7; + NEEDS(precision + 9); + } + break; + default: format--; goto unknown; } - if (gmt) { - off = 0; - } - else { - off = NUM2LONG(rb_funcall(vtm->utc_offset, rb_intern("round"), 0)); - } - if (off < 0) { - off = -off; - sign = -1; - } else { - sign = +1; - } i = snprintf(s, endp - s, (padding == ' ' ? "%+*ld" : "%+.*ld"), precision + 1, sign * (off / 3600)); if (i < 0) goto err; @@ -500,12 +515,16 @@ } s += i; off = off % 3600; + if (colons == 3 && off == 0) + continue; if (1 <= colons) *s++ = ':'; i = snprintf(s, endp - s, "%02d", (int)(off / 60)); if (i < 0) goto err; s += i; off = off % 60; + if (colons == 3 && off == 0) + continue; if (2 <= colons) { *s++ = ':'; i = snprintf(s, endp - s, "%02d", (int)off); Index: test/ruby/test_time.rb =================================================================== --- test/ruby/test_time.rb (revision 35835) +++ test/ruby/test_time.rb (revision 35836) @@ -695,6 +695,20 @@ assert_raise(Errno::ERANGE, bug4457) {Time.now.strftime('%8192z')} end + def test_strfimte_zoneoffset + t = T2000.getlocal("+09:00:00") + assert_equal("+0900", t.strftime("%z")) + assert_equal("+09:00", t.strftime("%:z")) + assert_equal("+09:00:01", t.strftime("%::z")) + assert_equal("+09", t.strftime("%:::z")) + + t = T2000.getlocal("+09:00:01") + assert_equal("+0900", t.strftime("%z")) + assert_equal("+09:00", t.strftime("%:z")) + assert_equal("+09:00:01", t.strftime("%::z")) + assert_equal("+09:00:01", t.strftime("%:::z")) + end + def test_strftime_padding bug4458 = '[ruby-dev:43287]' t = T2000.getlocal("+09:00") @@ -706,6 +720,7 @@ assert_equal("+000009:00", t.strftime("%10:z"), bug4458) assert_equal(" +9:00:00", t.strftime("%_10::z"), bug4458) assert_equal("+009:00:00", t.strftime("%10::z"), bug4458) + assert_equal("+000000009", t.strftime("%10:::z")) t = T2000.getlocal("-05:00") assert_equal("-0500", t.strftime("%z")) assert_equal("-05:00", t.strftime("%:z")) @@ -715,6 +730,7 @@ assert_equal("-000005:00", t.strftime("%10:z"), bug4458) assert_equal(" -5:00:00", t.strftime("%_10::z"), bug4458) assert_equal("-005:00:00", t.strftime("%10::z"), bug4458) + assert_equal("-000000005", t.strftime("%10:::z")) bug6323 = '[ruby-core:44447]' t = T2000.getlocal("+00:36") @@ -724,6 +740,7 @@ assert_equal("+000000:36", t.strftime("%10:z"), bug6323) assert_equal(" +0:36:00", t.strftime("%_10::z"), bug6323) assert_equal("+000:36:00", t.strftime("%10::z"), bug6323) + assert_equal("+000000:36", t.strftime("%10:::z")) t = T2000.getlocal("-00:55") assert_equal(" -055", t.strftime("%_10z"), bug6323) assert_equal("-000000055", t.strftime("%10z"), bug6323) @@ -731,6 +748,7 @@ assert_equal("-000000:55", t.strftime("%10:z"), bug6323) assert_equal(" -0:55:00", t.strftime("%_10::z"), bug6323) assert_equal("-000:55:00", t.strftime("%10::z"), bug6323) + assert_equal("-000000:55", t.strftime("%10:::z")) end def test_strftime_invalid_modifier -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/