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.



245
246
247
248
# File 'lib/ice_cube/time_util.rb', line 245

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



256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/ice_cube/time_util.rb', line 256

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



272
273
274
275
276
277
278
279
280
# File 'lib/ice_cube/time_util.rb', line 272

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



251
252
253
# File 'lib/ice_cube/time_util.rb', line 251

def to_time
  @time
end