Class: Quant::TimePeriod

Inherits:
Object
  • Object
show all
Defined in:
lib/quant/time_period.rb

Constant Summary collapse

LOWER_BOUND =
TimeMethods::EPOCH_TIME

Instance Method Summary collapse

Constructor Details

#initialize(start_at: nil, end_at: nil, span: nil) ⇒ TimePeriod

Returns a new instance of TimePeriod.



7
8
9
10
11
12
13
14
# File 'lib/quant/time_period.rb', line 7

def initialize(start_at: nil, end_at: nil, span: nil)
  @start_at = as_start_time(start_at)
  @end_at = as_end_time(end_at)
  validate_bounds!

  @start_at = @end_at - span if !lower_bound? && span
  @end_at = @start_at + span if !upper_bound? && span
end

Instance Method Details

#==(other) ⇒ Object



74
75
76
77
78
79
# File 'lib/quant/time_period.rb', line 74

def ==(other)
  return false unless other.is_a?(TimePeriod)

  [lower_bound?, upper_bound?, start_at, end_at] ==
    [other.lower_bound?, other.upper_bound?, other.start_at, other.end_at]
end

#as_end_time(value) ⇒ Object



22
23
24
25
26
# File 'lib/quant/time_period.rb', line 22

def as_end_time(value)
  return value if value.nil? || value.is_a?(Time)

  value.is_a?(Date) ? end_of_day(value) : value.to_time
end

#as_start_time(value) ⇒ Object



16
17
18
19
20
# File 'lib/quant/time_period.rb', line 16

def as_start_time(value)
  return value if value.nil? || value.is_a?(Time)

  value.is_a?(Date) ? beginning_of_day(value) : value.to_time
end

#beginning_of_day(date) ⇒ Object



32
33
34
# File 'lib/quant/time_period.rb', line 32

def beginning_of_day(date)
  Time.utc(date.year, date.month, date.day)
end

#cover?(value) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/quant/time_period.rb', line 42

def cover?(value)
  (start_at..end_at).cover?(value)
end

#durationObject



70
71
72
# File 'lib/quant/time_period.rb', line 70

def duration
  end_at - start_at
end

#end_atObject



66
67
68
# File 'lib/quant/time_period.rb', line 66

def end_at
  (@end_at || Time.now.utc).round
end

#end_of_day(date) ⇒ Object



28
29
30
# File 'lib/quant/time_period.rb', line 28

def end_of_day(date)
  Time.utc(date.year, date.month, date.day, 23, 59, 59)
end

#lower_bound?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/quant/time_period.rb', line 50

def lower_bound?
  !lower_unbound?
end

#lower_unbound?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/quant/time_period.rb', line 54

def lower_unbound?
  @start_at.nil?
end

#start_atObject



46
47
48
# File 'lib/quant/time_period.rb', line 46

def start_at
  (@start_at || LOWER_BOUND).round
end

#to_hObject



81
82
83
# File 'lib/quant/time_period.rb', line 81

def to_h
  { start_at:, end_at: }
end

#upper_bound?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/quant/time_period.rb', line 62

def upper_bound?
  !upper_unbound?
end

#upper_unbound?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/quant/time_period.rb', line 58

def upper_unbound?
  @end_at.nil?
end

#validate_bounds!Object



36
37
38
39
40
# File 'lib/quant/time_period.rb', line 36

def validate_bounds!
  return if lower_bound? || upper_bound?

  raise "TimePeriod cannot be unbound at start_at and end_at"
end