Module: Familia::Refinements::TimeLiterals::NumericMethods

Defined in:
lib/familia/refinements/time_literals.rb

Instance Method Summary collapse

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

Examples:

timestamp = 2.days.ago.to_i
timestamp.age_in(:days)         #=> ~2.0
timestamp.age_in('hours')       #=> ~48.0
timestamp.age_in(:days, 1.day.ago) #=> ~1.0

Parameters:

  • unit (String, Symbol)

    Time unit ('days', 'hours', 'minutes', 'weeks')

  • from_time (Time, nil) (defaults to: nil)

    Reference time (defaults to Familia.now)

Returns:

  • (Float)

    Age in specified unit



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

#agoObject

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

#dayObject



110
# File 'lib/familia/refinements/time_literals.rb', line 110

def day = days

#daysObject



99
# File 'lib/familia/refinements/time_literals.rb', line 99

def days         = seconds * PER_DAY

#days_oldFloat

Convenience methods for age_in(unit) calls.

Examples:

timestamp.days_old    #=> 2.5

Parameters:

  • from_time (Time, nil)

    Reference time (defaults to Familia.now)

Returns:

  • (Float)

    Age in days



221
# File 'lib/familia/refinements/time_literals.rb', line 221

def days_old(*) = age_in(:days, *)

#from_nowObject



132
# File 'lib/familia/refinements/time_literals.rb', line 132

def from_now     = Familia.now + seconds

#hourObject



109
# File 'lib/familia/refinements/time_literals.rb', line 109

def hour = hours

#hoursObject



98
# File 'lib/familia/refinements/time_literals.rb', line 98

def hours        = seconds * PER_HOUR

#hours_oldObject



222
# File 'lib/familia/refinements/time_literals.rb', line 222

def hours_old(*) = age_in(:hours, *)

#humanizeString

Converts the number to a human-readable string representation

Examples:

10.to_humanize #=> "10 seconds"
60.to_humanize #=> "1 minute"
3600.to_humanize #=> "1 hour"
86400.to_humanize #=> "1 day"

Returns:

  • (String)

    A formatted string e.g. "1 day" or "10 seconds"



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_daysObject



123
# File 'lib/familia/refinements/time_literals.rb', line 123

def in_days         = seconds / PER_DAY

#in_hoursObject



124
# File 'lib/familia/refinements/time_literals.rb', line 124

def in_hours        = seconds / PER_HOUR

#in_microsecondsObject



127
# File 'lib/familia/refinements/time_literals.rb', line 127

def in_microseconds = seconds / PER_MICROSECOND

#in_millisecondsObject



126
# File 'lib/familia/refinements/time_literals.rb', line 126

def in_milliseconds = seconds / PER_MILLISECOND

#in_minutesObject



125
# File 'lib/familia/refinements/time_literals.rb', line 125

def in_minutes      = seconds / PER_MINUTE

#in_monthsObject



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

Parameters:

  • unit (String, Symbol) (defaults to: nil)

    Unit to convert to

Returns:

  • (Float)

    Converted time value



128
# File 'lib/familia/refinements/time_literals.rb', line 128

def in_seconds      = seconds # for semantic purposes

#in_timeObject



135
# File 'lib/familia/refinements/time_literals.rb', line 135

def in_time = Time.at(seconds).utc

#in_weeksObject



122
# File 'lib/familia/refinements/time_literals.rb', line 122

def in_weeks        = seconds / PER_WEEK

#in_yearsObject

Seconds -> other time units



120
# File 'lib/familia/refinements/time_literals.rb', line 120

def in_years        = seconds / PER_YEAR

#microsecondObject

Aliases with singular forms



105
# File 'lib/familia/refinements/time_literals.rb', line 105

def microsecond = microseconds

#microsecondsObject



94
# File 'lib/familia/refinements/time_literals.rb', line 94

def microseconds = seconds * PER_MICROSECOND

#millisecondObject



106
# File 'lib/familia/refinements/time_literals.rb', line 106

def millisecond = milliseconds

#millisecondsObject



95
# File 'lib/familia/refinements/time_literals.rb', line 95

def milliseconds = seconds * PER_MILLISECOND

#minuteObject



108
# File 'lib/familia/refinements/time_literals.rb', line 108

def minute = minutes

#minutesObject



97
# File 'lib/familia/refinements/time_literals.rb', line 97

def minutes      = seconds * PER_MINUTE

#minutes_oldObject



223
# File 'lib/familia/refinements/time_literals.rb', line 223

def minutes_old(*) = age_in(:minutes, *)

#monthObject



112
# File 'lib/familia/refinements/time_literals.rb', line 112

def month = months

#monthsObject



101
# File 'lib/familia/refinements/time_literals.rb', line 101

def months       = seconds * PER_MONTH

#months_oldObject



225
# File 'lib/familia/refinements/time_literals.rb', line 225

def months_old(*) = age_in(:months, *)

#msObject

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

Examples:

Familia.now.newer_than?(1.second)    #=> false

Returns:

  • (Boolean)


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

Note:

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

Examples:

Familia.now.older_than?(1.second)    #=> false

Parameters:

  • duration (Numeric)

    Duration in seconds to compare against

Returns:

  • (Boolean)

    true if timestamp is older than duration



237
238
239
# File 'lib/familia/refinements/time_literals.rb', line 237

def older_than?(duration)
  self < (Familia.now - duration)
end

#secondObject



107
# File 'lib/familia/refinements/time_literals.rb', line 107

def second = seconds

#secondsObject



96
# File 'lib/familia/refinements/time_literals.rb', line 96

def seconds      = self

#to_bytesString

Converts the number to a human-readable byte representation using binary units

Examples:

1024.to_bytes      #=> "1.00 KiB"
2_097_152.to_bytes #=> "2.00 MiB"
3_221_225_472.to_bytes #=> "3.00 GiB"

Returns:

  • (String)

    A formatted string of bytes, KiB, MiB, GiB, or TiB



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_msObject

Milliseconds conversion



138
# File 'lib/familia/refinements/time_literals.rb', line 138

def to_ms = seconds * 1000.0

#weekObject



111
# File 'lib/familia/refinements/time_literals.rb', line 111

def week = weeks

#weeksObject



100
# File 'lib/familia/refinements/time_literals.rb', line 100

def weeks        = seconds * PER_WEEK

#weeks_oldObject



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)

Examples:

30.minutes.ago.to_i.within?(1.hour)     #=> true
30.minutes.from_now.to_i.within?(1.hour) #=> true
2.hours.ago.to_i.within?(1.hour)        #=> false

Parameters:

  • duration (Numeric)

    Duration in seconds to compare against

Returns:

  • (Boolean)

    true if timestamp is within duration of now



257
258
259
# File 'lib/familia/refinements/time_literals.rb', line 257

def within?(duration)
  (self - Familia.now).abs <= duration
end

#yearObject



113
# File 'lib/familia/refinements/time_literals.rb', line 113

def year = years

#yearsObject



102
# File 'lib/familia/refinements/time_literals.rb', line 102

def years        = seconds * PER_YEAR

#years_oldObject



226
# File 'lib/familia/refinements/time_literals.rb', line 226

def years_old(*) = age_in(:years, *)

#μsObject



117
# File 'lib/familia/refinements/time_literals.rb', line 117

def μs = microseconds