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
134 |
# File 'lib/familia/refinements/time_literals.rb', line 134 def after(time) = time + seconds |
#age_in(unit, from_time = nil) ⇒ Float
Calculates age of timestamp in specified unit from reference time
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/familia/refinements/time_literals.rb', line 201 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
131 |
# File 'lib/familia/refinements/time_literals.rb', line 131 def ago = Familia.now - seconds |
#before(time) ⇒ Object
133 |
# File 'lib/familia/refinements/time_literals.rb', line 133 def before(time) = time - seconds |
#day ⇒ Object
110 |
# File 'lib/familia/refinements/time_literals.rb', line 110 def day = days |
#days ⇒ Object
99 |
# File 'lib/familia/refinements/time_literals.rb', line 99 def days = seconds * PER_DAY |
#days_old ⇒ Float
Convenience methods for age_in(unit) calls.
221 |
# File 'lib/familia/refinements/time_literals.rb', line 221 def days_old(*) = age_in(:days, *) |
#from_now ⇒ Object
132 |
# File 'lib/familia/refinements/time_literals.rb', line 132 def from_now = Familia.now + seconds |
#hour ⇒ Object
109 |
# File 'lib/familia/refinements/time_literals.rb', line 109 def hour = hours |
#hours ⇒ Object
98 |
# File 'lib/familia/refinements/time_literals.rb', line 98 def hours = seconds * PER_HOUR |
#hours_old ⇒ Object
222 |
# File 'lib/familia/refinements/time_literals.rb', line 222 def hours_old(*) = age_in(:hours, *) |
#humanize ⇒ String
Converts the number to a human-readable string representation
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/familia/refinements/time_literals.rb', line 157 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
123 |
# File 'lib/familia/refinements/time_literals.rb', line 123 def in_days = seconds / PER_DAY |
#in_hours ⇒ Object
124 |
# File 'lib/familia/refinements/time_literals.rb', line 124 def in_hours = seconds / PER_HOUR |
#in_microseconds ⇒ Object
127 |
# File 'lib/familia/refinements/time_literals.rb', line 127 def in_microseconds = seconds / PER_MICROSECOND |
#in_milliseconds ⇒ Object
126 |
# File 'lib/familia/refinements/time_literals.rb', line 126 def in_milliseconds = seconds / PER_MILLISECOND |
#in_minutes ⇒ Object
125 |
# File 'lib/familia/refinements/time_literals.rb', line 125 def in_minutes = seconds / PER_MINUTE |
#in_months ⇒ Object
121 |
# File 'lib/familia/refinements/time_literals.rb', line 121 def in_months = seconds / PER_MONTH |
#in_seconds(unit = nil) ⇒ Float
Converts seconds to specified time unit
128 |
# File 'lib/familia/refinements/time_literals.rb', line 128 def in_seconds = seconds # for semantic purposes |
#in_time ⇒ Object
135 |
# File 'lib/familia/refinements/time_literals.rb', line 135 def in_time = Time.at(seconds).utc |
#in_weeks ⇒ Object
122 |
# File 'lib/familia/refinements/time_literals.rb', line 122 def in_weeks = seconds / PER_WEEK |
#in_years ⇒ Object
Seconds -> other time units
120 |
# File 'lib/familia/refinements/time_literals.rb', line 120 def in_years = seconds / PER_YEAR |
#microsecond ⇒ Object
Aliases with singular forms
105 |
# File 'lib/familia/refinements/time_literals.rb', line 105 def microsecond = microseconds |
#microseconds ⇒ Object
94 |
# File 'lib/familia/refinements/time_literals.rb', line 94 def microseconds = seconds * PER_MICROSECOND |
#millisecond ⇒ Object
106 |
# File 'lib/familia/refinements/time_literals.rb', line 106 def millisecond = milliseconds |
#milliseconds ⇒ Object
95 |
# File 'lib/familia/refinements/time_literals.rb', line 95 def milliseconds = seconds * PER_MILLISECOND |
#minute ⇒ Object
108 |
# File 'lib/familia/refinements/time_literals.rb', line 108 def minute = minutes |
#minutes ⇒ Object
97 |
# File 'lib/familia/refinements/time_literals.rb', line 97 def minutes = seconds * PER_MINUTE |
#minutes_old ⇒ Object
223 |
# File 'lib/familia/refinements/time_literals.rb', line 223 def minutes_old(*) = age_in(:minutes, *) |
#month ⇒ Object
112 |
# File 'lib/familia/refinements/time_literals.rb', line 112 def month = months |
#months ⇒ Object
101 |
# File 'lib/familia/refinements/time_literals.rb', line 101 def months = seconds * PER_MONTH |
#months_old ⇒ Object
225 |
# File 'lib/familia/refinements/time_literals.rb', line 225 def months_old(*) = age_in(:months, *) |
#ms ⇒ Object
Shortest aliases
116 |
# File 'lib/familia/refinements/time_literals.rb', line 116 def ms = milliseconds |
#newer_than?(duration) ⇒ Boolean
Checks if timestamp is newer than specified duration in the future
245 246 247 |
# File 'lib/familia/refinements/time_literals.rb', line 245 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
237 238 239 |
# File 'lib/familia/refinements/time_literals.rb', line 237 def older_than?(duration) self < (Familia.now - duration) end |
#second ⇒ Object
107 |
# File 'lib/familia/refinements/time_literals.rb', line 107 def second = seconds |
#seconds ⇒ Object
96 |
# File 'lib/familia/refinements/time_literals.rb', line 96 def seconds = self |
#to_bytes ⇒ String
Converts the number to a human-readable byte representation using binary units
178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/familia/refinements/time_literals.rb', line 178 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
138 |
# File 'lib/familia/refinements/time_literals.rb', line 138 def to_ms = seconds * 1000.0 |
#week ⇒ Object
111 |
# File 'lib/familia/refinements/time_literals.rb', line 111 def week = weeks |
#weeks ⇒ Object
100 |
# File 'lib/familia/refinements/time_literals.rb', line 100 def weeks = seconds * PER_WEEK |
#weeks_old ⇒ Object
224 |
# File 'lib/familia/refinements/time_literals.rb', line 224 def weeks_old(*) = age_in(:weeks, *) |
#within?(duration) ⇒ Boolean
Checks if timestamp is within specified duration of now (past or future)
257 258 259 |
# File 'lib/familia/refinements/time_literals.rb', line 257 def within?(duration) (self - Familia.now).abs <= duration end |
#year ⇒ Object
113 |
# File 'lib/familia/refinements/time_literals.rb', line 113 def year = years |
#years ⇒ Object
102 |
# File 'lib/familia/refinements/time_literals.rb', line 102 def years = seconds * PER_YEAR |
#years_old ⇒ Object
226 |
# File 'lib/familia/refinements/time_literals.rb', line 226 def years_old(*) = age_in(:years, *) |
#μs ⇒ Object
117 |
# File 'lib/familia/refinements/time_literals.rb', line 117 def μs = microseconds |