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
-
#ago(time = ::Time.now) ⇒ Object
(also: #until)
Reads best without arguments: 10.minutes.ago.
- #days ⇒ Object (also: #day)
- #fortnights ⇒ Object (also: #fortnight)
- #hours ⇒ Object (also: #hour)
- #minutes ⇒ Object (also: #minute)
- #months ⇒ Object (also: #month)
- #seconds ⇒ Object (also: #second)
-
#since(time = ::Time.now) ⇒ Object
(also: #from_now)
Reads best with argument: 10.minutes.since(time).
- #weeks ⇒ Object (also: #week)
- #years ⇒ Object (also: #year)
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 |
#days ⇒ Object Also known as: day
29 30 31 |
# File 'lib/active_support/core_ext/numeric/time.rb', line 29 def days self * 24.hours end |
#fortnights ⇒ Object Also known as: fortnight
39 40 41 |
# File 'lib/active_support/core_ext/numeric/time.rb', line 39 def fortnights self * 2.weeks end |
#hours ⇒ Object Also known as: hour
24 25 26 |
# File 'lib/active_support/core_ext/numeric/time.rb', line 24 def hours self * 60.minutes end |
#minutes ⇒ Object Also known as: minute
19 20 21 |
# File 'lib/active_support/core_ext/numeric/time.rb', line 19 def minutes self * 60 end |
#months ⇒ Object Also known as: month
44 45 46 |
# File 'lib/active_support/core_ext/numeric/time.rb', line 44 def months self * 30.days end |
#seconds ⇒ Object 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 |
#weeks ⇒ Object Also known as: week
34 35 36 |
# File 'lib/active_support/core_ext/numeric/time.rb', line 34 def weeks self * 7.days end |
#years ⇒ Object 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 |