aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/date_time/conversions.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-05-25 21:43:19 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-05-25 21:43:19 +0000
commitad4f1fd630dbeeb66b9cde0979518b4af96201ad (patch)
tree07c7f9265cac455eb587da68a68bea01e825edd2 /activesupport/lib/active_support/core_ext/date_time/conversions.rb
parentc4a31560bd686cbd16b4af20e764a9a0660cd170 (diff)
downloadrails-ad4f1fd630dbeeb66b9cde0979518b4af96201ad.tar.gz
rails-ad4f1fd630dbeeb66b9cde0979518b4af96201ad.tar.bz2
rails-ad4f1fd630dbeeb66b9cde0979518b4af96201ad.zip
Date, Time, and DateTime support formatting blocks in addition to strftime strings. Introduce :long_ordinal format, e.g. 'February 21st, 2005'. Closes #8191.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6844 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/core_ext/date_time/conversions.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/date_time/conversions.rb22
1 files changed, 16 insertions, 6 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 926a168cec..f0a0b5892b 100644
--- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb
@@ -8,26 +8,36 @@ module ActiveSupport #:nodoc:
:time => "%H:%M",
:short => "%d %b %H:%M",
:long => "%B %d, %Y %H:%M",
- :rfc822 => "%a, %d %b %Y %H:%M:%S %z"
+ :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)
- DATE_FORMATS[format] ? strftime(DATE_FORMATS[format]).strip : to_datetime_default_s
+ if formatter = DATE_FORMATS[format]
+ if formatter.respond_to?(:call)
+ formatter.call(self).to_s
+ else
+ strftime(formatter).strip
+ end
+ else
+ to_datetime_default_s
+ end
end
def to_date
::Date.new(year, month, day)
end
-
+
# To be able to keep Times and DateTimes interchangeable on conversions
def to_datetime
self
- end
+ end
end
end
end
-end \ No newline at end of file
+end