Skip to content
This repository
Browse code

TimeWithZone#xmlschema accepts optional fraction_digits argument [#1725

… state:resolved]
  • Loading branch information...
commit 296ca4da1700eb27a7043112d22027444ea0e548 1 parent f1fff0a
authored January 10, 2009 gbuesing committed January 11, 2009
2  activesupport/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *2.3.0 [Edge]*
2 2
 
  3
+* TimeWithZone#xmlschema accepts optional fraction_digits argument [#1725 state:resolved] [Nicholas Dainty]
  4
+
3 5
 * Object#tap shim for Ruby < 1.8.7. Similar to Object#returning, tap yields self then returns self.  [Jeremy Kemper]
4 6
     array.select { ... }.tap(&:inspect).map { ... }
5 7
 
8  activesupport/lib/active_support/time_with_zone.rb
@@ -99,8 +99,12 @@ def inspect
99 99
       "#{time.strftime('%a, %d %b %Y %H:%M:%S')} #{zone} #{formatted_offset}"
100 100
     end
101 101
 
102  
-    def xmlschema
103  
-      "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{formatted_offset(true, 'Z')}"
  102
+    def xmlschema(fraction_digits = 0)
  103
+      fraction = if fraction_digits > 0
  104
+        ".%i" % time.usec.to_s[0, fraction_digits]
  105
+      end
  106
+
  107
+      "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{fraction}#{formatted_offset(true, 'Z')}"
104 108
     end
105 109
     alias_method :iso8601, :xmlschema
106 110
 
9  activesupport/test/core_ext/time_with_zone_test.rb
@@ -105,6 +105,15 @@ def test_xmlschema
105 105
     end
106 106
   end
107 107
 
  108
+  def test_xmlschema_with_fractional_seconds
  109
+    silence_warnings do # silence warnings raised by tzinfo gem
  110
+      @twz += 0.123456 # advance the time by a fraction of a second
  111
+      assert_equal "1999-12-31T19:00:00.123-05:00", @twz.xmlschema(3)
  112
+      assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(6)
  113
+      assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(12)
  114
+    end
  115
+  end
  116
+
108 117
   def test_to_yaml
109 118
     silence_warnings do # silence warnings raised by tzinfo gem
110 119
       assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml

0 notes on commit 296ca4d

Please sign in to comment.
Something went wrong with that request. Please try again.