Class: IceCube::TimeUtil::TimeWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/ice_cube/time_util.rb

Overview

A utility class for safely moving time around

Constant Summary collapse

CLEAR_ORDER =

Clear everything below a certain type

[:sec, :min, :hour, :day, :month, :year]

Instance Method Summary collapse

Constructor Details

#initialize(time, dst_adjust = true) ⇒ TimeWrapper

Returns a new instance of TimeWrapper.



167
168
169
170
# File 'lib/ice_cube/time_util.rb', line 167

def initialize(time, dst_adjust = true)
  @dst_adjust = dst_adjust
  @time = time
end

Instance Method Details

#add(type, val) ⇒ Object

DST-safely add an interval of time to the wrapped time



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ice_cube/time_util.rb', line 178

def add(type, val)
  type = :day if type == :wday
  adjust do
    @time += case type
    when :year then TimeUtil.days_in_n_years(@time, val) * ONE_DAY
    when :month then TimeUtil.days_in_n_months(@time, val) * ONE_DAY
    when :day  then val * ONE_DAY
    when :hour then val * ONE_HOUR
    when :min  then val * ONE_MINUTE
    when :sec  then val
    end
  end
end

#clear_below(type) ⇒ Object



194
195
196
197
198
199
200
201
202
# File 'lib/ice_cube/time_util.rb', line 194

def clear_below(type)
  type = :day if type == :wday
  CLEAR_ORDER.each do |ptype|
    break if ptype == type
    adjust do
      send(:"clear_#{ptype}")
    end
  end
end

#to_timeObject

Get the wrapper time back



173
174
175
# File 'lib/ice_cube/time_util.rb', line 173

def to_time
  @time
end