Agile Web Development

Build it. Launch it. Love it.

interpolated_time_formats

interpolated_time_formats adds support for the interpolation of time/date formats when generating strings with Time#strftime or Date#strftime.

Resources

Wiki

API

Development

Source

Description

Specifying the format of times and dates can sometimes be difficult if you want to include more complex operations like ordinalizing the day. For example, suppose you want to create a string format that outputs the following:

  31st of Decemember
  5th of January
  2nd of February

In order to use this type of format, you would normally need to create a helper method like so:

  def fancy_date(date)
    "#{date.day.ordinalize} of #{date.strftime('%B')}"
  end

However, this helper method would not be available everywhere within your application unless the method was defined in the Object class.

The best way to approach this would be by adding additional formats through Time::DATE_FORMATS in ActiveSupport and the use of interpolation.

Usage

  >> Time::DATE_FORMATS[:fancy] = '#{day.ordinalize} of %B'
  => "\#{day.ordinalize} of %B"
  >> Time.parse('12/31/2006').to_s(:fancy)
  => "31st of December"

Through the use of interpolation, day.ordinalize will only be evaluated when strftime evaluates the format. As a result, in addition to using the Time#strftime directives, you also get access to the local variables and methods in the instance.

Dependencies

This plugin does not depend on the presence of any other plugins.

Vitals

Home http://wiki.pluginaweek.org/Interpolated_time_formats
Repository http://svn.pluginaweek.org/trunk/interpolated_time_formats
License Rails' (MIT)
Tags Tag_red
Rating (0 votes)
Owner Aaron Pfeifer
Created 11 December 2006

Comments

Add a comment