Class: Drexed::Datetime::Engine

Inherits:
Rails::Engine
  • Object
show all
Defined in:
lib/drexed/datetime.rb

Instance Method Summary collapse

Instance Method Details

#datetime_to(tach) ⇒ Object

Time.datetime_to(“yyyy-mm-dd”) => “2012-03-04”



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/drexed/datetime.rb', line 7

def datetime_to(tach)
  delineators = tach.scan /\W+/
  formatters  = tach.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