time_chunk 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
data/lib/time_chunk.rb CHANGED
@@ -17,19 +17,29 @@ module TimeChunk
17
17
  end
18
18
 
19
19
  def self.iterate(range, step_size)
20
- begin_time = range.first.is_a?(Time) ? range.first : Time.parse(range.first)
21
- end_time = range.last.is_a?(Time) ? range.last : Time.parse(range.last)
22
- raise ArgumentError, "Given range's begin (#{begin_time.iso8601}) is after its end (#{end_time.iso8601})." if begin_time > end_time
23
-
24
- current_end_time = begin_time - 1
20
+ if range.first.is_a?(Time)
21
+ begin_at = range.first
22
+ end_at = range.last.is_a?(Time) ? range.last : Time.parse(range.last.to_s)
23
+ elsif range.first.is_a?(Date)
24
+ begin_at = range.first
25
+ end_at = range.last.is_a?(Date) ? range.last : Date.parse(range.last.to_s)
26
+ # step size is always seconds, regardless of whether range is Time or Date instances
27
+ step_size = step_size/86400
28
+ else
29
+ begin_at = Time.parse(range.first.to_s)
30
+ end_at = Time.parse(range.last.to_s)
31
+ end
32
+
33
+ raise ArgumentError, "Given range's begin (#{begin_at.to_time.utc.iso8601}) is after its end (#{end_at.to_time.utc.iso8601})." if begin_at > end_at
34
+
35
+ current_end = begin_at - 1
25
36
 
26
37
  begin
27
- current_begin_time = current_end_time + 1
28
- current_end_time = current_begin_time + step_size - 1
29
- current_end_time = [current_end_time, end_time].min
38
+ current_begin = current_end + 1
39
+ current_end = current_begin + step_size - 1
40
+ current_end = [current_end, end_at].min
30
41
 
31
- yield current_begin_time..current_end_time
32
- end while current_end_time < end_time
42
+ yield current_begin..current_end
43
+ end while current_end < end_at
33
44
  end
34
-
35
45
  end
@@ -88,6 +88,21 @@ describe TimeChunk do
88
88
  (t('2013-01-01T02:00:00Z')..t('2013-01-01T02:59:59Z')),
89
89
  ]
90
90
  end
91
+
92
+ it "should handle date arguments" do
93
+ TimeChunk.iterate(
94
+ Date.parse('2013-02-27')..Date.parse('2013-03-05'), 86400*2
95
+ ) do |range|
96
+ @calls << range
97
+ end
98
+
99
+ @calls.map(&:to_s).should eq [
100
+ '2013-02-27..2013-02-28',
101
+ '2013-03-01..2013-03-02',
102
+ '2013-03-03..2013-03-04',
103
+ '2013-03-05..2013-03-05'
104
+ ]
105
+ end
91
106
  end
92
107
 
93
108
  it "should iterate over each_day" do
data/time_chunk.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "time_chunk"
8
- s.version = "0.0.1"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alex Dean"]
12
- s.date = "2013-02-04"
12
+ s.date = "2013-03-08"
13
13
  s.description = "Iterate over time ranges in discrete chunks."
14
14
  s.email = "alex@crackpot.org"
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_chunk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-04 00:00:00.000000000 Z
12
+ date: 2013-03-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -110,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
110
  version: '0'
111
111
  segments:
112
112
  - 0
113
- hash: 446086649663897477
113
+ hash: -3932107950510678518
114
114
  required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  none: false
116
116
  requirements: