Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the month detection for lowercase and long abbreviated month names. Fix ddd and mmm formats to make them fit more locales. #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/timeliness/definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 ],
Expand Down
6 changes: 3 additions & 3 deletions lib/timeliness/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions spec/timeliness/format_set_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding: UTF-8
require 'spec_helper'

describe Timeliness::FormatSet do
Expand Down Expand Up @@ -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
Expand All @@ -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|
Expand Down
53 changes: 44 additions & 9 deletions spec/timeliness/format_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding: UTF-8
require 'spec_helper'

describe Timeliness::Format do
Expand Down Expand Up @@ -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
Expand Down