Class: IceCube::TimeUtil::TimeWrapper
- Inherits:
-
Object
- Object
- IceCube::TimeUtil::TimeWrapper
- 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
-
#add(type, val) ⇒ Object
DST-safely add an interval of time to the wrapped time.
- #clear_below(type) ⇒ Object
-
#initialize(time, dst_adjust = true) ⇒ TimeWrapper
constructor
A new instance of TimeWrapper.
-
#to_time ⇒ Object
Get the wrapped time back in its original zone & format.
Constructor Details
#initialize(time, dst_adjust = true) ⇒ TimeWrapper
Returns a new instance of TimeWrapper.
270 271 272 273 274 275 276 277 278 |
# File 'lib/ice_cube/time_util.rb', line 270 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 + time.subsec) else @time = time end end |
Instance Method Details
#add(type, val) ⇒ Object
DST-safely add an interval of time to the wrapped time
288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/ice_cube/time_util.rb', line 288 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
302 303 304 305 306 307 308 |
# File 'lib/ice_cube/time_util.rb', line 302 def clear_below(type) type = :day if type == :wday CLEAR_ORDER.each do |ptype| break if ptype == type send :"clear_#{ptype}" end end |
#to_time ⇒ Object
Get the wrapped time back in its original zone & format
281 282 283 284 285 |
# File 'lib/ice_cube/time_util.rb', line 281 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 |