Class: TimeCalc
- Inherits:
-
Object
- Object
- TimeCalc
- Defined in:
- lib/time_calc.rb,
lib/time_calc/op.rb,
lib/time_calc/dst.rb,
lib/time_calc/diff.rb,
lib/time_calc/types.rb,
lib/time_calc/units.rb,
lib/time_calc/value.rb,
lib/time_calc/version.rb,
lib/time_calc/sequence.rb
Overview
Module for time arithmetic.
Examples of usage:
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.
Defined Under Namespace
Modules: DST, Types, Units Classes: Diff, Op, Sequence, Value
Constant Summary collapse
- MATH_OPERATIONS =
%i[merge truncate floor ceil round + - iterate].freeze
- OPERATIONS =
MATH_OPERATIONS.+(%i[to step for]).freeze
- VERSION =
'0.0.4'
Instance Attribute Summary collapse
- #value ⇒ Object readonly
Class Method Summary collapse
-
.+(span, unit) ⇒ Op
Creates operation to perform #+
(span, unit). - .-(span, unit) ⇒ Op
- .ceil(unit) ⇒ Op
- .floor(unit) ⇒ Op
-
.from(date_or_time) ⇒ Value
(also: wrap)
Returns Value wrapper, useful for performing several operations at once:.
-
.from_now ⇒ Value
(also: wrap_now)
Shortcut for
TimeCalc.from(Time.now). -
.from_today ⇒ Value
(also: wrap_today)
Shortcut for
TimeCalc.from(Date.today). - .iterate(span, unit, &block) ⇒ Op
-
.now ⇒ TimeCalc
Shortcut for
TimeCalc.(Time.now). - .round(unit) ⇒ Op
-
.today ⇒ TimeCalc
Shortcut for
TimeCalc.(Date.today).
Instance Method Summary collapse
-
#+(span, unit) ⇒ Date, ...
Add
<span units>to wrapped value. - #-(span_or_other, unit = nil) ⇒ Time or Diff
- #==(other) ⇒ true, false
-
#ceil(unit) ⇒ Time, ...
Ceils (rounds up) date/time to nearest
unit. -
#floor(unit) ⇒ Time, ...
Floors (rounds down) date/time to nearest
unit. -
#for(span, unit) ⇒ Sequence
Produces Sequence from this value to
this + <span units>. -
#initialize(date_or_time) ⇒ TimeCalc
constructor
Creates a "temporary" wrapper, which would be unwrapped after first operation:.
- #inspect ⇒ Object
-
#iterate(span, unit) {|Time/Date/DateTime| ... } ⇒ Date, ...
Like #+, but allows conditional skipping of some periods.
-
#merge(**attrs) ⇒ Time, ...
Replaces specified components of date/time, preserves the rest.
-
#round(unit) ⇒ Time, ...
Rounds (up or down) date/time to nearest
unit. -
#step(span, unit = nil) ⇒ Sequence
Produces endless Sequence from this value, with step specified.
-
#to(date_or_time) ⇒ Sequence
Produces Sequence from this value to
date_or_time.
Constructor Details
#initialize(date_or_time) ⇒ TimeCalc
Creates a "temporary" wrapper, which would be unwrapped after first operation:
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:
TimeCalc.(Time.now).round(:hour)
# => 2019-07-03 23:00:00 +0300
# There is another shortcut for those who disapprove on `.()`
TimeCalc[Time.now].+(1, :month)
See from if you need to perform several math operations on same value.
130 131 132 |
# File 'lib/time_calc.rb', line 130 def initialize(date_or_time) @value = Value.new(date_or_time) end |
Instance Attribute Details
#value ⇒ Object (readonly)
108 109 110 |
# File 'lib/time_calc.rb', line 108 def value @value end |
Class Method Details
.+(span, unit) ⇒ Op
Creates operation to perform #+(span, unit)
3 |
# File 'lib/time_calc.rb', line 3 def TimeCalc.+(span, unit); end |
.from(date_or_time) ⇒ Value Also known as: wrap
86 87 88 |
# File 'lib/time_calc.rb', line 86 def from(date_or_time) Value.new(date_or_time) end |
.from_now ⇒ Value Also known as: wrap_now
Shortcut for TimeCalc.from(Time.now)
92 93 94 |
# File 'lib/time_calc.rb', line 92 def from_now from(Time.now) end |
.from_today ⇒ Value Also known as: wrap_today
Shortcut for TimeCalc.from(Date.today)
98 99 100 |
# File 'lib/time_calc.rb', line 98 def from_today from(Date.today) end |
.iterate(span, unit, &block) ⇒ Op
6 |
# File 'lib/time_calc.rb', line 6 def TimeCalc.iterate(span, unit, &block); end |
.now ⇒ TimeCalc
Shortcut for TimeCalc.(Time.now)
67 68 69 |
# File 'lib/time_calc.rb', line 67 def now new(Time.now) end |
.today ⇒ TimeCalc
Shortcut for TimeCalc.(Date.today)
73 74 75 |
# File 'lib/time_calc.rb', line 73 def today new(Date.today) end |
Instance Method Details
#+(span, unit) ⇒ Date, ...
Add <span units> to wrapped value
|
|
# File 'lib/time_calc.rb', line 184
|
#==(other) ⇒ true, false
140 141 142 |
# File 'lib/time_calc.rb', line 140 def ==(other) other.is_a?(self.class) && other.value == value end |
#ceil(unit) ⇒ Time, ...
Ceils (rounds up) date/time to nearest unit.
|
|
# File 'lib/time_calc.rb', line 164
|
#floor(unit) ⇒ Time, ...
Floors (rounds down) date/time to nearest unit.
|
|
# File 'lib/time_calc.rb', line 154
|
#for(span, unit) ⇒ Sequence
Produces Sequence from this value to this + <span units>
|
|
# File 'lib/time_calc.rb', line 261
|
#inspect ⇒ Object
135 136 137 |
# File 'lib/time_calc.rb', line 135 def inspect '#<%s(%s)>' % [self.class, @value.unwrap] end |
#iterate(span, unit) {|Time/Date/DateTime| ... } ⇒ Date, ...
Like #+, but allows conditional skipping of some periods. Increases value by unit
at least span times, on each iteration checking with block provided if this point
matches desired period; if it is not, it is skipped without increasing iterations
counter. Useful for "business date/time" algorithms.
|
|
# File 'lib/time_calc.rb', line 193
|
#merge(**attrs) ⇒ Time, ...
Replaces specified components of date/time, preserves the rest.
|
|
# File 'lib/time_calc.rb', line 144
|
#round(unit) ⇒ Time, ...
Rounds (up or down) date/time to nearest unit.
|
|
# File 'lib/time_calc.rb', line 174
|