timeliness 0.5.1 → 0.5.2
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/timeliness/format.rb +15 -11
- data/lib/timeliness/format_set.rb +5 -5
- data/lib/timeliness/helpers.rb +12 -7
- data/lib/timeliness/parser.rb +8 -4
- data/lib/timeliness/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6fd0587977d3e4e37b368cb52cf19aa81f61f92bd476b5ee5b75ca077179797e
|
4
|
+
data.tar.gz: 69fb540b605ad50751253181acd7c391b9d72d43fb0337f7246a9197c90e2717
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34cefe7db667f7e4eee4440ac6806e866b38176bb47b0d006c71562031fdb47c4a6e29b111178ba05e8008553f7c64577d6300c6cf711fba277256e5fd7443f5
|
7
|
+
data.tar.gz: 52dfc42917b78c105ff1e70b9523444abbb5c6c03b11d58e03d95e598959edf9f16eef3e2e69cfa9a3bac1112c1697c8f553dedd078e7d0ddf5e9d54abf11e8c
|
data/lib/timeliness/format.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Timeliness
|
2
4
|
class Format
|
3
5
|
include Helpers
|
@@ -11,7 +13,6 @@ module Timeliness
|
|
11
13
|
end
|
12
14
|
|
13
15
|
def compile!
|
14
|
-
@token_count = 0
|
15
16
|
found_tokens, token_order = [], []
|
16
17
|
|
17
18
|
format = format_string.dup
|
@@ -19,20 +20,16 @@ module Timeliness
|
|
19
20
|
|
20
21
|
# Substitute tokens with numbered placeholder
|
21
22
|
Definitions.sorted_token_keys.each do |token|
|
22
|
-
count = 0
|
23
23
|
format.gsub!(token) do
|
24
24
|
token_regexp_str, arg_key = Definitions.format_tokens[token]
|
25
|
-
token_index = found_tokens.size
|
26
25
|
|
27
|
-
if arg_key
|
26
|
+
if arg_key && found_tokens.rassoc(arg_key)
|
28
27
|
raise CompilationFailed, "Token '#{token}' was found more than once in format '#{format_string}'. This has unexpected effects should be removed." if count > 0
|
29
|
-
count += 1
|
30
|
-
|
31
|
-
token_regexp_str = "(#{token_regexp_str})"
|
32
|
-
@token_count += 1
|
33
28
|
end
|
29
|
+
|
34
30
|
found_tokens << [ token_regexp_str, arg_key ]
|
35
31
|
|
32
|
+
token_index = found_tokens.size - 1
|
36
33
|
"%<#{token_index}>"
|
37
34
|
end
|
38
35
|
end
|
@@ -40,11 +37,18 @@ module Timeliness
|
|
40
37
|
# Replace placeholders with token regexps
|
41
38
|
format.gsub!(/%<(\d+)>/) do
|
42
39
|
token_regexp_str, arg_key = found_tokens[$1.to_i]
|
43
|
-
|
44
|
-
|
40
|
+
|
41
|
+
if arg_key
|
42
|
+
token_order << arg_key
|
43
|
+
"(#{token_regexp_str})"
|
44
|
+
else
|
45
|
+
token_regexp_str
|
46
|
+
end
|
45
47
|
end
|
46
48
|
|
47
|
-
|
49
|
+
@token_count = token_order.size
|
50
|
+
|
51
|
+
define_process_method(token_order)
|
48
52
|
@regexp_string = format
|
49
53
|
@regexp = Regexp.new("^(?>#{format})$")
|
50
54
|
self
|
@@ -31,12 +31,12 @@ module Timeliness
|
|
31
31
|
|
32
32
|
def match(string, format_string=nil)
|
33
33
|
format = single_format(format_string) if format_string
|
34
|
-
match_regexp = format
|
34
|
+
match_regexp = format ? format.regexp : @regexp
|
35
35
|
|
36
|
-
match_regexp.match(string)
|
37
|
-
captures = match_data.captures
|
38
|
-
index
|
39
|
-
values
|
36
|
+
if (match_data = match_regexp.match(string))
|
37
|
+
captures = match_data.captures # For a multi-format regexp there are lots of nils
|
38
|
+
index = captures.index { |e| !e.nil? } # Find the start of captures for matched format
|
39
|
+
values = captures[index, 8]
|
40
40
|
format ||= @match_indexes[index]
|
41
41
|
format.process(*values)
|
42
42
|
end
|
data/lib/timeliness/helpers.rb
CHANGED
@@ -1,10 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Timeliness
|
2
4
|
module Helpers
|
5
|
+
# Helper methods used in format component processing. See Definitions.
|
3
6
|
|
4
7
|
def full_hour(hour, meridian)
|
5
8
|
hour = hour.to_i
|
6
9
|
return hour if meridian.nil?
|
7
|
-
|
10
|
+
|
11
|
+
meridian.delete!('.')
|
12
|
+
meridian.downcase!
|
13
|
+
|
14
|
+
if meridian == 'am'
|
8
15
|
raise(ArgumentError) if hour == 0 || hour > 12
|
9
16
|
hour == 12 ? 0 : hour
|
10
17
|
else
|
@@ -20,9 +27,9 @@ module Timeliness
|
|
20
27
|
end
|
21
28
|
year.to_i
|
22
29
|
end
|
23
|
-
|
30
|
+
|
24
31
|
def month_index(month)
|
25
|
-
return month.to_i if month.
|
32
|
+
return month.to_i if month.match?(/\d/)
|
26
33
|
(month.length > 3 ? month_names : abbr_month_names).index { |str| month.casecmp?(str) }
|
27
34
|
end
|
28
35
|
|
@@ -39,10 +46,8 @@ module Timeliness
|
|
39
46
|
end
|
40
47
|
|
41
48
|
def offset_in_seconds(offset)
|
42
|
-
|
43
|
-
|
44
|
-
parts[1] = parts[1].to_f / 60
|
45
|
-
(parts[0] + parts[1]) * sign * 3600
|
49
|
+
offset =~ /^([-+])?(\d{2}):?(\d{2})/
|
50
|
+
($1 == '-' ? -1 : 1) * ($2.to_f * 3600 + $3.to_f)
|
46
51
|
end
|
47
52
|
|
48
53
|
def i18n_loaded?
|
data/lib/timeliness/parser.rb
CHANGED
@@ -18,19 +18,20 @@ module Timeliness
|
|
18
18
|
|
19
19
|
default_values_by_type(time_array, type, options) unless type == :datetime
|
20
20
|
|
21
|
-
make_time(time_array
|
21
|
+
make_time(time_array, options[:zone])
|
22
22
|
rescue NoMethodError => ex
|
23
23
|
raise ex unless ex.message =~ /undefined method `(zone|use_zone|current)' for Time:Class/
|
24
24
|
raise MissingTimezoneSupport, "ActiveSupport timezone support must be loaded to use timezones other than :utc and :local."
|
25
25
|
end
|
26
26
|
|
27
27
|
def make_time(time_array, zone_option=nil)
|
28
|
-
return nil unless fast_date_valid_with_fallback(
|
28
|
+
return nil unless fast_date_valid_with_fallback(time_array[0], time_array[1], time_array[2])
|
29
29
|
|
30
30
|
zone, offset = zone_and_offset(time_array[7]) if time_array[7]
|
31
31
|
|
32
32
|
value = create_time_in_zone(time_array[0..6].compact, zone || zone_option)
|
33
33
|
value = shift_time_to_zone(value, zone_option) if zone
|
34
|
+
|
34
35
|
return nil unless value
|
35
36
|
|
36
37
|
offset ? value + (value.utc_offset - offset) : value
|
@@ -74,9 +75,12 @@ module Timeliness
|
|
74
75
|
def default_values_by_type(values, type, options)
|
75
76
|
case type
|
76
77
|
when :date
|
77
|
-
values
|
78
|
+
values.fill(nil, 3..7)
|
78
79
|
when :time
|
79
|
-
|
80
|
+
current_date = current_date(options)
|
81
|
+
values[0] = current_date[0]
|
82
|
+
values[1] = current_date[1]
|
83
|
+
values[2] = current_date[2]
|
80
84
|
when nil
|
81
85
|
dummy_date = current_date(options)
|
82
86
|
values[0] ||= dummy_date[0]
|
data/lib/timeliness/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timeliness
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Meehan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-01-
|
11
|
+
date: 2025-01-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|