Module: TimeFormatter

Defined in:
lib/time_formatter.rb

Instance Method Summary collapse

Instance Method Details

#format(string) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/time_formatter.rb', line 2

def format string
  delineators = string.scan /\W+/
  formatters	= string.scan /[a-z]+/i
  units	= { "yy"   => "y",
    "yyyy" => "Y",
    "m"    => "-m",
    "mm"   => "m",
    "M"    => "b",
    "MM"   => "B",
    "d"    => "-d",
    "dd"   => "d",
    "D"    => "a",
    "DD"   => "A",
    "h"    => "-I",
    "hh"   => "I",
    "H"    => "-H",
    "HH"   => "H",
    "mi"   => "M",
    "p"    => "P",
    "P"    => "p" }
  strftime(formatters.map{ |f| "%#{units[f]}#{delineators.shift || ""}" }.join)
end