american_date 1.0.1 → 1.2.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 +5 -5
- data/CHANGELOG +13 -1
- data/MIT-LICENSE +1 -1
- data/README.rdoc +15 -22
- data/Rakefile +13 -19
- data/lib/american_date.rb +48 -12
- data/spec/american_date_keyword_spec.rb +40 -0
- data/spec/american_date_spec.rb +110 -42
- metadata +51 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2fd9a4c53baa027ff94f1802a0f51e78bdc68fc9892d49152072b8eb2205f671
|
4
|
+
data.tar.gz: d56ba6c7c8a88eeb79a2993fac0c3eb69c2318437a3299eaf087e94213720ce1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dfd6235350c26f3ed600bdcc5a6497cb9633666bda0df00c7129d142f145edd8fe6e14a8e9aa8b82398abf3210141e50d09a4c2c12021da69183cd3a691282cf
|
7
|
+
data.tar.gz: dc035e0b03255603061bd2a419250ab05c57187c60b441b64afe0de3a2730f620e594c46f7a7c7a2b67dfdbc3025c2c9583a2c840306a8db95e98793454461a5
|
data/CHANGELOG
CHANGED
@@ -1,4 +1,16 @@
|
|
1
|
-
===
|
1
|
+
=== 1.2.0 (2021-11-17)
|
2
|
+
|
3
|
+
* Support the limit keyword argument supported by date 3.2.1+ (jeremyevans)
|
4
|
+
|
5
|
+
=== 1.1.1 (2015-10-27)
|
6
|
+
|
7
|
+
* Support the third argument to Date/DateTime.parse (costi) (#13)
|
8
|
+
|
9
|
+
=== 1.1.0 (2013-03-23)
|
10
|
+
|
11
|
+
* Handle MM/DD/YYYY substrings in the middle of strings, not just the beginning (sd, clonezone, jeremyevans) (#5)
|
12
|
+
|
13
|
+
=== 1.0.1 (2013-03-20)
|
2
14
|
|
3
15
|
* Don't freeze the regular expression used, to allow easier overrides (jeremyevans)
|
4
16
|
|
data/MIT-LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
= ruby-american_date
|
2
2
|
|
3
|
-
ruby-american_date exists to make ruby
|
4
|
-
|
5
|
-
|
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 versions, 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,28 +12,21 @@ 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
|
-
|
16
|
-
|
17
|
-
|
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.
|
15
|
+
Note that this gem only handles / separated dates. It does not
|
16
|
+
handle - or . separated dates. This is by design, for compatibility
|
17
|
+
with ruby 1.8.7.
|
28
18
|
|
29
|
-
==
|
19
|
+
== Design
|
30
20
|
|
31
|
-
|
32
|
-
|
33
|
-
|
21
|
+
The general idea is fairly simple. We look for a month/day/year
|
22
|
+
substring in the input string, and if we find it, we transform it
|
23
|
+
into a year-month-day ISO format string before passing it to the
|
24
|
+
standard date parsing methods. This is probably the least invasive
|
25
|
+
way that works correctly on both the pure-ruby date parser (<1.9.3)
|
26
|
+
and the C extension date parser (>=1.9.3).
|
34
27
|
|
35
28
|
== Installation
|
36
|
-
|
29
|
+
|
37
30
|
ruby-american_date is distributed as a gem, and can be installed with:
|
38
31
|
|
39
32
|
gem install american_date
|
data/Rakefile
CHANGED
@@ -9,30 +9,24 @@ rdoc_task_class = begin
|
|
9
9
|
RDOC_OPTS.concat(['-f', 'hanna'])
|
10
10
|
RDoc::Task
|
11
11
|
rescue LoadError
|
12
|
-
|
13
|
-
|
12
|
+
begin
|
13
|
+
require "rake/rdoctask"
|
14
|
+
Rake::RDocTask
|
15
|
+
rescue RuntimeError
|
16
|
+
end
|
14
17
|
end
|
15
18
|
|
16
|
-
rdoc_task_class
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
begin
|
23
|
-
require "spec/rake/spectask"
|
24
|
-
spec_class = Spec::Rake::SpecTask
|
25
|
-
spec_files_meth = :spec_files=
|
26
|
-
rescue LoadError
|
27
|
-
# RSpec 2
|
28
|
-
require "rspec/core/rake_task"
|
29
|
-
spec_class = RSpec::Core::RakeTask
|
30
|
-
spec_files_meth = :pattern=
|
19
|
+
if rdoc_task_class
|
20
|
+
rdoc_task_class.new do |rdoc|
|
21
|
+
rdoc.rdoc_dir = "rdoc"
|
22
|
+
rdoc.options += RDOC_OPTS
|
23
|
+
rdoc.rdoc_files.add %w"lib/american_date.rb MIT-LICENSE CHANGELOG README.rdoc"
|
24
|
+
end
|
31
25
|
end
|
32
26
|
|
33
27
|
desc "Run specs"
|
34
|
-
|
35
|
-
|
28
|
+
task :spec do
|
29
|
+
sh "#{FileUtils::RUBY} -I lib spec/american_date_spec.rb"
|
36
30
|
end
|
37
31
|
task :default=>[:spec]
|
38
32
|
|
data/lib/american_date.rb
CHANGED
@@ -1,26 +1,54 @@
|
|
1
1
|
require 'date'
|
2
2
|
|
3
3
|
if RUBY_VERSION >= '1.9'
|
4
|
+
long_date = ' ' * 128 + '2021-10-11'
|
5
|
+
limit_supported = begin
|
6
|
+
Date.parse(long_date)
|
7
|
+
rescue ArgumentError
|
8
|
+
(Date.parse(long_date, true, Date::ITALY, :limit=>nil) == Date.new(2021, 10, 11)) rescue false
|
9
|
+
else
|
10
|
+
false
|
11
|
+
end
|
12
|
+
|
4
13
|
# Modify parsing methods to handle american date format correctly.
|
5
|
-
|
14
|
+
Date.instance_eval do
|
6
15
|
# American date format detected by the library.
|
7
|
-
AMERICAN_DATE_RE = %r_
|
16
|
+
AMERICAN_DATE_RE = eval('%r_(?<!\d)(\d{1,2})/(\d{1,2})/(\d{4}|\d{2})(?!\d)_').freeze
|
17
|
+
# Negative lookbehinds, which are not supported in Ruby 1.8
|
18
|
+
# so by using eval, we prevent an error when this file is first parsed
|
19
|
+
# since the regexp itself will only be parsed at runtime if the RUBY_VERSION condition is met.
|
8
20
|
|
9
21
|
# Alias for stdlib Date._parse
|
10
22
|
alias _parse_without_american_date _parse
|
11
23
|
|
12
|
-
|
13
|
-
|
14
|
-
|
24
|
+
if limit_supported
|
25
|
+
instance_eval(<<-END, __FILE__, __LINE__+1)
|
26
|
+
def _parse(string, comp=true, limit: 128)
|
27
|
+
_parse_without_american_date(convert_american_to_iso(string), comp, limit: limit)
|
28
|
+
end
|
29
|
+
END
|
30
|
+
else
|
31
|
+
# Transform american dates into ISO dates before parsing.
|
32
|
+
def _parse(string, comp=true)
|
33
|
+
_parse_without_american_date(convert_american_to_iso(string), comp)
|
34
|
+
end
|
15
35
|
end
|
16
36
|
|
17
37
|
if RUBY_VERSION >= '1.9.3'
|
18
38
|
# Alias for stdlib Date.parse
|
19
39
|
alias parse_without_american_date parse
|
20
40
|
|
21
|
-
|
22
|
-
|
23
|
-
|
41
|
+
if limit_supported
|
42
|
+
instance_eval(<<-END, __FILE__, __LINE__+1)
|
43
|
+
def parse(string, comp=true, start=Date::ITALY, limit: 128)
|
44
|
+
parse_without_american_date(convert_american_to_iso(string), comp, start, limit: limit)
|
45
|
+
end
|
46
|
+
END
|
47
|
+
else
|
48
|
+
# Transform american dates into ISO dates before parsing.
|
49
|
+
def parse(string, comp=true, start=Date::ITALY)
|
50
|
+
parse_without_american_date(convert_american_to_iso(string), comp, start)
|
51
|
+
end
|
24
52
|
end
|
25
53
|
end
|
26
54
|
|
@@ -45,13 +73,21 @@ if RUBY_VERSION >= '1.9'
|
|
45
73
|
|
46
74
|
if RUBY_VERSION >= '1.9.3'
|
47
75
|
# Modify parsing methods to handle american date format correctly.
|
48
|
-
|
76
|
+
DateTime.instance_eval do
|
49
77
|
# Alias for stdlib Date.parse
|
50
78
|
alias parse_without_american_date parse
|
51
79
|
|
52
|
-
|
53
|
-
|
54
|
-
|
80
|
+
if limit_supported
|
81
|
+
instance_eval(<<-END, __FILE__, __LINE__+1)
|
82
|
+
def parse(string, comp=true, start=Date::ITALY, limit: 128)
|
83
|
+
parse_without_american_date(convert_american_to_iso(string), comp, start, limit: limit)
|
84
|
+
end
|
85
|
+
END
|
86
|
+
else
|
87
|
+
# Transform american dates into ISO dates before parsing.
|
88
|
+
def parse(string, comp=true, start=Date::ITALY)
|
89
|
+
parse_without_american_date(convert_american_to_iso(string), comp, start)
|
90
|
+
end
|
55
91
|
end
|
56
92
|
end
|
57
93
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
long_date = ' ' * 128 + '01/02/2003'
|
2
|
+
limit_supported = begin
|
3
|
+
Date.parse(long_date)
|
4
|
+
rescue ArgumentError
|
5
|
+
if ((Date.parse(long_date, true, Date::ITALY, :limit=>nil) == Date.new(2003, 1, 2)) rescue false)
|
6
|
+
describe "Date.parse" do
|
7
|
+
specify "should not support long dates without limit keyword" do
|
8
|
+
proc{Date.parse(long_date, true)}.must_raise ArgumentError
|
9
|
+
end
|
10
|
+
|
11
|
+
specify "should not support long dates with limit keyword" do
|
12
|
+
Date.parse(long_date, true, limit: 150).must_equal Date.new(2003, 1, 2)
|
13
|
+
Date.parse(long_date, true, limit: nil).must_equal Date.new(2003, 1, 2)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "DateTime.parse" do
|
18
|
+
long_datetime = long_date + ' 10:11:12'
|
19
|
+
specify "should not support long dates without limit keyword" do
|
20
|
+
proc{DateTime.parse(long_datetime, true)}.must_raise ArgumentError
|
21
|
+
end
|
22
|
+
|
23
|
+
specify "should not support long dates with limit keyword" do
|
24
|
+
DateTime.parse(long_datetime, true, limit: 150).must_equal DateTime.new(2003, 1, 2, 10, 11, 12)
|
25
|
+
DateTime.parse(long_datetime, true, limit: nil).must_equal DateTime.new(2003, 1, 2, 10, 11, 12)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "Date._parse" do
|
30
|
+
specify "should not support long dates without limit keyword" do
|
31
|
+
proc{Date._parse(long_date, true)}.must_raise ArgumentError
|
32
|
+
end
|
33
|
+
|
34
|
+
specify "should not support long dates with limit keyword" do
|
35
|
+
Date._parse(long_date, true, limit: 150).must_equal(:year=>2003, :mon=>1, :mday=>2)
|
36
|
+
Date._parse(long_date, true, limit: nil).must_equal(:year=>2003, :mon=>1, :mday=>2)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/american_date_spec.rb
CHANGED
@@ -1,201 +1,269 @@
|
|
1
1
|
require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'lib', 'american_date')
|
2
2
|
require 'time'
|
3
|
+
require 'rubygems'
|
4
|
+
ENV['MT_NO_PLUGINS'] = '1' # Work around stupid autoloading of plugins
|
5
|
+
gem 'minitest'
|
6
|
+
require 'minitest/global_expectations/autorun'
|
7
|
+
|
8
|
+
if RUBY_VERSION >= '2.0'
|
9
|
+
require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'spec', 'american_date_keyword_spec')
|
10
|
+
end
|
3
11
|
|
4
12
|
describe "Date.parse" do
|
5
13
|
specify "should use american date format for dd/mm/yy" do
|
6
|
-
Date.parse('01/02/03', true).
|
14
|
+
Date.parse('01/02/03', true).must_equal Date.new(2003, 1, 2)
|
15
|
+
Date.parse('01/02/03', true, Date::ITALY).must_equal Date.new(2003, 1, 2)
|
7
16
|
end
|
8
17
|
|
9
18
|
specify "should use american date format for d/m/yy" do
|
10
|
-
Date.parse('1/2/03', true).
|
11
|
-
Date.parse('1/2/03', false).
|
19
|
+
Date.parse('1/2/03', true).must_equal Date.new(2003, 1, 2)
|
20
|
+
Date.parse('1/2/03', false).must_equal Date.new(3, 1, 2)
|
12
21
|
end
|
13
22
|
|
14
23
|
specify "should use american date format for dd/mm/yyyy" do
|
15
|
-
Date.parse('01/02/2003').
|
24
|
+
Date.parse('01/02/2003').must_equal Date.new(2003, 1, 2)
|
16
25
|
end
|
17
26
|
|
18
27
|
specify "should use american date format for dd/mm" do
|
19
|
-
Date.parse('01/02').
|
28
|
+
Date.parse('01/02').must_equal Date.new(Time.now.year, 1, 2)
|
20
29
|
end
|
21
30
|
|
22
31
|
specify "should use american date format for d/m" do
|
23
|
-
Date.parse('1/2').
|
32
|
+
Date.parse('1/2').must_equal Date.new(Time.now.year, 1, 2)
|
24
33
|
end
|
25
34
|
|
26
35
|
specify "should ignore preceding whitespace" do
|
27
|
-
Date.parse(' 01/02/2003').
|
36
|
+
Date.parse(' 01/02/2003').must_equal Date.new(2003, 1, 2)
|
37
|
+
end
|
38
|
+
|
39
|
+
specify "should ignore preceding weekday" do
|
40
|
+
Date.parse('Day 01/02/2003').must_equal Date.new(2003, 1, 2)
|
41
|
+
end
|
42
|
+
|
43
|
+
specify "should work just like 1.8 does" do
|
44
|
+
Date.parse('10:20:30something01/02/2003else').must_equal Date.new(2003, 1, 2)
|
45
|
+
end
|
46
|
+
|
47
|
+
specify "should not mismatch years" do
|
48
|
+
Date.parse('2003/01/02').must_equal Date.new(2003, 1, 2)
|
49
|
+
end
|
50
|
+
|
51
|
+
specify "should behave like 1.8 and only allow / as delimiters in american-style dates" do
|
52
|
+
Date.parse("10/11/2012").must_equal Date.new(2012, 10, 11)
|
53
|
+
Date.parse("10-11-2012").must_equal Date.new(2012, 11, 10)
|
54
|
+
Date.parse("10.11.2012").must_equal Date.new(2012, 11, 10)
|
28
55
|
end
|
29
56
|
|
30
57
|
if RUBY_VERSION > '1.9'
|
31
58
|
specify "should raise TypeError for invalid values" do
|
32
59
|
[nil, 1, 1.0, [], {}].each do |x|
|
33
|
-
proc{Date.parse(x)}.
|
60
|
+
proc{Date.parse(x)}.must_raise(TypeError)
|
34
61
|
end
|
35
62
|
end
|
36
63
|
|
37
64
|
specify "should handle values implicitly convertible to String" do
|
38
65
|
o = Object.new
|
39
66
|
def o.to_str() '01/02/2003' end
|
40
|
-
Date.parse(o).
|
67
|
+
Date.parse(o).must_equal Date.new(2003, 1, 2)
|
41
68
|
end
|
42
69
|
|
43
70
|
specify "should handle values implicitly convertible to String" do
|
44
71
|
o = Object.new
|
45
72
|
def o.to_str() 1 end
|
46
|
-
proc{Date.parse(o)}.
|
73
|
+
proc{Date.parse(o)}.must_raise(TypeError)
|
47
74
|
end
|
48
75
|
end
|
49
76
|
end
|
50
77
|
|
51
78
|
describe "DateTime.parse" do
|
52
79
|
specify "should use american date format for dd/mm/yy" do
|
53
|
-
DateTime.parse('01/02/03', true).
|
80
|
+
DateTime.parse('01/02/03', true).must_equal DateTime.new(2003, 1, 2)
|
81
|
+
DateTime.parse('01/02/03', true, DateTime::ITALY).must_equal DateTime.new(2003, 1, 2)
|
54
82
|
end
|
55
83
|
|
56
84
|
specify "should use american date format for d/m/yy" do
|
57
|
-
DateTime.parse('1/2/03', true).
|
58
|
-
DateTime.parse('1/2/03', false).
|
85
|
+
DateTime.parse('1/2/03', true).must_equal DateTime.new(2003, 1, 2)
|
86
|
+
DateTime.parse('1/2/03', false).must_equal DateTime.new(3, 1, 2)
|
59
87
|
end
|
60
88
|
|
61
89
|
specify "should use american date format for dd/mm/yyyy" do
|
62
|
-
DateTime.parse('01/02/2003').
|
90
|
+
DateTime.parse('01/02/2003').must_equal DateTime.new(2003, 1, 2)
|
63
91
|
end
|
64
92
|
|
65
93
|
specify "should use american date format for dd/mm" do
|
66
|
-
DateTime.parse('01/02').
|
94
|
+
DateTime.parse('01/02').must_equal DateTime.new(Time.now.year, 1, 2)
|
67
95
|
end
|
68
96
|
|
69
97
|
specify "should use american date format for d/m" do
|
70
|
-
DateTime.parse('1/2').
|
98
|
+
DateTime.parse('1/2').must_equal DateTime.new(Time.now.year, 1, 2)
|
71
99
|
end
|
72
100
|
|
73
101
|
specify "should ignore preceding whitespace" do
|
74
|
-
DateTime.parse(' 01/02/2003').
|
102
|
+
DateTime.parse(' 01/02/2003').must_equal DateTime.new(2003, 1, 2)
|
103
|
+
end
|
104
|
+
|
105
|
+
specify "should ignore preceding weekday" do
|
106
|
+
DateTime.parse('Day 01/02/2003').must_equal Date.new(2003, 1, 2)
|
75
107
|
end
|
76
108
|
|
77
109
|
specify "should work with times" do
|
78
|
-
DateTime.parse('01/02/2003 10:20:30').
|
110
|
+
DateTime.parse('01/02/2003 10:20:30').must_equal DateTime.new(2003, 1, 2, 10, 20, 30)
|
111
|
+
end
|
112
|
+
|
113
|
+
specify "should work with times and weekdays" do
|
114
|
+
DateTime.parse('Day 01/02/2003 10:20:30').must_equal DateTime.new(2003, 1, 2, 10, 20, 30)
|
115
|
+
end
|
116
|
+
|
117
|
+
specify "should work just like 1.8 does" do
|
118
|
+
DateTime.parse('10:20:30something01/02/2003else').must_equal DateTime.new(2003, 1, 2, 10, 20, 30)
|
119
|
+
end
|
120
|
+
|
121
|
+
specify "should not mismatch years" do
|
122
|
+
DateTime.parse('2003/01/02').must_equal Date.new(2003, 1, 2)
|
79
123
|
end
|
80
124
|
|
81
125
|
if RUBY_VERSION > '1.9'
|
82
126
|
specify "should raise TypeError for invalid values" do
|
83
127
|
[nil, 1, 1.0, [], {}].each do |x|
|
84
|
-
proc{DateTime.parse(x)}.
|
128
|
+
proc{DateTime.parse(x)}.must_raise(TypeError)
|
85
129
|
end
|
86
130
|
end
|
87
131
|
|
88
132
|
specify "should handle values implicitly convertible to String" do
|
89
133
|
o = Object.new
|
90
134
|
def o.to_str() '01/02/2003' end
|
91
|
-
DateTime.parse(o).
|
135
|
+
DateTime.parse(o).must_equal DateTime.new(2003, 1, 2)
|
92
136
|
end
|
93
137
|
|
94
138
|
specify "should handle values implicitly convertible to String" do
|
95
139
|
o = Object.new
|
96
140
|
def o.to_str() 1 end
|
97
|
-
proc{DateTime.parse(o)}.
|
141
|
+
proc{DateTime.parse(o)}.must_raise(TypeError)
|
98
142
|
end
|
99
143
|
end
|
100
144
|
end
|
101
145
|
|
102
146
|
describe "Time.parse" do
|
103
147
|
specify "should use american date format for dd/mm/yy" do
|
104
|
-
Time.parse('01/02/03'
|
148
|
+
Time.parse('01/02/03').must_equal Time.local(2003, 1, 2)
|
105
149
|
end
|
106
150
|
|
107
151
|
specify "should use american date format for d/m/yy" do
|
108
|
-
Time.parse('1/2/03'
|
152
|
+
Time.parse('1/2/03').must_equal Time.local(2003, 1, 2)
|
109
153
|
end
|
110
154
|
|
111
155
|
specify "should use american date format for dd/mm/yyyy" do
|
112
|
-
Time.parse('01/02/2003').
|
156
|
+
Time.parse('01/02/2003').must_equal Time.local(2003, 1, 2)
|
113
157
|
end
|
114
158
|
|
115
159
|
specify "should use american date format for dd/mm" do
|
116
|
-
Time.parse('01/02').
|
160
|
+
Time.parse('01/02').must_equal Time.local(Time.now.year, 1, 2)
|
117
161
|
end
|
118
162
|
|
119
163
|
specify "should use american date format for d/m" do
|
120
|
-
Time.parse('1/2').
|
164
|
+
Time.parse('1/2').must_equal Time.local(Time.now.year, 1, 2)
|
121
165
|
end
|
122
166
|
|
123
167
|
specify "should ignore preceding whitespace" do
|
124
|
-
Time.parse(' 01/02/2003').
|
168
|
+
Time.parse(' 01/02/2003').must_equal Time.local(2003, 1, 2)
|
169
|
+
end
|
170
|
+
|
171
|
+
specify "should ignore preceding weekdays" do
|
172
|
+
Time.parse('Day 01/02/2003').must_equal Time.local(2003, 1, 2)
|
125
173
|
end
|
126
174
|
|
127
175
|
specify "should work with times" do
|
128
|
-
Time.parse('01/02/2003 10:20:30').
|
176
|
+
Time.parse('01/02/2003 10:20:30').must_equal Time.local(2003, 1, 2, 10, 20, 30)
|
177
|
+
end
|
178
|
+
|
179
|
+
specify "should work with times and weekdays" do
|
180
|
+
Time.parse('Day 01/02/2003 10:20:30').must_equal Time.local(2003, 1, 2, 10, 20, 30)
|
181
|
+
end
|
182
|
+
|
183
|
+
specify "should work with time first and date second" do
|
184
|
+
Time.parse('10:20:30 01/02/2003').must_equal Time.local(2003, 1, 2, 10, 20, 30)
|
185
|
+
end
|
186
|
+
|
187
|
+
specify "should work with time first and date second and weekday in the middle" do
|
188
|
+
Time.parse('10:20:30 Thu 01/02/2003').must_equal Time.local(2003, 1, 2, 10, 20, 30)
|
189
|
+
end
|
190
|
+
|
191
|
+
specify "should work just like 1.8 does" do
|
192
|
+
Time.parse('10:20:30something01/02/2003else').must_equal Time.local(2003, 1, 2, 10, 20, 30)
|
193
|
+
end
|
194
|
+
|
195
|
+
specify "should not mismatch years" do
|
196
|
+
Time.parse('2003/01/02').must_equal Time.local(2003, 1, 2, 0, 0, 0)
|
129
197
|
end
|
130
198
|
|
131
199
|
if RUBY_VERSION > '1.9'
|
132
200
|
specify "should raise TypeError for invalid values" do
|
133
201
|
[nil, 1, 1.0, [], {}].each do |x|
|
134
|
-
proc{Time.parse(x)}.
|
202
|
+
proc{Time.parse(x)}.must_raise(TypeError)
|
135
203
|
end
|
136
204
|
end
|
137
205
|
|
138
206
|
specify "should handle values implicitly convertible to String" do
|
139
207
|
o = Object.new
|
140
208
|
def o.to_str() '01/02/2003' end
|
141
|
-
Time.parse(o).
|
209
|
+
Time.parse(o).must_equal Time.local(2003, 1, 2)
|
142
210
|
end
|
143
211
|
|
144
212
|
specify "should handle values implicitly convertible to String" do
|
145
213
|
o = Object.new
|
146
214
|
def o.to_str() 1 end
|
147
|
-
proc{Time.parse(o)}.
|
215
|
+
proc{Time.parse(o)}.must_raise(TypeError)
|
148
216
|
end
|
149
217
|
end
|
150
218
|
end
|
151
219
|
|
152
220
|
describe "Date._parse" do
|
153
221
|
specify "should use american date format for dd/mm/yy" do
|
154
|
-
Date._parse('01/02/03', true).
|
222
|
+
Date._parse('01/02/03', true).must_equal(:year=>2003, :mon=>1, :mday=>2)
|
155
223
|
end
|
156
224
|
|
157
225
|
specify "should use american date format for d/m/yy" do
|
158
|
-
Date._parse('1/2/03', true).
|
159
|
-
Date._parse('1/2/03', false).
|
226
|
+
Date._parse('1/2/03', true).must_equal(:year=>2003, :mon=>1, :mday=>2)
|
227
|
+
Date._parse('1/2/03', false).must_equal(:year=>3, :mon=>1, :mday=>2)
|
160
228
|
end
|
161
229
|
|
162
230
|
specify "should use american date format for dd/mm/yyyy" do
|
163
|
-
Date._parse('01/02/2003').
|
231
|
+
Date._parse('01/02/2003').must_equal(:year=>2003, :mon=>1, :mday=>2)
|
164
232
|
end
|
165
233
|
|
166
234
|
specify "should use american date format for dd/mm" do
|
167
|
-
Date._parse('01/02').
|
235
|
+
Date._parse('01/02').must_equal(:mon=>1, :mday=>2)
|
168
236
|
end
|
169
237
|
|
170
238
|
specify "should use american date format for d/m" do
|
171
|
-
Date._parse('1/2').
|
239
|
+
Date._parse('1/2').must_equal(:mon=>1, :mday=>2)
|
172
240
|
end
|
173
241
|
|
174
242
|
specify "should ignore preceding whitespace" do
|
175
|
-
Date._parse(' 01/02/2003').
|
243
|
+
Date._parse(' 01/02/2003').must_equal(:year=>2003, :mon=>1, :mday=>2)
|
176
244
|
end
|
177
245
|
|
178
246
|
specify "should work with times" do
|
179
|
-
DateTime._parse('01/02/2003 10:20:30').
|
247
|
+
DateTime._parse('01/02/2003 10:20:30').must_equal(:year=>2003, :mon=>1, :mday=>2, :hour=>10, :min=>20, :sec=>30)
|
180
248
|
end
|
181
249
|
|
182
250
|
if RUBY_VERSION > '1.9'
|
183
251
|
specify "should raise TypeError for invalid values" do
|
184
252
|
[nil, 1, 1.0, [], {}].each do |x|
|
185
|
-
proc{DateTime._parse(x)}.
|
253
|
+
proc{DateTime._parse(x)}.must_raise(TypeError)
|
186
254
|
end
|
187
255
|
end
|
188
256
|
|
189
257
|
specify "should handle values implicitly convertible to String" do
|
190
258
|
o = Object.new
|
191
259
|
def o.to_str() '01/02/2003' end
|
192
|
-
DateTime._parse(o).
|
260
|
+
DateTime._parse(o).must_equal(:year=>2003, :mon=>1, :mday=>2)
|
193
261
|
end
|
194
262
|
|
195
263
|
specify "should handle values implicitly convertible to String" do
|
196
264
|
o = Object.new
|
197
265
|
def o.to_str() 1 end
|
198
|
-
proc{DateTime._parse(o)}.
|
266
|
+
proc{DateTime._parse(o)}.must_raise(TypeError)
|
199
267
|
end
|
200
268
|
end
|
201
269
|
end
|
metadata
CHANGED
@@ -1,16 +1,44 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: american_date
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.2.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:
|
12
|
-
dependencies:
|
13
|
-
|
11
|
+
date: 2021-11-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest-global_expectations
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: American style month/day/year date parsing for ruby 1.9+
|
14
42
|
email: code@jeremyevans.net
|
15
43
|
executables: []
|
16
44
|
extensions: []
|
@@ -19,40 +47,44 @@ extra_rdoc_files:
|
|
19
47
|
- CHANGELOG
|
20
48
|
- MIT-LICENSE
|
21
49
|
files:
|
22
|
-
- MIT-LICENSE
|
23
50
|
- CHANGELOG
|
51
|
+
- MIT-LICENSE
|
24
52
|
- README.rdoc
|
25
53
|
- Rakefile
|
26
|
-
- spec/american_date_spec.rb
|
27
54
|
- lib/american_date.rb
|
55
|
+
- spec/american_date_keyword_spec.rb
|
56
|
+
- spec/american_date_spec.rb
|
28
57
|
homepage: https://github.com/jeremyevans/ruby-american_date
|
29
|
-
licenses:
|
30
|
-
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata:
|
61
|
+
bug_tracker_uri: https://github.com/jeremyevans/ruby-american_date/issues
|
62
|
+
changelog_uri: https://github.com/jeremyevans/ruby-american_date/blob/master/CHANGELOG
|
63
|
+
source_code_uri: https://github.com/jeremyevans/ruby-american_date
|
31
64
|
post_install_message:
|
32
65
|
rdoc_options:
|
33
|
-
- --quiet
|
34
|
-
- --inline-source
|
35
|
-
- --line-numbers
|
36
|
-
- --title
|
37
|
-
- 'american_date: American style month/day/year date parsing for ruby 1.9'
|
38
|
-
- --main
|
66
|
+
- "--quiet"
|
67
|
+
- "--inline-source"
|
68
|
+
- "--line-numbers"
|
69
|
+
- "--title"
|
70
|
+
- 'american_date: American style month/day/year date parsing for ruby 1.9+'
|
71
|
+
- "--main"
|
39
72
|
- README.rdoc
|
40
73
|
require_paths:
|
41
74
|
- lib
|
42
75
|
required_ruby_version: !ruby/object:Gem::Requirement
|
43
76
|
requirements:
|
44
|
-
- -
|
77
|
+
- - ">="
|
45
78
|
- !ruby/object:Gem::Version
|
46
79
|
version: '0'
|
47
80
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
81
|
requirements:
|
49
|
-
- -
|
82
|
+
- - ">="
|
50
83
|
- !ruby/object:Gem::Version
|
51
84
|
version: '0'
|
52
85
|
requirements: []
|
53
|
-
|
54
|
-
rubygems_version: 2.0.0
|
86
|
+
rubygems_version: 3.2.22
|
55
87
|
signing_key:
|
56
88
|
specification_version: 4
|
57
|
-
summary: American style month/day/year date parsing for ruby 1.9
|
89
|
+
summary: American style month/day/year date parsing for ruby 1.9+
|
58
90
|
test_files: []
|