time_array 0.5.1 → 0.5.7.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/time_array/compactor.rb +79 -0
- data/lib/time_array/exceptions.rb +5 -0
- data/lib/time_array/time_array.rb +162 -3
- data/lib/time_array/time_ext.rb +27 -0
- data/lib/time_array/version.rb +1 -1
- data/lib/time_array.rb +5 -0
- data/spec/time_array_compact_spec.rb +112 -0
- data/spec/time_array_counts_spec.rb +17 -0
- data/spec/time_array_operations_spec.rb +94 -0
- data/spec/time_array_spec.rb +55 -1
- data/spec/time_spec.rb +41 -0
- data/usage/usage_01.rb +38 -12
- metadata +11 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d5965e94e2612aaf9f33334ec98289aea593c762
|
4
|
+
data.tar.gz: 679f503a720a0fb2e5bce7d8246e4de9e8476539
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3944a04e10b519709ebaae2d6c216c06a51e3b41a08879e93409d201414ff97c0bb395cf163c3fe6ad6528e54f2ff6e8b2f6027c7fd5694e4e8e6314ba83855a
|
7
|
+
data.tar.gz: aaf432f844f63690dae1e57d66084d42363f0525114500532d0f5bc67c158f26c317f028bba6721d61e0eed0d60ccf8882bbaacb3722da4e901875b9878a7bc7
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module TimeArray
|
2
|
+
|
3
|
+
|
4
|
+
class CompactorTime
|
5
|
+
attr_reader :time
|
6
|
+
def initialize(t)
|
7
|
+
@time = t
|
8
|
+
# @time = Time.new(t.year, t.month, t.day, t.hour, 0, 0)
|
9
|
+
end
|
10
|
+
|
11
|
+
def change(step=:hour)
|
12
|
+
nxt = @time+1.send(step)
|
13
|
+
[:year, :month, :day, :hour].each do |st|
|
14
|
+
return st if nxt.send(st)!=@time.send(st)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def increment(step=:hour)
|
19
|
+
@time+1.send(step)
|
20
|
+
end
|
21
|
+
|
22
|
+
def increment!(step=:hour)
|
23
|
+
@time = increment(step)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
class GreatestUnit
|
29
|
+
H = {eternity: 5, year: 4, month: 3, day: 2, hour: 1}
|
30
|
+
attr_reader :c
|
31
|
+
|
32
|
+
def initialize(initial_interval=:eternity)
|
33
|
+
@c = initial_interval
|
34
|
+
end
|
35
|
+
|
36
|
+
def set(interval)
|
37
|
+
@c = interval if H[interval]<H[@c]
|
38
|
+
end
|
39
|
+
|
40
|
+
def smallest
|
41
|
+
H.keys.last
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
class Compact
|
47
|
+
attr_reader :v, :unit
|
48
|
+
def initialize(v, unit)
|
49
|
+
@v = v
|
50
|
+
@unit = unit
|
51
|
+
end
|
52
|
+
alias :values :v
|
53
|
+
alias :array :v
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
class Unit
|
58
|
+
ETERNITY = 4
|
59
|
+
YEAR = 3
|
60
|
+
MONTH = 2
|
61
|
+
DAY = 1
|
62
|
+
HOUR = 0
|
63
|
+
|
64
|
+
def self.name(num)
|
65
|
+
case num
|
66
|
+
when ETERNITY
|
67
|
+
:eternity
|
68
|
+
when YEAR
|
69
|
+
:year
|
70
|
+
when MONTH
|
71
|
+
:month
|
72
|
+
when DAY
|
73
|
+
:day
|
74
|
+
else
|
75
|
+
:hour
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -8,8 +8,20 @@ module TimeArray
|
|
8
8
|
class TimeArray
|
9
9
|
attr_reader :start_time, :v, :unit
|
10
10
|
|
11
|
+
# Used by each method
|
12
|
+
class TimeAndValue
|
13
|
+
attr_reader :time, :value
|
14
|
+
def initialize(time, value)
|
15
|
+
@time = time
|
16
|
+
@value = value
|
17
|
+
end
|
18
|
+
|
19
|
+
alias_method :datetime, :time
|
20
|
+
alias_method :v, :value
|
21
|
+
end
|
22
|
+
|
11
23
|
def initialize(start_time, values=Vector.new, options={})
|
12
|
-
manage_options
|
24
|
+
manage_options options
|
13
25
|
set_start_time start_time
|
14
26
|
set_values values
|
15
27
|
end
|
@@ -27,10 +39,9 @@ module TimeArray
|
|
27
39
|
TimeArray.new(@start_time, (@v.clone rescue Vector.new), zone: Time.zone.name)
|
28
40
|
end
|
29
41
|
|
30
|
-
# Set
|
42
|
+
# Set array values to the given new value
|
31
43
|
def all_to(new_value)
|
32
44
|
@v = Vector.new(@v.size, new_value)
|
33
|
-
# @v.map!{|e| e=new_value}
|
34
45
|
self
|
35
46
|
end
|
36
47
|
|
@@ -199,6 +210,153 @@ module TimeArray
|
|
199
210
|
h
|
200
211
|
end
|
201
212
|
|
213
|
+
|
214
|
+
# Will return a Compact object, a compacted version of values
|
215
|
+
# compact.gu gives the greatest_unit, compact.v gives the values array
|
216
|
+
def compact
|
217
|
+
gu = greatest_unit
|
218
|
+
puts "gu: #{gu.inspect}"
|
219
|
+
return Compact.new(@v, gu) if gu==:hour
|
220
|
+
i, st, v = 0, @start_time, []
|
221
|
+
while i<@v.size
|
222
|
+
v << @v[i]
|
223
|
+
next_st = st+1.send(gu)
|
224
|
+
h = (next_st-st)/3600
|
225
|
+
i = i+h.to_i
|
226
|
+
st = next_st
|
227
|
+
end
|
228
|
+
Compact.new(v, gu)
|
229
|
+
end
|
230
|
+
|
231
|
+
|
232
|
+
# Find the greatest unit time.
|
233
|
+
# If the values are contant along the month, the
|
234
|
+
# greatest unit is :month
|
235
|
+
def greatest_unit
|
236
|
+
v = @v.first
|
237
|
+
unit = Unit::YEAR # Unit::ETERNITY
|
238
|
+
each_with_time do |el|
|
239
|
+
|
240
|
+
# el.time
|
241
|
+
# el.value
|
242
|
+
if v!=el.value
|
243
|
+
if el.time.year_changed? and unit>Unit::YEAR
|
244
|
+
unit = Unit::YEAR
|
245
|
+
else
|
246
|
+
if el.time.month_changed? and unit>Unit::MONTH
|
247
|
+
unit = Unit::MONTH
|
248
|
+
else
|
249
|
+
if el.time.day_changed? and unit>Unit::DAY
|
250
|
+
unit = Unit::DAY
|
251
|
+
else
|
252
|
+
unit = Unit::HOUR
|
253
|
+
break
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
258
|
+
v = el.value
|
259
|
+
end
|
260
|
+
return Unit.name(unit)
|
261
|
+
end
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
def if_zero_then(other_array)
|
266
|
+
return self if other_array.nil?
|
267
|
+
raise NilVectorError if @v.nil? || (other_array.is_a?(TimeArray) && other_array.v.nil?)
|
268
|
+
|
269
|
+
c = self.clone
|
270
|
+
if other_array.is_a? Numeric
|
271
|
+
default_value = other_array
|
272
|
+
else
|
273
|
+
c.align_with(other_array)
|
274
|
+
end
|
275
|
+
|
276
|
+
c.size.times do |i|
|
277
|
+
c.set_value(i, default_value || other_array.value(i) || 0.0) if c.value(i).zero?
|
278
|
+
end
|
279
|
+
c
|
280
|
+
end
|
281
|
+
alias_method :if_zero, :if_zero_then
|
282
|
+
|
283
|
+
|
284
|
+
def month_sum
|
285
|
+
h = {}
|
286
|
+
@v.each_with_index do |value, i|
|
287
|
+
time = @start_time + i.hours
|
288
|
+
h[time.month] ||= 0.0
|
289
|
+
h[time.month] += value
|
290
|
+
end
|
291
|
+
c = self.clone
|
292
|
+
c.size.times do |i|
|
293
|
+
time = @start_time + i.hours
|
294
|
+
c.set_value(i, h[time.month])
|
295
|
+
end
|
296
|
+
c
|
297
|
+
end
|
298
|
+
|
299
|
+
# get the count of month hours
|
300
|
+
def month_count
|
301
|
+
h = {}
|
302
|
+
@v.size.times do |i|
|
303
|
+
time = @start_time + i.hours
|
304
|
+
h[time.month] ||= 0.0
|
305
|
+
h[time.month] += 1
|
306
|
+
end
|
307
|
+
|
308
|
+
# @v.each_with_index do |value, i|
|
309
|
+
# time = @start_time + i.hours
|
310
|
+
# h[time.month] ||= 0.0
|
311
|
+
# h[time.month] += 1
|
312
|
+
# end
|
313
|
+
|
314
|
+
c = self.clone
|
315
|
+
c.size.times do |i|
|
316
|
+
time = @start_time + i.hours
|
317
|
+
c.set_value(i, h[time.month])
|
318
|
+
end
|
319
|
+
c
|
320
|
+
end
|
321
|
+
|
322
|
+
# execute the block (likely a calculation) on each vector element.
|
323
|
+
# and return a new time_array obj
|
324
|
+
def execute(&block)
|
325
|
+
v = @v.map &block
|
326
|
+
TimeArray.new(@start_time, v, zone: Time.zone.name)
|
327
|
+
end
|
328
|
+
|
329
|
+
# Iterates each hour, the elements are the values of array
|
330
|
+
# time_array.each do |value|
|
331
|
+
# value
|
332
|
+
# end
|
333
|
+
def each(&block)
|
334
|
+
@v.each &block
|
335
|
+
end
|
336
|
+
|
337
|
+
|
338
|
+
# Get value associated to a date
|
339
|
+
def value_at(datetime_or_year, month=nil, day=nil, hour=nil)
|
340
|
+
if datetime_or_year.is_a? Time
|
341
|
+
dt = datetime_or_year
|
342
|
+
else
|
343
|
+
dt = Time.zone.parse("#{datetime_or_year}-#{month}-#{day} #{hour}:00")
|
344
|
+
end
|
345
|
+
i = ((dt - @start_time)/3600.0).to_i
|
346
|
+
raise DateTimeOutRangeError if i<0 || i>(size-1)
|
347
|
+
value(i)
|
348
|
+
end
|
349
|
+
|
350
|
+
|
351
|
+
# Iterates each hour, the elements are TimeAndValue objects
|
352
|
+
# time_array.each_with_time do |el|
|
353
|
+
# e.time
|
354
|
+
# e.value
|
355
|
+
# end
|
356
|
+
def each_with_time(&block)
|
357
|
+
arr = @v.map.with_index{|v, i| TimeAndValue.new(@start_time+i.hours, v)}
|
358
|
+
arr.each(&block)
|
359
|
+
end
|
202
360
|
# ===========================================================
|
203
361
|
|
204
362
|
private
|
@@ -244,6 +402,7 @@ module TimeArray
|
|
244
402
|
vector
|
245
403
|
end
|
246
404
|
|
405
|
+
|
247
406
|
def set_start_time(start_time)
|
248
407
|
st = read_start_time(start_time)
|
249
408
|
@start_time = floor_start_time(st, @unit)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'active_support/core_ext/time/zones'
|
2
|
+
|
3
|
+
module TimeArray
|
4
|
+
module TimeExt
|
5
|
+
|
6
|
+
module InstanceMethods
|
7
|
+
# has year changed in last hour?
|
8
|
+
def year_changed?
|
9
|
+
self.year != (self-1.hour).year
|
10
|
+
end
|
11
|
+
|
12
|
+
# has year changed in last month?
|
13
|
+
def month_changed?
|
14
|
+
self.month != (self-1.hour).month
|
15
|
+
end
|
16
|
+
|
17
|
+
# has year changed in last day?
|
18
|
+
def day_changed?
|
19
|
+
self.day != (self-1.hour).day
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.included(receiver)
|
24
|
+
receiver.send :include, InstanceMethods
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/time_array/version.rb
CHANGED
data/lib/time_array.rb
CHANGED
@@ -7,6 +7,8 @@
|
|
7
7
|
array_ext
|
8
8
|
units
|
9
9
|
group_hash
|
10
|
+
compactor
|
11
|
+
time_ext
|
10
12
|
).each { |file| require File.join(File.dirname(__FILE__), 'time_array', file) }
|
11
13
|
|
12
14
|
|
@@ -14,3 +16,6 @@ class Array
|
|
14
16
|
include TimeArray::ArrayExt
|
15
17
|
end
|
16
18
|
|
19
|
+
class Time
|
20
|
+
include TimeArray::TimeExt
|
21
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'active_support/core_ext/time/zones'
|
3
|
+
|
4
|
+
RSpec.describe 'compactor' do
|
5
|
+
subject(:year) { "2013" }
|
6
|
+
subject(:y_arr) { [3.14] }
|
7
|
+
subject(:m_arr) { (1..12).to_a.map{|e| e%2==0 ? 1.2 : 2.98} }
|
8
|
+
subject(:d_arr) { (1..365).to_a.map{|e| e%2==0 ? 1.2 : 2.98} }
|
9
|
+
subject(:h_arr) { (1..8760).to_a.map{|e| e%2==0 ? 1.2 : 2.98} }
|
10
|
+
|
11
|
+
def build_flat_array(year, step)
|
12
|
+
Time.zone = "Rome"
|
13
|
+
t0 = Time.zone.parse("#{year}-01-01")
|
14
|
+
t1 = t0+1.year
|
15
|
+
size = (t1-t0)/3600
|
16
|
+
Array.new(size){|i| (t0+i.hours).send(step) }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'initial unit is hour' do
|
20
|
+
it "compact month to hour" do
|
21
|
+
arr = build_flat_array(2013, :month)
|
22
|
+
ta = TimeArray::TimeArray.new(year, arr)
|
23
|
+
expect(ta.greatest_unit).to eq(:month)
|
24
|
+
|
25
|
+
arr[2] = -1.0 # dirty, degrade to :hour
|
26
|
+
ta = TimeArray::TimeArray.new(year, arr)
|
27
|
+
expect(ta.greatest_unit).to eq(:hour)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "compact day to hour" do
|
31
|
+
arr = build_flat_array(2013, :day)
|
32
|
+
ta = TimeArray::TimeArray.new(year, arr)
|
33
|
+
expect(ta.greatest_unit).to eq(:day)
|
34
|
+
|
35
|
+
arr[2] = -1.0 # dirty, degrade to :hour
|
36
|
+
ta = TimeArray::TimeArray.new(year, arr)
|
37
|
+
expect(ta.greatest_unit).to eq(:hour)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "cannot compact" do
|
41
|
+
ta = TimeArray::TimeArray.new(year, h_arr)
|
42
|
+
expect(ta.greatest_unit).to eq(:hour)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "compact an array shorten than year" do
|
46
|
+
arr = Array.new(24, 1.4)
|
47
|
+
ta = TimeArray::TimeArray.new(year, arr)
|
48
|
+
expect(ta.greatest_unit).to eq(:day)
|
49
|
+
|
50
|
+
arr = Array.new(24*31, 1.4)
|
51
|
+
ta = TimeArray::TimeArray.new(year, arr)
|
52
|
+
expect(ta.greatest_unit).to eq(:month)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'initial unit is not hour' do
|
57
|
+
|
58
|
+
it "compact to year" do
|
59
|
+
ta = TimeArray::TimeArray.new(year, y_arr, unit: :year)
|
60
|
+
expect(ta.greatest_unit).to eq(:year)
|
61
|
+
|
62
|
+
arr = Array.new(8760, 4.1)
|
63
|
+
ta = TimeArray::TimeArray.new(year, arr)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "compact to month" do
|
67
|
+
ta = TimeArray::TimeArray.new(year, m_arr, unit: :month)
|
68
|
+
st = ta.start_time.clone
|
69
|
+
expect(ta.greatest_unit).to eq(:month)
|
70
|
+
expect(ta.start_time).to eq(st)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "compact to day" do
|
74
|
+
ta = TimeArray::TimeArray.new(year, d_arr, unit: :day)
|
75
|
+
expect(ta.greatest_unit).to eq(:day)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
context 'compact method' do
|
81
|
+
it "compact to year" do
|
82
|
+
ta = TimeArray::TimeArray.new(year, y_arr, unit: :year)
|
83
|
+
compact = ta.compact
|
84
|
+
expect(compact.unit).to eq(:year)
|
85
|
+
expect(compact).to respond_to(:v)
|
86
|
+
expect(compact).to respond_to(:values)
|
87
|
+
expect(compact).to respond_to(:array)
|
88
|
+
expect(compact.v).to eq(y_arr)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "compact to month" do
|
92
|
+
ta = TimeArray::TimeArray.new(year, m_arr, unit: :month)
|
93
|
+
compact = ta.compact
|
94
|
+
expect(compact.unit).to eq(:month)
|
95
|
+
expect(compact.v).to eq(m_arr)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "compact to day" do
|
99
|
+
ta = TimeArray::TimeArray.new(year, d_arr, unit: :day)
|
100
|
+
compact = ta.compact
|
101
|
+
expect(compact.unit).to eq(:day)
|
102
|
+
expect(compact.v).to eq(d_arr)
|
103
|
+
end
|
104
|
+
|
105
|
+
it "compact to hour" do
|
106
|
+
ta = TimeArray::TimeArray.new(year, h_arr, unit: :hour)
|
107
|
+
compact = ta.compact
|
108
|
+
expect(compact.unit).to eq(:hour)
|
109
|
+
expect(compact.v).to eq(h_arr)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'hour array counts' do
|
4
|
+
# subject(:v1) { Array.new([0,1,-2,5,-7,11,-13]) }
|
5
|
+
# subject(:v2) { Array.new([1,7,19,31,-13,5,-2]) }
|
6
|
+
v1 = [0,1,-2,5,-7,11,-13]
|
7
|
+
v2 = [1,7,19,31,-13,5,-2]
|
8
|
+
v12 = v1.map.with_index{|e,i| v1[i]+v2[i]}
|
9
|
+
ta1 = TimeArray::TimeArray.new("2013", v1)
|
10
|
+
ta2 = TimeArray::TimeArray.new("2013", v2)
|
11
|
+
ta12 = TimeArray::TimeArray.new("2013", v12)
|
12
|
+
|
13
|
+
it 'sum all elements' do
|
14
|
+
# expect((ta1.sum(ta2)).v).to eq(ta12.v)
|
15
|
+
# expect(ta1.v).to eq(v1)
|
16
|
+
end
|
17
|
+
end
|
@@ -50,4 +50,98 @@ RSpec.describe 'operations' do
|
|
50
50
|
expect(c.size).to eq(8760)
|
51
51
|
expect(c.sum).to eq(Float::INFINITY)
|
52
52
|
end
|
53
|
+
|
54
|
+
it "sum month values" do
|
55
|
+
year = 2013
|
56
|
+
b = TimeArray::TimeArray.new(year.to_s, [1], unit: :year)
|
57
|
+
b = b.month_sum
|
58
|
+
h = {}
|
59
|
+
Time.zone = "Rome"
|
60
|
+
12.times do |m|
|
61
|
+
t1 = Time.zone.parse("#{year}-#{m+1}-01 00:00")
|
62
|
+
t2 = t1+1.month
|
63
|
+
t3 = (t2-t1)/3600
|
64
|
+
hours = t3.to_i
|
65
|
+
h[m+1] = hours
|
66
|
+
end
|
67
|
+
h.each_pair do |month, count|
|
68
|
+
expect(b.value_at(year,month,1,0)).to eq(count)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "#value_at" do
|
73
|
+
context 'date as datetime' do
|
74
|
+
context 'correct date' do
|
75
|
+
it "return the correct first value" do
|
76
|
+
Time.zone = "Rome"
|
77
|
+
dt = Time.zone.parse("2013-01-01 00:00")
|
78
|
+
expect(a1.value_at(dt)).to eq(2.1)
|
79
|
+
end
|
80
|
+
it "return the correct last value" do
|
81
|
+
Time.zone = "Rome"
|
82
|
+
dt = Time.zone.parse("2013-12-31 23:00")
|
83
|
+
expect(a1.value_at(dt)).to eq(2.1)
|
84
|
+
end
|
85
|
+
it "return the correct inner value" do
|
86
|
+
Time.zone = "Rome"
|
87
|
+
dt = Time.zone.parse("2013-05-21 05:00")
|
88
|
+
expect(a1.value_at(dt)).to eq(2.1)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
context 'date not correct' do
|
92
|
+
it "raise error with a date before" do
|
93
|
+
Time.zone = "Rome"
|
94
|
+
dt = Time.zone.parse("2012-12-31 23:00")
|
95
|
+
expect{ a1.value_at(dt)}.to raise_error(RuntimeError)
|
96
|
+
end
|
97
|
+
it "raise error with a date after" do
|
98
|
+
Time.zone = "Rome"
|
99
|
+
dt = Time.zone.parse("2014-01-01 00:00")
|
100
|
+
expect{ a1.value_at(dt)}.to raise_error(RuntimeError)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'date as numbers' do
|
107
|
+
context 'correct date' do
|
108
|
+
it "return the correct first value" do
|
109
|
+
expect(a1.value_at(2013,1,1,0)).to eq(2.1)
|
110
|
+
end
|
111
|
+
it "return the correct last value" do
|
112
|
+
expect(a1.value_at(2013,12,31,23)).to eq(2.1)
|
113
|
+
end
|
114
|
+
it "return the correct inner value" do
|
115
|
+
expect(a1.value_at(2013,5,21,5)).to eq(2.1)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
context 'date not correct' do
|
119
|
+
it "raise error with a date before" do
|
120
|
+
expect{ a1.value_at(2012,12,31,23)}.to raise_error(RuntimeError)
|
121
|
+
end
|
122
|
+
it "raise error with a date after" do
|
123
|
+
expect{ a1.value_at(2014,1,1,0)}.to raise_error(RuntimeError)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
it "#month_count" do
|
130
|
+
a = a1.month_count
|
131
|
+
expect(a.value(0)).to eq(24*31)
|
132
|
+
expect(a.value(1)).to eq(24*31)
|
133
|
+
expect(a.value(24*31)).to eq(24*28)
|
134
|
+
expect(a.value(24*31+24*28)).to eq(24*31-1) # because of daylight saving time
|
135
|
+
expect(a.value(a.size-1)).to eq(24*31)
|
136
|
+
end
|
137
|
+
|
138
|
+
it '#execute' do
|
139
|
+
b = a1.execute do |v|
|
140
|
+
2 * v + 1
|
141
|
+
end
|
142
|
+
expect(b.value(0)).to eq(2*2.1+1)
|
143
|
+
expect(b.value(1000)).to eq(2*2.1+1)
|
144
|
+
expect(b.value(b.size-1)).to eq(2*2.1+1)
|
145
|
+
end
|
146
|
+
end
|
53
147
|
end
|
data/spec/time_array_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
3
|
+
describe TimeArray do
|
4
4
|
subject(:a1) { TimeArray::TimeArray.new("2013", [1,2,3], zone: "Rome", unit: :hour) }
|
5
5
|
values = [1,2,3]
|
6
6
|
subject(:v1) { [0,1,2,-3,0,2,-1] }
|
@@ -209,4 +209,58 @@ RSpec.describe 'time array' do
|
|
209
209
|
a = TimeArray::TimeArray.new("2013", arr, zone: "Rome", unit: :hour)
|
210
210
|
expect(a.first_values(23)).to eq(arr[0...23])
|
211
211
|
end
|
212
|
+
|
213
|
+
|
214
|
+
describe "#if_zero_then" do
|
215
|
+
it "compare to another timearray" do
|
216
|
+
a = TimeArray::TimeArray.new("2013", [1,2,0,4,5,0,7,0,0], zone: "Rome", unit: :hour)
|
217
|
+
b = TimeArray::TimeArray.new("2013", [9,0,7,6,5,4,3,0,1], zone: "Rome", unit: :hour)
|
218
|
+
|
219
|
+
expect(a.if_zero_then(b).v).to eq([1,2,7,4,5,4,7,0,1])
|
220
|
+
expect(b.if_zero_then(a).v).to eq([9,2,7,6,5,4,3,0,1])
|
221
|
+
end
|
222
|
+
|
223
|
+
it "compare to a number" do
|
224
|
+
a = TimeArray::TimeArray.new("2013", [1,2,0,4,5,0,7,0,0], zone: "Rome", unit: :hour)
|
225
|
+
expect(a.if_zero_then(10).v).to eq([1,2,10,4,5,10,7,10,10])
|
226
|
+
end
|
227
|
+
|
228
|
+
end
|
229
|
+
|
230
|
+
describe "#each" do
|
231
|
+
it "get values" do
|
232
|
+
a = TimeArray::TimeArray.new("2013", [1,2,3,4], zone: "Rome", unit: :hour)
|
233
|
+
a.each.with_index do |el, i|
|
234
|
+
expect(el).to eq(i+1)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
describe "#each_with_time" do
|
240
|
+
it "get values" do
|
241
|
+
a = TimeArray::TimeArray.new("2013", [1,2,3,4], zone: "Rome", unit: :hour)
|
242
|
+
a.each_with_time.with_index do |el, i|
|
243
|
+
expect(el.value).to eq(i+1)
|
244
|
+
expect(el.v).to eq(i+1) # alias
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
it "get datetimes" do
|
249
|
+
a = TimeArray::TimeArray.new("2013", [1,2,3,4], zone: "Rome", unit: :hour)
|
250
|
+
|
251
|
+
a.each_with_time.with_index do |el, i|
|
252
|
+
expect(el.datetime).to eq(a.start_time+i.hours)
|
253
|
+
expect(el.time).to eq(a.start_time+i.hours) # alias
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
it "get other if zero" do
|
259
|
+
a = TimeArray::TimeArray.new("2013", [1,2,0,4,5,0,7,0,0], zone: "Rome", unit: :hour)
|
260
|
+
b = TimeArray::TimeArray.new("2013", [9,0,7,6,5,4,3,0,1], zone: "Rome", unit: :hour)
|
261
|
+
|
262
|
+
c = a.if_zero(b)
|
263
|
+
expect(a.if_zero(b).v).to eq([1,2,7,4,5,4,7,0,1])
|
264
|
+
expect(b.if_zero(a).v).to eq([9,2,7,6,5,4,3,0,1])
|
265
|
+
end
|
212
266
|
end
|
data/spec/time_spec.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'time' do
|
4
|
+
subject(:a1) { [1,2,3].timed("2014") }
|
5
|
+
|
6
|
+
describe 'year_changed?' do
|
7
|
+
it "is true" do
|
8
|
+
expect(Time.new(2015,1,1,0,20).year_changed?).to be true
|
9
|
+
end
|
10
|
+
|
11
|
+
it "is false" do
|
12
|
+
expect(Time.new(2015,1,1,1,0).year_changed?).to be false
|
13
|
+
expect(Time.new(2015,1,2,0,20).year_changed?).to be false
|
14
|
+
expect(Time.new(2015,2,1,0,20).year_changed?).to be false
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'month_changed?' do
|
19
|
+
it "is true" do
|
20
|
+
expect(Time.new(2015,1,1,0,20).month_changed?).to be true
|
21
|
+
expect(Time.new(2015,2,1,0,20).month_changed?).to be true
|
22
|
+
end
|
23
|
+
|
24
|
+
it "is false" do
|
25
|
+
expect(Time.new(2015,1,1,1,0).month_changed?).to be false
|
26
|
+
expect(Time.new(2015,1,2,0,20).month_changed?).to be false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'day_changed?' do
|
31
|
+
it "is true" do
|
32
|
+
expect(Time.new(2015,1,1,0,20).day_changed?).to be true
|
33
|
+
expect(Time.new(2015,1,2,0,20).day_changed?).to be true
|
34
|
+
expect(Time.new(2015,2,1,0,20).day_changed?).to be true
|
35
|
+
end
|
36
|
+
|
37
|
+
it "is false" do
|
38
|
+
expect(Time.new(2015,1,1,1,0).day_changed?).to be false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/usage/usage_01.rb
CHANGED
@@ -1,20 +1,46 @@
|
|
1
1
|
require_relative '../lib/time_array'
|
2
2
|
|
3
|
+
if false
|
4
|
+
arr = TimeArray::TimeArray.new("2016-03", [1,2,3])
|
5
|
+
puts arr.start_time.to_s
|
6
|
+
puts arr.start_time.class
|
7
|
+
puts arr.start_time.inspect
|
3
8
|
|
4
|
-
|
5
|
-
puts
|
6
|
-
puts arr.start_time.class
|
7
|
-
puts arr.start_time.inspect
|
9
|
+
# puts Time.zone.parse("2014")
|
10
|
+
# puts Time.zone.parse("2020-01-02")
|
8
11
|
|
9
|
-
|
10
|
-
|
12
|
+
a = [2,3,4].timed("2012", unit: :day)
|
13
|
+
puts a.size
|
14
|
+
puts a.v.inspect
|
11
15
|
|
12
|
-
a = [2,3,4].timed("2012", unit: :day)
|
13
|
-
puts a.size
|
14
|
-
puts a.v.inspect
|
15
16
|
|
17
|
+
puts [1,2,3].timed("2014", unit: :day).size
|
16
18
|
|
17
|
-
puts
|
19
|
+
puts arr.group_by(:day)
|
20
|
+
puts arr.group_by(:hour)
|
21
|
+
|
22
|
+
|
23
|
+
puts arr.start_time.strftime("%Y-%m-%d %H:%M")
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
if true
|
28
|
+
ta = TimeArray::TimeArray.new("2015", [1,2,2,2,2,2,2,2,2,2,2,2], unit: :month)
|
29
|
+
# puts ta.v.inspect
|
30
|
+
ta2 = ta.compact
|
31
|
+
puts ta2.v.inspect
|
32
|
+
puts ta2.inspect # TimeArray::Compact
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
if true
|
37
|
+
interval = :month
|
38
|
+
start_time = Time.new(2015,1,1)
|
39
|
+
array = [1,2,2,2,2,2,2,2,2,2,2,2]
|
40
|
+
ta1 = array.timed(start_time, unit: interval)
|
41
|
+
# puts ta1
|
42
|
+
ta2 = ta1.compact
|
43
|
+
puts ta2.inspect
|
44
|
+
puts ta2
|
45
|
+
end
|
18
46
|
|
19
|
-
puts arr.group_by(:day)
|
20
|
-
puts arr.group_by(:hour)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_array
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.7.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Iwan Buetti
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -181,18 +181,23 @@ files:
|
|
181
181
|
- Rakefile
|
182
182
|
- lib/time_array.rb
|
183
183
|
- lib/time_array/array_ext.rb
|
184
|
+
- lib/time_array/compactor.rb
|
184
185
|
- lib/time_array/exceptions.rb
|
185
186
|
- lib/time_array/group_hash.rb
|
186
187
|
- lib/time_array/hour_array.rb
|
187
188
|
- lib/time_array/time_array.rb
|
189
|
+
- lib/time_array/time_ext.rb
|
188
190
|
- lib/time_array/units.rb
|
189
191
|
- lib/time_array/vector.rb
|
190
192
|
- lib/time_array/version.rb
|
191
193
|
- spec/array_spec.rb
|
192
194
|
- spec/hour_array_spec.rb
|
193
195
|
- spec/spec_helper.rb
|
196
|
+
- spec/time_array_compact_spec.rb
|
197
|
+
- spec/time_array_counts_spec.rb
|
194
198
|
- spec/time_array_operations_spec.rb
|
195
199
|
- spec/time_array_spec.rb
|
200
|
+
- spec/time_spec.rb
|
196
201
|
- spec/units_spec.rb
|
197
202
|
- spec/vector_spec.rb
|
198
203
|
- time_array.gemspec
|
@@ -217,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
217
222
|
version: '0'
|
218
223
|
requirements: []
|
219
224
|
rubyforge_project:
|
220
|
-
rubygems_version: 2.
|
225
|
+
rubygems_version: 2.4.5.1
|
221
226
|
signing_key:
|
222
227
|
specification_version: 4
|
223
228
|
summary: Some tools to deal with time based arrays
|
@@ -225,8 +230,10 @@ test_files:
|
|
225
230
|
- spec/array_spec.rb
|
226
231
|
- spec/hour_array_spec.rb
|
227
232
|
- spec/spec_helper.rb
|
233
|
+
- spec/time_array_compact_spec.rb
|
234
|
+
- spec/time_array_counts_spec.rb
|
228
235
|
- spec/time_array_operations_spec.rb
|
229
236
|
- spec/time_array_spec.rb
|
237
|
+
- spec/time_spec.rb
|
230
238
|
- spec/units_spec.rb
|
231
239
|
- spec/vector_spec.rb
|
232
|
-
has_rdoc:
|