Created
December 17, 2010 18:44
-
-
Save harmon/745470 to your computer and use it in GitHub Desktop.
Rake Task: Rails Date Formats (v2.3.10)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Outputs the default formats for Date, Datetime, and Time Ruby classes. | |
# Stolen from http://onrails.org/2008/08/20/what-are-all-the-rails-date-formats | |
desc "Show the date/time format strings defined and example output" | |
task :date_formats => :environment do | |
now = Time.now | |
[:to_date, :to_datetime, :to_time].each do |conv_meth| | |
obj = now.send(conv_meth) | |
puts obj.class.name | |
puts "=" * obj.class.name.length | |
name_and_fmts = obj.class::DATE_FORMATS.map { |k, v| [k, %Q('#{String === v ? v : '&proc'}')] } | |
max_name_size = name_and_fmts.map { |k, _| k.to_s.length }.max + 2 | |
max_fmt_size = name_and_fmts.map { |_, v| v.length }.max + 1 | |
name_and_fmts.each do |format_name, format_str| | |
puts sprintf("%#{max_name_size}s:%-#{max_fmt_size}s %s", format_name, format_str, obj.to_s(format_name)) | |
end | |
puts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment