timerator 0.1

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.
Files changed (4) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +22 -0
  3. data/lib/timerator/timerator.rb +46 -0
  4. metadata +68 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Sean Sorrell
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ # timerator
2
+ timerator is a time computing library written that allows you to work with date ranges in smaller chunks
3
+
4
+ ## example
5
+ Timerator.new(Time.gm(2000,"jan",1), Time.gm(2000,"jan",10)).each(:day) do |beginning,ending|
6
+ puts "#{beginning} - #{ending}"
7
+ end
8
+
9
+ =>
10
+
11
+ Sat Jan 01 00:00:00 UTC 2000..Sun Jan 02 00:00:00 UTC 2000
12
+ Sun Jan 02 00:00:00 UTC 2000..Mon Jan 03 00:00:00 UTC 2000
13
+ Mon Jan 03 00:00:00 UTC 2000..Tue Jan 04 00:00:00 UTC 2000
14
+ Tue Jan 04 00:00:00 UTC 2000..Wed Jan 05 00:00:00 UTC 2000
15
+ Wed Jan 05 00:00:00 UTC 2000..Thu Jan 06 00:00:00 UTC 2000
16
+ Thu Jan 06 00:00:00 UTC 2000..Fri Jan 07 00:00:00 UTC 2000
17
+ Fri Jan 07 00:00:00 UTC 2000..Sat Jan 08 00:00:00 UTC 2000
18
+ Sat Jan 08 00:00:00 UTC 2000..Sun Jan 09 00:00:00 UTC 2000
19
+ Sun Jan 09 00:00:00 UTC 2000..Mon Jan 10 00:00:00 UTC 2000
20
+
21
+ ### notes
22
+ supports seconds, minutes, hours, days, and weeks
@@ -0,0 +1,46 @@
1
+ class Timerator
2
+
3
+ TIME_SPANS = {
4
+ :second => 1,
5
+ :minute => 60,
6
+ :hour => 3600,
7
+ :day => 86_400,
8
+ :week => 604_800
9
+ }
10
+
11
+ def initialize start_date = Time.now, end_date = Time.now, inclusive = false
12
+ @start_date = start_date
13
+ @end_date = end_date
14
+ @inclusive = inclusive
15
+ end
16
+
17
+ def slice period = :second
18
+ diff = (@end_date - @start_date) - inclusive_modifier
19
+
20
+ diff /= TIME_SPANS[period]
21
+
22
+ (0..diff).map do |tick|
23
+ span_start = @start_date + TIME_SPANS[period] * tick
24
+ span_end = @start_date + TIME_SPANS[period] * (tick + 1)
25
+ (span_start..[span_end,@end_date].min)
26
+ end
27
+ end
28
+
29
+ def each period = :second
30
+ diff = (@end_date - @start_date) - inclusive_modifier
31
+
32
+ diff /= TIME_SPANS[period]
33
+
34
+ (0..diff).each do |tick|
35
+ span_start = @start_date + TIME_SPANS[period] * tick
36
+ span_end = @start_date + TIME_SPANS[period] * (tick + 1)
37
+ yield span_start, [span_end,@end_date].min
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def inclusive_modifier
44
+ @inclusive ? 0 : 1
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timerator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Sean Sorrell
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-24 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: "timerator takes a start and end Time and allows the programmer to iterate for different segments of time "
22
+ email: seansorrell@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/timerator/timerator.rb
31
+ - LICENSE
32
+ - README.md
33
+ has_rdoc: true
34
+ homepage: http://github.com/rudle/timerator
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 3
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.7
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: timerator is a time range computing library for ruby
67
+ test_files: []
68
+