diff --git a/ChangeLog b/ChangeLog index 50c932396e..a5c5e8bf8d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Tue Feb 28 17:16:01 2012 Nobuyoshi Nakada + + * 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 * lib/net/protocol.rb: Add OpenTimeout subclass of Timeout::Error diff --git a/lib/time.rb b/lib/time.rb index c8aa3b12b2..977010087e 100644 --- a/lib/time.rb +++ b/lib/time.rb @@ -620,20 +620,12 @@ class Time # 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 diff --git a/test/test_time.rb b/test/test_time.rb index 9dbce3f654..0a4e8a70c3 100644 --- a/test/test_time.rb +++ b/test/test_time.rb @@ -154,22 +154,28 @@ class TestTimeExtension < Test::Unit::TestCase # :nodoc: 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 @@ class TestTimeExtension < Test::Unit::TestCase # :nodoc: 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)