Skip to content

Instantly share code, notes, and snippets.

@thesp0nge

thesp0nge/README.md

Created May 19, 2011
Embed
What would you like to do?
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