american_date 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6fc099c8faff7f7a245c01f54bcc32c4770fa8d
4
- data.tar.gz: 0b072c3a750fc6fb6ac0bc81c730ff1fae5e6889
3
+ metadata.gz: 31ef507a1c4d779f57d7d4b282cc9ab599486a51
4
+ data.tar.gz: 81ab0504ac210e53f7ee924733b9563209fbdc61
5
5
  SHA512:
6
- metadata.gz: deff8c7fd14b1b48d4e88fd6f3257931623206746cdd2f8947f329ec441edf136fe94b3fce081cecfb4e2601563669b0bdc5ed33288a06e690ce14fefa8e4803
7
- data.tar.gz: 1a9b4920b7396ba32f4589168943fa1870d256e3ef6fa5c7ed529fc396fc9d05d32f53513b8cff6a4b61f893c2ddd7292276a7b2a90058dbcf95a4f869b29377
6
+ metadata.gz: ac9c93467143930a15273c7b89890e017a0cfeedae78899279912585883c666ff1e4ad6355639f442d2b29c30241b09d6888b3892a8d9f7b9bebdef747eefe87
7
+ data.tar.gz: 2918ae5d191f828fb89500181c2c53e10720f52621cc51156c7f5d32400397b6fe7d75b8c09f412fd7b4a1588133e2499f10dee609b5e0a748233bf0801c6c7a
data/CHANGELOG CHANGED
@@ -1,4 +1,8 @@
1
- === HEAD
1
+ === 1.1.0 (2013-03-23)
2
+
3
+ * Handle MM/DD/YYYY substrings in the middle of strings, not just the beginning (sd, clonezone, jeremyevans) (#5)
4
+
5
+ === 1.0.1 (2013-03-20)
2
6
 
3
7
  * Don't freeze the regular expression used, to allow easier overrides (jeremyevans)
4
8
 
@@ -1,9 +1,9 @@
1
1
  = ruby-american_date
2
2
 
3
- ruby-american_date exists to make ruby versions greater than 1.8
4
- parse american-style month/day/year dates correctly, just like
5
- ruby 1.8. It can also be used on ruby 1.8, but it is basically a
6
- noop there.
3
+ ruby-american_date exists to make ruby 1.9+ parse american-style
4
+ month/day/year dates correctly, with behavior matching ruby 1.8.7.
5
+ It can also be used on earlier ruby version, but it is basically
6
+ a noop there.
7
7
 
8
8
  As far as I know, there isn't a gem that already handles this. You
9
9
  can find many snippets on the web that partially solve the issue, but
@@ -12,19 +12,17 @@ DateTime.parse no longer call Date._parse directly on 1.9.3. Also
12
12
  most don't handle cases where an american date format is used in
13
13
  addition to a time format.
14
14
 
15
+ Note that this gem only handles / separated dates. It does not
16
+ handle - or . separated dates. This is by design.
17
+
15
18
  == Design
16
19
 
17
- The general idea is fairly simple. We just check the beginning of
18
- the input string for an american date format, and transform it into
19
- a year-month-day ISO format before passing it to the standard date
20
- parsing methods. This is probably the least invasive way that works
21
- correctly on both the pure-ruby date parser (<1.9.3) and the C
22
- extension date parser (>=1.9.3).
23
-
24
- To reduce the possibility of problems, only the beginning of the
25
- input string is checked. So if you have an american date format
26
- embedded in the middle of the input string, it won't be translated.
27
- That may change in the future if it is determined to be safe.
20
+ The general idea is fairly simple. We look for a month/day/year
21
+ substring in the input string, and if we find it, we transform it
22
+ into a year-month-day ISO format string before passing it to the
23
+ standard date parsing methods. This is probably the least invasive
24
+ way that works correctly on both the pure-ruby date parser (<1.9.3)
25
+ and the C extension date parser (>=1.9.3).
28
26
 
29
27
  == Tested ruby versions
30
28
 
@@ -4,7 +4,10 @@ if RUBY_VERSION >= '1.9'
4
4
  # Modify parsing methods to handle american date format correctly.
5
5
  class << Date
6
6
  # American date format detected by the library.
7
- AMERICAN_DATE_RE = %r_\A\s*(\d{1,2})/(\d{1,2})/(\d{4}|\d{2})_
7
+ AMERICAN_DATE_RE = eval('%r_(?<!\d)(\d{1,2})/(\d{1,2})/(\d{4}|\d{2})(?!\d)_').freeze
8
+ # Negative lookbehinds, which are not supported in Ruby 1.8
9
+ # so by using eval, we prevent an error when this file is first parsed
10
+ # since the regexp itself will only be parsed at runtime if the RUBY_VERSION condition is met.
8
11
 
9
12
  # Alias for stdlib Date._parse
10
13
  alias _parse_without_american_date _parse
@@ -27,6 +27,24 @@ describe "Date.parse" do
27
27
  Date.parse(' 01/02/2003').should == Date.new(2003, 1, 2)
28
28
  end
29
29
 
30
+ specify "should ignore preceding weekday" do
31
+ Date.parse('Day 01/02/2003').should == Date.new(2003, 1, 2)
32
+ end
33
+
34
+ specify "should work just like 1.8 does" do
35
+ Date.parse('10:20:30something01/02/2003else').should == Date.new(2003, 1, 2)
36
+ end
37
+
38
+ specify "should not mismatch years" do
39
+ Date.parse('2003/01/02').should == Date.new(2003, 1, 2)
40
+ end
41
+
42
+ specify "should behave like 1.8 and only allow / as delimiters in american-style dates" do
43
+ Date.parse("10/11/2012").should == Date.new(2012, 10, 11)
44
+ Date.parse("10-11-2012").should == Date.new(2012, 11, 10)
45
+ Date.parse("10.11.2012").should == Date.new(2012, 11, 10)
46
+ end
47
+
30
48
  if RUBY_VERSION > '1.9'
31
49
  specify "should raise TypeError for invalid values" do
32
50
  [nil, 1, 1.0, [], {}].each do |x|
@@ -74,10 +92,26 @@ describe "DateTime.parse" do
74
92
  DateTime.parse(' 01/02/2003').should == DateTime.new(2003, 1, 2)
75
93
  end
76
94
 
95
+ specify "should ignore preceding weekday" do
96
+ DateTime.parse('Day 01/02/2003').should == Date.new(2003, 1, 2)
97
+ end
98
+
77
99
  specify "should work with times" do
78
100
  DateTime.parse('01/02/2003 10:20:30').should == DateTime.new(2003, 1, 2, 10, 20, 30)
79
101
  end
80
102
 
103
+ specify "should work with times and weekdays" do
104
+ DateTime.parse('Day 01/02/2003 10:20:30').should == DateTime.new(2003, 1, 2, 10, 20, 30)
105
+ end
106
+
107
+ specify "should work just like 1.8 does" do
108
+ DateTime.parse('10:20:30something01/02/2003else').should == DateTime.new(2003, 1, 2, 10, 20, 30)
109
+ end
110
+
111
+ specify "should not mismatch years" do
112
+ DateTime.parse('2003/01/02').should == Date.new(2003, 1, 2)
113
+ end
114
+
81
115
  if RUBY_VERSION > '1.9'
82
116
  specify "should raise TypeError for invalid values" do
83
117
  [nil, 1, 1.0, [], {}].each do |x|
@@ -124,10 +158,34 @@ describe "Time.parse" do
124
158
  Time.parse(' 01/02/2003').should == Time.local(2003, 1, 2)
125
159
  end
126
160
 
161
+ specify "should ignore preceding weekdays" do
162
+ Time.parse('Day 01/02/2003').should == Time.local(2003, 1, 2)
163
+ end
164
+
127
165
  specify "should work with times" do
128
166
  Time.parse('01/02/2003 10:20:30').should == Time.local(2003, 1, 2, 10, 20, 30)
129
167
  end
130
168
 
169
+ specify "should work with times and weekdays" do
170
+ Time.parse('Day 01/02/2003 10:20:30').should == Time.local(2003, 1, 2, 10, 20, 30)
171
+ end
172
+
173
+ specify "should work with time first and date second" do
174
+ Time.parse('10:20:30 01/02/2003').should == Time.local(2003, 1, 2, 10, 20, 30)
175
+ end
176
+
177
+ specify "should work with time first and date second and weekday in the middle" do
178
+ Time.parse('10:20:30 Thu 01/02/2003').should == Time.local(2003, 1, 2, 10, 20, 30)
179
+ end
180
+
181
+ specify "should work just like 1.8 does" do
182
+ Time.parse('10:20:30something01/02/2003else').should == Time.local(2003, 1, 2, 10, 20, 30)
183
+ end
184
+
185
+ specify "should not mismatch years" do
186
+ Time.parse('2003/01/02').should == Time.local(2003, 1, 2, 0, 0, 0)
187
+ end
188
+
131
189
  if RUBY_VERSION > '1.9'
132
190
  specify "should raise TypeError for invalid values" do
133
191
  [nil, 1, 1.0, [], {}].each do |x|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: american_date
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-20 00:00:00.000000000 Z
11
+ date: 2013-03-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: American style month/day/year date parsing for ruby 1.9
14
14
  email: code@jeremyevans.net