diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-05-26 01:07:25 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-05-26 01:07:25 +0000 |
commit | 66bde4ca521eddb3210f8d5a0ab20e55a3047e9c (patch) | |
tree | 2f8862cb27ce167fe96506eecc917c492d994347 | |
parent | 26f88892a96cf6297572ca6a644d5fbcbb1f2f10 (diff) | |
download | rails-66bde4ca521eddb3210f8d5a0ab20e55a3047e9c.tar.gz rails-66bde4ca521eddb3210f8d5a0ab20e55a3047e9c.tar.bz2 rails-66bde4ca521eddb3210f8d5a0ab20e55a3047e9c.zip |
DateTime uses Time formats. Closes #8476.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6854 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activesupport/lib/active_support/core_ext/date_time/conversions.rb | 11 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_time_ext_test.rb | 6 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_ext_test.rb | 6 |
3 files changed, 13 insertions, 10 deletions
diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb index f0a0b5892b..0b66593499 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -3,22 +3,13 @@ module ActiveSupport #:nodoc: module DateTime #:nodoc: # Getting datetimes in different convenient string representations and other objects module Conversions - DATE_FORMATS = { - :db => "%Y-%m-%d %H:%M:%S", - :time => "%H:%M", - :short => "%d %b %H:%M", - :long => "%B %d, %Y %H:%M", - :long_ordinal => lambda { |datetime| datetime.strftime("%B #{datetime.day.ordinalize}, %Y %H:%M") }, - :rfc822 => "%a, %d %b %Y %H:%M:%S %z", - } - def self.included(klass) klass.send(:alias_method, :to_datetime_default_s, :to_s) klass.send(:alias_method, :to_s, :to_formatted_s) end def to_formatted_s(format = :default) - if formatter = DATE_FORMATS[format] + if formatter = ::Time::DATE_FORMATS[format] if formatter.respond_to?(:call) formatter.call(self).to_s else diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index a6fb8dd7e7..1b7a091635 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -11,6 +11,12 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_equal "Mon, 21 Feb 2005 14:30:00 +0000", datetime.to_s(:rfc822) assert_equal "February 21st, 2005 14:30", datetime.to_s(:long_ordinal) end + + def test_custom_date_format + Time::DATE_FORMATS[:custom] = '%Y%m%d%H%M%S' + assert_equal '20050221143000', DateTime.new(2005, 2, 21, 14, 30, 0).to_s(:custom) + Time::DATE_FORMATS.delete(:custom) + end def test_to_date assert_equal Date.new(2005, 2, 21), DateTime.new(2005, 2, 21).to_date diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 87ee8c2894..38a6389674 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -281,6 +281,12 @@ class TimeExtCalculationsTest < Test::Unit::TestCase time = Time.utc(2005, 2, 21, 17, 44, 30) assert_equal "Mon, 21 Feb 2005 17:44:30 +0000", time.to_s(:rfc822) 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) + Time::DATE_FORMATS.delete(:custom) + end def test_to_date assert_equal Date.new(2005, 2, 21), Time.local(2005, 2, 21, 17, 44, 30).to_date |