module TimeMath
TimeMath
is a small library for easy time units arithmetics (like “floor the timestamp to the nearest hour”, “advance the time value by 3 days” and so on).
It has clean and easy-to-remember API, just like this:
“`ruby TimeMath.day.floor(Time.now) # or TimeMath.floor(Time.now) “`
`TimeMath` and `TimeMath.<unit>` give you an instance of time unit, which incapsulates most of the functionality. Refer to {Units::Base} to see what you can get of it.
See also `TimeMath()` method in global namespace, it is lot of fun!
Constants
- VERSION
@private
Public Instance Methods
Main method to do something with TimeMath
. Returns an object representing some time measurement unit. See {Units::Base} documentation to know what you can do with it.
@return [Units::Base]
# File lib/time_math.rb, line 43 def [](unit) Units.get(unit) end
Measures distance between two time values in all units at once.
Just like this:
“`ruby birthday = Time.parse('1983-02-14 13:30')
TimeMath.measure
(birthday, Time.now) # => {:years=>33, :months=>3, :weeks=>2, :days=>0, :hours=>1, :minutes=>25, :seconds=>52} “`
@param from [Time,Date,DateTime] @param to [Time,Date,DateTime] @param options [Hash] options @option options [Boolean] :weeks pass `false` to exclude weeks from calculation; @option options [Symbol] :upto pass max unit to use (e.g. if you'll
pass `:day`, period would be measured in days, hours, minutes and seconds).
@return [Hash]
# File lib/time_math.rb, line 98 def measure(from, to, options = {}) Measure.measure(from, to, options) end
List all unit names known.
@return [Array<Symbol>]
# File lib/time_math.rb, line 34 def units Units.names end