Skip to content

Instantly share code, notes, and snippets.

@Fosome
Created October 21, 2013 20:41
Show Gist options
  • Save Fosome/7090617 to your computer and use it in GitHub Desktop.
Save Fosome/7090617 to your computer and use it in GitHub Desktop.
dat stank
class Stank
MONTHS = {
"january" => 1,
"jan" => 1,
"february" => 2,
"feb" => 2,
"march" => 3,
"mar" => 3,
"april" => 4,
"apr" => 4,
"may" => 5,
"june" => 6,
"july" => 7,
"august" => 8,
"aug" => 8,
"september" => 9,
"sept" => 9,
"october" => 10,
"oct" => 10,
"november" => 11,
"nov" => 11,
"december" => 12,
"dec" => 12
}
def self.parse(str)
new(str).date
end
def initialize(str)
@str = str.present? ? str.strip.gsub('.', '') : nil
end
def date
return nil unless @str
year_date || chronic_date || josh_date
end
def year_only?
@str.length == 4 && /\d{4}/.match(@str)
end
def year_date
@year_date ||= begin
Date.new(@str.to_i) if year_only?
end
end
def chronic_date
@chronic_date ||= Chronic.parse(@str)
end
def josh_date
@josh_date ||= begin
Date.new(josh_year, josh_month || 1) if josh_year
end
end
def josh_year
@josh_year ||= begin
match = /\d{4}/.match(@str)
match[0].to_i if match
end
end
def josh_month
@josh_month ||= begin
MONTHS[MONTHS.keys.detect{|month| @str.downcase.include?(month)}]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment