Skip to content

Instantly share code, notes, and snippets.

@thesp0nge
Created May 19, 2011 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thesp0nge/980402 to your computer and use it in GitHub Desktop.
Save thesp0nge/980402 to your computer and use it in GitHub Desktop.
date_normalizer.rb

DateNormalizer

DateNormalizer is a very simple class coded in almost 5 minutes to solve the problem to convert a string like '12-ott-2321' in a ruby Time object.

Please note that no sanity checks are perfomed over the string and the month names allowed are in Italian only and in the short 3 letters format.

License

Consider this code as new BSD licensed.

Link

I blogged about this gist in my blog (http://thesp0nge.com/2011/05/19/convertitore_da_data_in_stringa_in_oggetto_time.html). Sorry, Italian only.

Thanks

Thanks to @weppos - (http://www.simonecarletti.com) for the code improvement and review.

class DateNormalizer
# Thanks to @weppos for the suggestions and the review
MONTHS = {
'GEN' => 1,
'FEB' => 2,
'MAR' => 3,
'APR' => 4,
'MAG' => 5,
'GIU' => 6,
'LUG' => 7,
'AGO' => 8,
'SET' => 9,
'OTT' => 10,
'NOV' => 11,
'DIC' => 12,
}
def initialize(string)
s = string.split('-')
@time = Time.mktime(s[2], month_convert(s[1]), s[0])
end
def get_time
@time.to_s
end
private
def month_convert(m)
MONTHS[m.upcase] || m
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment