1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/time.rb (Time#xmlschema): use strftime specifiers instead of

fractional exponential calcuation which yields undesirable
  result.  [ruby-core:42997][Bug #6100]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34845 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-02-28 08:16:03 +00:00
parent 3ee4c4fdf0
commit 007b7fcdcf
3 changed files with 18 additions and 13 deletions

View file

@ -1,3 +1,9 @@
Tue Feb 28 17:16:01 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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@segment7.net>
* lib/net/protocol.rb: Add OpenTimeout subclass of Timeout::Error

View file

@ -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

View file

@ -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)