Module: EZTime

Included in:
Date, Time
Defined in:
lib/eztime.rb

Defined Under Namespace

Classes: FormattedTime

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ordinal(number, include_cardinal = false) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/eztime.rb', line 5

def self.ordinal(number, include_cardinal=false)
  # The following code was based on work found at:
  # http://www.bigbold.com/snippets/user/jswizard#post2368
  # Returns the cardinal (number) and ordinal (st, nd, rd, th, etc.)
  # Pass include_cardinal as false to only return the ordinal
  cardinal = number.to_i.abs
  if (10...20).include?(cardinal) then
    include_cardinal ? cardinal.to_s << 'th' : 'th'
  else
    ord = %w{th st nd rd th th th th th th}[cardinal % 10]
    include_cardinal ? cardinal.to_s << ord : ord
  end
end

Instance Method Details

#eztime(format_str) ⇒ Object

Formats the date/time according to the formatting string format_str The formatting string consists of any of the methods defined in EZTime (such as meridian, ordinal, zhour, etc.) as well as any other methods available to the object class. The methods are named in the string by preceeded them with a single colon (:). Any characters not preceeded by a colon will be passed through directly.

Example

d = DateTime.civil(2003, 12, 20, 17, 30, 0) 
puts d.eztime(":day :nmonth :year at :hour12::minute::second :lmeridian")

Output: 20 December 2003 at 5:30:00 pm


129
130
131
# File 'lib/eztime.rb', line 129

def eztime(format_str)
  EZTime::FormattedTime.new(self).format(format_str)
end