Class: TimePeriod

Inherits:
TimeStep show all
Defined in:
lib/timesteps/timeperiod.rb

Instance Attribute Summary collapse

Attributes inherited from TimeStep

#calendar, #interval, #numeric, #origin, #symbol

Instance Method Summary collapse

Methods inherited from TimeStep

#==, #definition, #duration_at, #in, #index_at, #interval_spec, #new_origin, #next_index_of, #next_time_of, #offset, #origin_spec, #parse, #period, #prev_index_of, #prev_time_of, #query, #range, #right_time?, split_interval_spec, #time_at, #to, #truncate

Constructor Details

#initialize(spec, since: nil, format: nil, offset: nil, calendar: "standard", tz: nil, ends: "[]") ⇒ TimePeriod



5
6
7
8
9
10
11
12
# File 'lib/timesteps/timeperiod.rb', line 5

def initialize (spec, since: nil, format: nil, offset: nil, calendar: "standard", tz: nil, ends: "[]" )
  super(spec, since: since, format: format, offset: offset, calendar: calendar, tz: tz)
  raise "invalid ends specification" unless ends =~ /\A[\[\(][\]\)]\z/
  @ends = ends
  @include_start = ( ends[0] == "[" ) ? true : false
  @include_last  = ( ends[1] == "]" ) ? true : false 
  @last = time_at(1)
end

Instance Attribute Details

#endsObject (readonly)

Returns the value of attribute ends.



14
15
16
# File 'lib/timesteps/timeperiod.rb', line 14

def ends
  @ends
end

#lastObject (readonly)

Returns the value of attribute last.



14
15
16
# File 'lib/timesteps/timeperiod.rb', line 14

def last
  @last
end

Instance Method Details

#clip(other) ⇒ Object



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
# File 'lib/timesteps/timeperiod.rb', line 142

def clip (other)
  raise "can't clip period without contacted period" unless contact?(other)
  if ( @origin < other.origin ) ||
     ( @origin == other.origin && include_start? && ( not other.include_start? ) )
    left = other.origin
    left_end = other.include_start? ? "[" : "("
  else
    left = @origin
    left_end = include_start? ? "[" : "("      
  end
  if ( other.last < @last ) ||
     ( other.last == @last && include_last? && ( not other.include_last? ) )
    right = other.last
    right_end = other.include_last? ? "]" : ")"
  else
    right = @last
    right_end = include_last? ? ")" : "]"      
  end
  ridx = index_at(right)
  lidx = index_at(left)
  numeric = (ridx - lidx) * @numeric
  origin = left
  interval_spec = format("%g %s", numeric, @symbol)
  ends = left_end + right_end
  return TimePeriod.new(interval_spec, since: origin, calendar: @calendar, ends: ends)
end

#contact?(other) ⇒ Boolean



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/timesteps/timeperiod.rb', line 94

def contact? (other)
  case other
  when TimePeriod
    if @origin <= other.last
       left = true
     else
       left = false
    end
    if other.origin <= @last 
       right = true
     else
       right = false
    end
    return left && right
  else
    left  = @origin <= other
    right = other <= @last
    return left & right
  end    
end

#include?(other) ⇒ Boolean



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
# File 'lib/timesteps/timeperiod.rb', line 42

def include? (other)
  case other
  when TimePeriod
    if ( other.origin < @origin ) ||
       ( other.origin == @origin && ( not @include_start ) && other.include_start? )
      left = false
    else
      left = true
    end
    if ( @last < other.last ) ||
       ( @last == other.last && ( not @include_last ) && other.include_last? )
      right = false
    else
      right = true        
    end        
    return left & right
  else
    if @include_start
      left = @origin <= other
    else
      left = @origin < other
    end
    if @include_last
      right = other <= @last
    else
      right = other < @last
    end
    return left & right
  end
end

#include_last?Boolean



24
25
26
# File 'lib/timesteps/timeperiod.rb', line 24

def include_last?
  return @include_last
end

#include_start?Boolean



20
21
22
# File 'lib/timesteps/timeperiod.rb', line 20

def include_start?
  return @include_start
end

#inspectObject

Returns the value as a string for inspection.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/timesteps/timeperiod.rb', line 30

def inspect
  options = ""
  case @calendar.name
  when "standard", "gregorian"
  else
    options << " calendar='#{calendar.name}'"
  end
  left_paren = @include_start ? "[" : "("
  right_paren = @include_last ? "]" : ")"
  "#<TimePeriod '#{interval_spec}' #{left_paren}#{start.to_s}, #{last.to_s}#{right_paren} #{options}>"
end

#merge(other) ⇒ Object



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
# File 'lib/timesteps/timeperiod.rb', line 115

def merge (other)
  raise "can't merge period without contacted period" unless contact?(other)
  if ( other.origin < @origin ) ||
     ( other.origin == @origin && ( not include_start? ) && other.include_start? )
    left = other.origin
    left_end = other.include_start? ? "[" : "("
  else
    left = @origin
    left_end = include_start? ? "[" : "("      
  end
  if ( @last < other.last ) ||
     ( @last == other.last && ( not include_last? ) && other.include_last? )
    right = other.last
    right_end = other.include_last? ? "]" : ")"
  else
    right = @last
    right_end = include_last? ? ")" : "]"      
  end
  ridx = index_at(right)
  lidx = index_at(left)
  numeric = (ridx - lidx) * @numeric
  origin = left
  interval_spec = format("%g %s", numeric, @symbol)
  ends = left_end + right_end
  return TimePeriod.new(interval_spec, since: origin, calendar: @calendar, ends: ends)
end

#nextObject



184
185
186
# File 'lib/timesteps/timeperiod.rb', line 184

def next
  return TimePeriod.new(interval_spec, since: @last, calendar: @calendar, ends: ends)    
end

#overlap?(other) ⇒ Boolean



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/timesteps/timeperiod.rb', line 73

def overlap? (other)
  case other
  when TimePeriod
    if ( @origin < other.last ) ||
       ( @origin == other.last && @include_start && other.include_last?  )
       left = true
     else
       left = false
    end
    if ( other.origin < @last ) ||
       ( other.origin == @last && @include_last && other.include_start?  )
       right = true
     else
       right = false
    end
    return left && right
  else
    return include?(other)
  end    
end

#prevObject



188
189
190
191
# File 'lib/timesteps/timeperiod.rb', line 188

def prev
  origin = index_at(-1)
  return TimePeriod.new(interval_spec, since: origin, calendar: @calendar, ends: ends)    
end

#shift_origin(index, with: "index") ⇒ Object



193
194
195
196
197
198
199
200
201
202
# File 'lib/timesteps/timeperiod.rb', line 193

def shift_origin (index, with: "index")
  case with
  when :index, "index"
    return TimePeriod.new(interval_spec, since: time_at(index), calendar: @calendar, ends: ends)    
  when :duration, "duration", :days, "days"
    time = @origin + index
    return TimePeriod.new(interval_spec, since: time, calendar: @calendar, ends: ends)
  end

end

#split(time, ends: ")[") ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/timesteps/timeperiod.rb', line 169

def split (time, ends: ")[")
  raise "can't split period without contacted time" unless contact?(time)
  # left
  numeric = index_at(time) * @numeric
  interval_spec = format("%g %s", numeric, @symbol)
  ends   = (@include_start ? "[" : "(") + ends[0]
  left_period  = TimePeriod.new(interval_spec, since: @origin, calendar: @calendar, ends: ends)
  # right
  numeric = (1 - index_at(time)) * @numeric
  interval_spec = format("%g %s", numeric, @symbol)
  ends   = ends[1] + (@include_last ? "]" : ")")
  right_period  = TimePeriod.new(interval_spec, since: @origin, calendar: @calendar, ends: ends)
  return left_period, right_period
end

#startObject



16
17
18
# File 'lib/timesteps/timeperiod.rb', line 16

def start
  return @origin
end