pseudo_date 0.2.0 → 0.3.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/Gemfile.lock +4 -7
- data/lib/core_extensions/string.rb +2 -2
- data/lib/pseudo_date/parser.rb +99 -94
- data/lib/pseudo_date/version.rb +1 -1
- data/pseudo_date.gemspec +2 -3
- data/spec/spec_helper.rb +1 -2
- metadata +8 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 194f7ad213e4723e13dadd0bb7e4808b0e7d078d96302f27e67c926a471740a7
|
4
|
+
data.tar.gz: 5924349cb3530554acc4778d3983351afecb4f22a413906555259b3a70a008b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6af99fbd3a7e1a14c4e0966e145e60abd25242b9bc127e24304134eee90a35c66224878da002fb8f7e168d61065cdf22b7ed99c4ab56d3e134d13e7ef56e9a1f
|
7
|
+
data.tar.gz: f18fde6cdade727c95c9768a713a664dc4842a80afc8e4d464c2c44d02d78d30b4cf83930a0359b22f03772f2f9a464ed79227844995412ee8ee27024277161c
|
data/Gemfile.lock
CHANGED
@@ -1,16 +1,11 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
pseudo_date (0.
|
4
|
+
pseudo_date (0.2.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
-
byebug (3.1.2)
|
10
|
-
columnize (~> 0.8)
|
11
|
-
debugger-linecache (~> 1.2)
|
12
|
-
columnize (0.8.9)
|
13
|
-
debugger-linecache (1.2.0)
|
14
9
|
diff-lcs (1.2.5)
|
15
10
|
rspec (3.0.0)
|
16
11
|
rspec-core (~> 3.0.0)
|
@@ -29,6 +24,8 @@ PLATFORMS
|
|
29
24
|
ruby
|
30
25
|
|
31
26
|
DEPENDENCIES
|
32
|
-
byebug
|
33
27
|
pseudo_date!
|
34
28
|
rspec
|
29
|
+
|
30
|
+
BUNDLED WITH
|
31
|
+
2.1.4
|
data/lib/pseudo_date/parser.rb
CHANGED
@@ -1,110 +1,115 @@
|
|
1
1
|
require 'date'
|
2
|
-
|
2
|
+
require_relative 'pseudo_date'
|
3
|
+
|
4
|
+
class PseudoDate
|
5
|
+
class Parser
|
3
6
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
def self.parse(input)
|
8
|
-
date_hash = {}
|
9
|
-
# Minor Pre Cleanup
|
10
|
-
input.strip!; input.gsub!('~','')
|
11
|
-
|
12
|
-
date = parse_with_poro_date(input)
|
13
|
-
|
14
|
-
if date
|
15
|
-
date_hash = { :year => date.year.to_s, :month => date.month.to_s, :day => date.day.to_s }
|
16
|
-
else
|
17
|
-
year, month, day = parse_string(input)
|
18
|
-
date_hash = { :year => year, :month => month, :day => day }
|
19
|
-
end
|
7
|
+
AMERICAN_DATE_FORMAT = '%m/%d/%Y'
|
8
|
+
EUROPEAN_DATE_FORMAT = '%Y-%m-%d'
|
20
9
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
10
|
+
def self.parse(input)
|
11
|
+
date_hash = {}
|
12
|
+
# Minor Pre Cleanup
|
13
|
+
input.strip!; input.gsub!('~','')
|
14
|
+
|
15
|
+
date = parse_with_poro_date(input)
|
16
|
+
|
17
|
+
if date
|
18
|
+
date_hash = { :year => date.year.to_s, :month => date.month.to_s, :day => date.day.to_s }
|
25
19
|
else
|
26
|
-
|
20
|
+
year, month, day = parse_string(input)
|
21
|
+
date_hash = { :year => year, :month => month, :day => day }
|
27
22
|
end
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
23
|
+
|
24
|
+
# Post parsing cleanup
|
25
|
+
date_hash.each do |key, value|
|
26
|
+
date_hash[key] = if value.nil?
|
27
|
+
key.to_s == 'year' ? '0000' : '00'
|
28
|
+
else
|
29
|
+
date_hash[key] = value.to_s.strip
|
30
|
+
end
|
34
31
|
end
|
32
|
+
|
33
|
+
# Cleanup the single digit values
|
34
|
+
unless date_hash.empty?
|
35
|
+
date_hash.each do |key,value|
|
36
|
+
date_hash[key] = "0#{value}" if value.to_s.length == 1
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Two character years
|
41
|
+
if date_hash[:year].length == 2
|
42
|
+
date_hash[:year] = date_hash[:year].to_i > Date.today.year.to_s.slice(2..4).to_i ? "19#{date_hash[:year]}" : "20#{date_hash[:year]}"
|
43
|
+
end
|
44
|
+
|
45
|
+
# Attempt to correct some known OCR issues
|
46
|
+
if date_hash[:year].to_s.match('00') && date_hash[:year] != '0000'
|
47
|
+
date_hash[:year] = "2#{date_hash[:year].slice(1..3)}"
|
48
|
+
end
|
49
|
+
|
50
|
+
return date_hash.empty? ? nil : date_hash
|
35
51
|
end
|
36
52
|
|
37
|
-
|
38
|
-
if date_hash[:year].length == 2
|
39
|
-
date_hash[:year] = date_hash[:year].to_i > Date.today.year.to_s.slice(2..4).to_i ? "19#{date_hash[:year]}" : "20#{date_hash[:year]}"
|
40
|
-
end
|
41
|
-
|
42
|
-
# Attempt to correct some known OCR issues
|
43
|
-
if date_hash[:year].to_s.match('00') && date_hash[:year] != '0000'
|
44
|
-
date_hash[:year] = "2#{date_hash[:year].slice(1..3)}"
|
45
|
-
end
|
53
|
+
private
|
46
54
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
when /\// # Americans usually use a / to separate date pieces
|
59
|
-
Date.strptime(string, AMERICAN_DATE_FORMAT)
|
55
|
+
def self.parse_with_poro_date(string)
|
56
|
+
# If our date has 3 parts then let's try to parse it with Date::strptime
|
57
|
+
if string.split(/\/|-/).length < 3
|
58
|
+
case string
|
59
|
+
when /-/ # Europeans generally use hyphens to separate date pieces
|
60
|
+
Date.strptime(string, EUROPEAN_DATE_FORMAT)
|
61
|
+
when /\// # Americans usually use a / to separate date pieces
|
62
|
+
Date.strptime(string, AMERICAN_DATE_FORMAT)
|
63
|
+
end
|
64
|
+
else
|
65
|
+
nil # Not enough parts so just return nil
|
60
66
|
end
|
61
|
-
|
62
|
-
nil #
|
67
|
+
rescue
|
68
|
+
nil # We don't actually care why Date is complaining. We'll fall back to slower parsing later.
|
63
69
|
end
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
month,
|
70
|
+
|
71
|
+
def self.parse_string(input)
|
72
|
+
day, month, year = "00", "00", "0000"
|
73
|
+
if input.match('/') # 02/25/2008
|
74
|
+
date_array = input.split('/')
|
75
|
+
if date_array.length == 3
|
76
|
+
begin
|
77
|
+
parsed_date = Date.parse(self)
|
78
|
+
month, day, year = parsed_date.month, parsed_date.day, parsed_date.year
|
79
|
+
rescue
|
80
|
+
month, day, year = date_array
|
81
|
+
end
|
82
|
+
elsif date_array.length == 2
|
83
|
+
month, year = date_array
|
78
84
|
end
|
79
|
-
elsif
|
80
|
-
month,
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
month =
|
93
|
-
day = date_array[1]
|
85
|
+
elsif input.length == 8 && is_numeric?(input) # 20080225
|
86
|
+
year, month, day = input.slice(0..3), input.slice(4..5), input.slice(6..7)
|
87
|
+
elsif input.match('-') # 1985-09-25 or 02-25-2008
|
88
|
+
date_array = input.split('-')
|
89
|
+
year = date_array.select{ |part| part.length == 4 }.first
|
90
|
+
unless year.nil? || date_array.length != 3
|
91
|
+
if date_array.first == year
|
92
|
+
month = date_array.last
|
93
|
+
day = date_array[1]
|
94
|
+
else
|
95
|
+
month = date_array.first
|
96
|
+
day = date_array[1]
|
97
|
+
end
|
98
|
+
month, day = [day, month] if month.to_i > 12 && month.to_i > day.to_i
|
94
99
|
end
|
95
|
-
|
100
|
+
elsif input.length == 4 # 2004
|
101
|
+
year = input.to_s
|
102
|
+
elsif input.length == 2 # 85
|
103
|
+
year = (input.to_i > Date.today.year.to_s.slice(2..4).to_i) ? "19#{input}" : "20#{input}"
|
104
|
+
elsif input.match(/\w/) # Jun 23, 2004
|
105
|
+
begin
|
106
|
+
d = Date.parse(input)
|
107
|
+
year, month, day = d.year.to_s, d.month.to_s, d.day.to_s
|
108
|
+
rescue; end
|
96
109
|
end
|
97
|
-
|
98
|
-
year = input.to_s
|
99
|
-
elsif input.length == 2 # 85
|
100
|
-
year = (input.to_i > Date.today.year.to_s.slice(2..4).to_i) ? "19#{input}" : "20#{input}"
|
101
|
-
elsif input.match(/\w/) # Jun 23, 2004
|
102
|
-
begin
|
103
|
-
d = Date.parse(input)
|
104
|
-
year, month, day = d.year.to_s, d.month.to_s, d.day.to_s
|
105
|
-
rescue; end
|
110
|
+
return [year, month, day]
|
106
111
|
end
|
107
|
-
|
112
|
+
|
108
113
|
end
|
109
|
-
|
110
|
-
end
|
114
|
+
|
115
|
+
end
|
data/lib/pseudo_date/version.rb
CHANGED
data/pseudo_date.gemspec
CHANGED
@@ -17,7 +17,6 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
gem.license = 'MIT'
|
20
|
-
|
21
|
-
gem.add_development_dependency("rspec"
|
22
|
-
gem.add_development_dependency("byebug", "~> 0")
|
20
|
+
|
21
|
+
gem.add_development_dependency("rspec")
|
23
22
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,41 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pseudo_date
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick Tulskie
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: byebug
|
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
|
-
- - "~>"
|
24
|
+
- - ">="
|
39
25
|
- !ruby/object:Gem::Version
|
40
26
|
version: '0'
|
41
27
|
description: Date parser and container for partial or incomplete dates.
|
@@ -66,7 +52,7 @@ homepage: http://github.com/PatrickTulskie/pseudo_date
|
|
66
52
|
licenses:
|
67
53
|
- MIT
|
68
54
|
metadata: {}
|
69
|
-
post_install_message:
|
55
|
+
post_install_message:
|
70
56
|
rdoc_options: []
|
71
57
|
require_paths:
|
72
58
|
- lib
|
@@ -81,9 +67,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
67
|
- !ruby/object:Gem::Version
|
82
68
|
version: '0'
|
83
69
|
requirements: []
|
84
|
-
|
85
|
-
|
86
|
-
signing_key:
|
70
|
+
rubygems_version: 3.2.3
|
71
|
+
signing_key:
|
87
72
|
specification_version: 4
|
88
73
|
summary: Date parser and container for partial or incomplete dates.
|
89
74
|
test_files:
|