This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtime_interval.rb
397 lines (347 loc) · 11.3 KB
/
time_interval.rb
1
2
3
4
5
6
7
8
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# frozen_string_literal: true
module ISO8601
##
# A Time Interval representation.
# See https://en.wikipedia.org/wiki/ISO_8601#Time_intervals
#
# @example
# ti = ISO8601::TimeInterval.parse('P1MT2H/2014-05-28T19:53Z')
# ti.size # => 2635200.0
# ti2 = ISO8601::TimeInterval.parse('2014-05-28T19:53Z/2014-05-28T20:53Z')
# ti2.to_f # => 3600.0
#
# @example
# start_time = ISO8601::DateTime.new('2014-05-28T19:53Z')
# end_time = ISO8601::DateTime.new('2014-05-30T19:53Z')
# ti = ISO8601::TimeInterval.from_datetimes(start_time, end_time)
# ti.size # => 172800.0 (Seconds)
#
# @example
# duration = ISO8601::Duration.new('P1MT2H')
# end_time = ISO8601::DateTime.new('2014-05-30T19:53Z')
# ti = ISO8601::TimeInterval.from_duration(duration, end_time)
# ti.size # => 2635200.0 (Seconds)
#
# @example
# start_time = ISO8601::DateTime.new('2014-05-30T19:53Z')
# duration = ISO8601::Duration.new('P1MT2H', base)
# ti = ISO8601::TimeInterval.from_duration(start_time, duration)
# ti.size # => 2635200.0 (Seconds)
#
class TimeInterval
include Comparable
##
# Initializes a time interval based on two time points.
#
# @overload from_datetimes(start_time, end_time)
# @param [ISO8601::DateTime] start_time The start time point of the
# interval.
# @param [ISO8601::DateTime] end_time The end time point of the interaval.
#
# @raise [ISO8601::Errors::TypeError] If both params are not instances of
# `ISO8601::DateTime`.
#
# @return [ISO8601::TimeInterval]
def self.from_datetimes(*atoms)
guard_from_datetimes(atoms, 'Start and end times must instances of ISO8601::DateTime')
new(atoms)
end
##
# Initializes a TimeInterval based on a `ISO8601::Duration` and a
# `ISO8601::DateTime`. The order of the params define the strategy to
# compute the interval.
#
# @overload from_duration(start_time, duration)
# Equivalent to the `<start>/<duration>` pattern.
# @param [ISO8601::DateTime] start_time The start time point of the
# interval.
# @param [ISO8601::Duration] duration The size of the interval.
#
# @overload from_duration(duration, end_time)
# Equivalent to the `<duration>/<end>` pattern.
# @param [ISO8601::Duration] duration The size of the interval.
# @param [ISO8601::DateTime] end_time The end time point of the interaval.
#
# @raise [ISO8601::Errors::TypeError] If the params aren't a mix of
# `ISO8601::DateTime` and `ISO8601::Duration`.
#
# @return [ISO8601::TimeInterval]
def self.from_duration(*atoms)
guard_from_duration(atoms, 'Expected one date time and one duration')
new(atoms)
end
##
# Dispatches the constructor based on the type of the input.
#
# @overload new(pattern)
# Parses a pattern.
# @param [String] input A time interval pattern.
#
# @overload new([start_time, duration])
# Equivalent to the `<start>/<duration>` pattern.
# @param [Array<(ISO8601::DateTime, ISO8601::Duration)>] input
#
# @overload new([duration, end_time])
# Equivalent to the `<duration>/<end>` pattern.
# @param [Array<(ISO8601::Duration, ISO8601::DateTime)>] input
#
# @return [ISO8601::TimeInterval]
def initialize(input)
case input
when String
parse(input)
when Array
from_atoms(input)
else
raise(ISO8601::Errors::TypeError, 'The pattern must be a String or a Hash')
end
end
##
# Alias of `initialize` to have a closer interface to the core `Time`,
# `Date` and `DateTime` interfaces.
def self.parse(pattern)
new(pattern)
end
##
# The start time (first) of the interval.
#
# @return [ISO8601::DateTime] start time
attr_reader :first
alias start_time first
##
# The end time (last) of the interval.
#
# @return [ISO8601::DateTime] end time
attr_reader :last
alias end_time last
##
# The pattern for the interval.
#
# @return [String] The pattern of this interval
def pattern
return @pattern if @pattern
"#{@atoms.first}/#{@atoms.last}"
end
alias to_s pattern
##
# The size of the interval. If any bound is a Duration, the
# size of the interval is the number of seconds of the interval.
#
# @return [Float] Size of the interval in seconds
attr_reader :size
alias to_f size
alias length size
##
# Checks if the interval is empty.
#
# @return [Boolean]
def empty?
first == last
end
##
# Check if a given time is inside the current TimeInterval.
#
# @param [#to_time] other DateTime to check if it's
# inside the current interval.
#
# @raise [ISO8601::Errors::TypeError] if time param is not a compatible
# Object.
#
# @return [Boolean]
def include?(other)
raise(ISO8601::Errors::TypeError, "The parameter must respond_to #to_time") \
unless other.respond_to?(:to_time)
(first.to_time <= other.to_time &&
last.to_time >= other.to_time)
end
alias member? include?
##
# Returns true if the interval is a subset of the given interval.
#
# @param [ISO8601::TimeInterval] other a time interval.
#
# @raise [ISO8601::Errors::TypeError] if time param is not a compatible
# Object.
#
# @return [Boolean]
def subset?(other)
raise(ISO8601::Errors::TypeError, "The parameter must be an instance of #{self.class}") \
unless other.is_a?(self.class)
other.include?(first) && other.include?(last)
end
##
# Returns true if the interval is a superset of the given interval.
#
# @param [ISO8601::TimeInterval] other a time interval.
#
# @raise [ISO8601::Errors::TypeError] if time param is not a compatible
# Object.
#
# @return [Boolean]
def superset?(other)
raise(ISO8601::Errors::TypeError, "The parameter must be an instance of #{self.class}") \
unless other.is_a?(self.class)
include?(other.first) && include?(other.last)
end
##
# Check if two intervarls intersect.
#
# @param [ISO8601::TimeInterval] other Another interval to check if they
# intersect.
#
# @raise [ISO8601::Errors::TypeError] if the param is not a TimeInterval.
#
# @return [Boolean]
def intersect?(other)
raise(ISO8601::Errors::TypeError, "The parameter must be an instance of #{self.class}") \
unless other.is_a?(self.class)
include?(other.first) || include?(other.last)
end
##
# Return the intersection between two intervals.
#
# @param [ISO8601::TimeInterval] other time interval
#
# @raise [ISO8601::Errors::TypeError] if the param is not a TimeInterval.
#
# @return [Boolean]
def intersection(other)
raise(ISO8601::Errors::IntervalError, "The intervals are disjoint") \
if disjoint?(other) && other.disjoint?(self)
return self if subset?(other)
return other if other.subset?(self)
a, b = sort_pair(self, other)
self.class.from_datetimes(b.first, a.last)
end
##
# Check if two intervarls have no element in common. This method is the
# opposite of `#intersect?`.
#
# @param [ISO8601::TimeInterval] other Time interval.
#
# @raise [ISO8601::Errors::TypeError] if the param is not a TimeInterval.
#
# @return [Boolean]
def disjoint?(other)
!intersect?(other)
end
##
# @param [ISO8601::TimeInterval] other
#
# @return [-1, 0, 1, nil]
def <=>(other)
return nil unless other.is_a?(self.class)
to_f <=> other.to_f
end
##
# Equality by hash.
#
# @param [ISO8601::TimeInterval] other
#
# @return [Boolean]
def eql?(other)
(hash == other.hash)
end
##
# @return [Fixnum]
def hash
@atoms.hash
end
def self.valid_date_time?(time, message = "Expected a ISO8601::DateTime")
return true if time.is_a?(ISO8601::DateTime)
raise(ISO8601::Errors::TypeError, message)
end
def self.guard_from_datetimes(atoms, message)
atoms.all? { |x| valid_date_time?(x, message) }
end
def self.guard_from_duration(atoms, message)
raise(ISO8601::Errors::TypeError, message) \
unless atoms.any? { |x| x.is_a?(ISO8601::Duration) } &&
atoms.any? { |x| x.is_a?(ISO8601::DateTime) }
end
private
# Initialize a TimeInterval ISO8601 by a pattern. If you initialize it with
# a duration pattern, the second argument is mandatory because you need to
# specify an start/end point to calculate the interval.
#
# @param [String] pattern This parameter defines a full time interval.
# Valid patterns are defined in the ISO8601 as:
# * <start_time>/<end_time>
# * <start_time>/<duration>
# * <duration>/<end_time>
#
# @raise [ISO8601::Errors::UnknownPattern] If given pattern is not a valid
# ISO8601 pattern.
def parse(pattern)
raise(ISO8601::Errors::UnknownPattern, pattern) unless pattern.include?('/')
@pattern = pattern
subpatterns = pattern.split('/')
raise(ISO8601::Errors::UnknownPattern, pattern) if subpatterns.size != 2
fst = parse_start_subpattern(subpatterns.first)
snd = parse_subpattern(subpatterns.last)
@atoms = [fst, snd]
@first, @last, @size = limits(@atoms)
end
def sort_pair(a, b)
a.first < b.first ? [a, b] : [b, a]
end
##
# Parses a subpattern to a correct type.
#
# @param [String] pattern
#
# @return [ISO8601::Duration, ISO8601::DateTime]
def parse_subpattern(pattern)
return ISO8601::Duration.new(pattern) if pattern.start_with?('P')
ISO8601::DateTime.new(pattern)
end
def parse_start_subpattern(pattern)
return ISO8601::Duration.new("-#{pattern}") if pattern.start_with?('P')
ISO8601::DateTime.new(pattern)
end
##
# See the constructor methods.
#
# @param [Array] atoms
def from_atoms(atoms)
@atoms = atoms
@first, @last, @size = limits(@atoms)
end
##
# Calculates the limits (first, last) and the size of the interval.
#
# @param [Array] atoms The atoms result of parsing the pattern.
#
# @return [Array<(ISO8601::DateTime, ISO8601::DateTime, ISO8601::Duration)>]
def limits(atoms)
valid_atoms?(atoms)
return tuple_by_both(atoms) if atoms.none? { |x| x.is_a?(ISO8601::Duration) }
return tuple_by_end(atoms) if atoms.first.is_a?(ISO8601::Duration)
tuple_by_start(atoms)
end
def tuple_by_both(atoms)
[atoms.first,
atoms.last,
(atoms.last.to_time - atoms.first.to_time)]
end
def tuple_by_end(atoms)
seconds = atoms.first.to_seconds(atoms.last)
[(atoms.last + seconds),
atoms.last,
seconds.abs]
end
def tuple_by_start(atoms)
seconds = atoms.last.to_seconds(atoms.first)
[atoms.first,
(atoms.first + seconds),
seconds]
end
def valid_atoms?(atoms)
raise(ISO8601::Errors::UnknownPattern, "The pattern of a time interval can't be <duration>/<duration>") \
if atoms.all? { |x| x.is_a?(ISO8601::Duration) }
end
def valid_date_time?(time)
valid_date_time?(time)
end
end
end