Schedulability::

TimeRefinements

module

Refinements to Numeric to add time-related convenience methods

Constants

DAYS
HOURS
MINUTES

Approximate Time Constants (in seconds)

MONTHS
WEEKS
YEARS

Public Instance Methods

anchor
after( time )

Returns the Time <receiver> number of seconds after the given time. E.g., 10.minutes.after( header.expiration )

# File lib/schedulability/mixins.rb, line 111
def after( time )
        return time + self
end
anchor
ago()

Returns the Time <receiver> number of seconds ago. (e.g., expiration > 2.hours.ago )

# File lib/schedulability/mixins.rb, line 104
def ago
        return self.before( ::Time.now )
end
anchor
as_delta()

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/schedulability/mixins.rb, line 153
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
anchor
before( time )

Returns the Time <receiver> number of seconds before the specified time. E.g., 2.hours.before( header.expiration )

# File lib/schedulability/mixins.rb, line 97
def before( time )
        return time - self
end
anchor
days()

Returns the number of seconds in <receiver> days

# File lib/schedulability/mixins.rb, line 65
def days
        return TimeFunctions.calculate_seconds( self, :day )
end
anchor
fortnights()

Returns the number of seconds in <receiver> fortnights

# File lib/schedulability/mixins.rb, line 77
def fortnights
        return TimeFunctions.calculate_seconds( self, :fortnights )
end
anchor
from_now()

Return a new Time <receiver> number of seconds from now.

# File lib/schedulability/mixins.rb, line 117
def from_now
        return self.after( ::Time.now )
end
anchor
future?()

Returns true if the receiver is a Time in the future.

# File lib/schedulability/mixins.rb, line 136
def future?
        return self > Time.now
end
anchor
hours()

Returns the number of seconds in <receiver> hours

# File lib/schedulability/mixins.rb, line 59
def hours
        return TimeFunctions.calculate_seconds( self, :hours )
end
anchor
minutes()

Returns number of seconds in <receiver> minutes

# File lib/schedulability/mixins.rb, line 53
def minutes
        return TimeFunctions.calculate_seconds( self, :minutes )
end
anchor
months()

Returns the number of seconds in <receiver> months (approximate)

# File lib/schedulability/mixins.rb, line 83
def months
        return TimeFunctions.calculate_seconds( self, :months )
end
anchor
past?()

Returns true if the receiver is a Time in the past.

# File lib/schedulability/mixins.rb, line 142
def past?
        return self < Time.now
end
anchor
seconds()

Number of seconds (returns receiver unmodified)

# File lib/schedulability/mixins.rb, line 47
def seconds
        return self
end
anchor
timeperiod( seconds )

Return a description of seconds as the nearest whole unit of time.

# File lib/schedulability/mixins.rb, line 166
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
anchor
weeks()

Return the number of seconds in <receiver> weeks

# File lib/schedulability/mixins.rb, line 71
def weeks
        return TimeFunctions.calculate_seconds( self, :weeks )
end
anchor
years()

Returns the number of seconds in <receiver> years (approximate)

# File lib/schedulability/mixins.rb, line 89
def years
        return TimeFunctions.calculate_seconds( self, :years )
end