Changeset 8609
- Timestamp:
- 01/09/08 09:49:12 (6 months ago)
- Files:
-
- branches/2-0-stable/activesupport/lib/active_support/core_ext/date_time/conversions.rb (modified) (3 diffs)
- branches/2-0-stable/activesupport/lib/active_support/core_ext/date/conversions.rb (modified) (3 diffs)
- branches/2-0-stable/activesupport/lib/active_support/core_ext/time/conversions.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/2-0-stable/activesupport/lib/active_support/core_ext/date_time/conversions.rb
r8607 r8609 2 2 module CoreExtensions #:nodoc: 3 3 module DateTime #:nodoc: 4 # Getting datetimes in different convenient string representations and other objects. 5 # 6 # == Adding your own time formats in to_formatted_s 7 # You can add your own time formats by merging them into the ::Time::Conversions::DATE_FORMATS constant. Use a string with 8 # Ruby's strftime formatting (http://ruby-doc.org/core/classes/Time.html#M000297), or 9 # pass a lambda. The lambda yields the instance to_formatted_s is called on, so that calculations 10 # can be performed on that instance. This is handy when Ruby's strftime formatting is insufficient. See 11 # the +short_ordinal+ example below. 12 # 13 # See ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS for the list of built-in formats, and 14 # to_formatted_s for implementation details. 15 # 16 # === Examples: 17 # # config/initializers/time_formats.rb 18 # ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!( 19 # :month_and_year => "%B %Y", 20 # :short_ordinal => lambda { |time| time.strftime("%B #{time.day.ordinalize}") } 21 # ) 22 # 23 # Calling it on a Time instance: 24 # 25 # Time.now.to_s(:short_ordinal) 4 # Converting datetimes to formatted strings, dates, and times. 26 5 module Conversions 27 6 def self.included(base) #:nodoc: … … 37 16 end 38 17 end 39 40 # Convert to a formatted string - see DATE_FORMATS for predefined formats. 41 # You can also add your own formats to the DATE_FORMATS constant and use them with this method. 18 19 # Convert to a formatted string. See Time::DATE_FORMATS for predefined formats. 42 20 # 43 # This method is al so aliased as<tt>to_s</tt>.21 # This method is aliased to <tt>to_s</tt>. 44 22 # 45 23 # === Examples: … … 53 31 # datetime.to_formatted_s(:long_ordinal) # => "December 4th, 2007 00:00" 54 32 # datetime.to_formatted_s(:rfc822) # => "Tue, 04 Dec 2007 00:00:00 +0000" 33 # 34 # == Adding your own datetime formats to to_formatted_s 35 # DateTime formats are shared with Time. You can add your own to the 36 # Time::DATE_FORMATS hash. Use the format name as the hash key and 37 # either a strftime string or Proc instance that takes a time or 38 # datetime argument as the value. 39 # 40 # # config/initializers/time_formats.rb 41 # Time::DATE_FORMATS[:month_and_year] = "%B %Y" 42 # Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") } 55 43 def to_formatted_s(format = :default) 56 44 if formatter = ::Time::DATE_FORMATS[format] branches/2-0-stable/activesupport/lib/active_support/core_ext/date/conversions.rb
r8607 r8609 2 2 module CoreExtensions #:nodoc: 3 3 module Date #:nodoc: 4 # Getting datetimes in different convenient string representations and other objects. 5 # 6 # == Adding your own time formats in to_formatted_s 7 # You can add your own time formats by merging them into the DATE_FORMATS constant. Use a string with 8 # Ruby's strftime formatting (http://ruby-doc.org/core/classes/Time.html#M000297), or 9 # pass a lambda. The lambda yields the instance to_formatted_s is called on, so that calculations 10 # can be performed on that instance. This is handy when Ruby's strftime formatting is insufficient. See 11 # the +short_ordinal+ example below. 12 # 13 # See DATE_FORMATS for the list of built-in formats, and to_formatted_s for implementation details. 14 # 15 # === Examples: 16 # # config/initializers/time_formats.rb 17 # ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!( 18 # :month_and_year => "%B %Y", 19 # :short_ordinal => lambda { |time| time.strftime("%B #{time.day.ordinalize}") } 20 # ) 21 # 22 # Calling it on a Time instance: 23 # 24 # Time.now.to_s(:short_ordinal) 4 # Converting dates to formatted strings, times, and datetimes. 25 5 module Conversions 26 6 DATE_FORMATS = { … … 47 27 end 48 28 49 # Convert to a formatted string - see DATE_FORMATS for predefined formats. 50 # You can also add your own formats to the DATE_FORMATS constant and use them with this method. 29 # Convert to a formatted string. See DATE_FORMATS for predefined formats. 51 30 # 52 # This method is al so aliased as<tt>to_s</tt>.31 # This method is aliased to <tt>to_s</tt>. 53 32 # 54 33 # ==== Examples: … … 62 41 # date.to_formatted_s(:long_ordinal) # => "November 10th, 2007" 63 42 # date.to_formatted_s(:rfc822) # => "10 Nov 2007" 43 # 44 # == Adding your own time formats to to_formatted_s 45 # You can add your own formats to the Date::DATE_FORMATS hash. 46 # Use the format name as the hash key and either a strftime string 47 # or Proc instance that takes a date argument as the value. 48 # 49 # # config/initializers/time_formats.rb 50 # Date::DATE_FORMATS[:month_and_year] = "%B %Y" 51 # Date::DATE_FORMATS[:short_ordinal] = lambda { |date| date.strftime("%B #{date.day.ordinalize}") } 64 52 def to_formatted_s(format = :default) 65 53 if formatter = DATE_FORMATS[format] branches/2-0-stable/activesupport/lib/active_support/core_ext/time/conversions.rb
r8607 r8609 2 2 module CoreExtensions #:nodoc: 3 3 module Time #:nodoc: 4 # Getting times in different convenient string representations and other objects. 5 # 6 # == Adding your own time formats in to_formatted_s 7 # You can add your own time formats by merging them into the DATE_FORMATS constant. Use a string with 8 # Ruby's strftime formatting (http://ruby-doc.org/core/classes/Time.html#M000297), or 9 # pass a lambda. The lambda yields the instance to_formatted_s is called on, so that calculations 10 # can be performed on that instance. This is handy when Ruby's strftime formatting is insufficient. See 11 # the +short_ordinal+ example below. 12 # 13 # See ::Time::DATE_FORMATS for the list of built-in formats, and to_formatted_s for implementation details. 14 # 15 # === Examples: 16 # # config/initializers/time_formats.rb 17 # ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!( 18 # :month_and_year => "%B %Y", 19 # :short_ordinal => lambda { |time| time.strftime("%B #{time.day.ordinalize}") } 20 # ) 21 # 22 # Calling it on a Time instance: 23 # 24 # Time.now.to_s(:short_ordinal) 4 # Converting times to formatted strings, dates, and datetimes. 25 5 module Conversions 26 6 DATE_FORMATS = { … … 40 20 end 41 21 42 # Convert to a formatted string - see DATE_FORMATS for predefined formats. 43 # You can also add your own formats to the DATE_FORMATS constant and use them with this method. 22 # Convert to a formatted string. See DATE_FORMATS for builtin formats. 44 23 # 45 # This method is al so aliased as<tt>to_s</tt>.24 # This method is aliased to <tt>to_s</tt>. 46 25 # 47 26 # ==== Examples: … … 56 35 # time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10" 57 36 # time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600" 37 # 38 # == Adding your own time formats to to_formatted_s 39 # You can add your own formats to the Time::DATE_FORMATS hash. 40 # Use the format name as the hash key and either a strftime string 41 # or Proc instance that takes a time argument as the value. 42 # 43 # # config/initializers/time_formats.rb 44 # Time::DATE_FORMATS[:month_and_year] = "%B %Y" 45 # Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") } 58 46 def to_formatted_s(format = :default) 59 47 if formatter = DATE_FORMATS[format]