Skip to content
Browse files

Merge sd's changes into the master branch

  • Loading branch information...
2 parents 721cbe7 + 2ffa50c commit a046f55f38461b5de73afc5103368271dbabf739 @jeremyevans committed Mar 22, 2013
Showing with 62 additions and 1 deletion.
  1. +4 −1 lib/american_date.rb
  2. +58 −0 spec/american_date_spec.rb
View
5 lib/american_date.rb
@@ -4,7 +4,10 @@
# 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})_
+ AMERICAN_DATE_RE = eval('%r_(?<!\d)(\d{1,2})/(\d{1,2})/(\d{4}|\d{2})(?!\d)_').freeze
+ # Negative lookbehinds, which are not supported in Ruby 1.8
+ # so by using eval, we prevent an error when this file is first parsed
+ # since the regexp itself will only be parsed at runtime if the RUBY_VERSION condition is met.
# Alias for stdlib Date._parse
alias _parse_without_american_date _parse
View
58 spec/american_date_spec.rb
@@ -27,6 +27,24 @@
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
+
+ specify "should work just like 1.8 does" do
+ Date.parse('10:20:30something01/02/2003else').should == Date.new(2003, 1, 2)
+ end
+
+ specify "should not mismatch years" do
+ Date.parse('2003/01/02').should == Date.new(2003, 1, 2)
+ end
+
+ specify "should behave like 1.8 and only allow / as delimiters in american-style dates" do
+ Date.parse("10/11/2012").should == Date.new(2012, 10, 11)
+ Date.parse("10-11-2012").should == Date.new(2012, 11, 10)
+ Date.parse("10.11.2012").should == Date.new(2012, 11, 10)
+ end
+
if RUBY_VERSION > '1.9'
specify "should raise TypeError for invalid values" do
[nil, 1, 1.0, [], {}].each do |x|
@@ -74,10 +92,26 @@ def o.to_str() 1 end
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
+
+ specify "should work just like 1.8 does" do
+ DateTime.parse('10:20:30something01/02/2003else').should == DateTime.new(2003, 1, 2, 10, 20, 30)
+ end
+
+ specify "should not mismatch years" do
+ DateTime.parse('2003/01/02').should == Date.new(2003, 1, 2)
+ end
+
if RUBY_VERSION > '1.9'
specify "should raise TypeError for invalid values" do
[nil, 1, 1.0, [], {}].each do |x|
@@ -124,10 +158,34 @@ def o.to_str() 1 end
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
+
+ specify "should work just like 1.8 does" do
+ Time.parse('10:20:30something01/02/2003else').should == Time.local(2003, 1, 2, 10, 20, 30)
+ end
+
+ specify "should not mismatch years" do
+ Time.parse('2003/01/02').should == Time.local(2003, 1, 2, 0, 0, 0)
+ end
+
if RUBY_VERSION > '1.9'
specify "should raise TypeError for invalid values" do
[nil, 1, 1.0, [], {}].each do |x|

0 comments on commit a046f55

Please sign in to comment.
Something went wrong with that request. Please try again.