Permalink
Browse files

* 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
  • Loading branch information...
1 parent 3ee4c4f commit 007b7fcdcf709cbffb2ee2c6c01f9edf4daaccac @nobu nobu committed Feb 28, 2012
Showing with 18 additions and 13 deletions.
  1. +6 −0 ChangeLog
  2. +5 −13 lib/time.rb
  3. +7 −0 test/test_time.rb
View
6 ChangeLog
@@ -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
18 lib/time.rb
@@ -620,20 +620,12 @@ def httpdate
# 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
7 test/test_time.rb
@@ -154,29 +154,36 @@ def test_xmlschema
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))
assert_equal("1970-01-01T09:00:00.012345678+09:00", t.xmlschema(9))
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)

0 comments on commit 007b7fc

Please sign in to comment.