Skip to content

Commit

Permalink
TimeWithZone#xmlschema accepts optional fraction_digits argument [rai…
Browse files Browse the repository at this point in the history
…ls#1725 state:resolved]
  • Loading branch information
Nicholas Dainty authored and gbuesing committed Jan 11, 2009
1 parent b009109 commit 5664f24
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*2.2.3 (next release)*

* TimeWithZone#xmlschema accepts optional fraction_digits argument [#1725 state:resolved] [Nicholas Dainty]

* TimeWithZone#- gives correct result with wrapped DateTime, and with DateTime argument [Geoff Buesing]

* Multibyte: add multibyte-safe Chars#ord rather than falling back to String#ord. #1483 [Jason Cheow]
Expand Down
8 changes: 6 additions & 2 deletions activesupport/lib/active_support/time_with_zone.rb
Expand Up @@ -98,8 +98,12 @@ def inspect
"#{time.strftime('%a, %d %b %Y %H:%M:%S')} #{zone} #{formatted_offset}"
end

def xmlschema
"#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{formatted_offset(true, 'Z')}"
def xmlschema(fraction_digits = 0)
fraction = if fraction_digits > 0
".%i" % time.usec.to_s[0, fraction_digits]
end

"#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{fraction}#{formatted_offset(true, 'Z')}"
end
alias_method :iso8601, :xmlschema

Expand Down
9 changes: 9 additions & 0 deletions activesupport/test/core_ext/time_with_zone_test.rb
Expand Up @@ -105,6 +105,15 @@ def test_xmlschema
end
end

def test_xmlschema_with_fractional_seconds
silence_warnings do # silence warnings raised by tzinfo gem
@twz += 0.123456 # advance the time by a fraction of a second
assert_equal "1999-12-31T19:00:00.123-05:00", @twz.xmlschema(3)
assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(6)
assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(12)
end
end

def test_to_yaml
silence_warnings do # silence warnings raised by tzinfo gem
assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml
Expand Down

0 comments on commit 5664f24

Please sign in to comment.