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 274 def initialize(time, dst_adjust = true) @dst_adjust = dst_adjust @base = time if dst_adjust @time = Time.utc(time.year, time.month, time.day, time.hour, time.min, time.sec + TimeUtil.subsec(time)) else @time = time end 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 292 def add(type, val) type = :day if type == :wday @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
clear_below(type)
click to toggle source
# File lib/ice_cube/time_util.rb, line 306 def clear_below(type) type = :day if type == :wday CLEAR_ORDER.each do |ptype| break if ptype == type send :"clear_#{ptype}" 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 339 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 334 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 330 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 344 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 326 def clear_sec @time.sec > 0 ? @time -= @time.sec : @time end
hour=(value)
click to toggle source
# File lib/ice_cube/time_util.rb, line 314 def hour=(value) @time += (value * ONE_HOUR) - (@time.hour * ONE_HOUR) end
min=(value)
click to toggle source
# File lib/ice_cube/time_util.rb, line 318 def min=(value) @time += (value * ONE_MINUTE) - (@time.min * ONE_MINUTE) end
sec=(value)
click to toggle source
# File lib/ice_cube/time_util.rb, line 322 def sec=(value) @time += (value) - (@time.sec) end
to_time()
click to toggle source
Get the wrapped time back in its original zone & format
# File lib/ice_cube/time_util.rb, line 285 def to_time return @time unless @dst_adjust parts = @time.year, @time.month, @time.day, @time.hour, @time.min, @time.sec + @time.subsec TimeUtil.build_in_zone(parts, @base) end