Module: Time::Units
- Included in:
- Numeric
- Defined in:
- lib/familia/core_ext.rb
Overview
Provides methods for working with various time units
Constant Summary collapse
- PER_MICROSECOND =
rubocop:disable Style/SingleLineMethods, Layout/ExtraSpacing
0.000001
- PER_MILLISECOND =
0.001
- PER_MINUTE =
60.0
- PER_HOUR =
3600.0
- PER_DAY =
86_400.0
Instance Method Summary collapse
-
#days ⇒ Object
(also: #day)
-
#hours ⇒ Object
(also: #hour)
-
#in_days ⇒ Object
-
#in_hours ⇒ Object
-
#in_microseconds ⇒ Object
-
#in_milliseconds ⇒ Object
-
#in_minutes ⇒ Object
-
#in_seconds(u = nil) ⇒ Float
Converts seconds to the specified time unit.
-
#in_time ⇒ Time
Converts seconds to a Time object.
-
#in_weeks ⇒ Object
-
#in_years ⇒ Object
From seconds -> other time units.
-
#microseconds ⇒ Object
(also: #μs)
Conversion methods.
-
#milliseconds ⇒ Object
(also: #ms)
-
#minutes ⇒ Object
(also: #minute)
-
#seconds ⇒ Object
(also: #second)
-
#weeks ⇒ Object
(also: #week)
-
#years ⇒ Object
(also: #year)
Instance Method Details
#days ⇒ Object Also known as: day
42 |
# File 'lib/familia/core_ext.rb', line 42 def days() seconds * PER_DAY end |
#hours ⇒ Object Also known as: hour
41 |
# File 'lib/familia/core_ext.rb', line 41 def hours() seconds * PER_HOUR end |
#in_days ⇒ Object
50 |
# File 'lib/familia/core_ext.rb', line 50 def in_days() seconds / PER_DAY end |
#in_hours ⇒ Object
51 |
# File 'lib/familia/core_ext.rb', line 51 def in_hours() seconds / PER_HOUR end |
#in_microseconds ⇒ Object
54 |
# File 'lib/familia/core_ext.rb', line 54 def in_microseconds() seconds / PER_MICROSECOND end |
#in_milliseconds ⇒ Object
53 |
# File 'lib/familia/core_ext.rb', line 53 def in_milliseconds() seconds / PER_MILLISECOND end |
#in_minutes ⇒ Object
52 |
# File 'lib/familia/core_ext.rb', line 52 def in_minutes() seconds / PER_MINUTE end |
#in_seconds(u = nil) ⇒ Float
Converts seconds to the specified time unit
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/familia/core_ext.rb', line 68 def in_seconds(u = nil) case u.to_s when /\A(y)|(years?)\z/ years when /\A(w)|(weeks?)\z/ weeks when /\A(d)|(days?)\z/ days when /\A(h)|(hours?)\z/ hours when /\A(m)|(minutes?)\z/ minutes when /\A(ms)|(milliseconds?)\z/ milliseconds when /\A(us)|(microseconds?)|(μs)\z/ microseconds else self end end |
#in_time ⇒ Time
Converts seconds to a Time object
60 61 62 |
# File 'lib/familia/core_ext.rb', line 60 def in_time Time.at(self).utc end |
#in_weeks ⇒ Object
49 |
# File 'lib/familia/core_ext.rb', line 49 def in_weeks() seconds / PER_DAY / 7 end |
#in_years ⇒ Object
From seconds -> other time units
48 |
# File 'lib/familia/core_ext.rb', line 48 def in_years() seconds / PER_DAY / 365 end |
#microseconds ⇒ Object Also known as: μs
Conversion methods
From other time units -> seconds
37 |
# File 'lib/familia/core_ext.rb', line 37 def microseconds() seconds * PER_MICROSECOND end |
#milliseconds ⇒ Object Also known as: ms
38 |
# File 'lib/familia/core_ext.rb', line 38 def milliseconds() seconds * PER_MILLISECOND end |
#minutes ⇒ Object Also known as: minute
40 |
# File 'lib/familia/core_ext.rb', line 40 def minutes() seconds * PER_MINUTE end |
#seconds ⇒ Object Also known as: second
39 |
# File 'lib/familia/core_ext.rb', line 39 def seconds() self end |
#weeks ⇒ Object Also known as: week
43 |
# File 'lib/familia/core_ext.rb', line 43 def weeks() seconds * PER_DAY * 7 end |
#years ⇒ Object Also known as: year
44 |
# File 'lib/familia/core_ext.rb', line 44 def years() seconds * PER_DAY * 365 end |