From d39f573377b8bbd66d6ec85e66fb6042a39a4214 Mon Sep 17 00:00:00 2001 From: Exoth Date: Mon, 19 Aug 2013 14:41:44 +0700 Subject: [PATCH 1/2] Fix the month detection for lowercase and long abbreviated month names. --- lib/timeliness/helpers.rb | 6 ++-- spec/timeliness/format_spec.rb | 53 ++++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/lib/timeliness/helpers.rb b/lib/timeliness/helpers.rb index b591af2..c3b4a91 100644 --- a/lib/timeliness/helpers.rb +++ b/lib/timeliness/helpers.rb @@ -23,15 +23,15 @@ def unambiguous_year(year) def month_index(month) return month.to_i if month.to_i > 0 || /0+/ =~ month - month.length > 3 ? month_names.index(month.capitalize) : abbr_month_names.index(month.capitalize) + month_names.index(month.downcase) || abbr_month_names.index(month.downcase) end def month_names - i18n_loaded? ? I18n.t('date.month_names') : Date::MONTHNAMES + (i18n_loaded? ? I18n.t('date.month_names') : Date::MONTHNAMES).map { |month| month.try(&:downcase) } end def abbr_month_names - i18n_loaded? ? I18n.t('date.abbr_month_names') : Date::ABBR_MONTHNAMES + (i18n_loaded? ? I18n.t('date.abbr_month_names') : Date::ABBR_MONTHNAMES).map { |month| month.try(&:downcase) } end def microseconds(usec) diff --git a/spec/timeliness/format_spec.rb b/spec/timeliness/format_spec.rb index 4305c77..0c4e939 100644 --- a/spec/timeliness/format_spec.rb +++ b/spec/timeliness/format_spec.rb @@ -1,3 +1,4 @@ +# encoding: UTF-8 require 'spec_helper' describe Timeliness::Format do @@ -65,18 +66,52 @@ let(:format) { format_for('dd mmm yyyy') } context "with I18n loaded" do - before(:all) do - I18n.locale = :es - I18n.backend.store_translations :es, :date => { :month_names => %w{ ~ Enero Febrero Marzo } } - I18n.backend.store_translations :es, :date => { :abbr_month_names => %w{ ~ Ene Feb Mar } } + context 'with capitalized month names and short abbreviations' do + before(:all) do + I18n.locale = :de + I18n.backend.store_translations :de, :date => { :month_names => %w{ ~ Januar Februar März } } + I18n.backend.store_translations :de, :date => { :abbr_month_names => %w{ ~ Jan Feb Mär } } + end + + it 'should parse abbreviated capitalized month for current locale to correct value' do + format.process('2', 'Mär', '2000').should eq [2000,3,2,nil,nil,nil,nil,nil] + end + + it 'should parse full capitalized month for current locale to correct value' do + format.process('2', 'März', '2000').should eq [2000,3,2,nil,nil,nil,nil,nil] + end + + it 'should parse abbreviated lowercase month for current locale to correct value' do + format.process('2', 'mär', '2000').should eq [2000,3,2,nil,nil,nil,nil,nil] + end + + it 'should parse full lowercase month for current locale to correct value' do + format.process('2', 'märz', '2000').should eq [2000,3,2,nil,nil,nil,nil,nil] + end end - it 'should parse abbreviated month for current locale to correct value' do - format.process('2', 'Ene', '2000').should eq [2000,1,2,nil,nil,nil,nil,nil] - end + context 'with lowercase month names and long abbreviations' do + before(:all) do + I18n.locale = :fr + I18n.backend.store_translations :fr, :date => { :month_names => %w{ ~ janvier février mars } } + I18n.backend.store_translations :fr, :date => { :abbr_month_names => %w{ ~ jan. fév. mar. } } + end + + it 'should parse abbreviated capitalized month for current locale to correct value' do + format.process('2', 'Fév.', '2000').should eq [2000,2,2,nil,nil,nil,nil,nil] + end + + it 'should parse full capitalized month for current locale to correct value' do + format.process('2', 'Février', '2000').should eq [2000,2,2,nil,nil,nil,nil,nil] + end + + it 'should parse abbreviated lowercase month for current locale to correct value' do + format.process('2', 'fév.', '2000').should eq [2000,2,2,nil,nil,nil,nil,nil] + end - it 'should parse full month for current locale to correct value' do - format.process('2', 'Enero', '2000').should eq [2000,1,2,nil,nil,nil,nil,nil] + it 'should parse full lowercase month for current locale to correct value' do + format.process('2', 'février', '2000').should eq [2000,2,2,nil,nil,nil,nil,nil] + end end after(:all) do From 3a5e4ef8e6c4498717b6c656cd6876aed7f3e5d2 Mon Sep 17 00:00:00 2001 From: Exoth Date: Tue, 20 Aug 2013 17:22:07 +0700 Subject: [PATCH 2/2] Fix ddd and mmm formats to make them fit more locales. --- lib/timeliness/definitions.rb | 4 ++-- spec/timeliness/format_set_spec.rb | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/timeliness/definitions.rb b/lib/timeliness/definitions.rb index fd9c34c..bb1ac53 100644 --- a/lib/timeliness/definitions.rb +++ b/lib/timeliness/definitions.rb @@ -85,10 +85,10 @@ module Definitions # regexp and key for format component mapping, if any. # @format_tokens = { - 'ddd' => [ '\w{3,9}' ], + 'ddd' => [ '[[[:alnum:]]\.]{1,14}' ], 'dd' => [ '\d{2}', :day ], 'd' => [ '\d{1,2}', :day ], - 'mmm' => [ '\w{3,9}', :month ], + 'mmm' => [ '[[[:alnum:]]\.]{2,11}', :month ], 'mm' => [ '\d{2}', :month ], 'm' => [ '\d{1,2}', :month ], 'yyyy' => [ '\d{4}', :year ], diff --git a/spec/timeliness/format_set_spec.rb b/spec/timeliness/format_set_spec.rb index 8d8b422..474955a 100644 --- a/spec/timeliness/format_set_spec.rb +++ b/spec/timeliness/format_set_spec.rb @@ -1,3 +1,4 @@ +# encoding: UTF-8 require 'spec_helper' describe Timeliness::FormatSet do @@ -45,8 +46,9 @@ 'd\m\yy' => {:pass => ['1\2\01', '1\02\00', '01\02\2000'], :fail => ['1\2\0', '1/2/01']}, 'd-m-yy' => {:pass => ['1-2-01', '1-02-00', '01-02-2000'], :fail => ['1-2-0', '1/2/01']}, 'd.m.yy' => {:pass => ['1.2.01', '1.02.00', '01.02.2000'], :fail => ['1.2.0', '1/2/01']}, - 'd mmm yy' => {:pass => ['1 Feb 00', '1 Feb 2000', '1 February 00', '01 February 2000'], - :fail => ['1 Fe 00', 'Feb 1 2000', '1 Feb 0']} + 'd mmm yy' => {:pass => ['1 Feb 00', '1 Feb 2000', '1 February 00', '01 February 2000', '1 6月 12', + '1 янв. 12', '1 październik 12'], + :fail => ['Feb 1 2000', '1 Feb 0']} } format_tests.each do |format, values| it "should correctly match dates in format '#{format}'" do @@ -59,7 +61,8 @@ context "for datetime formats" do format_tests = { - 'ddd mmm d hh:nn:ss zo yyyy' => {:pass => ['Sat Jul 19 12:00:00 +1000 2008'], :fail => []}, + 'ddd mmm d hh:nn:ss zo yyyy' => {:pass => ['Sat Jul 19 12:00:00 +1000 2008', 'Ş. İyn 19 12:00:00 +1000 2008', + 'ส ก.ค. 19 12:00:00 +1000 2008', 'ketvirtadienis rugpjūčio 19 12:00:00 +1000 2008'], :fail => []}, 'yyyy-mm-ddThh:nn:ss(?:Z|zo)' => {:pass => ['2008-07-19T12:00:00+10:00', '2008-07-19T12:00:00Z'], :fail => ['2008-07-19T12:00:00Z+10:00']}, } format_tests.each do |format, values|