Skip to content

Commit

Permalink
Look for dates anywhere in the string, to mimic normal [Time/Date].pa…
Browse files Browse the repository at this point in the history
…rse behavior, allowing for weekdays and dates after times
  • Loading branch information
sd committed Mar 18, 2013
1 parent db09160 commit b535bba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/american_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Modify parsing methods to handle american date format correctly.
class << Date
# American date format detected by the library.
AMERICAN_DATE_RE = %r_\A\s*(\d{1,2})/(\d{1,2})/(\d{4}|\d{2})_.freeze
AMERICAN_DATE_RE = %r_\b(\d{1,2})/(\d{1,2})/(\d{4}|\d{2})\b_.freeze

# Alias for stdlib Date._parse
alias _parse_without_american_date _parse
Expand Down
28 changes: 28 additions & 0 deletions spec/american_date_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
specify "should ignore preceding whitespace" do
Date.parse(' 01/02/2003').should == Date.new(2003, 1, 2)
end

specify "should ignore preceding weekday" do
Date.parse('Day 01/02/2003').should == Date.new(2003, 1, 2)
end
end

describe "DateTime.parse" do
Expand Down Expand Up @@ -54,9 +58,17 @@
DateTime.parse(' 01/02/2003').should == DateTime.new(2003, 1, 2)
end

specify "should ignore preceding weekday" do
DateTime.parse('Day 01/02/2003').should == Date.new(2003, 1, 2)
end

specify "should work with times" do
DateTime.parse('01/02/2003 10:20:30').should == DateTime.new(2003, 1, 2, 10, 20, 30)
end

specify "should work with times and weekdays" do
DateTime.parse('Day 01/02/2003 10:20:30').should == DateTime.new(2003, 1, 2, 10, 20, 30)
end
end

describe "Time.parse" do
Expand Down Expand Up @@ -84,9 +96,25 @@
Time.parse(' 01/02/2003').should == Time.local(2003, 1, 2)
end

specify "should ignore preceding weekdays" do
Time.parse('Day 01/02/2003').should == Time.local(2003, 1, 2)
end

specify "should work with times" do
Time.parse('01/02/2003 10:20:30').should == Time.local(2003, 1, 2, 10, 20, 30)
end

specify "should work with times and weekdays" do
Time.parse('Day 01/02/2003 10:20:30').should == Time.local(2003, 1, 2, 10, 20, 30)
end

specify "should work with time first and date second" do
Time.parse('10:20:30 01/02/2003').should == Time.local(2003, 1, 2, 10, 20, 30)
end

specify "should work with time first and date second and weekday in the middle" do
Time.parse('10:20:30 Thu 01/02/2003').should == Time.local(2003, 1, 2, 10, 20, 30)
end
end

describe "Date._parse" do
Expand Down

0 comments on commit b535bba

Please sign in to comment.