Module: ICU::Util::Date

Defined in:
lib/icu_tournament/util.rb

Class Method Summary collapse

Class Method Details

.parse(date) ⇒ Object

Parse dates into yyyy-mm-dd format, preferring European over US convention. Returns nil on error.

Date.parse('1955-11-09')       # => '1955-11-09'
Date.parse('02/03/2009')       # => '2009-03-02'
Date.parse('02/23/2009')       # => '2009-02-23'
Date.parse('16th June 1986')   # => '1986-06-16'

Note that the parse method of the Date class behaves differently in Ruby 1.8 and 1.9. In 1.8 it assumes American dates and will raise ArgumentError on “30/03/2003”. In 1.9 it assumes European dates and will raise ArgumentError on “03/30/2003”.



14
15
16
17
18
19
20
21
22
23
# File 'lib/icu_tournament/util.rb', line 14

def self.parse(date)
  date = date.to_s.strip
  return nil unless date.match(/[1-9]/)
  date = [$3].concat($2.to_i > 12 ? [$1, $2] : [$2, $1]).join('-') if date.match(/^(\d{1,2}).(\d{1,2}).(\d{4})$/)
  begin
    ::Date.parse(date, true).to_s
  rescue
    nil
  end
end