don't allow 0 or > 12 for meridian am hour

This commit is contained in:
Adam Meehan 2010-09-17 14:52:39 +10:00
parent 0ea0201051
commit d6ddae9977
2 changed files with 10 additions and 0 deletions

View File

@ -209,6 +209,8 @@ module ValidatesTimeliness
values[0..2] = dummy_date_for_time_type if type == :time
return values
end
rescue
nil
end
# Delete formats of specified type. Error raised if format not found.
@ -255,6 +257,7 @@ module ValidatesTimeliness
hour = hour.to_i
return hour if meridian.nil?
if meridian.delete('.').downcase == 'am'
raise if hour == 0 || hour > 12
hour == 12 ? 0 : hour
else
hour == 12 ? hour : hour + 12

View File

@ -102,6 +102,13 @@ describe ValidatesTimeliness::Formats do
time_array.should == [2000,1,1,12,13,14,0]
end
it "should return nil if time hour is out of range for AM meridian" do
time_array = formats.parse('13:14 am', :time, :strict => true)
time_array.should == nil
time_array = formats.parse('00:14 am', :time, :strict => true)
time_array.should == nil
end
it "should return date array from time string" do
time_array = formats.parse('2000-02-01', :date, :strict => true)
time_array.should == [2000,2,1,0,0,0,0]