9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/cotcube-helpers/range_ext.rb', line 9
def to_time_intervals(step:,
timezone: Time.find_zone('America/Chicago'),
debug: false)
unless step.is_a? ActiveSupport::Duration
raise ArgumentError,
":step must be a 'ActiveSupport::Duration', like '15.minutes', but '#{step}' is a '#{step.class}'"
end
unless [15.minutes, 60.minutes, 1.hour, 1.day].include? step
raise ArgumentError, 'Sorry, currently supporting only 15.minutes, 1.hour, 1.day as :step'
end
valid_classes = [ActiveSupport::TimeWithZone, Time, Date, DateTime]
unless timezone.is_a? ActiveSupport::TimeZone
raise "Expecting 'ActiveSupport::TimeZone' for :timezone, got '#{timezone.class}"
end
starting = self.begin
ending = self.end
starting = timezone.parse(starting) if starting.is_a? String
ending = timezone.parse(ending) if ending.is_a? String
unless valid_classes.include? starting.class
raise ArgumentError,
":self.begin seems not to be proper time value: #{starting} is a #{starting.class}"
end
unless valid_classes.include? ending.class
raise ArgumentError,
":self.end seems not to be proper time value: #{ending} is a #{ending.class}"
end
if step.to_i >= 1.day
(starting.to_date..ending.to_date).to_a.map(&:to_datetime)
else
actual_starting = starting.to_time.to_i
actual_ending = ending.to_time.to_i
actual_ending -= 3600 if starting.dst? && (not ending.dst?)
actual_ending += 3600 if ending.dst? && (not starting.dst?)
result = (actual_starting..actual_ending).step(step).to_a.map { |x| timezone.at(x) }
convert_to_sec_since = lambda do |clocking|
from_src, to_src = clocking.split(' - ')
regex = /^(?<hour>\d+):(?<minute>\d+)(?<morning>[pa]).?m.*/
from = from_src.match(regex)
to = to_src.match(regex)
from_i = from[:hour].to_i * 3600 + from[:minute].to_i * 60 + (from[:morning] == 'a' ? 2 : 1) * 12 * 3600
to_i = to[:hour].to_i * 3600 + to[:minute].to_i * 60 + (to[:morning] == 'a' ? 2 : 3) * 12 * 3600
(0...5).to_a.map { |i| [from_i + i * 24 * 3600, to_i + i * 24 * 3600] }
end
convert_to_sec_since.call('9:00a.m - 5:00p.m.')
result.map! do |time|
print "#{time}\t" if debug
if (not starting.dst?) && time.dst?
time -= 3600
print "Time reduced (not starting_DST, but current\t" if debug
elsif starting.dst? && (not time.dst?)
time += 3600
print "Time extended (starting DST, but not current\t" if debug
end
puts "#{time} " if debug
time
end
result
end
end
|