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
-
.[](unit) ⇒ Units::Base
Main method to do something with TimeMath.
-
.day ⇒ Units::Base
Shortcut to get day unit.
-
.hour ⇒ Units::Base
Shortcut to get hour unit.
-
.measure(from, to, options = {}) ⇒ Hash
Measures distance between two time values in all units at once.
-
.min ⇒ Units::Base
Shortcut to get minute unit.
-
.month ⇒ Units::Base
Shortcut to get month unit.
-
.sec ⇒ Units::Base
Shortcut to get second unit.
-
.units ⇒ Array<Symbol>
List all unit names known.
-
.week ⇒ Units::Base
Shortcut to get week unit.
-
.year ⇒ Units::Base
Shortcut to get year unit.
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.
43 44 45 |
# File 'lib/time_math.rb', line 43 def [](unit) Units.get(unit) end |
.day ⇒ Units::Base
Shortcut to get day unit.
75 76 77 |
# File 'lib/time_math.rb', line 75 Units.names.each do |unit| define_singleton_method(unit) { Units.get(unit) } end |
.hour ⇒ Units::Base
Shortcut to get hour unit.
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}
98 99 100 |
# File 'lib/time_math.rb', line 98 def measure(from, to, = {}) Measure.measure(from, to, ) end |
.min ⇒ Units::Base
Shortcut to get minute unit.
75 76 77 |
# File 'lib/time_math.rb', line 75 Units.names.each do |unit| define_singleton_method(unit) { Units.get(unit) } end |
.month ⇒ Units::Base
Shortcut to get month unit.
75 76 77 |
# File 'lib/time_math.rb', line 75 Units.names.each do |unit| define_singleton_method(unit) { Units.get(unit) } end |
.sec ⇒ Units::Base
Shortcut to get second unit.
75 76 77 |
# File 'lib/time_math.rb', line 75 Units.names.each do |unit| define_singleton_method(unit) { Units.get(unit) } end |
.units ⇒ Array<Symbol>
List all unit names known.
34 35 36 |
# File 'lib/time_math.rb', line 34 def units Units.names end |
.week ⇒ Units::Base
Shortcut to get week unit.
75 76 77 |
# File 'lib/time_math.rb', line 75 Units.names.each do |unit| define_singleton_method(unit) { Units.get(unit) } end |
.year ⇒ Units::Base
Shortcut to get year unit.
75 76 77 |
# File 'lib/time_math.rb', line 75 Units.names.each do |unit| define_singleton_method(unit) { Units.get(unit) } end |