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.



268
269
270
271
272
273
274
275
276
# 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

Instance Method Details

#add(type, val) ⇒ Object

DST-safely add an interval of time to the wrapped time



286
287
288
289
290
291
292
293
294
295
296
# 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

#clear_below(type) ⇒ Object



300
301
302
303
304
305
306
# 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

#clear_dayObject

Move to the first of the month, 0 hours



333
334
335
# File 'lib/ice_cube/time_util.rb', line 333

def clear_day
  (@time.day > 1) ? @time -= (@time.day - 1) * ONE_DAY : @time
end

#clear_hourObject



328
329
330
# File 'lib/ice_cube/time_util.rb', line 328

def clear_hour
  (@time.hour > 0) ? @time -= (@time.hour * ONE_HOUR) : @time
end

#clear_minObject



324
325
326
# File 'lib/ice_cube/time_util.rb', line 324

def clear_min
  (@time.min > 0) ? @time -= (@time.min * ONE_MINUTE) : @time
end

#clear_monthObject

Clear to january 1st



338
339
340
341
342
343
344
# 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_secObject



320
321
322
# File 'lib/ice_cube/time_util.rb', line 320

def clear_sec
  (@time.sec > 0) ? @time -= @time.sec : @time
end

#hour=(value) ⇒ Object



308
309
310
# File 'lib/ice_cube/time_util.rb', line 308

def hour=(value)
  @time += (value * ONE_HOUR) - (@time.hour * ONE_HOUR)
end

#min=(value) ⇒ Object



312
313
314
# File 'lib/ice_cube/time_util.rb', line 312

def min=(value)
  @time += (value * ONE_MINUTE) - (@time.min * ONE_MINUTE)
end

#sec=(value) ⇒ Object



316
317
318
# File 'lib/ice_cube/time_util.rb', line 316

def sec=(value)
  @time += value - @time.sec
end

#to_timeObject

Get the wrapped time back in its original zone & format



279
280
281
282
283
# 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