class TimeCalc

Module for time arithmetic.

Examples of usage:

“`ruby TimeCalc.(Time.now).+(1, :day) # => 2019-07-04 23:28:54 +0300 TimeCalc.(Time.now).round(:hour) # => 2019-07-03 23:00:00 +0300

# Operations with Time.now and Date.today also have their shortcuts: TimeCalc.now.-(3, :days) # => 2019-06-30 23:28:54 +0300 TimeCalc.today.ceil(:month) # => #<Date: 2019-08-01 ((2458697j,0s,0n),+0s,2299161j)>

# If you need to perform several operations TimeCalc.from wraps your value: TimeCalc.from(Time.parse('2019-06-14 13:40')).+(10, :days).floor(:week).unwrap # => 2019-06-24 00:00:00 +0300

# TimeCalc#- also can be used to calculate difference between time values diff = TimeCalc.(Time.parse('2019-07-03 23:32')) - Time.parse('2019-06-14 13:40') # => #<TimeCalc::Diff(2019-07-03 23:32:00 +0300 − 2019-06-14 13:40:00 +0300)> diff.days # => 19 diff.hours # => 465 diff.factorize # => {:year=>0, :month=>0, :week=>2, :day=>5, :hour=>9, :min=>52, :sec=>0} diff.factorize(max: :day) # => {:day=>19, :hour=>9, :min=>52, :sec=>0}

# Enumerable sequences of time values sequence = TimeCalc.(Time.parse('2019-06-14 13:40'))

.to(Time.parse('2019-07-03 23:32'))
.step(5, :hours)

# => #<TimeCalc::Sequence (2019-06-14 13:40:00 +0300 - 2019-07-03 23:32:00 +0300):step(5 hours)> sequence.to_a # => [2019-06-14 13:40:00 +0300, 2019-06-14 18:40:00 +0300, 2019-06-14 23:40:00 +0300, … sequence.first(2) # => [2019-06-14 13:40:00 +0300, 2019-06-14 18:40:00 +0300]

# Construct operations to apply as a proc: times = ['2019-06-01 14:30', '2019-06-05 17:10', '2019-07-02 13:40'].map { |t| Time.parse(t) } # => [2019-06-01 14:30:00 +0300, 2019-06-05 17:10:00 +0300, 2019-07-02 13:40:00 +0300] times.map(&TimeCalc.+(1, :week).round(:day)) # => [2019-06-09 00:00:00 +0300, 2019-06-13 00:00:00 +0300, 2019-07-10 00:00:00 +0300] “`

See method docs below for details and supported arguments.

Constants

MATH_OPERATIONS

@private

OPERATIONS

@private

VERSION

@private

Attributes

value[R]

@private

Public Class Methods

from(date_or_time) click to toggle source

Returns {Value} wrapper, useful for performing several operations at once:

“`ruby TimeCalc.from(Time.parse('2019-06-14 13:40')).+(10, :days).floor(:week).unwrap # => 2019-06-24 00:00:00 +0300 “`

@param date_or_time [Time, Date, DateTime] @return [Value]

# File lib/time_calc.rb, line 86
def from(date_or_time)
  Value.new(date_or_time)
end
Also aliased as: wrap
from_now() click to toggle source

Shortcut for `TimeCalc.from(Time.now)` @return [Value]

# File lib/time_calc.rb, line 92
def from_now
  from(Time.now)
end
Also aliased as: wrap_now
from_today() click to toggle source

Shortcut for `TimeCalc.from(Date.today)` @return [Value]

# File lib/time_calc.rb, line 98
def from_today
  from(Date.today)
end
Also aliased as: wrap_today
new(date_or_time) click to toggle source

Creates a “temporary” wrapper, which would be unwrapped after first operation:

“`ruby TimeCalc.new(Time.now).round(:hour) # => 2019-07-03 23:00:00 +0300 “`

The constructor also aliased as `.call` which allows for nicer (for some eyes) code:

“`ruby TimeCalc.(Time.now).round(:hour) # => 2019-07-03 23:00:00 +0300

# There is another shortcut for those who disapprove on `.()` TimeCalc.+(1, :month) “`

See {.from} if you need to perform several math operations on same value.

@param date_or_time [Time, Date, DateTime]

# File lib/time_calc.rb, line 130
def initialize(date_or_time)
  @value = Value.new(date_or_time)
end
now() click to toggle source

Shortcut for `TimeCalc.(Time.now)` @return [TimeCalc]

# File lib/time_calc.rb, line 67
def now
  new(Time.now)
end
today() click to toggle source

Shortcut for `TimeCalc.(Date.today)` @return [TimeCalc]

# File lib/time_calc.rb, line 73
def today
  new(Date.today)
end
wrap(date_or_time)
Alias for: from
wrap_now()
Alias for: from_now
wrap_today()
Alias for: from_today

Public Instance Methods

==(other) click to toggle source

@return [true,false]

# File lib/time_calc.rb, line 140
def ==(other)
  other.is_a?(self.class) && other.value == value
end
inspect() click to toggle source

@private

# File lib/time_calc.rb, line 135
def inspect
  '#<%s(%s)>' % [self.class, @value.unwrap]
end