module Arborist::TimeRefinements
Refinements to Numeric and Time to add convenience methods
Constants
- DAYS
- HOURS
- MINUTES
Approximate Time Constants (in seconds)
- MONTHS
- WEEKS
- YEARS
Public Instance Methods
Returns the Time <receiver> number of seconds after the given time
. E.g., 10.minutes.after( header.expiration )
# File lib/arborist/mixins.rb, line 215 def after( time ) return time + self end
Returns the Time <receiver> number of seconds ago. (e.g., expiration > 2.hours.ago )
# File lib/arborist/mixins.rb, line 208 def ago return self.before( ::Time.now ) end
Return a description of the receiving Time object in relation to the current time.
Example:
"Saved %s ago." % object.updated_at.as_delta
# File lib/arborist/mixins.rb, line 248 def as_delta now = Time.now if now > self seconds = now - self return "%s ago" % [ timeperiod(seconds) ] else seconds = self - now return "%s from now" % [ timeperiod(seconds) ] end end
Returns the Time <receiver> number of seconds before the specified time
. E.g., 2.hours.before( header.expiration )
# File lib/arborist/mixins.rb, line 201 def before( time ) return time - self end
Returns the number of seconds in <receiver> days
# File lib/arborist/mixins.rb, line 169 def days return TimeFunctions.calculate_seconds( self, :day ) end
Returns the number of seconds in <receiver> fortnights
# File lib/arborist/mixins.rb, line 181 def fortnights return TimeFunctions.calculate_seconds( self, :fortnights ) end
Return a new Time <receiver> number of seconds from now.
# File lib/arborist/mixins.rb, line 221 def from_now return self.after( ::Time.now ) end
Returns true
if the receiver is a Time in the future.
# File lib/arborist/mixins.rb, line 231 def future? return self > Time.now end
Returns the number of seconds in <receiver> hours
# File lib/arborist/mixins.rb, line 163 def hours return TimeFunctions.calculate_seconds( self, :hours ) end
Returns number of seconds in <receiver> minutes
# File lib/arborist/mixins.rb, line 157 def minutes return TimeFunctions.calculate_seconds( self, :minutes ) end
Returns the number of seconds in <receiver> months (approximate)
# File lib/arborist/mixins.rb, line 187 def months return TimeFunctions.calculate_seconds( self, :months ) end
Returns true
if the receiver is a Time in the past.
# File lib/arborist/mixins.rb, line 237 def past? return self < Time.now end
Number of seconds (returns receiver unmodified)
# File lib/arborist/mixins.rb, line 151 def seconds return self end
Return a description of seconds
as the nearest whole unit of time.
# File lib/arborist/mixins.rb, line 261 def timeperiod( seconds ) return case when seconds < MINUTES - 5 'less than a minute' when seconds < 50 * MINUTES if seconds <= 89 "a minute" else "%d minutes" % [ (seconds.to_f / MINUTES).ceil ] end when seconds < 90 * MINUTES 'about an hour' when seconds < 18 * HOURS "%d hours" % [ (seconds.to_f / HOURS).ceil ] when seconds < 30 * HOURS 'about a day' when seconds < WEEKS "%d days" % [ (seconds.to_f / DAYS).ceil ] when seconds < 2 * WEEKS 'about a week' when seconds < 3 * MONTHS "%d weeks" % [ (seconds.to_f / WEEKS).round ] when seconds < 18 * MONTHS "%d months" % [ (seconds.to_f / MONTHS).ceil ] else "%d years" % [ (seconds.to_f / YEARS).ceil ] end end
Return the number of seconds in <receiver> weeks
# File lib/arborist/mixins.rb, line 175 def weeks return TimeFunctions.calculate_seconds( self, :weeks ) end
Returns the number of seconds in <receiver> years (approximate)
# File lib/arborist/mixins.rb, line 193 def years return TimeFunctions.calculate_seconds( self, :years ) end