date_common 0.1.0 → 0.11

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
  SHA256:
3
- metadata.gz: 753ff6e5025bcc900e386a04367b0fa6ce7da70f5d5bacd82327100038c6b23c
4
- data.tar.gz: ca22a994569f4abaee8ca63b7beb6cde30d789527633d9ff20b4368ef15f78a7
3
+ metadata.gz: bd1918d5fe6029dd42ff846b01064a42adea2bea8f74c386e3dbb7b17ae210e5
4
+ data.tar.gz: 1a05ddd001250a2305975d68bdc67eabfef1ba407b21d337ae19ae4526b5a061
5
5
  SHA512:
6
- metadata.gz: 86ec142d1061a1ab075bd95bb43a0d6acddbd895e4bdaded7b838058a540a3e769d49ff910afad209e631664971d7666cf794c7d9d57bd1062e9bdc0bcabd05d
7
- data.tar.gz: f02aa1f5b08099f52b7f087baea0372f11f4cbf71dc06933e259878f57e9244011ac48cb3f229e8177456f55fbafc24872974052b4222de4cc21633b49ebecd1
6
+ metadata.gz: db36d974950e3c3efda72e437b246aaa0e2aa743ff4d0c75bf10bb5498cd1affb5165cbe78ab54753f6d9e2453765607d19c55dd626830bafa2c44419d87136d
7
+ data.tar.gz: bbed39728ba2f07445271073fe47469da62d617f01be478b94e8de6742acd87103326155aa1502210e8f1041141eedc96815ee9a54ece38549a10f593c5bee95
@@ -180,4 +180,107 @@ module DateCommon
180
180
  return wday[0..2] if abbrev
181
181
  return wday
182
182
  end
183
+
184
+ # day before yesterday
185
+ # yesterday
186
+ # on thursday
187
+ # last monday
188
+ # on 9 4
189
+ #
190
+ def self.parse_date(text)
191
+ date_text = extract_date_string(text)
192
+ date = recognize_date(date_text)
193
+ return date
194
+ end
195
+
196
+ def self.extract_date_string(text)
197
+ patterns = []
198
+ patterns << / (o?n?|l?a?s?t?) ?(monday) ?/i
199
+ patterns << / (o?n?|l?a?s?t?) ?(tuesday) ?/i
200
+ patterns << / (o?n?|l?a?s?t?) ?(wednesday) ?/i
201
+ patterns << / (o?n?|l?a?s?t?) ?(thursday) ?/i
202
+ patterns << / (o?n?|l?a?s?t?) ?(friday) ?/i
203
+ patterns << / (o?n?|l?a?s?t?) ?(saturday) ?/i
204
+ patterns << / (o?n?|l?a?s?t?) ?(sunday) ?/i
205
+ patterns.each do |pattern|
206
+ if text =~ pattern
207
+ return $2.downcase
208
+ end
209
+ end
210
+
211
+ patterns = []
212
+ patterns << / (yesterday) ?/i
213
+ patterns << / (today) ?/i
214
+ patterns.each do |pattern|
215
+ if text =~ pattern
216
+ return $1.downcase
217
+ end
218
+ end
219
+
220
+ patterns = []
221
+ patterns << / on ([0-9]+) ([0-9]+) ?/
222
+ patterns.each do |pattern|
223
+ if text =~ pattern
224
+ current_year = Date.today.year
225
+ return Date.new(current_year, $1.to_i, $2.to_i).to_s.downcase
226
+ end
227
+ end
228
+
229
+
230
+ # patterns.each do |pattern|
231
+ # if text =~ pattern
232
+ # open_parentheses_count = pattern.to_s.count("(")
233
+ # close_parentheses_count = pattern.to_s.count(")")
234
+ # if open_parentheses_count || close_parentheses_count
235
+ # total_parentheses_count = open_parentheses_count + close_parentheses_count
236
+ # else
237
+ # total_parentheses_count = 0
238
+ # end
239
+ # # return the last match_group
240
+ # match_group1 = $1
241
+ # match_group2 = $2
242
+ # return match_group2 if total_parentheses_count >= 4
243
+ # return match_group1
244
+ # end
245
+ # end
246
+ end
247
+
248
+ def self.recognize_date(text)
249
+ # yesterday
250
+ return Date.today if text == "today"
251
+
252
+ # yesterday
253
+ return Date.today - 1 if text == "yesterday"
254
+
255
+ # 2020-9-4
256
+ if text =~ /[0-9]+-[0-9]+-[0-9]+/
257
+ parts = text.split("-")
258
+ return Date.new(parts[0].to_i, parts[1].to_i, parts[2].to_i)
259
+ end
260
+
261
+ # monday / tuesday / wednesday.
262
+ if text =~ /.+day/
263
+ current_date = Date.today
264
+ day_range = 7
265
+ possible_dates = []
266
+ possible_dates << current_date - 1
267
+ possible_dates << current_date - 2
268
+ possible_dates << current_date - 3
269
+ possible_dates << current_date - 4
270
+ possible_dates << current_date - 5
271
+ possible_dates << current_date - 6
272
+ possible_dates << current_date - 7
273
+ possible_dates.each do |date|
274
+ return date if date.monday? && text == 'monday'
275
+ return date if date.tuesday? && text == 'tuesday'
276
+ return date if date.wednesday? && text == 'wednesday'
277
+ return date if date.thursday? && text == 'thursday'
278
+ return date if date.friday? && text == 'friday'
279
+ return date if date.saturday? && text == 'saturday'
280
+ return date if date.sunday? && text == 'sunday'
281
+ end
282
+ end
283
+
284
+ end
285
+
183
286
  end
@@ -1,3 +1,3 @@
1
1
  module DateCommon
2
- VERSION = "0.1.0"
2
+ VERSION = "0.11"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: date_common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: '0.11'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Johnson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-14 00:00:00.000000000 Z
11
+ date: 2020-09-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A small library of date handling routines that I use on almost every
14
14
  project.