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.



144
145
146
147
# File 'lib/ice_cube/time_util.rb', line 144

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



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/ice_cube/time_util.rb', line 155

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



171
172
173
174
175
176
177
178
179
# File 'lib/ice_cube/time_util.rb', line 171

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



150
151
152
# File 'lib/ice_cube/time_util.rb', line 150

def to_time
  @time
end