Module: Familia::Refinements::TimeLiterals::NumericMethods
- Defined in:
- lib/familia/refinements/time_literals.rb
Instance Method Summary collapse
- #after(time) ⇒ Object
-
#age_in(unit, from_time = nil) ⇒ Float
Calculates age of timestamp in specified unit from reference time.
-
#ago ⇒ Object
Time manipulation.
- #before(time) ⇒ Object
- #day ⇒ Object
- #days ⇒ Object
-
#days_old ⇒ Float
Convenience methods for
age_in(unit)calls. - #from_now ⇒ Object
- #hour ⇒ Object
- #hours ⇒ Object
- #hours_old ⇒ Object
-
#humanize ⇒ String
Converts the number to a human-readable string representation.
- #in_days ⇒ Object
- #in_hours ⇒ Object
- #in_microseconds ⇒ Object
- #in_milliseconds ⇒ Object
- #in_minutes ⇒ Object
- #in_months ⇒ Object
-
#in_seconds(unit = nil) ⇒ Float
Converts seconds to specified time unit.
- #in_time ⇒ Object
- #in_weeks ⇒ Object
-
#in_years ⇒ Object
Seconds -> other time units.
-
#microsecond ⇒ Object
Aliases with singular forms.
- #microseconds ⇒ Object
- #millisecond ⇒ Object
- #milliseconds ⇒ Object
- #minute ⇒ Object
- #minutes ⇒ Object
- #minutes_old ⇒ Object
- #month ⇒ Object
- #months ⇒ Object
- #months_old ⇒ Object
-
#ms ⇒ Object
Shortest aliases.
-
#newer_than?(duration) ⇒ Boolean
Checks if timestamp is newer than specified duration in the future.
-
#older_than?(duration) ⇒ Boolean
Checks if timestamp is older than specified duration in seconds.
- #second ⇒ Object
- #seconds ⇒ Object
-
#to_bytes ⇒ String
Converts the number to a human-readable byte representation using binary units.
-
#to_ms ⇒ Object
Milliseconds conversion.
- #week ⇒ Object
- #weeks ⇒ Object
- #weeks_old ⇒ Object
-
#within?(duration) ⇒ Boolean
Checks if timestamp is within specified duration of now (past or future).
- #year ⇒ Object
- #years ⇒ Object
- #years_old ⇒ Object
- #μs ⇒ Object
Instance Method Details
#after(time) ⇒ Object
132 |
# File 'lib/familia/refinements/time_literals.rb', line 132 def after(time) = time + seconds |
#age_in(unit, from_time = nil) ⇒ Float
Calculates age of timestamp in specified unit from reference time
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/familia/refinements/time_literals.rb', line 199 def age_in(unit, from_time = nil) from_time ||= Familia.now age_seconds = from_time.to_f - to_f case UNIT_METHODS.fetch(unit.to_s.downcase, nil) when :days then age_seconds / PER_DAY when :hours then age_seconds / PER_HOUR when :minutes then age_seconds / PER_MINUTE when :weeks then age_seconds / PER_WEEK when :months then age_seconds / PER_MONTH when :years then age_seconds / PER_YEAR else age_seconds end end |
#ago ⇒ Object
Time manipulation
129 |
# File 'lib/familia/refinements/time_literals.rb', line 129 def ago = Familia.now - seconds |
#before(time) ⇒ Object
131 |
# File 'lib/familia/refinements/time_literals.rb', line 131 def before(time) = time - seconds |
#day ⇒ Object
108 |
# File 'lib/familia/refinements/time_literals.rb', line 108 def day = days |
#days ⇒ Object
97 |
# File 'lib/familia/refinements/time_literals.rb', line 97 def days = seconds * PER_DAY |
#days_old ⇒ Float
Convenience methods for age_in(unit) calls.
219 |
# File 'lib/familia/refinements/time_literals.rb', line 219 def days_old(*) = age_in(:days, *) |
#from_now ⇒ Object
130 |
# File 'lib/familia/refinements/time_literals.rb', line 130 def from_now = Familia.now + seconds |
#hour ⇒ Object
107 |
# File 'lib/familia/refinements/time_literals.rb', line 107 def hour = hours |
#hours ⇒ Object
96 |
# File 'lib/familia/refinements/time_literals.rb', line 96 def hours = seconds * PER_HOUR |
#hours_old ⇒ Object
220 |
# File 'lib/familia/refinements/time_literals.rb', line 220 def hours_old(*) = age_in(:hours, *) |
#humanize ⇒ String
Converts the number to a human-readable string representation
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/familia/refinements/time_literals.rb', line 155 def humanize gte_zero = positive? || zero? duration = (gte_zero ? self : abs) # let's keep it positive up in here text = case (num = duration.to_i) in 0..59 then "#{num} second#{'s' if num != 1}" in 60..3599 then "#{num /= 60} minute#{'s' if num != 1}" in 3600..86_399 then "#{num /= 3600} hour#{'s' if num != 1}" else "#{num /= 86_400} day#{'s' if num != 1}" end gte_zero ? text : "#{text} ago" end |
#in_days ⇒ Object
121 |
# File 'lib/familia/refinements/time_literals.rb', line 121 def in_days = seconds / PER_DAY |
#in_hours ⇒ Object
122 |
# File 'lib/familia/refinements/time_literals.rb', line 122 def in_hours = seconds / PER_HOUR |
#in_microseconds ⇒ Object
125 |
# File 'lib/familia/refinements/time_literals.rb', line 125 def in_microseconds = seconds / PER_MICROSECOND |
#in_milliseconds ⇒ Object
124 |
# File 'lib/familia/refinements/time_literals.rb', line 124 def in_milliseconds = seconds / PER_MILLISECOND |
#in_minutes ⇒ Object
123 |
# File 'lib/familia/refinements/time_literals.rb', line 123 def in_minutes = seconds / PER_MINUTE |
#in_months ⇒ Object
119 |
# File 'lib/familia/refinements/time_literals.rb', line 119 def in_months = seconds / PER_MONTH |
#in_seconds(unit = nil) ⇒ Float
Converts seconds to specified time unit
126 |
# File 'lib/familia/refinements/time_literals.rb', line 126 def in_seconds = seconds # for semantic purposes |
#in_time ⇒ Object
133 |
# File 'lib/familia/refinements/time_literals.rb', line 133 def in_time = Time.at(seconds).utc |
#in_weeks ⇒ Object
120 |
# File 'lib/familia/refinements/time_literals.rb', line 120 def in_weeks = seconds / PER_WEEK |
#in_years ⇒ Object
Seconds -> other time units
118 |
# File 'lib/familia/refinements/time_literals.rb', line 118 def in_years = seconds / PER_YEAR |
#microsecond ⇒ Object
Aliases with singular forms
103 |
# File 'lib/familia/refinements/time_literals.rb', line 103 def microsecond = microseconds |
#microseconds ⇒ Object
92 |
# File 'lib/familia/refinements/time_literals.rb', line 92 def microseconds = seconds * PER_MICROSECOND |
#millisecond ⇒ Object
104 |
# File 'lib/familia/refinements/time_literals.rb', line 104 def millisecond = milliseconds |
#milliseconds ⇒ Object
93 |
# File 'lib/familia/refinements/time_literals.rb', line 93 def milliseconds = seconds * PER_MILLISECOND |
#minute ⇒ Object
106 |
# File 'lib/familia/refinements/time_literals.rb', line 106 def minute = minutes |
#minutes ⇒ Object
95 |
# File 'lib/familia/refinements/time_literals.rb', line 95 def minutes = seconds * PER_MINUTE |
#minutes_old ⇒ Object
221 |
# File 'lib/familia/refinements/time_literals.rb', line 221 def minutes_old(*) = age_in(:minutes, *) |
#month ⇒ Object
110 |
# File 'lib/familia/refinements/time_literals.rb', line 110 def month = months |
#months ⇒ Object
99 |
# File 'lib/familia/refinements/time_literals.rb', line 99 def months = seconds * PER_MONTH |
#months_old ⇒ Object
223 |
# File 'lib/familia/refinements/time_literals.rb', line 223 def months_old(*) = age_in(:months, *) |
#ms ⇒ Object
Shortest aliases
114 |
# File 'lib/familia/refinements/time_literals.rb', line 114 def ms = milliseconds |
#newer_than?(duration) ⇒ Boolean
Checks if timestamp is newer than specified duration in the future
243 244 245 |
# File 'lib/familia/refinements/time_literals.rb', line 243 def newer_than?(duration) self > (Familia.now + duration) end |
#older_than?(duration) ⇒ Boolean
Both older_than? and newer_than? can return false when timestamp is within the same second. Use within? to check this case.
Checks if timestamp is older than specified duration in seconds
235 236 237 |
# File 'lib/familia/refinements/time_literals.rb', line 235 def older_than?(duration) self < (Familia.now - duration) end |
#second ⇒ Object
105 |
# File 'lib/familia/refinements/time_literals.rb', line 105 def second = seconds |
#seconds ⇒ Object
94 |
# File 'lib/familia/refinements/time_literals.rb', line 94 def seconds = self |
#to_bytes ⇒ String
Converts the number to a human-readable byte representation using binary units
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/familia/refinements/time_literals.rb', line 176 def to_bytes units = %w[B KiB MiB GiB TiB] size = abs.to_f unit = 0 while size >= 1024 && unit < units.length - 1 size /= 1024 unit += 1 end format('%3.2f %s', size, units[unit]) end |
#to_ms ⇒ Object
Milliseconds conversion
136 |
# File 'lib/familia/refinements/time_literals.rb', line 136 def to_ms = seconds * 1000.0 |
#week ⇒ Object
109 |
# File 'lib/familia/refinements/time_literals.rb', line 109 def week = weeks |
#weeks ⇒ Object
98 |
# File 'lib/familia/refinements/time_literals.rb', line 98 def weeks = seconds * PER_WEEK |
#weeks_old ⇒ Object
222 |
# File 'lib/familia/refinements/time_literals.rb', line 222 def weeks_old(*) = age_in(:weeks, *) |
#within?(duration) ⇒ Boolean
Checks if timestamp is within specified duration of now (past or future)
255 256 257 |
# File 'lib/familia/refinements/time_literals.rb', line 255 def within?(duration) (self - Familia.now).abs <= duration end |
#year ⇒ Object
111 |
# File 'lib/familia/refinements/time_literals.rb', line 111 def year = years |
#years ⇒ Object
100 |
# File 'lib/familia/refinements/time_literals.rb', line 100 def years = seconds * PER_YEAR |
#years_old ⇒ Object
224 |
# File 'lib/familia/refinements/time_literals.rb', line 224 def years_old(*) = age_in(:years, *) |
#μs ⇒ Object
115 |
# File 'lib/familia/refinements/time_literals.rb', line 115 def μs = microseconds |