class IceCube::TimeUtil::TimeWrapper
A utility class for safely moving time around
Constants
- CLEAR_ORDER
Clear everything below a certain type
Public Class Methods
new(time, dst_adjust = true)
click to toggle source
# File lib/ice_cube/time_util.rb, line 245 def initialize(time, dst_adjust = true) @dst_adjust = dst_adjust @time = time end
Public Instance Methods
add(type, val)
click to toggle source
DST-safely add an interval of time to the wrapped time
# 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)
click to toggle source
# 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_time()
click to toggle source
Get the wrapper time back
# File lib/ice_cube/time_util.rb, line 251 def to_time @time end
Private Instance Methods
adjust() { || ... }
click to toggle source
# File lib/ice_cube/time_util.rb, line 284 def adjust(&block) if @dst_adjust off = @time.utc_offset yield diff = off - @time.utc_offset @time += diff if diff != 0 else yield end end
clear_day()
click to toggle source
Move to the first of the month, 0 hours
# File lib/ice_cube/time_util.rb, line 308 def clear_day @time.day > 1 ? @time -= (@time.day - 1) * ONE_DAY : @time end
clear_hour()
click to toggle source
# File lib/ice_cube/time_util.rb, line 303 def clear_hour @time.hour > 0 ? @time -= (@time.hour * ONE_HOUR) : @time end
clear_min()
click to toggle source
# File lib/ice_cube/time_util.rb, line 299 def clear_min @time.min > 0 ? @time -= (@time.min * ONE_MINUTE) : @time end
clear_month()
click to toggle source
Clear to january 1st
# File lib/ice_cube/time_util.rb, line 313 def clear_month @time -= ONE_DAY until @time.month == 12 @time -= TimeUtil.days_in_month(@time) * ONE_DAY end @time += ONE_DAY end
clear_sec()
click to toggle source
# File lib/ice_cube/time_util.rb, line 295 def clear_sec @time.sec > 0 ? @time -= @time.sec : @time end
clear_year()
click to toggle source
# File lib/ice_cube/time_util.rb, line 321 def clear_year @time end