From 71583805c89cd012f98b5fdd8c1ca91f0a79aa32 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Wed, 21 Jan 2009 14:07:35 +1100 Subject: [PATCH] fixed regex for yy format token which wasn't greedy enough when datetime string parsed as date causing a 4 digit year to be extracted as first 2 digits --- lib/validates_timeliness/formats.rb | 2 +- spec/formats_spec.rb | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/validates_timeliness/formats.rb b/lib/validates_timeliness/formats.rb index ba242b7..a0000f2 100644 --- a/lib/validates_timeliness/formats.rb +++ b/lib/validates_timeliness/formats.rb @@ -115,7 +115,7 @@ module ValidatesTimeliness { 'mm' => [ /m{2}/, '(\d{2})', :month ] }, { 'm' => [ /(\A|[^ap])m{1}/, '(\d{1,2})', :month ] }, { 'yyyy' => [ /y{4,}/, '(\d{4})', :year ] }, - { 'yy' => [ /y{2,}/, '(\d{2}|\d{4})', :year ] }, + { 'yy' => [ /y{2,}/, '(\d{4}|\d{2})', :year ] }, { 'hh' => [ /h{2,}/, '(\d{2})', :hour ] }, { 'h' => [ /h{1}/, '(\d{1,2})', :hour ] }, { 'nn' => [ /n{2,}/, '(\d{2})', :min ] }, diff --git a/spec/formats_spec.rb b/spec/formats_spec.rb index 10a1c97..a166a76 100644 --- a/spec/formats_spec.rb +++ b/spec/formats_spec.rb @@ -157,11 +157,16 @@ describe ValidatesTimeliness::Formats do time_array = formats.parse('2000-02-01', :datetime, false) time_array.should == [2000,2,1,0,0,0,0] end - + it "should ignore time when extracting date and strict is false" do time_array = formats.parse('2000-02-01 12:12', :date, false) time_array.should == [2000,2,1,0,0,0,0] end + + it "should ignore time when extracting date from format with trailing year and strict is false" do + time_array = formats.parse('01-02-2000 12:12', :date, false) + time_array.should == [2000,2,1,0,0,0,0] + end it "should ignore date when extracting time and strict is false" do time_array = formats.parse('2000-02-01 12:12', :time, false)