From 0a359f2deb1dd742095f87d4263fde77c94e574a Mon Sep 17 00:00:00 2001 From: Erik Lindblad Date: Mon, 5 Jan 2009 15:38:56 +0100 Subject: [PATCH] Added a SOAP datetime format to Time::DATE_FORMATS --- .../active_support/core_ext/time/conversions.rb | 2 ++ activesupport/test/core_ext/time_ext_test.rb | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/time/conversions.rb b/activesupport/lib/active_support/core_ext/time/conversions.rb index f42be46..e596d66 100644 --- a/activesupport/lib/active_support/core_ext/time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/time/conversions.rb @@ -10,6 +10,7 @@ module ActiveSupport #:nodoc: :short => "%d %b %H:%M", :long => "%B %d, %Y %H:%M", :long_ordinal => lambda { |time| time.strftime("%B #{time.day.ordinalize}, %Y %H:%M") }, + :soap => lambda { |time| time.utc.strftime("%Y-%m-%dT%H:%M:%S.#{time.usec/100}Z") }, :rfc822 => "%a, %d %b %Y %H:%M:%S %z" } @@ -34,6 +35,7 @@ module ActiveSupport #:nodoc: # time.to_formatted_s(:short) # => "18 Jan 06:10" # time.to_formatted_s(:long) # => "January 18, 2007 06:10" # time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10" + # time.to_formatted_s(:soap) # => "2007-02-21T06:10:17.582Z" # time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600" # # == Adding your own time formats to +to_formatted_s+ diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 52d6c18..c0f01c7 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -462,7 +462,12 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_equal "Mon, 21 Feb 2005 17:44:30 +0000", time.to_s(:rfc822) end end - + + def test_to_s_with_microseconds + time_with_microseconds = Time.at(Time.utc(2005, 2, 21, 17, 44, 30).to_i, 58273) + assert_equal "2005-02-21T17:44:30.582Z", time_with_microseconds.to_s(:soap) + end + def test_custom_date_format Time::DATE_FORMATS[:custom] = '%Y%m%d%H%M%S' assert_equal '20050221143000', Time.local(2005, 2, 21, 14, 30, 0).to_s(:custom) -- 1.5.2.5