time_intervals 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 69b4cc127773810eb273ecac77dcbd394c1b6824ebfadb89e29ed04be5225af2
4
+ data.tar.gz: 13c841569f5cd527baacd07b2d90479c9630064014e10af5d273506fe24c26f8
5
+ SHA512:
6
+ metadata.gz: d2fe45965ef0568c4b7acec42fffd0a47b199ea4d0f21fecb31f7ec321d7b38a1d413a4c9146abd11321d802d2dea28241e8dc6265e280e0f334c8adf925479d
7
+ data.tar.gz: 4fd36047bfecce978595fe9c8f369cdd8d9daa3429c8e2d21c07ccb95c2d1c6e7cf2864124eb25a413102d25cd7c835ca1bb5a2ae168ea282c08ba5517bcad18
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.16.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at alistairm@nulogy.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in time_intervals.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Alistair McKinnell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ # TimeIntervals
2
+
3
+ Library for doing operations on collections of time intervals.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'time_intervals'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Development
18
+
19
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
20
+
21
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
22
+
23
+ ## Contributing
24
+
25
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nulogy/time_intervals. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
26
+
27
+ ## License
28
+
29
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
30
+
31
+ ## Code of Conduct
32
+
33
+ Everyone interacting in the TimeIntervals project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nulogy/time_intervals/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "time_intervals"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ require "time_intervals/version"
2
+ require "time_intervals/interval"
3
+ require "time_intervals/collection"
4
+
5
+ module TimeIntervals
6
+ end
@@ -0,0 +1,192 @@
1
+ require "forwardable"
2
+
3
+ #
4
+ # Represents an ordered collection of TimeIntervals::Intervals.
5
+ #
6
+ module TimeIntervals
7
+ class Collection
8
+ include Enumerable
9
+ extend Forwardable
10
+
11
+ ONE_HOUR_IN_SECONDS = 60 * 60
12
+
13
+ def_delegators :time_intervals, :[], :each, :empty?, :hash, :length, :size, :to_ary
14
+
15
+ attr_reader :time_intervals
16
+
17
+ def self.wrap(intervals)
18
+ time_intervals = intervals.map { |interval| Interval.new(interval.started_at, interval.ended_at) }
19
+ new(time_intervals)
20
+ end
21
+
22
+ def initialize(time_intervals = [])
23
+ @time_intervals = Array(time_intervals).sort
24
+ end
25
+
26
+ # Returns true if all of the contained TimeIntervals::Intervals are wholly contained
27
+ # within the bounding interval.
28
+ def all_intervals_within?(bounding_interval)
29
+ length_in_seconds == intersect(bounding_interval).length_in_seconds
30
+ end
31
+
32
+ # Returns true if any of the contained TimeIntervals::Intervals overlap.
33
+ def has_overlapping_intervals?
34
+ length_in_seconds != coalesce.length_in_seconds
35
+ end
36
+
37
+ # Returns a new coalesced collection of TimeIntervals::Intervals where any that
38
+ # overlap or are adjacent are combined into a single TimeIntervals::Interval.
39
+ #
40
+ # Given these TimeIntervals::Intervals in the collection:
41
+ #
42
+ # [--------)
43
+ # [------)
44
+ # [-----) [-------)
45
+ # [-------)
46
+ #
47
+ # Calling #coalesce returns a TimeIntervals::Collection containing
48
+ # these TimeIntervals::Intervals:
49
+ #
50
+ # [------------------) [------------)
51
+ #
52
+ def coalesce
53
+ return self if empty?
54
+
55
+ coalescing = Interval.create(first)
56
+
57
+ result = each_with_object([]) do |current, memo|
58
+ if coalescing.ended_at < current.started_at
59
+ memo << coalescing
60
+ coalescing = Interval.create(current)
61
+ else
62
+ coalescing = Interval.new(
63
+ coalescing.started_at,
64
+ [coalescing.ended_at, current.ended_at].max
65
+ )
66
+ end
67
+ end
68
+
69
+ result << Interval.create(coalescing)
70
+
71
+ Collection.new(result)
72
+ end
73
+
74
+ # Returns a new collection of TimeIntervals::Intervals that contains only
75
+ # intersections with the specified intersections: either a nil,
76
+ # a single TimeIntervals::Interval, or a TimeIntervals::Collection.
77
+ #
78
+ # Note: the intersections are assumed to be disjoint. That is,
79
+ # none of the TimeIntervals::Intervals in intersections overlap.
80
+ #
81
+ # Given these TimeIntervals::Intervals in the collection:
82
+ #
83
+ # [--------)
84
+ # [------)
85
+ # [-----) [-------)
86
+ # [-------)
87
+ #
88
+ # Calling #intersect with these TimeIntervals::Intervals
89
+ #
90
+ # [--------------) [------)
91
+ #
92
+ # returns a TimeIntervals::Collection containing these TimeIntervals::Intervals:
93
+ #
94
+ # [--)
95
+ # [-----) [-) [---)
96
+ # [-----)
97
+ #
98
+ def intersect(intersections)
99
+ result = Array(intersections).each_with_object([]) do |intersection, memo|
100
+ memo.concat(intersect_with_time_interval(intersection))
101
+ end
102
+
103
+ Collection.new(result)
104
+ end
105
+
106
+ def partition_count
107
+ partition_intervals = partition
108
+
109
+ intersect_count(partition_intervals)
110
+ end
111
+
112
+ # Returns a new collection of TimeIntervals::Intervals that are partitions of
113
+ # the original collection.
114
+ #
115
+ # Given the upper TimeIntervals::Intervals, #partition returns the lower TimeIntervals::Intervals:
116
+ #
117
+ # [--------) [-----) [---)
118
+ # | [------) | | |
119
+ # | | | [----) | | |
120
+ # | | | | | | | | |
121
+ # | | | | | | | | |
122
+ # [-----) [-) [--) [---) |
123
+ # [--) [-) [--) [---)
124
+ #
125
+ def partition
126
+ time_points = @time_intervals.flat_map { |i| [i.started_at, i.ended_at] }.uniq.sort
127
+ start_time_points = time_points[0..-2]
128
+ end_time_points = time_points[1..-1]
129
+ raw_intervals = start_time_points.zip(end_time_points)
130
+ Collection.new(raw_intervals.map { |r| Interval.new(*r) })
131
+ end
132
+
133
+ # Counts the number of TimeIntervals::Intervals that intersect with the given
134
+ # collection of TimeIntervals::Intervals.
135
+ #
136
+ # Returns a list of [TimeIntervals::Interval, count] tuples. The TimeIntervals::Intervals in the result are
137
+ # the TimeIntervals::Intervals from the argument.
138
+ def intersect_count(intersections)
139
+ counts = intersections.map { |slice| intersect_with_time_interval(slice).length }
140
+ intersections.zip(counts)
141
+ end
142
+
143
+ # The sum of the lengths of the TimeIntervals::Intervals in the collection as hours.
144
+ #
145
+ def length_in_hours
146
+ length_in_seconds.to_f / ONE_HOUR_IN_SECONDS
147
+ end
148
+
149
+ # The sum of the lengths of the TimeIntervals::Intervals in the collection as seconds.
150
+ #
151
+ def length_in_seconds
152
+ time_intervals.reduce(0) { |total, time_intervals| total + time_intervals.length_in_seconds }
153
+ end
154
+
155
+ def ==(other)
156
+ other.class == self.class && other.time_intervals == time_intervals
157
+ end
158
+ alias_method :eql?, :==
159
+
160
+ private
161
+
162
+ # Returns an array of TimeIntervals::Intervals that contains only intersections
163
+ # with the specified intersecting TimeIntervals::Interval.
164
+ #
165
+ # Given these TimeIntervals::Intervals in the collection:
166
+ #
167
+ # [--------)
168
+ # [------)
169
+ # [-----) [-------)
170
+ # [-------)
171
+ #
172
+ # Calling #intersect_with_time_interval with this TimeIntervals::Interval
173
+ #
174
+ # [---------------)
175
+ #
176
+ # returns this array of TimeIntervals::Intervals:
177
+ #
178
+ # [--)
179
+ # [-----) [--)
180
+ #
181
+ def intersect_with_time_interval(intersecting)
182
+ each_with_object([]) do |current, memo|
183
+ next unless intersecting.overlaps?(current)
184
+
185
+ memo << Interval.new(
186
+ [intersecting.started_at, current.started_at].max,
187
+ [intersecting.ended_at, current.ended_at].min
188
+ )
189
+ end
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,97 @@
1
+ #
2
+ # Represents an interval in time.
3
+ #
4
+ # Each TimeIntervals is interpreted as: [started_at, ended_at). That is, include
5
+ # the started_at time and exclude the ended_at time.
6
+ #
7
+ # Note: both started_at and ended_at are values in seconds without the fractional
8
+ # part that represents microseconds.
9
+ #
10
+
11
+ module TimeIntervals
12
+ class Interval
13
+ include Comparable
14
+
15
+ attr_reader :started_at, :ended_at
16
+ alias_method :start_at, :started_at
17
+ alias_method :end_at, :ended_at
18
+
19
+ def self.create(interval)
20
+ new(interval.started_at, interval.ended_at)
21
+ end
22
+
23
+ def initialize(started_at, ended_at)
24
+ raise "Invalid interval" if started_at.nil? || ended_at.nil?
25
+
26
+ @started_at = as_seconds(started_at)
27
+ @ended_at = as_seconds(ended_at)
28
+
29
+ raise "Invalid interval: #{self}" if @ended_at < @started_at
30
+ end
31
+
32
+ def length_in_seconds
33
+ ended_at - started_at
34
+ end
35
+
36
+ def after?(other)
37
+ other.ended_at <= started_at
38
+ end
39
+
40
+ def before?(other)
41
+ ended_at <= other.started_at
42
+ end
43
+
44
+ def disjoint?(other)
45
+ before?(other) || after?(other)
46
+ end
47
+
48
+ def overlaps?(other)
49
+ !disjoint?(other)
50
+ end
51
+
52
+ def overlap_duration_in_seconds(other)
53
+ return 0 if disjoint?(other)
54
+ [other.ended_at, ended_at].min - [other.started_at, started_at].max
55
+ end
56
+
57
+ def include?(time)
58
+ started_at <= time && time < ended_at
59
+ end
60
+
61
+ def to_s
62
+ "[#{format(started_at)}, #{format(ended_at)}]"
63
+ end
64
+
65
+ def <=>(other)
66
+ comparison = started_at <=> other.started_at
67
+ comparison.zero? ? (other.ended_at <=> ended_at) : comparison
68
+ end
69
+
70
+ def ==(other)
71
+ other.class == self.class && other.state == state
72
+ end
73
+ alias_method :eql?, :==
74
+
75
+ def hash
76
+ state.hash
77
+ end
78
+
79
+ protected
80
+
81
+ def state
82
+ [started_at, ended_at]
83
+ end
84
+
85
+ private
86
+
87
+ # Round any fractional seconds.
88
+ #
89
+ def as_seconds(time_value)
90
+ time_value.round
91
+ end
92
+
93
+ def format(time_value)
94
+ time_value.strftime("%Y-%m-%d %H:%M:%S")
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,3 @@
1
+ module TimeIntervals
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "time_intervals/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "time_intervals"
8
+ spec.version = TimeIntervals::VERSION
9
+ spec.authors = ["Alistair McKinnell"]
10
+ spec.email = ["alistairm@nulogy.com"]
11
+
12
+ spec.summary = "Library for doing operations on collections of time intervals."
13
+ spec.homepage = "https://github.com/nulogy/time_intervals"
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(/^spec\//) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.16"
26
+ spec.add_development_dependency "rake", "~> 12.0"
27
+ spec.add_development_dependency "rspec", "~> 3.8"
28
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: time_intervals
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alistair McKinnell
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.8'
55
+ description:
56
+ email:
57
+ - alistairm@nulogy.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/time_intervals.rb
73
+ - lib/time_intervals/collection.rb
74
+ - lib/time_intervals/interval.rb
75
+ - lib/time_intervals/version.rb
76
+ - time_intervals.gemspec
77
+ homepage: https://github.com/nulogy/time_intervals
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.7.7
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Library for doing operations on collections of time intervals.
101
+ test_files: []