Module: TimeCalculations

Included in:
Date, DateTime, Time
Defined in:
lib/time_calculations.rb,
lib/time_calculations/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Method Summary collapse

Instance Method Details

#beginning_of_hourObject



5
6
7
# File 'lib/time_calculations.rb', line 5

def beginning_of_hour
  change(:min => 0, :sec => 0, :usec => 0)
end

#beginning_of_minuteObject



9
10
11
# File 'lib/time_calculations.rb', line 9

def beginning_of_minute
  change(:sec => 0, :usec => 0)
end

#round_up(what) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/time_calculations.rb', line 13

def round_up(what)
  case what
  when :year
    self == beginning_of_year ? self : advance(:years => 1).beginning_of_year
  when :month
    self == beginning_of_month ? self : advance(:months => 1).beginning_of_month
  when :day
    return self unless acts_like?(:time)
    self == beginning_of_day ? self : advance(:days => 1).beginning_of_day
  when :hour
    return self unless acts_like?(:time)
    self == beginning_of_hour ? self : advance(:hours => 1).beginning_of_hour
  when :minute
    return self unless acts_like?(:time)
    self == beginning_of_minute ? self : advance(:minutes => 1).beginning_of_minute
  else
    raise ArgumentError, "Don't know how to round up #{what}"
  end
end