-
-
Save simonprev/974566 to your computer and use it in GitHub Desktop.
Handling French date format in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Try 1 | |
# How could I get the following working nicely | |
Date.strptime("Ven 13 Mai. 2011, 16h00", "%a %d %b %Y, %Hh%M") | |
# I've tried to update Date::ABBR_DAYNAMES as follow | |
I18n.locale = :fr | |
Date::ABBR_DAYNAMES = I18n.t('date.abbr_day_names') | |
Date::ABBR_MONTHNAMES = I18n.t('date.abbr_month_names') | |
# So far no luck, it look like Date::ABBR_DAYNAMES, ... are only used for output, not parsing. | |
# Any trick ? | |
## Try 2 | |
# Patching Date::Format look like an option. Replacing Date::Format::MONTHS values, | |
# and so on make it working as expected. However that would work only for French | |
# so remain unthinkable. | |
# I've made up a patch that update this constants depending on I18n.locale, ... | |
# So far all looks good. The constant get updated properly but there's a remaining | |
# issue that doesn't make any sense to me. | |
# Here's a quick debug code to showcase the issue, debugging made inside lib/date/format.rb in self._strptime_i | |
# Pacthed code for debug | |
puts "str = \"#{str}\"\nstr.sub!(/\\A(#{Format::ABBR_MONTHS.keys.join('|')})/io, '')" | |
puts str.sub!(/\A(#{Format::ABBR_MONTHS.keys.join('|')})/io, '') | |
# With default Date::Format | |
MONTHS = { | |
'january' => 1, 'february' => 2, 'march' => 3, 'april' => 4, | |
'may' => 5, 'june' => 6, 'july' => 7, 'august' => 8, | |
'september'=> 9, 'october' =>10, 'november' =>11, 'december' =>12 | |
} | |
DAYS = { | |
'sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday'=> 3, | |
'thursday' => 4, 'friday' => 5, 'saturday' => 6 | |
} | |
ABBR_MONTHS = { | |
'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4, | |
'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8, | |
'sep' => 9, 'oct' =>10, 'nov' =>11, 'dec' =>12 | |
} | |
ABBR_DAYS = { | |
'sun' => 0, 'mon' => 1, 'tue' => 2, 'wed' => 3, | |
'thu' => 4, 'fri' => 5, 'sat' => 6 | |
} | |
str = "Apr 2011, 10:00 am" | |
str.sub!(/\A(oct|jul|jan|dec|jun|apr|feb|may|sep|aug|mar|nov)/io, '') | |
# => 2011, 10:00 am | |
str = "Feb 2011" | |
str.sub!(/\A(oct|jul|jan|dec|jun|apr|feb|may|sep|aug|mar|nov)/io, '') | |
# => 2011 | |
str = "Avr. 2011" | |
str.sub!(/\A(déc.|juil.|sept.|jan.|juin|nov.|oct.|août|fév.|mar.|avr.|mai)/io, '') | |
# => nil | |
str = "Avril 2011" | |
str.sub!(/\A(déc.|juil.|sept.|jan.|juin|nov.|oct.|août|fév.|mar.|avr.|mai)/io, '') | |
# => nil | |
# With French Date::Format values HARDCODED (Works) | |
MONTHS = {"avril"=>4, "septembre"=>9, "juin"=>6, "mars"=>3, "février"=>2, "janvier"=>1, "novembre"=>11, "août"=>8, "octobre"=>10, "juillet"=>7, "décembre"=>12, "mai"=>5} | |
DAYS = {"jeudi"=>3, "samedi"=>5, "dimanche"=>6, "mercredi"=>2, "mardi"=>1, "vendredi"=>4} | |
ABBR_MONTHS = {"déc."=>12, "sept."=>9, "juil."=>7, "nov."=>11, "juin"=>6, "jan."=>1, "oct."=>10, "août"=>8, "fév."=>2, "mar."=>3, "mai"=>5, "avr."=>4} | |
ABBR_DAYS = {"jeu"=>3, "mer"=>2, "dim"=>6, "mar"=>1, "sam"=>5, "ven"=>4} | |
str = "Feb 2011" | |
str.sub!(/\A(déc.|juil.|sept.|jan.|juin|nov.|oct.|août|fév.|mar.|avr.|mai)/io, '') | |
# => nil | |
str = "Avr. 2011" | |
str.sub!(/\A(déc.|juil.|sept.|jan.|juin|nov.|oct.|août|fév.|mar.|avr.|mai)/io, '') | |
# => 2011 | |
str = "Avril 2011" | |
str.sub!(/\A(déc.|juil.|sept.|jan.|juin|nov.|oct.|août|fév.|mar.|avr.|mai)/io, '') | |
# => l 2011 | |
# With French Date::Format values PATCHED (Fails) | |
str = "Apr 2011, 10:00 am" | |
str.sub!(/\A(oct|jul|jan|dec|jun|apr|feb|may|sep|aug|mar|nov)/io, '') | |
# => 2011, 10:00 am | |
str = "Feb 2011" | |
str.sub!(/\A(oct|jul|jan|dec|jun|apr|feb|may|sep|aug|mar|nov)/io, '') | |
# => 2011 | |
str = "Avr. 2011" | |
str.sub!(/\A(déc.|juil.|sept.|jan.|juin|nov.|oct.|août|fév.|mar.|avr.|mai)/io, '') | |
# => nil | |
str = "Avril 2011" | |
str.sub!(/\A(déc.|juil.|sept.|jan.|juin|nov.|oct.|août|fév.|mar.|avr.|mai)/io, '') | |
# => nil | |
### Why is that? What could potentially go wrong ??? | |
### Patch attempt available at https://github.com/ZenCocoon/I18n-date-parser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment