timechunker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Timechunker
2
+
3
+ ## What it does
4
+
5
+ Timechunker allows you to split a time range into pieces of a defined size, like this:
6
+
7
+ require "time"
8
+ require "timechunker"
9
+
10
+ timerange = Timechunker::Timerange.new(Time.parse("2011-02-03 18:04:02"),
11
+ Time.parse("2011-02-03 18:29:02"))
12
+
13
+ five_minutes = Timechunker::Chunksize.new(5, 'minutes')
14
+
15
+ chunker = Timechunker::Chunker.new
16
+ chunker.get_chunks(timerange, five_minutes)
17
+
18
+ This will give you a list of all 5-minute chunks this timerange fits into
19
+
20
+ => [Thu Feb 03 18:00:00 +0100 2011,
21
+ Thu Feb 03 18:05:00 +0100 2011,
22
+ Thu Feb 03 18:10:00 +0100 2011,
23
+ Thu Feb 03 18:15:00 +0100 2011,
24
+ Thu Feb 03 18:20:00 +0100 2011,
25
+ Thu Feb 03 18:25:00 +0100 2011]
26
+
27
+ ## Installation
28
+
29
+ gem install timechunker
30
+
31
+ ## TODO
32
+
33
+ Only works with 'minute' chunks, other chunk types need to be supported, too.
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2
+
3
+ require "rake/testtask"
4
+ require "timechunker/version"
5
+
6
+ desc "Default: run tests"
7
+ task :default => :test
8
+
9
+ desc "Run tests."
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << "lib"
12
+ t.libs << "test"
13
+ t.pattern = "test/**/*_test.rb"
14
+ t.verbose = true
15
+ end
16
+
17
+ desc "Build a gem from gemspec"
18
+ task :build do
19
+ system "gem build timechunker.gemspec"
20
+ end
21
+
22
+ desc "Release new gem version"
23
+ task :release => :build do
24
+ system "gem push timechunker-#{Timechunker::VERSION}.gem"
25
+ end
@@ -0,0 +1,3 @@
1
+ require 'timechunker/chunksize'
2
+ require 'timechunker/timerange'
3
+ require 'timechunker/chunker'
@@ -0,0 +1,37 @@
1
+ module Timechunker
2
+ class Chunker
3
+
4
+ def get_chunks(timerange, chunksize)
5
+ if chunksize.type == 'minutes'
6
+ number_of_chunks = ((timerange.end_time - timerange.start_time) / 60 / chunksize.size).ceil.to_i
7
+ number_of_chunks = number_of_chunks + 1
8
+
9
+ first_chunk = find_chunk(timerange.start_time, chunksize)
10
+
11
+ chunks = []
12
+ number_of_chunks.times do |i|
13
+ chunk = first_chunk + (i * 60 * chunksize.size)
14
+ chunks << chunk unless chunk > timerange.end_time
15
+ end
16
+
17
+ return chunks
18
+
19
+ end
20
+ end
21
+
22
+
23
+ private
24
+
25
+ def find_chunk(time, chunksize)
26
+ if chunksize.type == 'minutes'
27
+ first_chunk = Time.local(time.year,
28
+ time.month,
29
+ time.day,
30
+ time.hour,
31
+ time.min / chunksize.size * chunksize.size,
32
+ 0)
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,12 @@
1
+ module Timechunker
2
+ class Chunksize
3
+
4
+ attr_reader :size, :type
5
+
6
+ def initialize(size, type)
7
+ @size = size
8
+ @type = type
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Timechunker
2
+ class Timerange
3
+
4
+ attr_reader :start_time, :end_time
5
+
6
+ def initialize(start_time, end_time)
7
+ @start_time = start_time
8
+ @end_time = end_time
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Timechunker
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,112 @@
1
+ require "test/unit"
2
+ require "timechunker"
3
+
4
+ class ChunkerTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ end
8
+
9
+ def teardown
10
+ end
11
+
12
+ def test_chunk_calculation_default
13
+ expected = [Time.local(2011, 1, 24, 13, 55, 0),
14
+ Time.local(2011, 1, 24, 14, 0, 0),
15
+ Time.local(2011, 1, 24, 14, 5, 0),
16
+ Time.local(2011, 1, 24, 14, 10, 0)]
17
+
18
+ timerange = Timechunker::Timerange.new(Time.local(2011, 1, 24, 13, 57, 0), Time.local(2011, 1, 24, 14, 10, 3))
19
+ five_minutes = Timechunker::Chunksize.new(5, 'minutes')
20
+
21
+ chunker = Timechunker::Chunker.new
22
+ actual = chunker.get_chunks(timerange, five_minutes)
23
+
24
+ assert_equal(expected, actual)
25
+ end
26
+
27
+ def test_chunk_calculation_other
28
+ expected = [Time.local(2011, 1, 24, 14, 0, 0),
29
+ Time.local(2011, 1, 24, 14, 5, 0),
30
+ Time.local(2011, 1, 24, 14, 10, 0),
31
+ Time.local(2011, 1, 24, 14, 15, 0),
32
+ Time.local(2011, 1, 24, 14, 20, 0),
33
+ Time.local(2011, 1, 24, 14, 25, 0)]
34
+
35
+ timerange = Timechunker::Timerange.new(Time.local(2011, 1, 24, 14, 4, 0), Time.local(2011, 1, 24, 14, 26, 3))
36
+ five_minutes = Timechunker::Chunksize.new(5, 'minutes')
37
+
38
+ chunker = Timechunker::Chunker.new
39
+ actual = chunker.get_chunks(timerange, five_minutes)
40
+
41
+ assert_equal(expected, actual)
42
+ end
43
+
44
+ def test_chunk_calculation_singlechunk
45
+ expected = [Time.local(2011, 1, 24, 13, 55, 0)]
46
+
47
+ timerange = Timechunker::Timerange.new(Time.local(2011, 1, 24, 13, 57, 0), Time.local(2011, 1, 24, 13, 58, 3))
48
+ five_minutes = Timechunker::Chunksize.new(5, 'minutes')
49
+
50
+ chunker = Timechunker::Chunker.new
51
+ actual = chunker.get_chunks(timerange, five_minutes)
52
+
53
+ assert_equal(expected, actual)
54
+ end
55
+
56
+ def test_chunk_calculation_singlechunk_boundary
57
+ expected = [Time.local(2011, 1, 24, 13, 55, 0)]
58
+
59
+ timerange = Timechunker::Timerange.new(Time.local(2011, 1, 24, 13, 55, 0), Time.local(2011, 1, 24, 13, 59, 59))
60
+ five_minutes = Timechunker::Chunksize.new(5, 'minutes')
61
+
62
+ chunker = Timechunker::Chunker.new
63
+ actual = chunker.get_chunks(timerange, five_minutes)
64
+
65
+ assert_equal(expected, actual)
66
+ end
67
+
68
+ def test_chunk_calculation_twochunks_boundary
69
+ expected = [Time.local(2011, 1, 24, 13, 55, 0),
70
+ Time.local(2011, 1, 24, 14, 0, 0)]
71
+
72
+ timerange = Timechunker::Timerange.new(Time.local(2011, 1, 24, 13, 55, 0), Time.local(2011, 1, 24, 14, 0, 0))
73
+ five_minutes = Timechunker::Chunksize.new(5, 'minutes')
74
+
75
+ chunker = Timechunker::Chunker.new
76
+ actual = chunker.get_chunks(timerange, five_minutes)
77
+
78
+ assert_equal(expected, actual)
79
+ end
80
+
81
+ def test_chunk_calculation_fullhour
82
+ expected = [Time.local(2011, 1, 24, 13, 0, 0),
83
+ Time.local(2011, 1, 24, 13, 20, 0),
84
+ Time.local(2011, 1, 24, 13, 40, 0),
85
+ Time.local(2011, 1, 24, 14, 0, 0)]
86
+
87
+ timerange = Timechunker::Timerange.new(Time.local(2011, 1, 24, 13, 0, 0), Time.local(2011, 1, 24, 14, 0, 0))
88
+ twenty_minutes = Timechunker::Chunksize.new(20, 'minutes')
89
+
90
+ chunker = Timechunker::Chunker.new
91
+ actual = chunker.get_chunks(timerange, twenty_minutes)
92
+
93
+ assert_equal(expected, actual)
94
+ end
95
+
96
+ def test_chunk_calculation_uneven
97
+ expected = [Time.local(2011, 1, 24, 13, 0, 0),
98
+ Time.local(2011, 1, 24, 13, 21, 0),
99
+ Time.local(2011, 1, 24, 13, 42, 0),
100
+ Time.local(2011, 1, 24, 14, 3, 0),
101
+ Time.local(2011, 1, 24, 14, 24, 0)]
102
+
103
+ timerange = Timechunker::Timerange.new(Time.local(2011, 1, 24, 13, 0, 0), Time.local(2011, 1, 24, 14, 30, 0))
104
+ twentyone_minutes = Timechunker::Chunksize.new(21, 'minutes')
105
+
106
+ chunker = Timechunker::Chunker.new
107
+ actual = chunker.get_chunks(timerange, twentyone_minutes)
108
+
109
+ assert_equal(expected, actual)
110
+ end
111
+
112
+ end
@@ -0,0 +1,19 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "timechunker/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "timechunker"
6
+ s.version = Timechunker::VERSION.dup
7
+ s.description = "Simple class to chunk a time range into ranges of a defined size."
8
+ s.summary = "Simple class to chunk a time range into ranges of a defined size."
9
+ s.author = "Manuel Kiessling"
10
+ s.email = "manuel@kiessling.net"
11
+ s.homepage = "https://github.com/ManuelKiessling/rbTimeChunker"
12
+
13
+ s.rubyforge_project = "timechunker"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timechunker
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Manuel Kiessling
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-04 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Simple class to chunk a time range into ranges of a defined size.
23
+ email: manuel@kiessling.net
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - README.md
32
+ - Rakefile
33
+ - lib/timechunker.rb
34
+ - lib/timechunker/chunker.rb
35
+ - lib/timechunker/chunksize.rb
36
+ - lib/timechunker/timerange.rb
37
+ - lib/timechunker/version.rb
38
+ - test/unit/timechunker/chunker_test.rb
39
+ - timechunker.gemspec
40
+ has_rdoc: true
41
+ homepage: https://github.com/ManuelKiessling/rbTimeChunker
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project: timechunker
70
+ rubygems_version: 1.5.0
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Simple class to chunk a time range into ranges of a defined size.
74
+ test_files:
75
+ - test/unit/timechunker/chunker_test.rb