Module: TimeMath

Defined in:
lib/time_math.rb,
lib/time_math/op.rb,
lib/time_math/util.rb,
lib/time_math/units.rb,
lib/time_math/measure.rb,
lib/time_math/version.rb,
lib/time_math/sequence.rb,
lib/time_math/units/day.rb,
lib/time_math/resamplers.rb,
lib/time_math/units/base.rb,
lib/time_math/units/week.rb,
lib/time_math/units/year.rb,
lib/time_math/units/month.rb,
lib/time_math/units/simple.rb

Overview

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:

TimeMath.day.floor(Time.now)
# or
TimeMath[:day].floor(Time.now)

TimeMath[unit] 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!

Defined Under Namespace

Modules: Units Classes: Op, Sequence

Class Method Summary collapse

Class Method Details

.[](unit) ⇒ Units::Base

Main method to do something with TimeMath. Returns an object representing some time measurement unit. See TimeMath::Units::Base documentation to know what you can do with it.

Returns:



43
44
45
# File 'lib/time_math.rb', line 43

def [](unit)
  Units.get(unit)
end

.dayUnits::Base

Shortcut to get day unit.

Returns:



75
76
77
# File 'lib/time_math.rb', line 75

Units.names.each do |unit|
  define_singleton_method(unit) { Units.get(unit) }
end

.hourUnits::Base

Shortcut to get hour unit.

Returns:



75
76
77
# File 'lib/time_math.rb', line 75

Units.names.each do |unit|
  define_singleton_method(unit) { Units.get(unit) }
end

.measure(from, to, options = {}) ⇒ Hash

Measures distance between two time values in all units at once.

Just like this:

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}

Parameters:

  • from (Time, Date, DateTime)
  • to (Time, Date, DateTime)
  • options (Hash) (defaults to: {})

    options

Options Hash (options):

  • :weeks (Boolean)

    pass false to exclude weeks from calculation;

  • :upto (Symbol)

    pass max unit to use (e.g. if you'll pass :day, period would be measured in days, hours, minutes and seconds).

Returns:

  • (Hash)


98
99
100
# File 'lib/time_math.rb', line 98

def measure(from, to, options = {})
  Measure.measure(from, to, options)
end

.minUnits::Base

Shortcut to get minute unit.

Returns:



75
76
77
# File 'lib/time_math.rb', line 75

Units.names.each do |unit|
  define_singleton_method(unit) { Units.get(unit) }
end

.monthUnits::Base

Shortcut to get month unit.

Returns:



75
76
77
# File 'lib/time_math.rb', line 75

Units.names.each do |unit|
  define_singleton_method(unit) { Units.get(unit) }
end

.secUnits::Base

Shortcut to get second unit.

Returns:



75
76
77
# File 'lib/time_math.rb', line 75

Units.names.each do |unit|
  define_singleton_method(unit) { Units.get(unit) }
end

.unitsArray<Symbol>

List all unit names known.

Returns:

  • (Array<Symbol>)


34
35
36
# File 'lib/time_math.rb', line 34

def units
  Units.names
end

.weekUnits::Base

Shortcut to get week unit.

Returns:



75
76
77
# File 'lib/time_math.rb', line 75

Units.names.each do |unit|
  define_singleton_method(unit) { Units.get(unit) }
end

.yearUnits::Base

Shortcut to get year unit.

Returns:



75
76
77
# File 'lib/time_math.rb', line 75

Units.names.each do |unit|
  define_singleton_method(unit) { Units.get(unit) }
end