american_date 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ === 1.0.0 (2011-12-12)
2
+
3
+ * Initial public release
data/MIT-LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2011 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.
data/README.rdoc ADDED
@@ -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
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>
data/Rakefile ADDED
@@ -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,47 @@
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})_.freeze
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
+ string.sub(AMERICAN_DATE_RE){|m| "#$3-#$1-#$2"}
32
+ end
33
+ end
34
+
35
+ if RUBY_VERSION >= '1.9.3'
36
+ # Modify parsing methods to handle american date format correctly.
37
+ class << DateTime
38
+ # Alias for stdlib Date.parse
39
+ alias parse_without_american_date parse
40
+
41
+ # Transform american dates into ISO dates before parsing.
42
+ def parse(string, comp=true)
43
+ parse_without_american_date(convert_american_to_iso(string), comp)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,121 @@
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
+ end
30
+
31
+ describe "DateTime.parse" do
32
+ specify "should use american date format for dd/mm/yy" do
33
+ DateTime.parse('01/02/03', true).should == DateTime.new(2003, 1, 2)
34
+ end
35
+
36
+ specify "should use american date format for d/m/yy" do
37
+ DateTime.parse('1/2/03', true).should == DateTime.new(2003, 1, 2)
38
+ DateTime.parse('1/2/03', false).should == DateTime.new(3, 1, 2)
39
+ end
40
+
41
+ specify "should use american date format for dd/mm/yyyy" do
42
+ DateTime.parse('01/02/2003').should == DateTime.new(2003, 1, 2)
43
+ end
44
+
45
+ specify "should use american date format for dd/mm" do
46
+ DateTime.parse('01/02').should == DateTime.new(Time.now.year, 1, 2)
47
+ end
48
+
49
+ specify "should use american date format for d/m" do
50
+ DateTime.parse('1/2').should == DateTime.new(Time.now.year, 1, 2)
51
+ end
52
+
53
+ specify "should ignore preceding whitespace" do
54
+ DateTime.parse(' 01/02/2003').should == DateTime.new(2003, 1, 2)
55
+ end
56
+
57
+ specify "should work with times" do
58
+ DateTime.parse('01/02/2003 10:20:30').should == DateTime.new(2003, 1, 2, 10, 20, 30)
59
+ end
60
+ end
61
+
62
+ describe "Time.parse" do
63
+ specify "should use american date format for dd/mm/yy" do
64
+ Time.parse('01/02/03', true).should == Time.local(2003, 1, 2)
65
+ end
66
+
67
+ specify "should use american date format for d/m/yy" do
68
+ Time.parse('1/2/03', true).should == Time.local(2003, 1, 2)
69
+ end
70
+
71
+ specify "should use american date format for dd/mm/yyyy" do
72
+ Time.parse('01/02/2003').should == Time.local(2003, 1, 2)
73
+ end
74
+
75
+ specify "should use american date format for dd/mm" do
76
+ Time.parse('01/02').should == Time.local(Time.now.year, 1, 2)
77
+ end
78
+
79
+ specify "should use american date format for d/m" do
80
+ Time.parse('1/2').should == Time.local(Time.now.year, 1, 2)
81
+ end
82
+
83
+ specify "should ignore preceding whitespace" do
84
+ Time.parse(' 01/02/2003').should == Time.local(2003, 1, 2)
85
+ end
86
+
87
+ specify "should work with times" do
88
+ Time.parse('01/02/2003 10:20:30').should == Time.local(2003, 1, 2, 10, 20, 30)
89
+ end
90
+ end
91
+
92
+ describe "Date._parse" do
93
+ specify "should use american date format for dd/mm/yy" do
94
+ Date._parse('01/02/03', true).should == {:year=>2003, :mon=>1, :mday=>2}
95
+ end
96
+
97
+ specify "should use american date format for d/m/yy" do
98
+ Date._parse('1/2/03', true).should == {:year=>2003, :mon=>1, :mday=>2}
99
+ Date._parse('1/2/03', false).should == {:year=>3, :mon=>1, :mday=>2}
100
+ end
101
+
102
+ specify "should use american date format for dd/mm/yyyy" do
103
+ Date._parse('01/02/2003').should == {:year=>2003, :mon=>1, :mday=>2}
104
+ end
105
+
106
+ specify "should use american date format for dd/mm" do
107
+ Date._parse('01/02').should == {:mon=>1, :mday=>2}
108
+ end
109
+
110
+ specify "should use american date format for d/m" do
111
+ Date._parse('1/2').should == {:mon=>1, :mday=>2}
112
+ end
113
+
114
+ specify "should ignore preceding whitespace" do
115
+ Date._parse(' 01/02/2003').should == {:year=>2003, :mon=>1, :mday=>2}
116
+ end
117
+
118
+ specify "should work with times" do
119
+ DateTime._parse('01/02/2003 10:20:30').should == {:year=>2003, :mon=>1, :mday=>2, :hour=>10, :min=>20, :sec=>30}
120
+ end
121
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: american_date
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeremy Evans
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-12 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: American style month/day/year date parsing for ruby 1.9
15
+ email: code@jeremyevans.net
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.rdoc
20
+ - CHANGELOG
21
+ - MIT-LICENSE
22
+ files:
23
+ - MIT-LICENSE
24
+ - CHANGELOG
25
+ - README.rdoc
26
+ - Rakefile
27
+ - spec/american_date_spec.rb
28
+ - lib/american_date.rb
29
+ homepage: https://github.com/jeremyevans/ruby-american_date
30
+ licenses: []
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
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.11
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: American style month/day/year date parsing for ruby 1.9
60
+ test_files: []