Module: ActiveSupport::CoreExtensions::Numeric::Time

Included in:
Numeric
Defined in:
lib/active_support/core_ext/numeric/time.rb

Overview

Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.

If you need precise date calculations that doesn’t just treat months as 30 days, then have a look at Time#advance.

Some of these methods are approximations, Ruby’s core Date and Time should be used for precision date and time arithmetic

Instance Method Summary collapse

Instance Method Details

#ago(time = ::Time.now) ⇒ Object Also known as: until

Reads best without arguments: 10.minutes.ago



55
56
57
# File 'lib/active_support/core_ext/numeric/time.rb', line 55

def ago(time = ::Time.now)
  time - self
end

#daysObject Also known as: day



29
30
31
# File 'lib/active_support/core_ext/numeric/time.rb', line 29

def days
  self * 24.hours
end

#fortnightsObject Also known as: fortnight



39
40
41
# File 'lib/active_support/core_ext/numeric/time.rb', line 39

def fortnights
  self * 2.weeks
end

#hoursObject Also known as: hour



24
25
26
# File 'lib/active_support/core_ext/numeric/time.rb', line 24

def hours
  self * 60.minutes
end

#minutesObject Also known as: minute



19
20
21
# File 'lib/active_support/core_ext/numeric/time.rb', line 19

def minutes
  self * 60
end

#monthsObject Also known as: month



44
45
46
# File 'lib/active_support/core_ext/numeric/time.rb', line 44

def months
  self * 30.days
end

#secondsObject Also known as: second



14
15
16
# File 'lib/active_support/core_ext/numeric/time.rb', line 14

def seconds
  self
end

#since(time = ::Time.now) ⇒ Object Also known as: from_now

Reads best with argument: 10.minutes.since(time)



63
64
65
# File 'lib/active_support/core_ext/numeric/time.rb', line 63

def since(time = ::Time.now)
  time + self
end

#weeksObject Also known as: week



34
35
36
# File 'lib/active_support/core_ext/numeric/time.rb', line 34

def weeks
  self * 7.days
end

#yearsObject Also known as: year



49
50
51
# File 'lib/active_support/core_ext/numeric/time.rb', line 49

def years
  (self * 365.25.days).to_i
end