fuzzy-date 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fuzzy-date.rb +5 -149
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e05d588d34b5ef9a7d88a2349a33430ce52bf663
|
4
|
+
data.tar.gz: 77db67adfb77666190900c36f3940c34f590ae11
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7286ae86ea2dd0c5b716dc0601160412ee765f19450f8ec87eea802bd30d46aacb2c2ada53397e613de45898a12258e31cc3144fd5f71731c948c20b5e371aa9
|
7
|
+
data.tar.gz: 187e48e36faa6c7b3609b842b138b551dbe9a2da3764182ce573439b74fc6885c493e1acb118220c762ccce8c72d8e803a149e300161c7001fd5592e9789c033
|
data/lib/fuzzy-date.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require 'date'
|
2
1
|
|
3
2
|
require 'fuzzy-date/variables'
|
3
|
+
require 'fuzzy-date/fuzzy-date'
|
4
4
|
|
5
|
-
|
6
|
-
VERSION = '0.1.
|
5
|
+
class FuzzyDate
|
6
|
+
VERSION = '0.1.4'
|
7
7
|
|
8
8
|
# *Note*: This is only for single dates - not ranges.
|
9
9
|
#
|
@@ -26,151 +26,7 @@ module FuzzyDate
|
|
26
26
|
# - Dates may be prefixed by circa words (Circa, About, Abt).
|
27
27
|
|
28
28
|
def self.parse( date, euro = false )
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
return '' if date == ''
|
33
|
-
|
34
|
-
date_parts = set_up_date_parts date
|
35
|
-
|
36
|
-
date_in_parts = []
|
37
|
-
|
38
|
-
date_separator = Regexp.new DATE_SEPARATOR, true
|
39
|
-
|
40
|
-
#- Split the string
|
41
|
-
|
42
|
-
date_in_parts = date.split date_separator
|
43
|
-
date_in_parts.delete_if { |d| d.to_s.empty? }
|
44
|
-
if date_in_parts.first.match Regexp.new( @circa_words.join( '|' ), true )
|
45
|
-
date_parts[ :circa ] = true
|
46
|
-
date_in_parts.shift
|
47
|
-
end
|
48
|
-
if date_in_parts.last.match Regexp.new( @era_words.join( '|' ), true )
|
49
|
-
date_parts[ :era ] = date_in_parts.pop.upcase.strip
|
50
|
-
end
|
51
|
-
|
52
|
-
date = date_in_parts.join '-'
|
53
|
-
date_parts[ :fixed ] = date
|
54
|
-
|
55
|
-
#- Takes care of YYYY
|
56
|
-
if date =~ /^(\d{1,4})$/
|
57
|
-
year = $1.to_i.to_s
|
58
|
-
month = nil
|
59
|
-
day = nil
|
60
|
-
|
61
|
-
#- Takes care of YYYY-MM-DD and YYYY-MM
|
62
|
-
elsif date =~ /^(\d{3,4})(?:-(\d{1,2})(?:-(\d{1,2}))?)?$/
|
63
|
-
year = $1.to_i.to_s
|
64
|
-
month = $2 ? $2.to_i.to_s : nil
|
65
|
-
day = $3 ? $3.to_i.to_s : nil
|
66
|
-
|
67
|
-
#- Takes care of DD-MM-YYYY
|
68
|
-
elsif date =~ /^(\d{1,2})-(\d{1,2})-(\d{1,4})$/ and euro
|
69
|
-
day = $1.to_i.to_s
|
70
|
-
month = $2.to_i.to_s
|
71
|
-
year = $3.to_i.to_s
|
72
|
-
|
73
|
-
#- Takes care of MM-DD-YYYY
|
74
|
-
elsif date =~ /^(\d{1,2})-(\d{1,2})-(\d{1,4})$/
|
75
|
-
month = $1.to_i.to_s
|
76
|
-
day = $2.to_i.to_s
|
77
|
-
year = $3.to_i.to_s
|
78
|
-
|
79
|
-
#- Takes care of MM-YYYY
|
80
|
-
elsif date =~ /^(\d{1,2})-(\d{1,4})?$/
|
81
|
-
month = $1.to_i.to_s
|
82
|
-
day = nil
|
83
|
-
year = $2.to_i.to_s
|
84
|
-
|
85
|
-
#- Takes care of DD-MMM-YYYY and DD-MMM
|
86
|
-
elsif date =~ /^(\d{1,2})(?:-(#{ @month_abbreviations.keys.join( '|' ) }).*?(?:-(\d{1,4}))?)?$/i
|
87
|
-
month_text = $2.to_s.capitalize
|
88
|
-
month = @month_names.key( @month_abbreviations[ month_text ] ).to_i.to_s
|
89
|
-
day = $1.to_i.to_s
|
90
|
-
year = $3 ? $3.to_i.to_s : nil
|
91
|
-
|
92
|
-
#- Takes care of MMM-DD-YYYY
|
93
|
-
elsif date =~ /^(#{ @month_abbreviations.keys.join( '|' ) }).*?-(\d{1,2})-(\d{1,4})$/i
|
94
|
-
month_text = $1.to_s.capitalize
|
95
|
-
month = @month_names.key( @month_abbreviations[ month_text ] ).to_i.to_s
|
96
|
-
day = $2.to_i.to_s
|
97
|
-
year = $3 ? $3.to_i.to_s : nil
|
98
|
-
|
99
|
-
#- Takes care of MMM-YYYY and MMM
|
100
|
-
elsif date =~ /^(#{ @month_abbreviations.keys.join( '|' ) }).*?(?:-(\d{1,4}))?$/i
|
101
|
-
month_text = $1.to_s.capitalize
|
102
|
-
month = @month_names.key( @month_abbreviations[ month_text ] ).to_i.to_s
|
103
|
-
day = nil
|
104
|
-
year = $2 ? $2.to_i.to_s : nil
|
105
|
-
|
106
|
-
else
|
107
|
-
raise ArgumentError.new( 'Cannot parse date.' )
|
108
|
-
end
|
109
|
-
|
110
|
-
date_parts[ :year ] = year ? year.to_i : nil
|
111
|
-
date_parts[ :month ] = month ? month.to_i : nil
|
112
|
-
date_parts[ :day ] = day ? day.to_i : nil
|
113
|
-
#return { :circa => "day: #{ day }, month: #{ month }, year: #{ year }" }
|
114
|
-
|
115
|
-
#- Some error checking at this point
|
116
|
-
if month.to_i > 13
|
117
|
-
raise ArgumentError.new( 'Month cannot be greater than 12.' )
|
118
|
-
elsif month and day and day.to_i > @days_in_month[ month.to_i ]
|
119
|
-
unless month.to_i == 2 and year and Date.parse( '1/1/' + year ).leap? and day.to_i == 29
|
120
|
-
raise ArgumentError.new( 'Too many days in this month.' )
|
121
|
-
end
|
122
|
-
elsif month and month.to_i < 1
|
123
|
-
raise ArgumentError.new( 'Month cannot be less than 1.' )
|
124
|
-
elsif day and day.to_i < 1
|
125
|
-
raise ArgumentError.new( 'Day cannot be less than 1.' )
|
126
|
-
end
|
127
|
-
|
128
|
-
month_name = @month_names[ month.to_i ]
|
129
|
-
date_parts[ :month_name ] = month_name
|
130
|
-
|
131
|
-
# ----------------------------------------------------------------------
|
132
|
-
|
133
|
-
show_era = ' ' + date_parts[ :era ]
|
134
|
-
show_circa = date_parts[ :circa ] == true ? 'About ' : ''
|
135
|
-
|
136
|
-
if year and month and day
|
137
|
-
date_parts[ :short ] = show_circa + month + '/' + day + '/' + year + show_era
|
138
|
-
date_parts[ :long ] = show_circa + month_name + ' ' + day + ', ' + year + show_era
|
139
|
-
modified_long = show_circa + month_name + ' ' + day + ', ' + year.rjust( 4, "0" ) + show_era
|
140
|
-
date_parts[ :full ] = show_circa + Date.parse( modified_long ).strftime( '%A,' ) + Date.parse( day + ' ' + month_name + ' ' + year.rjust( 4, "0" ) ).strftime( ' %B %-1d, %Y' ) + show_era
|
141
|
-
elsif year and month
|
142
|
-
date_parts[ :short ] = show_circa + month + '/' + year + show_era
|
143
|
-
date_parts[ :long ] = show_circa + month_name + ', ' + year + show_era
|
144
|
-
date_parts[ :full ] = date_parts[ :long ]
|
145
|
-
elsif month and day
|
146
|
-
month_text = @month_abbreviations.key(month_text) || month_text
|
147
|
-
date_parts[ :short ] = show_circa + day + '-' + month_text
|
148
|
-
date_parts[ :long ] = show_circa + day + ' ' + month_name
|
149
|
-
date_parts[ :full ] = date_parts[ :long ]
|
150
|
-
elsif year
|
151
|
-
date_parts[ :short ] = show_circa + year + show_era
|
152
|
-
date_parts[ :long ] = date_parts[ :short ]
|
153
|
-
date_parts[ :full ] = date_parts[ :long ]
|
154
|
-
end
|
155
|
-
|
156
|
-
return date_parts
|
157
|
-
|
158
|
-
end
|
159
|
-
|
160
|
-
private
|
161
|
-
|
162
|
-
def self.clean_parameter( date )
|
163
|
-
date.to_s.strip if date.respond_to? :to_s
|
164
|
-
end
|
165
|
-
|
166
|
-
def self.set_up_date_parts( date )
|
167
|
-
date_parts = {}
|
168
|
-
date_parts[ :original ] = date
|
169
|
-
date_parts[ :circa ] = false
|
170
|
-
date_parts[ :year ] = nil
|
171
|
-
date_parts[ :month ] = nil
|
172
|
-
date_parts[ :day ] = nil
|
173
|
-
date_parts[ :era ] = 'AD'
|
174
|
-
date_parts
|
29
|
+
fuzzy_date = FuzzyDate.new date, euro
|
30
|
+
fuzzy_date.to_hash
|
175
31
|
end
|
176
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fuzzy-date
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Cole
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: The fuzzy-date gem provides a way to parse and use incomplete dates,
|
14
14
|
like those found in history or genealogy.
|