class Time

see also rtmatheson.com/2011/12/rounding-time-to-the-closest-hour-in-ruby/

Public Class Methods

parse(time) click to toggle source
# File lib/time_utilities.rb, line 8
def parse(time)
  # Chronic will usually return nil when unable to parse time
  # it throws an error, on 't' and a few other strings, so we
  # capture these here an assure that nil is returned
  begin
    chron = Chronic.parse time
    chron.round
  rescue Exception => e
    return nil
  end
end

Public Instance Methods

add_days(days) click to toggle source
# File lib/time_utilities.rb, line 27
def add_days(days)
  t = self + days * 86400 # 24 * 60 * 60
end
on_date(date) click to toggle source
# File lib/time_utilities.rb, line 31
def on_date(date)
  raise ArgumentError if ! date.kind_of? Time
  Time.new(date.year, date.month, date.day, self.hour, self.min, self.sec)
end
round(options={}) click to toggle source

default to whole minutes

# File lib/time_utilities.rb, line 22
def round(options={})
  seconds = 60
  Time.at((self.to_f / seconds).round * seconds)
end
same_day?(date) click to toggle source
# File lib/time_utilities.rb, line 36
def same_day?(date)
  raise ArgumentError if ! date.kind_of? Time
  Time.new(date.year, date.month, date.day) == Time.new(self.year, self.month, self.day)
end