Skip to content

Commit dd339bb

Browse files
committed
Make ActiveSupport::TimeWithZone#xmlschema consistent
Both Time#xmlschema and DateTime#xmlschema can accept nil values for the fraction_digits parameter. This commit makes this so for TimeWithZone values as well.
1 parent c096500 commit dd339bb

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

activesupport/lib/active_support/time_with_zone.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ def inspect
132132
end
133133

134134
def xmlschema(fraction_digits = 0)
135-
fraction = if fraction_digits > 0
136-
(".%06i" % time.usec)[0, fraction_digits + 1]
135+
fraction = if fraction_digits.to_i > 0
136+
(".%06i" % time.usec)[0, fraction_digits.to_i + 1]
137137
end
138138

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

activesupport/test/core_ext/time_with_zone_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ def test_xmlschema_with_fractional_seconds_lower_than_hundred_thousand
111111
assert_equal "1999-12-31T19:00:00.001234-05:00", @twz.xmlschema(12)
112112
end
113113

114+
def test_xmlschema_with_nil_fractional_seconds
115+
assert_equal "1999-12-31T19:00:00-05:00", @twz.xmlschema(nil)
116+
end
117+
114118
def test_to_yaml
115119
assert_match(/^--- 2000-01-01 00:00:00(\.0+)?\s*Z\n/, @twz.to_yaml)
116120
end

0 commit comments

Comments
 (0)