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.



274
275
276
277
278
279
280
281
282
# 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

Instance Method Details

#add(type, val) ⇒ Object

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



292
293
294
295
296
297
298
299
300
301
302
# 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) ⇒ Object



306
307
308
309
310
311
312
# 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_dayObject

Move to the first of the month, 0 hours



339
340
341
# File 'lib/ice_cube/time_util.rb', line 339

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

#clear_hourObject



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

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

#clear_minObject



330
331
332
# File 'lib/ice_cube/time_util.rb', line 330

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

#clear_monthObject

Clear to january 1st



344
345
346
347
348
349
350
# 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_secObject



326
327
328
# File 'lib/ice_cube/time_util.rb', line 326

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

#hour=(value) ⇒ Object



314
315
316
# File 'lib/ice_cube/time_util.rb', line 314

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

#min=(value) ⇒ Object



318
319
320
# File 'lib/ice_cube/time_util.rb', line 318

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

#sec=(value) ⇒ Object



322
323
324
# File 'lib/ice_cube/time_util.rb', line 322

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

#to_timeObject

Get the wrapped time back in its original zone & format



285
286
287
288
289
# 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