chronic-davispuh 0.10.2.v0.1da32066b3f46f2506b3471e39557497e34afa27

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.travis.yml +10 -0
  4. data/Gemfile +3 -0
  5. data/HISTORY.md +243 -0
  6. data/LICENSE +21 -0
  7. data/README.md +185 -0
  8. data/Rakefile +68 -0
  9. data/chronic.gemspec +27 -0
  10. data/lib/chronic.rb +122 -0
  11. data/lib/chronic/arrow.rb +270 -0
  12. data/lib/chronic/date.rb +272 -0
  13. data/lib/chronic/definition.rb +208 -0
  14. data/lib/chronic/dictionary.rb +36 -0
  15. data/lib/chronic/handler.rb +44 -0
  16. data/lib/chronic/handlers/anchor.rb +65 -0
  17. data/lib/chronic/handlers/arrow.rb +84 -0
  18. data/lib/chronic/handlers/date.rb +270 -0
  19. data/lib/chronic/handlers/date_time.rb +72 -0
  20. data/lib/chronic/handlers/general.rb +130 -0
  21. data/lib/chronic/handlers/narrow.rb +54 -0
  22. data/lib/chronic/handlers/time.rb +167 -0
  23. data/lib/chronic/handlers/time_zone.rb +50 -0
  24. data/lib/chronic/objects/anchor_object.rb +263 -0
  25. data/lib/chronic/objects/arrow_object.rb +27 -0
  26. data/lib/chronic/objects/date_object.rb +164 -0
  27. data/lib/chronic/objects/date_time_object.rb +64 -0
  28. data/lib/chronic/objects/handler_object.rb +81 -0
  29. data/lib/chronic/objects/narrow_object.rb +85 -0
  30. data/lib/chronic/objects/time_object.rb +96 -0
  31. data/lib/chronic/objects/time_zone_object.rb +27 -0
  32. data/lib/chronic/parser.rb +154 -0
  33. data/lib/chronic/span.rb +32 -0
  34. data/lib/chronic/tag.rb +84 -0
  35. data/lib/chronic/tags/day_name.rb +34 -0
  36. data/lib/chronic/tags/day_portion.rb +33 -0
  37. data/lib/chronic/tags/day_special.rb +30 -0
  38. data/lib/chronic/tags/grabber.rb +29 -0
  39. data/lib/chronic/tags/keyword.rb +63 -0
  40. data/lib/chronic/tags/month_name.rb +39 -0
  41. data/lib/chronic/tags/ordinal.rb +52 -0
  42. data/lib/chronic/tags/pointer.rb +28 -0
  43. data/lib/chronic/tags/rational.rb +35 -0
  44. data/lib/chronic/tags/scalar.rb +101 -0
  45. data/lib/chronic/tags/season_name.rb +31 -0
  46. data/lib/chronic/tags/separator.rb +130 -0
  47. data/lib/chronic/tags/sign.rb +35 -0
  48. data/lib/chronic/tags/time_special.rb +34 -0
  49. data/lib/chronic/tags/time_zone.rb +56 -0
  50. data/lib/chronic/tags/unit.rb +174 -0
  51. data/lib/chronic/time.rb +141 -0
  52. data/lib/chronic/time_zone.rb +80 -0
  53. data/lib/chronic/token.rb +61 -0
  54. data/lib/chronic/token_group.rb +271 -0
  55. data/lib/chronic/tokenizer.rb +42 -0
  56. data/lib/chronic/version.rb +3 -0
  57. data/test/helper.rb +12 -0
  58. data/test/test_chronic.rb +190 -0
  59. data/test/test_daylight_savings.rb +98 -0
  60. data/test/test_handler.rb +113 -0
  61. data/test/test_parsing.rb +1520 -0
  62. data/test/test_span.rb +23 -0
  63. data/test/test_token.rb +31 -0
  64. metadata +218 -0
@@ -0,0 +1,36 @@
1
+ require 'chronic/definition'
2
+
3
+ module Chronic
4
+ # A collection of definitions
5
+ class Dictionary
6
+ attr_reader :defined_items, :options
7
+
8
+ def initialize(options = {})
9
+ @options = options
10
+ @defined_items = []
11
+ end
12
+
13
+ # returns a hash of each word's Definitions
14
+ def definitions
15
+ defined_items.each_with_object({}) do |word, defs|
16
+ word_type = "#{word.to_s.split('_').map(&:capitalize).join + 'Definitions'}"
17
+ defs[word] = Chronic.const_get(word_type).new(options).definitions
18
+ end
19
+ end
20
+ end
21
+
22
+ class SpanDictionary < Dictionary
23
+ # Collection of SpanDefinitions
24
+ def initialize(options = {})
25
+ super
26
+ @defined_items = [:time, :date, :short_date, :timezone, :date_time, :anchor, :arrow, :narrow, :endian]
27
+ end
28
+
29
+ # returns the definitions of a specific subclass of SpanDefinitions
30
+ # SpanDefinition#definitions returns an Hash of Handler instances
31
+ # arguments should come in as symbols
32
+ def [](handler_type=:symbol)
33
+ definitions[handler_type]
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,44 @@
1
+ module Chronic
2
+ class Handler
3
+ # tokens - An Array of tokens to process.
4
+ # token_index - Index from which token to start matching from.
5
+ # pattern - An Array of patterns to check against. (eg. [ScalarDay, [SeparatorSpace, SeparatorDash, nil], MonthName])
6
+ #
7
+ # Returns true if a match is found.
8
+ def self.match(tokens, token_index, pattern)
9
+ count = 0
10
+ index = 0
11
+ pattern.each do |elements|
12
+ elements = [elements] unless elements.is_a?(Array)
13
+ optional = elements.include?(:optional)
14
+ dont_match = elements.include?(:none)
15
+ count += 1
16
+ match = 0
17
+ elements.each_index do |i|
18
+ if elements[i].is_a?(Class) and self.tags_match?(elements[i], tokens, token_index + index)
19
+ match += 1
20
+ index += 1
21
+ break unless dont_match
22
+ elsif i + 1 < elements.count
23
+ next
24
+ else
25
+ if optional or (dont_match and match < elements.count - 1)
26
+ count -= 1
27
+ index -= match
28
+ else
29
+ return false
30
+ end
31
+ end
32
+ end
33
+ end
34
+ return false if index != count
35
+ true
36
+ end
37
+
38
+ def self.tags_match?(klass, tokens, token_index)
39
+ return !tokens[token_index].get_tag(klass).nil? if tokens[token_index]
40
+ false
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,65 @@
1
+ require 'chronic/handlers/general'
2
+
3
+ module Chronic
4
+ module AnchorHandlers
5
+ include GeneralHandlers
6
+ # Handle grabber/time_special
7
+ # formats: gr ts
8
+ def handle_gr_ts
9
+ handle_gr
10
+ handle_possible(SeparatorSpace)
11
+ handle_ts
12
+ end
13
+
14
+ # Handle grabber/day-name
15
+ # formats: gr dn
16
+ def handle_gr_dn
17
+ handle_gr
18
+ next_tag
19
+ handle_dn
20
+ end
21
+
22
+ # Handle grabber/month-name
23
+ # formats: gr mn
24
+ def handle_gr_mn
25
+ handle_gr
26
+ next_tag
27
+ handle_mn
28
+ end
29
+
30
+ # Handle grabber/season-name
31
+ # formats: gr sn
32
+ def handle_gr_sn
33
+ handle_gr
34
+ next_tag
35
+ handle_sn
36
+ end
37
+
38
+ # Handle grabber/unit
39
+ # formats: gr u
40
+ def handle_gr_u
41
+ handle_gr
42
+ next_tag
43
+ handle_u
44
+ end
45
+
46
+ # Handle keyword-in/scalar/unit
47
+ # formats: in s u
48
+ def handle_in_s_u
49
+ next_tag
50
+ next_tag
51
+ handle_s
52
+ next_tag
53
+ handle_u
54
+ end
55
+
56
+ # Handle grabber/keyword-q/scalar
57
+ # formats: gr Qs
58
+ def handle_gr_q_s
59
+ handle_gr
60
+ next_tag
61
+ handle_q_s
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,84 @@
1
+ require 'chronic/handlers/general'
2
+
3
+ module Chronic
4
+ module ArrowHandlers
5
+ include GeneralHandlers
6
+ # Handle pointer
7
+ # formats: p
8
+ def handle_p
9
+ @pointer = @tokens[@index].get_tag(Pointer).type
10
+ next_tag
11
+ end
12
+
13
+ # Handle scalar/unit
14
+ # formats: su, s u
15
+ def handle_s_u
16
+ handle_s
17
+ handle_possible(SeparatorSpace)
18
+ handle_u
19
+ end
20
+
21
+ # Handle unit/pointer
22
+ # formats: su, s u
23
+ def handle_u_p
24
+ handle_u
25
+ next_tag
26
+ handle_p
27
+ @count = 1
28
+ end
29
+
30
+ # Handle scalar/pointer
31
+ # formats: s p
32
+ def handle_s_p
33
+ handle_s
34
+ next_tag
35
+ handle_p
36
+ @unit = :minute
37
+ end
38
+
39
+ # Handle rational/pointer
40
+ # formats: r p
41
+ def handle_r_p
42
+ handle_r
43
+ next_tag
44
+ handle_p
45
+ @count = (@count * 60).to_i
46
+ @unit = :minute
47
+ end
48
+
49
+ # Handle pointer/scalar/unit
50
+ # formats: p su, s u
51
+ def handle_p_s_u
52
+ handle_p
53
+ handle_possible(SeparatorSpace)
54
+ handle_s_u
55
+ end
56
+
57
+ # Handle scalar/unit/pointer
58
+ # formats: su p, s u p
59
+ def handle_s_u_p
60
+ handle_s_u
61
+ next_tag
62
+ handle_p
63
+ end
64
+
65
+ # Handle scalar/day-name
66
+ # formats: s dn
67
+ def handle_s_dn
68
+ handle_s
69
+ handle_possible(SeparatorSpace)
70
+ handle_dn
71
+ @special = @wday
72
+ @unit = :wday
73
+ end
74
+
75
+ # Handle scalar/day-name/pointer
76
+ # formats: s dn p
77
+ def handle_s_dn_p
78
+ handle_s_dn
79
+ next_tag
80
+ handle_p
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,270 @@
1
+ require 'chronic/handlers/general'
2
+
3
+ module Chronic
4
+ module DateHandlers
5
+ include GeneralHandlers
6
+
7
+ # Handle scalar-day
8
+ # formats: dd
9
+ def handle_sd
10
+ @day = @tokens[@index].get_tag(ScalarDay).type
11
+ @have_day = true
12
+ @index += 1
13
+ handle_possible(SeparatorComma)
14
+ @precision = :day
15
+ end
16
+
17
+ # Handle scalar-month
18
+ # formats: mm
19
+ def handle_sm
20
+ @month = @tokens[@index].get_tag(ScalarMonth).type
21
+ @have_month = true
22
+ @index += 1
23
+ @precision = :month
24
+ end
25
+
26
+ # Handle ordinal-day
27
+ # formats: dd(st|nd|rd|th),?
28
+ def handle_od
29
+ @day = @tokens[@index].get_tag(OrdinalDay).type
30
+ @have_day = true
31
+ @index += 1
32
+ handle_possible(SeparatorComma)
33
+ @precision = :day
34
+ end
35
+
36
+ # Handle ordinal-month
37
+ # formats: mm(st|nd|rd|th)
38
+ def handle_om
39
+ @month = @tokens[@index].get_tag(OrdinalMonth).type
40
+ @have_month = true
41
+ @index += 1
42
+ @precision = :month
43
+ end
44
+
45
+
46
+
47
+ # Handle scalar-day/month-name
48
+ # formats: dd mn, dd-mn
49
+ def handle_sd_mn
50
+ handle_sd
51
+ handle_possible(SeparatorDash)
52
+ handle_mn
53
+ @precision = :day
54
+ end
55
+
56
+ # Handle scalar-day/month-name/scalar-year
57
+ # formats: dd mn yyyy, dd-mn-yyyy
58
+ def handle_sd_mn_sy
59
+ handle_sd_mn
60
+ handle_possible(SeparatorDash)
61
+ handle_sy
62
+ @precision = :day
63
+ end
64
+
65
+ # Handle scalar-day/scalar-month
66
+ # formats: dd/mm, dd.mm
67
+ def handle_sd_sm
68
+ handle_sd
69
+ next_tag
70
+ handle_sm
71
+ @precision = :day
72
+ end
73
+
74
+ # Handle scalar-day/scalar-month/scalar-year
75
+ # formats: dd/mm/yyyy, dd.mm.yyyy
76
+ def handle_sd_sm_sy
77
+ handle_sd_sm
78
+ next_tag
79
+ handle_sy
80
+ @precision = :day
81
+ end
82
+
83
+
84
+
85
+ # Handle ordinal-day/month-name
86
+ # formats: dd(st|nd|rd|th) mn
87
+ def handle_od_mn
88
+ handle_od
89
+ next_tag
90
+ handle_mn
91
+ @precision = :day
92
+ end
93
+
94
+ # Handle ordinal-day/month-name/scalar-year
95
+ # formats: dd(st|nd|rd|th) mn yyyy
96
+ def handle_od_mn_sy
97
+ handle_od
98
+ next_tag
99
+ handle_mn
100
+ handle_possible(SeparatorSpace)
101
+ handle_sy
102
+ @precision = :day
103
+ end
104
+
105
+
106
+
107
+ # Handle day-name/ordinal-day
108
+ # formats: dn od
109
+ def handle_dn_od
110
+ handle_dn
111
+ next_tag
112
+ handle_od
113
+ end
114
+
115
+ # Handle day-name/month-name/scalar-day
116
+ # formats: dn month dd
117
+ def handle_dn_mn_sd
118
+ handle_dn
119
+ next_tag
120
+ handle_mn_sd
121
+ end
122
+
123
+ # Handle day-name/month-name/scalar-day/scalar-year
124
+ # formats: dn month dd yyyy
125
+ def handle_dn_mn_sd_sy
126
+ handle_dn_mn_sd
127
+ next_tag
128
+ handle_sy
129
+ @precision = :day
130
+ end
131
+
132
+ # Handle day-name/month-name/ordinal-day
133
+ # formats: dn month dd
134
+ def handle_dn_mn_od
135
+ handle_dn
136
+ next_tag
137
+ handle_mn_od
138
+ end
139
+
140
+ # Handle day-name/month-name/ordinal-day/scalar-year
141
+ # formats: dn month dd yyyy
142
+ def handle_dn_mn_od_sy
143
+ handle_dn_mn_od
144
+ next_tag
145
+ handle_sy
146
+ @precision = :day
147
+ end
148
+
149
+
150
+
151
+ # Handle month-name/scalar-day
152
+ # formats: month dd, month-dd
153
+ def handle_mn_sd
154
+ handle_mn
155
+ next_tag
156
+ handle_sd
157
+ end
158
+
159
+ # Handle month-name/scalar-day/scalar-year
160
+ # formats: month dd yyyy
161
+ def handle_mn_sd_sy
162
+ handle_mn
163
+ next_tag
164
+ handle_sd
165
+ handle_possible(SeparatorComma)
166
+ handle_sy
167
+ @precision = :day
168
+ end
169
+
170
+ # Handle month-name/ordinal-day
171
+ # formats: month dd, month-dd
172
+ def handle_mn_od
173
+ handle_mn
174
+ next_tag
175
+ handle_od
176
+ end
177
+
178
+ # Handle month-name/ordinal-day/scalar-year
179
+ # formats: mn dd yyyy
180
+ def handle_mn_od_sy
181
+ handle_mn
182
+ next_tag
183
+ handle_od
184
+ handle_sy
185
+ @precision = :day
186
+ end
187
+
188
+ # Handle month-name/scalar-year
189
+ # formats: month yyyy
190
+ def handle_mn_sy
191
+ handle_mn
192
+ next_tag
193
+ handle_sy
194
+ @precision = :month
195
+ end
196
+
197
+ # Handle scalar-month/scalar-year
198
+ # formats: mm/yy
199
+ def handle_sm_sy
200
+ handle_sm
201
+ next_tag
202
+ handle_sy
203
+ @precision = :month
204
+ end
205
+
206
+ # Handle scalar-month/scalar-day
207
+ # formats: mm/dd
208
+ def handle_sm_sd
209
+ handle_sm
210
+ next_tag
211
+ handle_sd
212
+ @precision = :day
213
+ end
214
+
215
+ # Handle scalar-month/scalar-day/scalar-year
216
+ # formats: mm/dd/yyyy
217
+ def handle_sm_sd_sy
218
+ handle_sm_sd
219
+ next_tag
220
+ handle_sy
221
+ @precision = :day
222
+ end
223
+
224
+
225
+
226
+ # Handle scalar-year/scalar-month
227
+ # formats: yyyy-sm
228
+ def handle_sy_sm
229
+ handle_sy
230
+ next_tag
231
+ handle_sm
232
+ end
233
+
234
+ # Handle scalar-year/scalar-month/scalar-day
235
+ # formats: yyyy-mm-dd, yyyy.mm.dd, yyyy/mm/dd, yyyy:mm:dd
236
+ def handle_sy_sm_sd
237
+ handle_sy
238
+ next_tag
239
+ handle_sm
240
+ next_tag
241
+ handle_sd
242
+ end
243
+
244
+ # Handle scalar-year/month-name
245
+ # formats: yyyy mn
246
+ def handle_sy_mn
247
+ handle_sy
248
+ handle_mn
249
+ end
250
+
251
+ # Handle scalar-year/month-name/scalar-day
252
+ # formats: yyyy mn dd
253
+ def handle_sy_mn_sd
254
+ handle_sy
255
+ handle_mn
256
+ next_tag
257
+ handle_sd
258
+ end
259
+
260
+ # Handle scalar-year/month-name/ordinal-day
261
+ # formats: yyyy mn dd(st|nd|rd|th)
262
+ def handle_sy_mn_od
263
+ handle_sy
264
+ handle_mn
265
+ next_tag
266
+ handle_od
267
+ end
268
+
269
+ end
270
+ end