ruby-rails-extensions 2.1.0 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -1
- data/lib/ruby-rails-extensions/extensions/to_new_time.rb +110 -0
- data/lib/ruby-rails-extensions/version.rb +1 -1
- data/lib/ruby-rails-extensions.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7bd4a821e87ee1a9683247bfd699e1e95ce101a09e864be821f829cb72fd9f8
|
4
|
+
data.tar.gz: 0eb83cf0df52e2134930577cae1006071fdbfea14e83e0bace927eb77c531bf2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c8d9cd934041ee7dd40746beb4c8ba06fa9f7012e874370de548d238f23bbd5978cea9e66deba458118451b84e7b79f509fa9d56cb38c2d2895200e77755b69
|
7
|
+
data.tar.gz: 8c9d3eff37ed8db19f5328f5a21360e5059c1128df87e84ec76aeca3be815153395b8c635a0fb0c8529160e9e9a1bd6ad220157e834d144fd0d14d9a5565b0d3
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
Unreleased Changes
|
2
2
|
------------------
|
3
3
|
|
4
|
-
2.1.
|
4
|
+
2.1.1 (2024-10-11)
|
5
|
+
------------------
|
6
|
+
|
7
|
+
* Fix bad `2.1.0` publish
|
8
|
+
|
9
|
+
2.1.0 (2024-10-11)
|
5
10
|
------------------
|
6
11
|
|
7
12
|
* Modules added:
|
@@ -19,6 +24,7 @@ Unreleased Changes
|
|
19
24
|
* `Time#quarter_str` and `DateTime#quarter_str`
|
20
25
|
* `String#to_new_date`, `String#to_new_date_safe`, and `String#to_new_date_mm_yy`
|
21
26
|
* `String#cutoff`
|
27
|
+
* `String#to_new_time`
|
22
28
|
* Other changes:
|
23
29
|
* Initial test framework added
|
24
30
|
* Initial benchmark framework added
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module StringToNewTime
|
4
|
+
NEW_TIME_REGEXES = {
|
5
|
+
# Supports: 9:00am, 12:15 pm, 10:12am
|
6
|
+
hh_mm_p: /^(1[0-2]|0?[1-9]).([0-5]?[0-9])(.?[AaPp][Mm])?$/i,
|
7
|
+
# Supports: 16:0, 23:23, 01:00
|
8
|
+
hh_mm: /^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$/i,
|
9
|
+
# Supports: 1p, 12a, 5a, 9p
|
10
|
+
h_p: /^[0-1][0-9]?(.?[AaPp][Mm]?)?$/i
|
11
|
+
}.freeze
|
12
|
+
|
13
|
+
# Converts a string to a date object when following one of the
|
14
|
+
# following patterns (no space delimiters):
|
15
|
+
# - HH:MM AM/PM
|
16
|
+
# - HH:MM (24 hour)
|
17
|
+
# - HH AM/PM
|
18
|
+
#
|
19
|
+
# @param options [Hash]
|
20
|
+
#
|
21
|
+
# @option options [Integer] :year Year to use as the base time object.
|
22
|
+
# This will default to today's year.
|
23
|
+
#
|
24
|
+
# @option options [Integer] :month Month to use as the base time object.
|
25
|
+
# This will default to today's month.
|
26
|
+
#
|
27
|
+
# @option options [Integer] :day Day to use as the base time object.
|
28
|
+
# This will default to today's day.
|
29
|
+
#
|
30
|
+
# @return [Date, String, nil]
|
31
|
+
#
|
32
|
+
def to_new_time(options = {})
|
33
|
+
opts = options || {}
|
34
|
+
today = Date.today
|
35
|
+
year = opts.fetch(:year, today.year)
|
36
|
+
month = opts.fetch(:month, today.month)
|
37
|
+
day = opts.fetch(:day, today.day)
|
38
|
+
parts = extract_time
|
39
|
+
|
40
|
+
Time.use_zone(RubyRailsExtensions.configuration.default_time_zone || 'Eastern Time (US & Canada)') do
|
41
|
+
# Use {Time#parse} as there is no `Time.zone.new` method
|
42
|
+
Time.zone.parse("#{year}-#{month}-#{day} #{parts[:hour]}:#{parts[:minute]} #{parts[:ampm]}")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
# Checks the string for any supported format,
|
49
|
+
# then converts the string time parts.
|
50
|
+
#
|
51
|
+
# @return [Hash, nil]
|
52
|
+
#
|
53
|
+
def extract_time
|
54
|
+
key = NEW_TIME_REGEXES.find { |_key, value| match?(value) }&.first
|
55
|
+
|
56
|
+
return if key.nil?
|
57
|
+
|
58
|
+
__send__(:"#{key}_time")
|
59
|
+
end
|
60
|
+
|
61
|
+
# @return [Hash]
|
62
|
+
def hh_mm_p_time
|
63
|
+
parts = scan(NEW_TIME_REGEXES[:hh_mm_p])
|
64
|
+
|
65
|
+
parts.flatten!
|
66
|
+
|
67
|
+
parts.map!(&:strip)
|
68
|
+
|
69
|
+
hour, min, ampm = parts
|
70
|
+
|
71
|
+
{
|
72
|
+
hour: hour,
|
73
|
+
minute: min,
|
74
|
+
second: 0,
|
75
|
+
ampm: ampm
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
# @return [Hash]
|
80
|
+
def hh_mm_time
|
81
|
+
parts = scan(NEW_TIME_REGEXES[:hh_mm])
|
82
|
+
|
83
|
+
parts.flatten!
|
84
|
+
|
85
|
+
parts.map!(&:strip)
|
86
|
+
|
87
|
+
hour, min = parts
|
88
|
+
|
89
|
+
{
|
90
|
+
hour: hour,
|
91
|
+
minute: min,
|
92
|
+
second: 0,
|
93
|
+
ampm: nil
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
# @return [Hash]
|
98
|
+
def h_p_time
|
99
|
+
{
|
100
|
+
hour: gsub(/[apm]/i, ''),
|
101
|
+
minute: 0,
|
102
|
+
second: 0,
|
103
|
+
ampm: match?(/a/i) ? 'am' : 'pm'
|
104
|
+
}
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
String.class_eval do
|
109
|
+
include StringToNewTime
|
110
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-rails-extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brands Insurance
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- lib/ruby-rails-extensions/extensions/to_money.rb
|
78
78
|
- lib/ruby-rails-extensions/extensions/to_negative_i.rb
|
79
79
|
- lib/ruby-rails-extensions/extensions/to_new_date.rb
|
80
|
+
- lib/ruby-rails-extensions/extensions/to_new_time.rb
|
80
81
|
- lib/ruby-rails-extensions/extensions/to_nonzero_i.rb
|
81
82
|
- lib/ruby-rails-extensions/extensions/to_or_sentence.rb
|
82
83
|
- lib/ruby-rails-extensions/extensions/to_percentage.rb
|