fuzzy-date 0.1.6 → 0.1.7

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: be67faf3dd4b030851460dba616f21e3cf0ba8da
4
- data.tar.gz: c865e0ef0a86ea0feef6c54138f487974e56056d
3
+ metadata.gz: f1c5f39aa71ab7653b430720262d166b8d42d425
4
+ data.tar.gz: 5849baad29c3d97756ea288a582e156391a3e646
5
5
  SHA512:
6
- metadata.gz: 1e7f5a0a1998319549eedcadd8c4fb22b394b08121d0fe7d3eb877517d7aa41cdf51acbfb2a30b92198d9bdd4f40118c9d4a2322978afd58355a53724cf44137
7
- data.tar.gz: 1f36b09a16547b6c13cf44b3da1a3b570739f3b58644b5b5f5d58af7acc13e643952280a3d37569ee3f3fafba963a13dac3eb892f3ffd64d8284ae06aefd869c
6
+ metadata.gz: cc59344c1eff14a1a46e10005a224e46bce57074baab089331b9af953cfcfa34e4cdad4f1ee34e60347dc67c4e99d87775a92f864080754076d9bff8e6349f42
7
+ data.tar.gz: d265085b4dd22b916ab4d954afd9dd93a59f5ab0bb7f52a3c25a25cd5fb7afd854ef089f26cbb6f935c6854a4f36a5025d766c973f23372211ff4a908ec45a8b
@@ -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.6'
6
+ VERSION = '0.1.7'
7
7
 
8
8
  def self.parse( date, euro = false )
9
9
  fuzzy_date = FuzzyDate.new date, euro
@@ -6,15 +6,15 @@ class FuzzyDate
6
6
  #
7
7
  # Possible incoming date formats:
8
8
  # * YYYY-MM-DD - starts with 3 or 4 digit year, and month and day may be 1 or 2 digits
9
- # * YYYY-MM - 3 or 4 digit year, then 1 or 2 digit month
10
- # * YYYY - 3 or 4 digit year
9
+ # * YYYY-MM - 3 or 4 digit year, then 1 or 2 digit month
10
+ # * YYYY - 3 or 4 digit year
11
11
  # * MM-DD-YYYY - 1 or 2 digit month, then 1 or 2 digit day, then 1 to 4 digit year
12
12
  # * DD-MM-YYYY - 1 or 2 digit day, then 1 or 2 digit month, then 1 to 4 digit year if euro is true
13
- # * MM-YYYY - 1 or 2 digit month, then 1 to 4 digit year
14
- # * DD-MMM - 1 or 2 digit day, then month name or abbreviation
15
- # * DD-MMM-YYYY - 1 or 2 digit day, then month name or abbreviation, then 1 to 4 digit year
16
- # * MMM-YYYY - month name or abbreviation, then 1 to 4 digit year
17
- # * MMM-DD-YYYY - month name or abbreviation, then 1 or 2 digit day, then 1 to 4 digit year
13
+ # * MM-YYYY - 1 or 2 digit month, then 1 to 4 digit year
14
+ # * DD-MMM - 1 or 2 digit day, then month name or abbreviation
15
+ # * DD-MMM-YYYY - 1 or 2 digit day, then month name or abbreviation, then 1 to 4 digit year
16
+ # * MMM-YYYY - month name or abbreviation, then 1 to 4 digit year
17
+ # * MMM-DD-YYYY - month name or abbreviation, then 1 or 2 digit day, then 1 to 4 digit year
18
18
  #
19
19
  # Notes:
20
20
  # - Commas are optional.
@@ -33,56 +33,46 @@ class FuzzyDate
33
33
  date = massage date
34
34
  @date_parts[ :fixed ] = date
35
35
 
36
- #- Takes care of YYYY
37
- if date =~ /^(\d{1,4})$/
38
- year = $1.to_i.to_s
39
- month = nil
40
- day = nil
41
-
42
- #- Takes care of YYYY-MM-DD and YYYY-MM
43
- elsif date =~ /^(\d{3,4})(?:-(\d{1,2})(?:-(\d{1,2}))?)?$/
44
- year = $1.to_i.to_s
45
- month = $2 ? $2.to_i.to_s : nil
46
- day = $3 ? $3.to_i.to_s : nil
47
-
48
- #- Takes care of DD-MM-YYYY
49
- elsif date =~ /^(\d{1,2})-(\d{1,2})-(\d{1,4})$/ and euro
50
- day = $1.to_i.to_s
51
- month = $2.to_i.to_s
52
- year = $3.to_i.to_s
53
-
54
- #- Takes care of MM-DD-YYYY
55
- elsif date =~ /^(\d{1,2})-(\d{1,2})-(\d{1,4})$/
56
- month = $1.to_i.to_s
57
- day = $2.to_i.to_s
58
- year = $3.to_i.to_s
59
-
60
- #- Takes care of MM-YYYY
61
- elsif date =~ /^(\d{1,2})-(\d{1,4})?$/
62
- month = $1.to_i.to_s
63
- day = nil
64
- year = $2.to_i.to_s
65
-
66
- #- Takes care of DD-MMM-YYYY and DD-MMM
67
- elsif date =~ /^(\d{1,2})(?:-(#{ @month_abbreviations.keys.join( '|' ) }).*?(?:-(\d{1,4}))?)?$/i
68
- month_text = $2.to_s.capitalize
69
- month = @month_names.key( @month_abbreviations[ month_text ] ).to_i.to_s
70
- day = $1.to_i.to_s
71
- year = $3 ? $3.to_i.to_s : nil
72
-
73
- #- Takes care of MMM-DD-YYYY
74
- elsif date =~ /^(#{ @month_abbreviations.keys.join( '|' ) }).*?-(\d{1,2})-(\d{1,4})$/i
75
- month_text = $1.to_s.capitalize
76
- month = @month_names.key( @month_abbreviations[ month_text ] ).to_i.to_s
77
- day = $2.to_i.to_s
78
- year = $3 ? $3.to_i.to_s : nil
79
-
80
- #- Takes care of MMM-YYYY and MMM
81
- elsif date =~ /^(#{ @month_abbreviations.keys.join( '|' ) }).*?(?:-(\d{1,4}))?$/i
82
- month_text = $1.to_s.capitalize
83
- month = @month_names.key( @month_abbreviations[ month_text ] ).to_i.to_s
84
- day = nil
85
- year = $2 ? $2.to_i.to_s : nil
36
+ year, month, day = nil
37
+
38
+ if date =~ @date_patterns[ :yyyy ]
39
+ year = stringify $1
40
+
41
+ elsif date =~ @date_patterns[ :yyyy_mm_dd_and_yyyy_mm ]
42
+ year = stringify $1
43
+ month = stringify $2 unless $2.nil?
44
+ day = stringify $3 unless $3.nil?
45
+
46
+ elsif date =~ @date_patterns[ :dd_mm_yyyy ] and euro
47
+ day = stringify $1
48
+ month = stringify $2
49
+ year = stringify $3
50
+
51
+ elsif date =~ @date_patterns[ :mm_dd_yyyy ]
52
+ month = stringify $1
53
+ day = stringify $2
54
+ year = stringify $3
55
+
56
+ elsif date =~ @date_patterns[ :mm_yyyy ]
57
+ month = stringify $1
58
+ year = stringify $2
59
+
60
+ elsif date =~ @date_patterns[ :dd_mmm_yyyy_and_dd_mmm ]
61
+ month_text = $2.to_s.capitalize
62
+ month = stringify @month_names.key( @month_abbreviations[ month_text ] )
63
+ day = stringify $1
64
+ year = stringify $3 unless $3.nil?
65
+
66
+ elsif date =~ @date_patterns[ :mmm_dd_yyyy ]
67
+ month_text = $1.to_s.capitalize
68
+ month = stringify @month_names.key( @month_abbreviations[ month_text ] )
69
+ day = stringify $2
70
+ year = stringify $3 unless $3.nil?
71
+
72
+ elsif date =~ @date_patterns[ :mmm_yyyy_and_mmm ]
73
+ month_text = $1.to_s.capitalize
74
+ month = stringify @month_names.key( @month_abbreviations[ month_text ] )
75
+ year = stringify $2 unless $2.nil?
86
76
 
87
77
  else
88
78
  raise ArgumentError.new( 'Cannot parse date.' )
@@ -91,7 +81,6 @@ class FuzzyDate
91
81
  @date_parts[ :year ] = year ? year.to_i : nil
92
82
  @date_parts[ :month ] = month ? month.to_i : nil
93
83
  @date_parts[ :day ] = day ? day.to_i : nil
94
- #return { :circa => "day: #{ day }, month: #{ month }, year: #{ year }" }
95
84
 
96
85
  #- Some error checking at this point
97
86
  if month.to_i > 13
@@ -163,4 +152,8 @@ class FuzzyDate
163
152
  date_in_parts.join '-'
164
153
  end
165
154
 
155
+ def stringify( capture )
156
+ capture.to_i.to_s
157
+ end
158
+
166
159
  end
@@ -79,6 +79,18 @@ class FuzzyDate
79
79
  'CE',
80
80
  'BCE'
81
81
  ]
82
+
83
+ @date_patterns = {
84
+ yyyy: /^(\d{1,4})$/,
85
+ yyyy_mm_dd_and_yyyy_mm: /^(\d{3,4})(?:-(\d{1,2})(?:-(\d{1,2}))?)?$/,
86
+ dd_mm_yyyy: /^(\d{1,2})-(\d{1,2})-(\d{1,4})$/,
87
+ mm_dd_yyyy: /^(\d{1,2})-(\d{1,2})-(\d{1,4})$/,
88
+ mm_yyyy: /^(\d{1,2})-(\d{1,4})?$/,
89
+ dd_mmm_yyyy_and_dd_mmm: /^(\d{1,2})(?:-(#{ @month_abbreviations.keys.join( '|' ) }).*?(?:-(\d{1,4}))?)?$/i,
90
+ mmm_dd_yyyy: /^(#{ @month_abbreviations.keys.join( '|' ) }).*?-(\d{1,2})-(\d{1,4})$/i,
91
+ mmm_yyyy_and_mmm: /^(#{ @month_abbreviations.keys.join( '|' ) }).*?(?:-(\d{1,4}))?$/i
92
+ }
93
+
82
94
  end
83
95
 
84
96
  def set_up_date_parts
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.6
4
+ version: 0.1.7
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-18 00:00:00.000000000 Z
11
+ date: 2014-04-20 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.
@@ -18,13 +18,13 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
- - README.md
22
21
  - LICENSE
22
+ - README.md
23
23
  - demo.rb
24
24
  - lib/fuzzy-date.rb
25
+ - lib/fuzzy-date/analyze.rb
25
26
  - lib/fuzzy-date/fuzzy-date.rb
26
27
  - lib/fuzzy-date/variables.rb
27
- - lib/fuzzy-date/analyze.rb
28
28
  homepage: https://github.com/davidcole/fuzzy-date
29
29
  licenses:
30
30
  - MIT
@@ -35,17 +35,17 @@ require_paths:
35
35
  - lib
36
36
  required_ruby_version: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  required_rubygems_version: !ruby/object:Gem::Requirement
42
42
  requirements:
43
- - - '>='
43
+ - - ">="
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
46
  requirements: []
47
47
  rubyforge_project:
48
- rubygems_version: 2.0.3
48
+ rubygems_version: 2.2.2
49
49
  signing_key:
50
50
  specification_version: 4
51
51
  summary: Provides a way to use partial and incomplete dates.