american_date 1.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d6fc099c8faff7f7a245c01f54bcc32c4770fa8d
4
+ data.tar.gz: 0b072c3a750fc6fb6ac0bc81c730ff1fae5e6889
5
+ SHA512:
6
+ metadata.gz: deff8c7fd14b1b48d4e88fd6f3257931623206746cdd2f8947f329ec441edf136fe94b3fce081cecfb4e2601563669b0bdc5ed33288a06e690ce14fefa8e4803
7
+ data.tar.gz: 1a9b4920b7396ba32f4589168943fa1870d256e3ef6fa5c7ed529fc396fc9d05d32f53513b8cff6a4b61f893c2ddd7292276a7b2a90058dbcf95a4f869b29377
@@ -0,0 +1,9 @@
1
+ === HEAD
2
+
3
+ * Don't freeze the regular expression used, to allow easier overrides (jeremyevans)
4
+
5
+ * Raise TypeError if passed object not implicitly convertible to String (jeremyevans) (#6)
6
+
7
+ === 1.0.0 (2011-12-12)
8
+
9
+ * Initial public release
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2011,2013 Jeremy Evans
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,55 @@
1
+ = ruby-american_date
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.
7
+
8
+ As far as I know, there isn't a gem that already handles this. You
9
+ can find many snippets on the web that partially solve the issue, but
10
+ most won't be compatible with ruby 1.9.3, because Date.parse and
11
+ DateTime.parse no longer call Date._parse directly on 1.9.3. Also
12
+ most don't handle cases where an american date format is used in
13
+ addition to a time format.
14
+
15
+ == Design
16
+
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.
28
+
29
+ == Tested ruby versions
30
+
31
+ * ruby 1.8.6, 1.8.7, 1.9.2, 1.9.3, 2.0.0
32
+ * rubinius 1.2.4
33
+ * jruby 1.6.5 (both --1.8 and --1.9 modes)
34
+
35
+ == Installation
36
+
37
+ ruby-american_date is distributed as a gem, and can be installed with:
38
+
39
+ gem install american_date
40
+
41
+ == Source
42
+
43
+ ruby-american_date is hosted on GitHub:
44
+
45
+ https://github.com/jeremyevans/ruby-american_date
46
+
47
+ == Issues
48
+
49
+ ruby-american_date uses GitHub Issues for issue tracking:
50
+
51
+ https://github.com/jeremyevans/ruby-american_date/issues
52
+
53
+ == Author
54
+
55
+ Jeremy Evans <code@jeremyevans.net>
@@ -0,0 +1,43 @@
1
+ require "rake"
2
+ require "rake/clean"
3
+
4
+ CLEAN.include ["*.gem", "rdoc"]
5
+ RDOC_OPTS = ['--inline-source', '--line-numbers', '--title', '', '--main', 'README.rdoc']
6
+
7
+ rdoc_task_class = begin
8
+ require "rdoc/task"
9
+ RDOC_OPTS.concat(['-f', 'hanna'])
10
+ RDoc::Task
11
+ rescue LoadError
12
+ require "rake/rdoctask"
13
+ Rake::RDocTask
14
+ end
15
+
16
+ rdoc_task_class.new do |rdoc|
17
+ rdoc.rdoc_dir = "rdoc"
18
+ rdoc.options += RDOC_OPTS
19
+ rdoc.rdoc_files.add %w"lib/american_date.rb MIT-LICENSE CHANGELOG README.rdoc"
20
+ end
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=
31
+ end
32
+
33
+ desc "Run specs"
34
+ spec_class.new("spec") do |t|
35
+ t.send(spec_files_meth, ["spec/american_date_spec.rb"])
36
+ end
37
+ task :default=>[:spec]
38
+
39
+ desc "Package american_date"
40
+ task :gem=>[:clean] do
41
+ load './american_date.gemspec'
42
+ Gem::Builder.new(AMERICAN_DATE_GEMSPEC).build
43
+ end
@@ -0,0 +1,58 @@
1
+ require 'date'
2
+
3
+ if RUBY_VERSION >= '1.9'
4
+ # Modify parsing methods to handle american date format correctly.
5
+ class << Date
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})_
8
+
9
+ # Alias for stdlib Date._parse
10
+ alias _parse_without_american_date _parse
11
+
12
+ # Transform american dates into ISO dates before parsing.
13
+ def _parse(string, comp=true)
14
+ _parse_without_american_date(convert_american_to_iso(string), comp)
15
+ end
16
+
17
+ if RUBY_VERSION >= '1.9.3'
18
+ # Alias for stdlib Date.parse
19
+ alias parse_without_american_date parse
20
+
21
+ # Transform american dates into ISO dates before parsing.
22
+ def parse(string, comp=true)
23
+ parse_without_american_date(convert_american_to_iso(string), comp)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ # Transform american date fromat into ISO format.
30
+ def convert_american_to_iso(string)
31
+ unless string.is_a?(String)
32
+ if string.respond_to?(:to_str)
33
+ str = string.to_str
34
+ unless str.is_a?(String)
35
+ raise TypeError, "no implicit conversion of #{string.inspect} into String"
36
+ end
37
+ string = str
38
+ else
39
+ raise TypeError, "no implicit conversion of #{string.inspect} into String"
40
+ end
41
+ end
42
+ string.sub(AMERICAN_DATE_RE){|m| "#$3-#$1-#$2"}
43
+ end
44
+ end
45
+
46
+ if RUBY_VERSION >= '1.9.3'
47
+ # Modify parsing methods to handle american date format correctly.
48
+ class << DateTime
49
+ # Alias for stdlib Date.parse
50
+ alias parse_without_american_date parse
51
+
52
+ # Transform american dates into ISO dates before parsing.
53
+ def parse(string, comp=true)
54
+ parse_without_american_date(convert_american_to_iso(string), comp)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,201 @@
1
+ require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'lib', 'american_date')
2
+ require 'time'
3
+
4
+ describe "Date.parse" do
5
+ specify "should use american date format for dd/mm/yy" do
6
+ Date.parse('01/02/03', true).should == Date.new(2003, 1, 2)
7
+ end
8
+
9
+ specify "should use american date format for d/m/yy" do
10
+ Date.parse('1/2/03', true).should == Date.new(2003, 1, 2)
11
+ Date.parse('1/2/03', false).should == Date.new(3, 1, 2)
12
+ end
13
+
14
+ specify "should use american date format for dd/mm/yyyy" do
15
+ Date.parse('01/02/2003').should == Date.new(2003, 1, 2)
16
+ end
17
+
18
+ specify "should use american date format for dd/mm" do
19
+ Date.parse('01/02').should == Date.new(Time.now.year, 1, 2)
20
+ end
21
+
22
+ specify "should use american date format for d/m" do
23
+ Date.parse('1/2').should == Date.new(Time.now.year, 1, 2)
24
+ end
25
+
26
+ specify "should ignore preceding whitespace" do
27
+ Date.parse(' 01/02/2003').should == Date.new(2003, 1, 2)
28
+ end
29
+
30
+ if RUBY_VERSION > '1.9'
31
+ specify "should raise TypeError for invalid values" do
32
+ [nil, 1, 1.0, [], {}].each do |x|
33
+ proc{Date.parse(x)}.should raise_error(TypeError)
34
+ end
35
+ end
36
+
37
+ specify "should handle values implicitly convertible to String" do
38
+ o = Object.new
39
+ def o.to_str() '01/02/2003' end
40
+ Date.parse(o).should == Date.new(2003, 1, 2)
41
+ end
42
+
43
+ specify "should handle values implicitly convertible to String" do
44
+ o = Object.new
45
+ def o.to_str() 1 end
46
+ proc{Date.parse(o)}.should raise_error(TypeError)
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "DateTime.parse" do
52
+ specify "should use american date format for dd/mm/yy" do
53
+ DateTime.parse('01/02/03', true).should == DateTime.new(2003, 1, 2)
54
+ end
55
+
56
+ specify "should use american date format for d/m/yy" do
57
+ DateTime.parse('1/2/03', true).should == DateTime.new(2003, 1, 2)
58
+ DateTime.parse('1/2/03', false).should == DateTime.new(3, 1, 2)
59
+ end
60
+
61
+ specify "should use american date format for dd/mm/yyyy" do
62
+ DateTime.parse('01/02/2003').should == DateTime.new(2003, 1, 2)
63
+ end
64
+
65
+ specify "should use american date format for dd/mm" do
66
+ DateTime.parse('01/02').should == DateTime.new(Time.now.year, 1, 2)
67
+ end
68
+
69
+ specify "should use american date format for d/m" do
70
+ DateTime.parse('1/2').should == DateTime.new(Time.now.year, 1, 2)
71
+ end
72
+
73
+ specify "should ignore preceding whitespace" do
74
+ DateTime.parse(' 01/02/2003').should == DateTime.new(2003, 1, 2)
75
+ end
76
+
77
+ specify "should work with times" do
78
+ DateTime.parse('01/02/2003 10:20:30').should == DateTime.new(2003, 1, 2, 10, 20, 30)
79
+ end
80
+
81
+ if RUBY_VERSION > '1.9'
82
+ specify "should raise TypeError for invalid values" do
83
+ [nil, 1, 1.0, [], {}].each do |x|
84
+ proc{DateTime.parse(x)}.should raise_error(TypeError)
85
+ end
86
+ end
87
+
88
+ specify "should handle values implicitly convertible to String" do
89
+ o = Object.new
90
+ def o.to_str() '01/02/2003' end
91
+ DateTime.parse(o).should == DateTime.new(2003, 1, 2)
92
+ end
93
+
94
+ specify "should handle values implicitly convertible to String" do
95
+ o = Object.new
96
+ def o.to_str() 1 end
97
+ proc{DateTime.parse(o)}.should raise_error(TypeError)
98
+ end
99
+ end
100
+ end
101
+
102
+ describe "Time.parse" do
103
+ specify "should use american date format for dd/mm/yy" do
104
+ Time.parse('01/02/03', true).should == Time.local(2003, 1, 2)
105
+ end
106
+
107
+ specify "should use american date format for d/m/yy" do
108
+ Time.parse('1/2/03', true).should == Time.local(2003, 1, 2)
109
+ end
110
+
111
+ specify "should use american date format for dd/mm/yyyy" do
112
+ Time.parse('01/02/2003').should == Time.local(2003, 1, 2)
113
+ end
114
+
115
+ specify "should use american date format for dd/mm" do
116
+ Time.parse('01/02').should == Time.local(Time.now.year, 1, 2)
117
+ end
118
+
119
+ specify "should use american date format for d/m" do
120
+ Time.parse('1/2').should == Time.local(Time.now.year, 1, 2)
121
+ end
122
+
123
+ specify "should ignore preceding whitespace" do
124
+ Time.parse(' 01/02/2003').should == Time.local(2003, 1, 2)
125
+ end
126
+
127
+ specify "should work with times" do
128
+ Time.parse('01/02/2003 10:20:30').should == Time.local(2003, 1, 2, 10, 20, 30)
129
+ end
130
+
131
+ if RUBY_VERSION > '1.9'
132
+ specify "should raise TypeError for invalid values" do
133
+ [nil, 1, 1.0, [], {}].each do |x|
134
+ proc{Time.parse(x)}.should raise_error(TypeError)
135
+ end
136
+ end
137
+
138
+ specify "should handle values implicitly convertible to String" do
139
+ o = Object.new
140
+ def o.to_str() '01/02/2003' end
141
+ Time.parse(o).should == Time.local(2003, 1, 2)
142
+ end
143
+
144
+ specify "should handle values implicitly convertible to String" do
145
+ o = Object.new
146
+ def o.to_str() 1 end
147
+ proc{Time.parse(o)}.should raise_error(TypeError)
148
+ end
149
+ end
150
+ end
151
+
152
+ describe "Date._parse" do
153
+ specify "should use american date format for dd/mm/yy" do
154
+ Date._parse('01/02/03', true).should == {:year=>2003, :mon=>1, :mday=>2}
155
+ end
156
+
157
+ specify "should use american date format for d/m/yy" do
158
+ Date._parse('1/2/03', true).should == {:year=>2003, :mon=>1, :mday=>2}
159
+ Date._parse('1/2/03', false).should == {:year=>3, :mon=>1, :mday=>2}
160
+ end
161
+
162
+ specify "should use american date format for dd/mm/yyyy" do
163
+ Date._parse('01/02/2003').should == {:year=>2003, :mon=>1, :mday=>2}
164
+ end
165
+
166
+ specify "should use american date format for dd/mm" do
167
+ Date._parse('01/02').should == {:mon=>1, :mday=>2}
168
+ end
169
+
170
+ specify "should use american date format for d/m" do
171
+ Date._parse('1/2').should == {:mon=>1, :mday=>2}
172
+ end
173
+
174
+ specify "should ignore preceding whitespace" do
175
+ Date._parse(' 01/02/2003').should == {:year=>2003, :mon=>1, :mday=>2}
176
+ end
177
+
178
+ specify "should work with times" do
179
+ DateTime._parse('01/02/2003 10:20:30').should == {:year=>2003, :mon=>1, :mday=>2, :hour=>10, :min=>20, :sec=>30}
180
+ end
181
+
182
+ if RUBY_VERSION > '1.9'
183
+ specify "should raise TypeError for invalid values" do
184
+ [nil, 1, 1.0, [], {}].each do |x|
185
+ proc{DateTime._parse(x)}.should raise_error(TypeError)
186
+ end
187
+ end
188
+
189
+ specify "should handle values implicitly convertible to String" do
190
+ o = Object.new
191
+ def o.to_str() '01/02/2003' end
192
+ DateTime._parse(o).should == {:year=>2003, :mon=>1, :mday=>2}
193
+ end
194
+
195
+ specify "should handle values implicitly convertible to String" do
196
+ o = Object.new
197
+ def o.to_str() 1 end
198
+ proc{DateTime._parse(o)}.should raise_error(TypeError)
199
+ end
200
+ end
201
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: american_date
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: American style month/day/year date parsing for ruby 1.9
14
+ email: code@jeremyevans.net
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files:
18
+ - README.rdoc
19
+ - CHANGELOG
20
+ - MIT-LICENSE
21
+ files:
22
+ - MIT-LICENSE
23
+ - CHANGELOG
24
+ - README.rdoc
25
+ - Rakefile
26
+ - spec/american_date_spec.rb
27
+ - lib/american_date.rb
28
+ homepage: https://github.com/jeremyevans/ruby-american_date
29
+ licenses: []
30
+ metadata: {}
31
+ post_install_message:
32
+ 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
39
+ - README.rdoc
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.0.0
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: American style month/day/year date parsing for ruby 1.9
58
+ test_files: []