ruby-changes:22796
From: nobu <ko1@a...>
Date: Tue, 28 Feb 2012 17:16:15 +0900 (JST)
Subject: [ruby-changes:22796] nobu:r34845 (trunk): * lib/time.rb (Time#xmlschema): use strftime specifiers instead of
nobu 2012-02-28 17:16:03 +0900 (Tue, 28 Feb 2012) New Revision: 34845 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=34845 Log: * lib/time.rb (Time#xmlschema): use strftime specifiers instead of fractional exponential calcuation which yields undesirable result. [ruby-core:42997][Bug #6100] Modified files: trunk/ChangeLog trunk/lib/time.rb trunk/test/test_time.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 34844) +++ ChangeLog (revision 34845) @@ -1,3 +1,9 @@ +Tue Feb 28 17:16:01 2012 Nobuyoshi Nakada <nobu@r...> + + * lib/time.rb (Time#xmlschema): use strftime specifiers instead of + fractional exponential calcuation which yields undesirable + result. [ruby-core:42997][Bug #6100] + Tue Feb 28 14:15:29 2012 Eric Hodel <drbrain@s...> * lib/net/protocol.rb: Add OpenTimeout subclass of Timeout::Error Index: lib/time.rb =================================================================== --- lib/time.rb (revision 34844) +++ lib/time.rb (revision 34845) @@ -620,20 +620,12 @@ # You must require 'time' to use this method. # def xmlschema(fraction_digits=0) - sprintf('%0*d-%02d-%02dT%02d:%02d:%02d', - year < 0 ? 5 : 4, year, mon, day, hour, min, sec) + - if fraction_digits <= 0 - '' - else - '.' + sprintf('%0*d', fraction_digits, (subsec * 10**fraction_digits).floor) - end + - if utc? - 'Z' - else - off = utc_offset - sign = off < 0 ? '-' : '+' - sprintf('%s%02d:%02d', sign, *(off.abs / 60).divmod(60)) + fraction_digits = fraction_digits.to_i + s = strftime("%FT%T") + if fraction_digits > 0 + s << strftime(".%#{fraction_digits}N") end + s << (utc? ? 'Z' : strftime("%:z")) end alias iso8601 xmlschema end Index: test/test_time.rb =================================================================== --- test/test_time.rb (revision 34844) +++ test/test_time.rb (revision 34845) @@ -154,22 +154,28 @@ end def test_encode_xmlschema + bug6100 = '[ruby-core:42997]' + t = Time.utc(2001, 4, 17, 19, 23, 17, 300000) assert_equal("2001-04-17T19:23:17Z", t.xmlschema) assert_equal("2001-04-17T19:23:17.3Z", t.xmlschema(1)) assert_equal("2001-04-17T19:23:17.300000Z", t.xmlschema(6)) assert_equal("2001-04-17T19:23:17.3000000Z", t.xmlschema(7)) + assert_equal("2001-04-17T19:23:17.3Z", t.xmlschema(1.9), bug6100) t = Time.utc(2001, 4, 17, 19, 23, 17, 123456) assert_equal("2001-04-17T19:23:17.1234560Z", t.xmlschema(7)) assert_equal("2001-04-17T19:23:17.123456Z", t.xmlschema(6)) assert_equal("2001-04-17T19:23:17.12345Z", t.xmlschema(5)) assert_equal("2001-04-17T19:23:17.1Z", t.xmlschema(1)) + assert_equal("2001-04-17T19:23:17.1Z", t.xmlschema(1.9), bug6100) t = Time.at(2.quo(3)).getlocal("+09:00") assert_equal("1970-01-01T09:00:00.666+09:00", t.xmlschema(3)) assert_equal("1970-01-01T09:00:00.6666666666+09:00", t.xmlschema(10)) assert_equal("1970-01-01T09:00:00.66666666666666666666+09:00", t.xmlschema(20)) + assert_equal("1970-01-01T09:00:00.6+09:00", t.xmlschema(1.1), bug6100) + assert_equal("1970-01-01T09:00:00.666+09:00", t.xmlschema(3.2), bug6100) t = Time.at(123456789.quo(9999999999)).getlocal("+09:00") assert_equal("1970-01-01T09:00:00.012+09:00", t.xmlschema(3)) @@ -177,6 +183,7 @@ assert_equal("1970-01-01T09:00:00.0123456789+09:00", t.xmlschema(10)) assert_equal("1970-01-01T09:00:00.0123456789012345678+09:00", t.xmlschema(19)) assert_equal("1970-01-01T09:00:00.01234567890123456789+09:00", t.xmlschema(20)) + assert_equal("1970-01-01T09:00:00.012+09:00", t.xmlschema(3.8), bug6100) t = Time.utc(1) assert_equal("0001-01-01T00:00:00Z", t.xmlschema) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/