fuzzy-date 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e05d588d34b5ef9a7d88a2349a33430ce52bf663
4
- data.tar.gz: 77db67adfb77666190900c36f3940c34f590ae11
3
+ metadata.gz: c03332e5af7f98fd582adced4d612eacc1dbfcc2
4
+ data.tar.gz: 0f0a9d6e3330d4a4008673acedcfa98111573302
5
5
  SHA512:
6
- metadata.gz: 7286ae86ea2dd0c5b716dc0601160412ee765f19450f8ec87eea802bd30d46aacb2c2ada53397e613de45898a12258e31cc3144fd5f71731c948c20b5e371aa9
7
- data.tar.gz: 187e48e36faa6c7b3609b842b138b551dbe9a2da3764182ce573439b74fc6885c493e1acb118220c762ccce8c72d8e803a149e300161c7001fd5592e9789c033
6
+ metadata.gz: 4c369ee5f9e83714d4f8c99b904cb146c64b5ac839b6e5477e49299c6ab6872dc067cb0e1704eac89cd724c8e6ad717ad96b855379baa094256bea95a4b223b6
7
+ data.tar.gz: c8738c3c79385f556ca2d06f2b87a8018974bff04b2b8cbb272210f48defe248f80f828277c2d399cff7237b17b5a41b401911932e4b8a294247cf30e6c4528f
data/README.md CHANGED
@@ -12,7 +12,7 @@ With fuzzy-date, when you parse an incomplete date, you'll be given a hash of in
12
12
 
13
13
  ## Contributing
14
14
 
15
- If you'd like to hack on FuzzyDate - and we home you do! - start by forking the repo on GitHub:
15
+ If you'd like to hack on FuzzyDate - and we hope you do! - start by forking the repo on GitHub:
16
16
 
17
17
  https://github.com/davidcole-fuzzydate
18
18
 
data/demo.rb CHANGED
@@ -3,4 +3,3 @@ require( 'fuzzy-date' )
3
3
 
4
4
  fuzzy_date = FuzzyDate::parse( '15 March 1971' )
5
5
  puts fuzzy_date.inspect
6
-
@@ -3,7 +3,7 @@ require 'fuzzy-date/variables'
3
3
  require 'fuzzy-date/fuzzy-date'
4
4
 
5
5
  class FuzzyDate
6
- VERSION = '0.1.4'
6
+ VERSION = '0.1.5'
7
7
 
8
8
  # *Note*: This is only for single dates - not ranges.
9
9
  #
@@ -0,0 +1,153 @@
1
+ require 'date'
2
+
3
+ class FuzzyDate
4
+ def initialize( date, euro = false )
5
+ setup
6
+ analyze date, euro
7
+ end
8
+
9
+ def analyze( date, euro )
10
+
11
+ date = clean_parameter date
12
+
13
+ @date_parts[ :original ] = date
14
+
15
+ date = massage date
16
+ @date_parts[ :fixed ] = date
17
+
18
+ #- Takes care of YYYY
19
+ if date =~ /^(\d{1,4})$/
20
+ year = $1.to_i.to_s
21
+ month = nil
22
+ day = nil
23
+
24
+ #- Takes care of YYYY-MM-DD and YYYY-MM
25
+ elsif date =~ /^(\d{3,4})(?:-(\d{1,2})(?:-(\d{1,2}))?)?$/
26
+ year = $1.to_i.to_s
27
+ month = $2 ? $2.to_i.to_s : nil
28
+ day = $3 ? $3.to_i.to_s : nil
29
+
30
+ #- Takes care of DD-MM-YYYY
31
+ elsif date =~ /^(\d{1,2})-(\d{1,2})-(\d{1,4})$/ and euro
32
+ day = $1.to_i.to_s
33
+ month = $2.to_i.to_s
34
+ year = $3.to_i.to_s
35
+
36
+ #- Takes care of MM-DD-YYYY
37
+ elsif date =~ /^(\d{1,2})-(\d{1,2})-(\d{1,4})$/
38
+ month = $1.to_i.to_s
39
+ day = $2.to_i.to_s
40
+ year = $3.to_i.to_s
41
+
42
+ #- Takes care of MM-YYYY
43
+ elsif date =~ /^(\d{1,2})-(\d{1,4})?$/
44
+ month = $1.to_i.to_s
45
+ day = nil
46
+ year = $2.to_i.to_s
47
+
48
+ #- Takes care of DD-MMM-YYYY and DD-MMM
49
+ elsif date =~ /^(\d{1,2})(?:-(#{ @month_abbreviations.keys.join( '|' ) }).*?(?:-(\d{1,4}))?)?$/i
50
+ month_text = $2.to_s.capitalize
51
+ month = @month_names.key( @month_abbreviations[ month_text ] ).to_i.to_s
52
+ day = $1.to_i.to_s
53
+ year = $3 ? $3.to_i.to_s : nil
54
+
55
+ #- Takes care of MMM-DD-YYYY
56
+ elsif date =~ /^(#{ @month_abbreviations.keys.join( '|' ) }).*?-(\d{1,2})-(\d{1,4})$/i
57
+ month_text = $1.to_s.capitalize
58
+ month = @month_names.key( @month_abbreviations[ month_text ] ).to_i.to_s
59
+ day = $2.to_i.to_s
60
+ year = $3 ? $3.to_i.to_s : nil
61
+
62
+ #- Takes care of MMM-YYYY and MMM
63
+ elsif date =~ /^(#{ @month_abbreviations.keys.join( '|' ) }).*?(?:-(\d{1,4}))?$/i
64
+ month_text = $1.to_s.capitalize
65
+ month = @month_names.key( @month_abbreviations[ month_text ] ).to_i.to_s
66
+ day = nil
67
+ year = $2 ? $2.to_i.to_s : nil
68
+
69
+ else
70
+ raise ArgumentError.new( 'Cannot parse date.' )
71
+ end
72
+
73
+ @date_parts[ :year ] = year ? year.to_i : nil
74
+ @date_parts[ :month ] = month ? month.to_i : nil
75
+ @date_parts[ :day ] = day ? day.to_i : nil
76
+ #return { :circa => "day: #{ day }, month: #{ month }, year: #{ year }" }
77
+
78
+ #- Some error checking at this point
79
+ if month.to_i > 13
80
+ raise ArgumentError.new( 'Month cannot be greater than 12.' )
81
+ elsif month and day and day.to_i > @days_in_month[ month.to_i ]
82
+ unless month.to_i == 2 and year and Date.parse( '1/1/' + year ).leap? and day.to_i == 29
83
+ raise ArgumentError.new( 'Too many days in this month.' )
84
+ end
85
+ elsif month and month.to_i < 1
86
+ raise ArgumentError.new( 'Month cannot be less than 1.' )
87
+ elsif day and day.to_i < 1
88
+ raise ArgumentError.new( 'Day cannot be less than 1.' )
89
+ end
90
+
91
+ month_name = @month_names[ month.to_i ]
92
+ @date_parts[ :month_name ] = month_name
93
+
94
+ # ----------------------------------------------------------------------
95
+
96
+ show_era = ' ' + @date_parts[ :era ]
97
+ show_circa = @date_parts[ :circa ] == true ? 'About ' : ''
98
+
99
+ if year and month and day
100
+ @date_parts[ :short ] = show_circa + month + '/' + day + '/' + year + show_era
101
+ @date_parts[ :long ] = show_circa + month_name + ' ' + day + ', ' + year + show_era
102
+ modified_long = show_circa + month_name + ' ' + day + ', ' + year.rjust( 4, "0" ) + show_era
103
+ @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
104
+ elsif year and month
105
+ @date_parts[ :short ] = show_circa + month + '/' + year + show_era
106
+ @date_parts[ :long ] = show_circa + month_name + ', ' + year + show_era
107
+ @date_parts[ :full ] = @date_parts[ :long ]
108
+ elsif month and day
109
+ month_text = @month_abbreviations.key(month_text) || month_text
110
+ @date_parts[ :short ] = show_circa + day + '-' + month_text
111
+ @date_parts[ :long ] = show_circa + day + ' ' + month_name
112
+ @date_parts[ :full ] = @date_parts[ :long ]
113
+ elsif year
114
+ @date_parts[ :short ] = show_circa + year + show_era
115
+ @date_parts[ :long ] = @date_parts[ :short ]
116
+ @date_parts[ :full ] = @date_parts[ :long ]
117
+ end
118
+
119
+ @date_parts
120
+
121
+ end
122
+
123
+ def to_hash
124
+ @date_parts
125
+ end
126
+
127
+ private
128
+
129
+ def clean_parameter( date )
130
+ date.to_s.strip if date.respond_to? :to_s
131
+ end
132
+
133
+ def massage( date )
134
+
135
+ date_in_parts = []
136
+
137
+ date_separator = Regexp.new DATE_SEPARATOR, true
138
+
139
+ #- Split the string
140
+
141
+ date_in_parts = date.split date_separator
142
+ date_in_parts.delete_if { |d| d.to_s.empty? }
143
+ if date_in_parts.first.match Regexp.new( @circa_words.join( '|' ), true )
144
+ @date_parts[ :circa ] = true
145
+ date_in_parts.shift
146
+ end
147
+ if date_in_parts.last.match Regexp.new( @era_words.join( '|' ), true )
148
+ @date_parts[ :era ] = date_in_parts.pop.upcase.strip
149
+ end
150
+
151
+ date_in_parts.join '-'
152
+ end
153
+ end
@@ -0,0 +1,94 @@
1
+ class FuzzyDate
2
+ DATE_SEPARATOR = '[^A-Za-z0-9]'
3
+
4
+ private
5
+
6
+ def setup
7
+
8
+ @date_parts = set_up_date_parts
9
+
10
+ @month_names = {
11
+ 1 => 'January',
12
+ 2 => 'February',
13
+ 3 => 'March',
14
+ 4 => 'April',
15
+ 5 => 'May',
16
+ 6 => 'June',
17
+ 7 => 'July',
18
+ 8 => 'August',
19
+ 9 => 'September',
20
+ 10 => 'October',
21
+ 11 => 'November',
22
+ 12 => 'December'
23
+ }
24
+
25
+ @month_abbreviations = {
26
+ 'Jan' => 'January',
27
+ 'Feb' => 'February',
28
+ 'Mar' => 'March',
29
+ 'Apr' => 'April',
30
+ 'May' => 'May',
31
+ 'Jun' => 'June',
32
+ 'Jul' => 'July',
33
+ 'Aug' => 'August',
34
+ 'Sep' => 'September',
35
+ 'Oct' => 'October',
36
+ 'Nov' => 'November',
37
+ 'Dec' => 'December'
38
+ }
39
+
40
+ @days_in_month = {
41
+ 1 => 31,
42
+ 2 => 28,
43
+ 3 => 31,
44
+ 4 => 30,
45
+ 5 => 31,
46
+ 6 => 30,
47
+ 7 => 31,
48
+ 8 => 31,
49
+ 9 => 30,
50
+ 10 => 31,
51
+ 11 => 30,
52
+ 12 => 31
53
+ }
54
+
55
+ @range_words = [
56
+ 'Between',
57
+ 'Bet',
58
+ 'Bet.',
59
+ 'From'
60
+ ]
61
+
62
+ @middle_range_words = [
63
+ # '-', - Not used because it is more commonly used as a delimiter
64
+ 'To',
65
+ 'And'
66
+ ]
67
+
68
+ @circa_words = [
69
+ 'Circa',
70
+ 'About',
71
+ 'Abt',
72
+ 'Abt.',
73
+ '~'
74
+ ]
75
+
76
+ @era_words = [
77
+ 'AD',
78
+ 'BC',
79
+ 'CE',
80
+ 'BCE'
81
+ ]
82
+ end
83
+
84
+ def set_up_date_parts
85
+ @date_parts = {}
86
+ @date_parts[ :original ] = nil
87
+ @date_parts[ :circa ] = false
88
+ @date_parts[ :year ] = nil
89
+ @date_parts[ :month ] = nil
90
+ @date_parts[ :day ] = nil
91
+ @date_parts[ :era ] = 'AD'
92
+ @date_parts
93
+ end
94
+ 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
4
+ version: 0.1.5
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-15 00:00:00.000000000 Z
11
+ date: 2014-04-17 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.
@@ -22,6 +22,8 @@ files:
22
22
  - LICENSE
23
23
  - demo.rb
24
24
  - lib/fuzzy-date.rb
25
+ - lib/fuzzy-date/fuzzy-date.rb
26
+ - lib/fuzzy-date/variables.rb
25
27
  homepage: https://github.com/davidcole/fuzzy-date
26
28
  licenses:
27
29
  - MIT
@@ -42,8 +44,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
44
  version: '0'
43
45
  requirements: []
44
46
  rubyforge_project:
45
- rubygems_version: 2.1.11
47
+ rubygems_version: 2.0.3
46
48
  signing_key:
47
49
  specification_version: 4
48
- summary: fuzzy-date provides a way to use partial and incomplete dates.
50
+ summary: Provides a way to use partial and incomplete dates.
49
51
  test_files: []