time_math2 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,9 +21,9 @@ module TimeMath
21
21
  return res unless res.is_a?(Time)
22
22
 
23
23
  if res.dst? && !src.dst?
24
- hour.decrease(res)
24
+ TimeMath.hour.decrease(res)
25
25
  elsif !res.dst? && src.dst?
26
- hour.advance(res)
26
+ TimeMath.hour.advance(res)
27
27
  else
28
28
  res
29
29
  end
@@ -16,14 +16,14 @@ module TimeMath
16
16
 
17
17
  protected
18
18
 
19
- def succ(tm)
19
+ def _succ(tm)
20
20
  return generate(tm, year: tm.year + 1, month: 1) if tm.month == 12
21
21
 
22
22
  t = generate(tm, month: tm.month + 1)
23
23
  fix_month(t, t.month + 1)
24
24
  end
25
25
 
26
- def prev(tm)
26
+ def _prev(tm)
27
27
  return generate(tm, year: tm.year - 1, month: 12) if tm.month == 1
28
28
 
29
29
  t = generate(tm, month: tm.month - 1)
@@ -31,11 +31,11 @@ module TimeMath
31
31
  end
32
32
 
33
33
  def _advance(tm, steps)
34
- steps.times.inject(tm) { |t| succ(t) }
34
+ steps.to_i.times.inject(tm) { |t| _succ(t) }
35
35
  end
36
36
 
37
37
  def _decrease(tm, steps)
38
- steps.times.inject(tm) { |t| prev(t) }
38
+ steps.to_i.times.inject(tm) { |t| _prev(t) }
39
39
  end
40
40
 
41
41
  # fix for too far advance/insufficient decrease:
@@ -24,7 +24,7 @@ module TimeMath
24
24
  case tm
25
25
  when Time
26
26
  tm + seconds
27
- when DateTime
27
+ when Date
28
28
  tm + Rational(seconds, 86_400)
29
29
  else
30
30
  raise ArgumentError, "Expected Time or DateTime, got #{tm.class}"
@@ -6,24 +6,27 @@ module TimeMath
6
6
  super(:week)
7
7
  end
8
8
 
9
- def floor(tm)
10
- f = day.floor(tm)
9
+ def floor(tm, span = 1)
10
+ span == 1 or
11
+ raise NotImplementedError, 'For now, week only can floor to one'
12
+
13
+ f = TimeMath.day.floor(tm)
11
14
  extra_days = tm.wday == 0 ? 6 : tm.wday - 1
12
- day.decrease(f, extra_days)
15
+ TimeMath.day.decrease(f, extra_days)
13
16
  end
14
17
 
15
18
  def to_seconds(sz = 1)
16
- day.to_seconds(sz * 7)
19
+ TimeMath.day.to_seconds(sz * 7)
17
20
  end
18
21
 
19
22
  protected
20
23
 
21
24
  def _advance(tm, steps)
22
- day.advance(tm, steps * 7)
25
+ TimeMath.day.advance(tm, steps * 7)
23
26
  end
24
27
 
25
28
  def _decrease(tm, steps)
26
- day.decrease(tm, steps * 7)
29
+ TimeMath.day.decrease(tm, steps * 7)
27
30
  end
28
31
  end
29
32
  end
@@ -17,11 +17,11 @@ module TimeMath
17
17
  protected
18
18
 
19
19
  def _advance(tm, steps)
20
- generate(tm, year: tm.year + steps)
20
+ generate(tm, year: tm.year + steps.to_i)
21
21
  end
22
22
 
23
23
  def _decrease(tm, steps)
24
- generate(tm, year: tm.year - steps)
24
+ generate(tm, year: tm.year - steps.to_i)
25
25
  end
26
26
  end
27
27
  end
@@ -0,0 +1,10 @@
1
+ module TimeMath
2
+ # @private
3
+ module Util
4
+ module_function
5
+
6
+ def timey?(val)
7
+ [Time, DateTime, Date].any? { |cls| val.is_a?(cls) }
8
+ end
9
+ end
10
+ end
@@ -1,4 +1,4 @@
1
1
  module TimeMath
2
2
  # @private
3
- VERSION = '0.0.4'.freeze
3
+ VERSION = '0.0.5'.freeze
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_math2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Shepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-28 00:00:00.000000000 Z
11
+ date: 2016-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -123,10 +123,11 @@ files:
123
123
  - LICENSE.txt
124
124
  - README.md
125
125
  - lib/time_math.rb
126
- - lib/time_math/core_ext.rb
126
+ - lib/time_math/backports.rb
127
127
  - lib/time_math/measure.rb
128
+ - lib/time_math/op.rb
129
+ - lib/time_math/resamplers.rb
128
130
  - lib/time_math/sequence.rb
129
- - lib/time_math/span.rb
130
131
  - lib/time_math/units.rb
131
132
  - lib/time_math/units/base.rb
132
133
  - lib/time_math/units/day.rb
@@ -134,6 +135,7 @@ files:
134
135
  - lib/time_math/units/simple.rb
135
136
  - lib/time_math/units/week.rb
136
137
  - lib/time_math/units/year.rb
138
+ - lib/time_math/util.rb
137
139
  - lib/time_math/version.rb
138
140
  - time_math2.gemspec
139
141
  homepage: https://github.com/zverok/time_math2
@@ -1,52 +0,0 @@
1
- module TimeMath
2
- # This module is included into `Time` and `DateTime`. It is optional
3
- # and only included by explicit `require "time_math/core_ext"`.
4
- module CoreExt
5
- # @!method floor_to(unit)
6
- # Shortcut to {Units::Base#floor}
7
- # @!method ceil_to(unit)
8
- # Shortcut to {Units::Base#ceil}
9
- # @!method round_to(unit)
10
- # Shortcut to {Units::Base#round}
11
- # @!method next_to(unit)
12
- # Shortcut to {Units::Base#next}
13
- # @!method prev_to(unit)
14
- # Shortcut to {Units::Base#prev}
15
- [:floor, :ceil, :round, :next, :prev].each do |sym|
16
- define_method("#{sym}_to") { |unit| TimeMath[unit].send(sym, self) }
17
- end
18
-
19
- # Shortcut to {Units::Base#round?}
20
- def round_to?(unit)
21
- TimeMath[unit].round?(self)
22
- end
23
-
24
- # Shortcut to {Units::Base#advance}
25
- def advance_by(unit, amount = 1)
26
- TimeMath[unit].advance(self, amount)
27
- end
28
-
29
- # Shortcut to {Units::Base#decrease}
30
- def decrease_by(unit, amount = 1)
31
- TimeMath[unit].decrease(self, amount)
32
- end
33
-
34
- # Shortcut to {Units::Base#range}
35
- def range_to(unit, amount = 1)
36
- TimeMath[unit].range(self, amount)
37
- end
38
-
39
- # Shortcut to {Units::Base#range_back}
40
- def range_from(unit, amount = 1)
41
- TimeMath[unit].range_back(self, amount)
42
- end
43
-
44
- # Shortcut to {Units::Base#sequence}. See its docs for possible options.
45
- def sequence_to(unit, other, options = {})
46
- TimeMath[unit].sequence(self, other, options)
47
- end
48
- end
49
-
50
- Time.send :include, CoreExt
51
- DateTime.send :include, CoreExt if ::Kernel.const_defined?(:DateTime)
52
- end
@@ -1,53 +0,0 @@
1
- module TimeMath
2
- # Represents time span (amount of units), like "4 years".
3
- # Allows to advance or decrease Time or DateTime.
4
- #
5
- # Use it like this:
6
- #
7
- # ```ruby
8
- # TimeMath.year.span(4).before(Time.now)
9
- # ```
10
- #
11
- class Span
12
- attr_reader :unit, :amount
13
-
14
- # Creates Span instance.
15
- # Typically, it is easire to use {Units::Base#span} than create
16
- # spans directly.
17
- #
18
- # @param unit [Symbol] one of {Units.names}, unit of span;
19
- # @param amount [Integer] amount of units in span.
20
- def initialize(unit, amount)
21
- @unit, @amount = unit, amount
22
- @unit_impl = Units.get(unit)
23
- end
24
-
25
- # Decreases `tm` by `amount` of `unit`.
26
- #
27
- # @param tm [Time,DateTime] time value to decrease;
28
- # @return [Time,DateTime] decreased time.
29
- def before(tm = Time.now)
30
- @unit_impl.decrease(tm, amount)
31
- end
32
-
33
- # Increases `tm` by `amount` of `unit`.
34
- #
35
- # @param tm [Time,DateTime] time value to increase;
36
- # @return [Time,DateTime] increased time.
37
- def after(tm = Time.now)
38
- @unit_impl.advance(tm, amount)
39
- end
40
-
41
- alias ago before
42
- alias from after
43
-
44
- def ==(other)
45
- self.class == other.class &&
46
- unit == other.unit && amount == other.amount
47
- end
48
-
49
- def inspect
50
- '#<%s(%s): %+i>' % [self.class, unit, amount]
51
- end
52
- end
53
- end