class IceCube::TimeUtil::TimeWrapper
A utility class for safely moving time around
Constants
- CLEAR_ORDER
-
Clear everything below a certain type
Public Class Methods
Source
# File lib/ice_cube/time_util.rb, line 268 def initialize(time, dst_adjust = true) @dst_adjust = dst_adjust @base = time @time = if dst_adjust Time.utc(time.year, time.month, time.day, time.hour, time.min, time.sec + TimeUtil.subsec(time)) else time end end
Public Instance Methods
Source
# File lib/ice_cube/time_util.rb, line 286 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
DST-safely add an interval of time to the wrapped time
Source
# File lib/ice_cube/time_util.rb, line 300 def clear_below(type) type = :day if type == :wday CLEAR_ORDER.each do |ptype| break if ptype == type send :"clear_#{ptype}" end end
Source
# File lib/ice_cube/time_util.rb, line 333 def clear_day (@time.day > 1) ? @time -= (@time.day - 1) * ONE_DAY : @time end
Move to the first of the month, 0 hours
Source
# File lib/ice_cube/time_util.rb, line 328 def clear_hour (@time.hour > 0) ? @time -= (@time.hour * ONE_HOUR) : @time end
Source
# File lib/ice_cube/time_util.rb, line 324 def clear_min (@time.min > 0) ? @time -= (@time.min * ONE_MINUTE) : @time end
Source
# File lib/ice_cube/time_util.rb, line 338 def clear_month @time -= ONE_DAY until @time.month == 12 @time -= TimeUtil.days_in_month(@time) * ONE_DAY end @time += ONE_DAY end
Clear to january 1st
Source
# File lib/ice_cube/time_util.rb, line 320 def clear_sec (@time.sec > 0) ? @time -= @time.sec : @time end
Source
# File lib/ice_cube/time_util.rb, line 308 def hour=(value) @time += (value * ONE_HOUR) - (@time.hour * ONE_HOUR) end
Source
# File lib/ice_cube/time_util.rb, line 312 def min=(value) @time += (value * ONE_MINUTE) - (@time.min * ONE_MINUTE) end
Source
# File lib/ice_cube/time_util.rb, line 316 def sec=(value) @time += value - @time.sec end
Source
# File lib/ice_cube/time_util.rb, line 279 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
Get the wrapped time back in its original zone & format