date_common 0.1.0 → 0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/date_common/version.rb +1 -1
- data/lib/date_common.rb +147 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a58c00b5f9a99c18054d14a1e4b19173b67069a0bad406b0515345fab59e0658
|
4
|
+
data.tar.gz: dbd92b79f4c7749a1e3fad58cd64bfd3cabbc9fb2dd1a4809e6db670e5e583c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c70b4b29b21af4d4e696955221afa833c2774ed0b27a09445624676c573ce86b14b0a5007f9bb01399a6c471eefcf43b235f97aa3993f9d65574ee190aec338
|
7
|
+
data.tar.gz: 709b78915fc1028de6b302592fa64159c16e1bbb62ec68b162e47d3962b66e9bdf08478ba53a999a84259f10c6404253fe0b31e7b7a54c0683c4da4b7e817fce
|
data/lib/date_common/version.rb
CHANGED
data/lib/date_common.rb
CHANGED
@@ -16,6 +16,20 @@ module DateCommon
|
|
16
16
|
BLACK_FRIDAY_2022 = Date.new(2022, 11, 25)
|
17
17
|
THANKSGIVING_2023 = Date.new(2023, 11, 23)
|
18
18
|
BLACK_FRIDAY_2023 = Date.new(2023, 11, 24)
|
19
|
+
|
20
|
+
MONTH_NAME_PATTERNS = /(january|jan|february|feb|march|mar|april|apr|may|june|jun|july|jul|august|aug|september|sep|sept|october|oct|november|nov|december|dec)/i
|
21
|
+
DAY_NAME_PATTERNS = /(mon|monday|tue|tues|tuesday|wed|wednesday|thurs|thursday|fri|friday|sat|saturday|sun|sunday)/i
|
22
|
+
MONTH_NAME_AND_DAY_NAME_PATTERNS = /(mon|monday|tue|tues|tuesday|wed|wednesday|thurs|thursday|fri|friday|sat|saturday|sun|sunday)? *(january|jan|february|feb|march|mar|april|apr|may|june|jun|july|jul|august|aug|september|sep|sept|october|oct|november|nov|december|dec) +([0-9]+)/i
|
23
|
+
|
24
|
+
def self.is_month_name?(text)
|
25
|
+
return true if text =~ MONTH_NAME_PATTERNS
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.is_day_name?(text)
|
30
|
+
return true if text =~ DAY_NAME_PATTERNS
|
31
|
+
return false
|
32
|
+
end
|
19
33
|
|
20
34
|
# DateCommon.holiday?(Date.current)
|
21
35
|
def self.holiday?(date)
|
@@ -92,7 +106,7 @@ module DateCommon
|
|
92
106
|
# TESTED
|
93
107
|
def self.last_date_of_month(date)
|
94
108
|
# fucking god awful approach but it works; refactor
|
95
|
-
|
109
|
+
|
96
110
|
# the 31st
|
97
111
|
begin
|
98
112
|
date_end = Date.new(date.year, date.month, 31)
|
@@ -180,4 +194,136 @@ module DateCommon
|
|
180
194
|
return wday[0..2] if abbrev
|
181
195
|
return wday
|
182
196
|
end
|
197
|
+
|
198
|
+
# day before yesterday
|
199
|
+
# yesterday
|
200
|
+
# on thursday
|
201
|
+
# last monday
|
202
|
+
# on 9 4
|
203
|
+
#
|
204
|
+
def self.parse_date(text)
|
205
|
+
date_text = extract_date_string(text)
|
206
|
+
puts date_text
|
207
|
+
date = recognize_date(date_text)
|
208
|
+
return date
|
209
|
+
end
|
210
|
+
|
211
|
+
def self.extract_date_string(text)
|
212
|
+
#raise "foo"
|
213
|
+
patterns = []
|
214
|
+
patterns << / (o?n?|l?a?s?t?) ?(monday) ?/i
|
215
|
+
patterns << / (o?n?|l?a?s?t?) ?(tuesday) ?/i
|
216
|
+
patterns << / (o?n?|l?a?s?t?) ?(wednesday) ?/i
|
217
|
+
patterns << / (o?n?|l?a?s?t?) ?(thursday) ?/i
|
218
|
+
patterns << / (o?n?|l?a?s?t?) ?(friday) ?/i
|
219
|
+
patterns << / (o?n?|l?a?s?t?) ?(saturday) ?/i
|
220
|
+
patterns << / (o?n?|l?a?s?t?) ?(sunday) ?/i
|
221
|
+
patterns.each do |pattern|
|
222
|
+
if text =~ pattern
|
223
|
+
return $2.downcase
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
patterns = []
|
228
|
+
patterns << / (yesterday) ?/i
|
229
|
+
patterns << / (today) ?/i
|
230
|
+
patterns.each do |pattern|
|
231
|
+
if text =~ pattern
|
232
|
+
return $1.downcase
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
patterns = []
|
237
|
+
patterns << / on ([0-9]+) ([0-9]+) ?/
|
238
|
+
patterns << /[0-9]+\/[0-9]+/
|
239
|
+
patterns << /[0-9]+-[0-9]+/
|
240
|
+
patterns << /[0-9]+ [0-9]+/
|
241
|
+
patterns.each do |pattern|
|
242
|
+
if text =~ pattern
|
243
|
+
|
244
|
+
current_year = Date.today.year
|
245
|
+
return Date.new(current_year, $1.to_i, $2.to_i).to_s.downcase
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
|
250
|
+
# patterns.each do |pattern|
|
251
|
+
# if text =~ pattern
|
252
|
+
# open_parentheses_count = pattern.to_s.count("(")
|
253
|
+
# close_parentheses_count = pattern.to_s.count(")")
|
254
|
+
# if open_parentheses_count || close_parentheses_count
|
255
|
+
# total_parentheses_count = open_parentheses_count + close_parentheses_count
|
256
|
+
# else
|
257
|
+
# total_parentheses_count = 0
|
258
|
+
# end
|
259
|
+
# # return the last match_group
|
260
|
+
# match_group1 = $1
|
261
|
+
# match_group2 = $2
|
262
|
+
# return match_group2 if total_parentheses_count >= 4
|
263
|
+
# return match_group1
|
264
|
+
# end
|
265
|
+
# end
|
266
|
+
end
|
267
|
+
|
268
|
+
def self.recognize_date(text)
|
269
|
+
# yesterday
|
270
|
+
return Date.today if text == "today"
|
271
|
+
|
272
|
+
# yesterday
|
273
|
+
return Date.today - 1 if text == "yesterday"
|
274
|
+
|
275
|
+
# 2020-9-4
|
276
|
+
if text =~ /[0-9]+-[0-9]+-[0-9]+/
|
277
|
+
parts = text.split("-")
|
278
|
+
return Date.new(parts[0].to_i, parts[1].to_i, parts[2].to_i)
|
279
|
+
end
|
280
|
+
|
281
|
+
if text =~ /[0-9]+\/[0-9]+\/[0-9]+/
|
282
|
+
parts = text.split("/")
|
283
|
+
return Date.new(parts[0].to_i, parts[1].to_i, parts[2].to_i)
|
284
|
+
end
|
285
|
+
|
286
|
+
if text =~ /[0-9]+-[0-9]+-[0-9]+/
|
287
|
+
parts = text.split("-")
|
288
|
+
return Date.new(parts[0].to_i, parts[1].to_i, parts[2].to_i)
|
289
|
+
end
|
290
|
+
|
291
|
+
if text =~ /([0-9]+)\/([0-9]+)/
|
292
|
+
#parts = text.split("-")
|
293
|
+
year = Date.today.year
|
294
|
+
return Date.new(year, $1.to_i, $2.to_i)
|
295
|
+
end
|
296
|
+
|
297
|
+
if text =~ /([0-9]+)-([0-9]+)/
|
298
|
+
#parts = text.split("-")
|
299
|
+
year = Date.today.year
|
300
|
+
return Date.new(year, $1.to_i, $2.to_i)
|
301
|
+
end
|
302
|
+
|
303
|
+
|
304
|
+
# monday / tuesday / wednesday.
|
305
|
+
if text =~ /.+day/
|
306
|
+
current_date = Date.today
|
307
|
+
day_range = 7
|
308
|
+
possible_dates = []
|
309
|
+
possible_dates << current_date - 1
|
310
|
+
possible_dates << current_date - 2
|
311
|
+
possible_dates << current_date - 3
|
312
|
+
possible_dates << current_date - 4
|
313
|
+
possible_dates << current_date - 5
|
314
|
+
possible_dates << current_date - 6
|
315
|
+
possible_dates << current_date - 7
|
316
|
+
possible_dates.each do |date|
|
317
|
+
return date if date.monday? && text == 'monday'
|
318
|
+
return date if date.tuesday? && text == 'tuesday'
|
319
|
+
return date if date.wednesday? && text == 'wednesday'
|
320
|
+
return date if date.thursday? && text == 'thursday'
|
321
|
+
return date if date.friday? && text == 'friday'
|
322
|
+
return date if date.saturday? && text == 'saturday'
|
323
|
+
return date if date.sunday? && text == 'sunday'
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
end
|
328
|
+
|
183
329
|
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.
|
4
|
+
version: '0.13'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Johnson
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-04 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.
|
@@ -39,7 +39,7 @@ metadata:
|
|
39
39
|
homepage_uri: https://github.com/fuzzygroup/date_common
|
40
40
|
source_code_uri: https://github.com/fuzzygroup/date_common/
|
41
41
|
changelog_uri: https://github.com/fuzzygroup/date_common/CHANGELOG.md
|
42
|
-
post_install_message:
|
42
|
+
post_install_message:
|
43
43
|
rdoc_options: []
|
44
44
|
require_paths:
|
45
45
|
- lib
|
@@ -54,8 +54,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '0'
|
56
56
|
requirements: []
|
57
|
-
rubygems_version: 3.
|
58
|
-
signing_key:
|
57
|
+
rubygems_version: 3.0.3
|
58
|
+
signing_key:
|
59
59
|
specification_version: 4
|
60
60
|
summary: A small library of date handling routines
|
61
61
|
test_files: []
|