Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/time_utilities.rb

Overview

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(time) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#add_days(days) ⇒ Object



27
28
29
# File 'lib/time_utilities.rb', line 27

def add_days(days)
  t = self + days * 86400 # 24 * 60 * 60
end

#on_date(date) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
# 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 = {}) ⇒ Object

default to whole minutes



22
23
24
25
# File 'lib/time_utilities.rb', line 22

def round(options={})
  seconds = 60
  Time.at((self.to_f / seconds).round * seconds)
end

#same_day?(date) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


36
37
38
39
# 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