3scale_time_range 0.0.6
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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/3scale_time_range.gemspec +27 -0
- data/Gemfile +8 -0
- data/LICENSE +202 -0
- data/NOTICE +14 -0
- data/README.md +29 -0
- data/Rakefile +15 -0
- data/lib/3scale_time_range/granulate.rb +85 -0
- data/lib/3scale_time_range/version.rb +3 -0
- data/lib/3scale_time_range.rb +190 -0
- data/test/granulate_test.rb +137 -0
- data/test/time_range_test.rb +103 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8f44bc184306146b1182c2b1453a8598193c52e8
|
4
|
+
data.tar.gz: 26b347ae284d20377a83dd6082f9ac2f1008518a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b55a7dba0dd5cfd701845de8b9ff69f29644b2184bc2822c72ab358ebaf5612be0afb1f150aab8150829b95632952d820aaf2fd401d2fc1a6be08aa18aa6ad1d
|
7
|
+
data.tar.gz: 5e44d1e22c17163c856de2468fe97bce1841195cd7c7ae918dbaf7ce68a0ec3ac76bde81de7ba36f48096d546038e575553067b4d576c0648342c06309020322
|
data/.gitignore
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require '3scale_time_range/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "3scale_time_range"
|
8
|
+
spec.version = TimeRange::VERSION
|
9
|
+
spec.authors = ["Wojciech Ogrodowczyk", "Alejandro Martinez Ruiz", "Michal Macejko", "Michal Cichra", "David Ortiz Lopez"]
|
10
|
+
spec.email = ["wojciech@haikuco.de", "amr@redhat.com", "mmacejko@redhat.com", "mcichra@redhat.com", "dortiz@redhat.com"]
|
11
|
+
spec.summary = %q{Utility class for ranges of times (time periods).}
|
12
|
+
spec.description = %q{Utility class for ranges of times (time periods). It's like Range, but has additional enumeration capabilities.}
|
13
|
+
spec.homepage = "https://github.com/3scale/3scale_time_range"
|
14
|
+
spec.license = 'Apache-2.0'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "activesupport", ">= 3.2.19"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", ">= 1.6"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "minitest", "~> 5.4.0"
|
26
|
+
spec.add_development_dependency "geminabox", "~> 0.12.4"
|
27
|
+
end
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/NOTICE
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
3scale time range
|
2
|
+
Copyright (c) 2017 Red Hat Inc.
|
3
|
+
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
you may not use this file except in compliance with the License.
|
6
|
+
You may obtain a copy of the License at
|
7
|
+
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
See the License for the specific language governing permissions and
|
14
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
Utility class for ranges of times (time periods). It's like Range, but has
|
2
|
+
additional enumeration capabilities. See examples for the tasty stuff.
|
3
|
+
|
4
|
+
Examples
|
5
|
+
--------
|
6
|
+
|
7
|
+
period = TimeRange.new(1.year.ago, Time.now)
|
8
|
+
|
9
|
+
Enumerate by days
|
10
|
+
|
11
|
+
period.each(:day) { |time| puts time }
|
12
|
+
|
13
|
+
Enumerate by weeks
|
14
|
+
|
15
|
+
period.each(:week) { |time| puts time }
|
16
|
+
|
17
|
+
(also years, months, hours, minutes and seconds)
|
18
|
+
|
19
|
+
Enumerate by custom period
|
20
|
+
|
21
|
+
period.each(42.seconds) { |time| puts time }
|
22
|
+
|
23
|
+
+each+ Returns Enumerator object, so this is also possible (extra yummy):
|
24
|
+
|
25
|
+
period.each(:month).map { |time| time.strftime('%B') }
|
26
|
+
|
27
|
+
Supports all Enumerable interface: find, select, reject, inject, etc.
|
28
|
+
|
29
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
ENV['gem_push'] = 'false'
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require "rake/testtask"
|
4
|
+
|
5
|
+
Rake::TestTask.new(:test) do |t|
|
6
|
+
t.libs << "test"
|
7
|
+
t.pattern = "test/**/*_test.rb"
|
8
|
+
end
|
9
|
+
|
10
|
+
desc 'Release and push the gem to geminabox'
|
11
|
+
task 'geminabox' do
|
12
|
+
Rake::Task['build'].invoke
|
13
|
+
Rake::Task['release'].invoke
|
14
|
+
system('gem inabox')
|
15
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
class TimeRange
|
2
|
+
class Granulate
|
3
|
+
attr_accessor :days, :months, :years, :rest
|
4
|
+
|
5
|
+
def initialize(range)
|
6
|
+
raise 'Supports only TimeRange objects' unless range.is_a? TimeRange
|
7
|
+
|
8
|
+
@days = []
|
9
|
+
@years = []
|
10
|
+
@months = []
|
11
|
+
@rest = []
|
12
|
+
extract(range, :year)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def extract(range, cycle)
|
18
|
+
if cycle.nil?
|
19
|
+
rest << range unless empty_range?(range)
|
20
|
+
return
|
21
|
+
end
|
22
|
+
|
23
|
+
cycle_start, cycle_end = extract_boundaries(range, cycle)
|
24
|
+
if cycle_start && cycle_end
|
25
|
+
send("#{cycle}s") << TimeRange.new(cycle_start, cycle_end)
|
26
|
+
|
27
|
+
if range.begin < cycle_start
|
28
|
+
extract(
|
29
|
+
TimeRange.new(range.begin, (cycle_start - 1.hour).end_of_day, false),
|
30
|
+
next_cycle(cycle)
|
31
|
+
)
|
32
|
+
end
|
33
|
+
if range.end > cycle_end
|
34
|
+
extract(
|
35
|
+
TimeRange.new(
|
36
|
+
(cycle_end + 1.hour).beginning_of_day, range.end, range.exclude_end?
|
37
|
+
),
|
38
|
+
next_cycle(cycle)
|
39
|
+
)
|
40
|
+
end
|
41
|
+
else
|
42
|
+
extract(range, next_cycle(cycle))
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def next_cycle(current_cycle)
|
47
|
+
case current_cycle
|
48
|
+
when :year then :month
|
49
|
+
when :month then :day
|
50
|
+
when :day then nil
|
51
|
+
else raise "Unknown cycle: #{current_cycle.inspect}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def extract_boundaries(range, cycle)
|
56
|
+
result = []
|
57
|
+
range.each(cycle) do |date|
|
58
|
+
if included_in_range?(range, date.send("beginning_of_#{cycle}")) &&
|
59
|
+
included_in_range?(range, date.send("end_of_#{cycle}"))
|
60
|
+
result << date
|
61
|
+
end
|
62
|
+
end
|
63
|
+
if result.empty?
|
64
|
+
return nil, nil
|
65
|
+
else
|
66
|
+
return result.first.send("beginning_of_#{cycle}"),
|
67
|
+
result.last.send("end_of_#{cycle}")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# TODO: Can be refactored to TimeRange#include?
|
72
|
+
# Refactoring requires investigation into TimeRange use in System.
|
73
|
+
def included_in_range?(range, value)
|
74
|
+
(range.begin <= value) &&
|
75
|
+
(value < range.end || (value.to_i == range.end.to_i && !range.exclude_end?))
|
76
|
+
end
|
77
|
+
|
78
|
+
# TODO: Can be refactored to TimeRange#empty?
|
79
|
+
# Refactoring requires investigation into TimeRange use in System.
|
80
|
+
def empty_range?(range)
|
81
|
+
(range.begin.to_i == range.end.to_i) && range.exclude_end?
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
require 'active_support/time'
|
2
|
+
require '3scale_time_range/version'
|
3
|
+
require '3scale_time_range/granulate'
|
4
|
+
|
5
|
+
class TimeRange < Range
|
6
|
+
|
7
|
+
def initialize(start_time, end_time, exclusive = false)
|
8
|
+
raise ArgumentError, 'start and end must act like Time' unless start_time.acts_like?(:time) && end_time.acts_like?(:time)
|
9
|
+
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
WEEKDAYS = {:monday => 0,
|
14
|
+
:tuesday => 1,
|
15
|
+
:wednesday => 2,
|
16
|
+
:thursday => 3,
|
17
|
+
:friday => 4,
|
18
|
+
:saturday => 5,
|
19
|
+
:sunday => 6}
|
20
|
+
|
21
|
+
def each(step = nil, &block)
|
22
|
+
if step
|
23
|
+
enumerator = if WEEKDAYS[step]
|
24
|
+
WeekdayEnumerator
|
25
|
+
elsif step == :end_of_month # the simple enumerator already works for the beginning, every month has a 1st
|
26
|
+
MonthEnumerator
|
27
|
+
else
|
28
|
+
SimpleEnumerator
|
29
|
+
end.new(self, step)
|
30
|
+
|
31
|
+
enumerator.each(&block) if block_given?
|
32
|
+
enumerator
|
33
|
+
else
|
34
|
+
super(&block)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Shifts the time range to given timezone via Time#in_time_zone method.
|
39
|
+
def in_time_zone(timezone)
|
40
|
+
TimeRange.new(self.begin.in_time_zone(timezone), self.end.in_time_zone(timezone))
|
41
|
+
end
|
42
|
+
|
43
|
+
def +(interval)
|
44
|
+
# TODO - check it
|
45
|
+
# raise ArgumentError.new('Only Fixnum is allowed') unless Fixnum === interval
|
46
|
+
TimeRange.new(self.begin + interval, self.end + interval)
|
47
|
+
end
|
48
|
+
|
49
|
+
def -(interval)
|
50
|
+
self + (-interval)
|
51
|
+
end
|
52
|
+
|
53
|
+
def utc
|
54
|
+
self.in_time_zone('UTC')
|
55
|
+
end
|
56
|
+
|
57
|
+
# TODO: TimeRange should check if both 'begin' and 'end' are in
|
58
|
+
# the same timezone?
|
59
|
+
def zone
|
60
|
+
assert_time_with_zone
|
61
|
+
self.begin.zone
|
62
|
+
end
|
63
|
+
|
64
|
+
def utc_offset
|
65
|
+
assert_time_with_zone
|
66
|
+
self.begin.utc_offset
|
67
|
+
end
|
68
|
+
|
69
|
+
def shift_in_hours
|
70
|
+
self.utc_offset / 3600
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
def assert_time_with_zone
|
75
|
+
raise TypeError.new('Has to be TimeWithZone') unless ActiveSupport::TimeWithZone === self.begin
|
76
|
+
end
|
77
|
+
|
78
|
+
private :assert_time_with_zone
|
79
|
+
|
80
|
+
def length
|
81
|
+
self.end - self.begin
|
82
|
+
end
|
83
|
+
|
84
|
+
# "round" range, so it starts at the beginning of the cycle and end at it's end.
|
85
|
+
# TODO: maybe round is not the best name for this method.
|
86
|
+
def round(cycle)
|
87
|
+
self.class.new(self.begin.beginning_of_cycle(cycle),
|
88
|
+
self.end.end_of_cycle(cycle))
|
89
|
+
end
|
90
|
+
|
91
|
+
# Previous time range, that is, a range with the same length as this range, but which
|
92
|
+
# ends when this range starts. Excludes the last element.
|
93
|
+
def previous
|
94
|
+
self.class.new(self.begin - length, self.begin, true)
|
95
|
+
end
|
96
|
+
|
97
|
+
def to_time_range
|
98
|
+
self
|
99
|
+
end
|
100
|
+
|
101
|
+
def to_s
|
102
|
+
"#{self.begin.strftime("%B %e, %Y (%k:%M)")} - #{self.end.strftime("%B %e, %Y (%k:%M)")}"
|
103
|
+
end
|
104
|
+
|
105
|
+
# Does this range cover a whole month?
|
106
|
+
def month?
|
107
|
+
self.begin.year == self.end.year &&
|
108
|
+
self.begin.month == self.end.month &&
|
109
|
+
self.begin == self.begin.beginning_of_month &&
|
110
|
+
self.end == self.end.end_of_month
|
111
|
+
end
|
112
|
+
|
113
|
+
def inspect
|
114
|
+
"#{self.class.name}(#{super})"
|
115
|
+
end
|
116
|
+
|
117
|
+
def granulate
|
118
|
+
@granulate ||= Granulate.new(self)
|
119
|
+
end
|
120
|
+
|
121
|
+
class SimpleEnumerator
|
122
|
+
include Enumerable
|
123
|
+
|
124
|
+
def initialize(range, step)
|
125
|
+
@range, @step = range, step.is_a?(Symbol) ? 1.send(step) : step
|
126
|
+
end
|
127
|
+
|
128
|
+
def each
|
129
|
+
current = @range.begin
|
130
|
+
last = @range.end
|
131
|
+
last -= @step if @range.exclude_end?
|
132
|
+
|
133
|
+
while current.to_i <= last.to_i
|
134
|
+
yield(current)
|
135
|
+
current += @step
|
136
|
+
end
|
137
|
+
|
138
|
+
self
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
class WeekdayEnumerator
|
143
|
+
include Enumerable
|
144
|
+
|
145
|
+
def initialize(range, weekday)
|
146
|
+
@range, @offset = range, WEEKDAYS[weekday].days
|
147
|
+
end
|
148
|
+
|
149
|
+
def each
|
150
|
+
current = @range.begin.beginning_of_week + @offset
|
151
|
+
current = current + 1.week if current < @range.begin
|
152
|
+
last = @range.end
|
153
|
+
|
154
|
+
while current <= last
|
155
|
+
yield(current)
|
156
|
+
current += 1.week
|
157
|
+
end
|
158
|
+
|
159
|
+
self
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
class MonthEnumerator
|
164
|
+
include Enumerable
|
165
|
+
|
166
|
+
def initialize(range, step)
|
167
|
+
@range = range
|
168
|
+
end
|
169
|
+
|
170
|
+
def each
|
171
|
+
current = @range.begin
|
172
|
+
last = @range.end
|
173
|
+
last -= 1.month if @range.exclude_end?
|
174
|
+
|
175
|
+
while current <= last
|
176
|
+
yield(current)
|
177
|
+
current = (current + 1.month).end_of_month
|
178
|
+
end
|
179
|
+
|
180
|
+
self
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
class Range
|
186
|
+
def to_time_range
|
187
|
+
TimeRange.new(self.begin, self.end, self.exclude_end?)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
@@ -0,0 +1,137 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require_relative '../lib/3scale_time_range'
|
3
|
+
|
4
|
+
class GranulateTest < Minitest::Test
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@gran = TimeRange.new(
|
8
|
+
DateTime.parse("2012-10-09 07:23"), DateTime.parse("2014-02-05 13:45")
|
9
|
+
).granulate
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_granulates_by_year
|
13
|
+
assert_equal @gran.years, [
|
14
|
+
TimeRange.new(
|
15
|
+
DateTime.parse("2013-01-01").beginning_of_year,
|
16
|
+
DateTime.parse("2013-12-31").end_of_year)
|
17
|
+
]
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_granulates_by_month
|
21
|
+
assert_equal @gran.months, [
|
22
|
+
TimeRange.new(
|
23
|
+
DateTime.parse("2012-11-01").beginning_of_month,
|
24
|
+
DateTime.parse("2012-12-31").end_of_month),
|
25
|
+
|
26
|
+
TimeRange.new(
|
27
|
+
DateTime.parse("2014-01-01").beginning_of_month,
|
28
|
+
DateTime.parse("2014-01-31").end_of_month)
|
29
|
+
]
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_granulates_by_day
|
33
|
+
assert_equal @gran.days, [
|
34
|
+
TimeRange.new(
|
35
|
+
DateTime.parse("2012-10-10").beginning_of_day,
|
36
|
+
DateTime.parse("2012-10-31").end_of_day),
|
37
|
+
|
38
|
+
TimeRange.new(
|
39
|
+
DateTime.parse("2014-02-01").beginning_of_day,
|
40
|
+
DateTime.parse("2014-02-04").end_of_day)
|
41
|
+
]
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_exposes_information_on_not_granulated_ranges
|
45
|
+
assert_equal @gran.rest, [
|
46
|
+
TimeRange.new(
|
47
|
+
DateTime.parse("2012-10-09 07:23"),
|
48
|
+
DateTime.parse("2012-10-09").end_of_day),
|
49
|
+
|
50
|
+
TimeRange.new(
|
51
|
+
DateTime.parse("2014-02-05").beginning_of_day,
|
52
|
+
DateTime.parse("2014-02-05 13:45"))
|
53
|
+
]
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_works_also_for_short_ranges
|
57
|
+
@gran = TimeRange.new(
|
58
|
+
DateTime.parse("2012-10-09 07:23"), DateTime.parse("2012-10-09 13:45")
|
59
|
+
).granulate
|
60
|
+
|
61
|
+
assert_equal @gran.rest, [
|
62
|
+
TimeRange.new(
|
63
|
+
DateTime.parse("2012-10-09 07:23"),
|
64
|
+
DateTime.parse("2012-10-09 13:45"))
|
65
|
+
]
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_granulation_is_cached_in_time_range
|
69
|
+
range = TimeRange.new(
|
70
|
+
DateTime.parse("2012-10-09 07:23"), DateTime.parse("2012-10-09 13:45")
|
71
|
+
)
|
72
|
+
|
73
|
+
assert_equal range.granulate.object_id, range.granulate.object_id
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_properly_parses_open_ended_ranges_1
|
77
|
+
gran = (DateTime.parse("2011-01-01")...DateTime.parse("2012-01-01")).
|
78
|
+
to_time_range.granulate
|
79
|
+
|
80
|
+
assert_equal 1, gran.years.size
|
81
|
+
assert_equal 0, gran.months.size
|
82
|
+
assert_equal 0, gran.days.size
|
83
|
+
assert_equal 0, gran.rest.size
|
84
|
+
|
85
|
+
refute gran.years.first.exclude_end?
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_properly_parses_open_ended_ranges_2
|
89
|
+
gran = (DateTime.parse("2011-01-01")...DateTime.parse("2011-12-31").end_of_year).
|
90
|
+
to_time_range.granulate
|
91
|
+
|
92
|
+
assert_equal 0, gran.years.size
|
93
|
+
assert_equal 1, gran.months.size
|
94
|
+
assert_equal 1, gran.days.size
|
95
|
+
assert_equal 1, gran.rest.size
|
96
|
+
|
97
|
+
refute gran.months.first.exclude_end?
|
98
|
+
refute gran.days.first.exclude_end?
|
99
|
+
assert gran.rest.first.exclude_end?
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_properly_parses_close_ended_ranges_1
|
103
|
+
gran = (DateTime.parse("2011-01-01")..DateTime.parse("2012-01-01")).
|
104
|
+
to_time_range.granulate
|
105
|
+
|
106
|
+
assert_equal 1, gran.years.size
|
107
|
+
assert_equal 0, gran.months.size
|
108
|
+
assert_equal 0, gran.days.size
|
109
|
+
assert_equal 1, gran.rest.size
|
110
|
+
|
111
|
+
refute gran.rest.first.exclude_end?
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_properly_parses_close_ended_ranges_2
|
115
|
+
gran = (DateTime.parse("2011-01-01")..DateTime.parse("2011-12-31").end_of_year).
|
116
|
+
to_time_range.granulate
|
117
|
+
|
118
|
+
assert_equal 1, gran.years.size
|
119
|
+
assert_equal 0, gran.months.size
|
120
|
+
assert_equal 0, gran.days.size
|
121
|
+
assert_equal 0, gran.rest.size
|
122
|
+
|
123
|
+
refute gran.years.first.exclude_end?
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_properly_handles_Time_UTC_ends_of_months
|
127
|
+
d = DateTime.parse('2010-01-01')
|
128
|
+
gran = (d.beginning_of_month.to_time.utc..d.end_of_month.to_time.utc).to_time_range.granulate
|
129
|
+
|
130
|
+
assert_equal 0, gran.years.size
|
131
|
+
assert_equal 1, gran.months.size
|
132
|
+
assert_equal 0, gran.days.size
|
133
|
+
assert_equal 0, gran.rest.size
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require_relative '../lib/3scale_time_range'
|
3
|
+
|
4
|
+
class TimeRangeTest < Minitest::Test
|
5
|
+
|
6
|
+
def test_is_creatable_only_from_Times
|
7
|
+
assert_raises(ArgumentError) { TimeRange.new(:foo, :bar) }
|
8
|
+
assert_raises(ArgumentError) { TimeRange.new(400, 700) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_converts_from_Range_only_if_extremes_are_Time_instances
|
12
|
+
assert_raises(ArgumentError) { (100..300).to_time_range }
|
13
|
+
assert_instance_of TimeRange, (Time.local(2009, 3, 2)..Time.local(2009, 3, 11)).to_time_range
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_enumerates_with_symbol
|
17
|
+
stuff = []
|
18
|
+
period = (Time.local(2009, 3, 2)..Time.local(2009, 3, 4)).to_time_range
|
19
|
+
|
20
|
+
period.each(:day) { |time| stuff << time }
|
21
|
+
|
22
|
+
assert_equal [Time.local(2009, 3, 2), Time.local(2009, 3, 3), Time.local(2009, 3, 4)],
|
23
|
+
stuff
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_enumerates_with_custom_step
|
27
|
+
stuff = []
|
28
|
+
period = (Time.local(2009, 3, 2)..Time.local(2009, 3, 3)).to_time_range
|
29
|
+
|
30
|
+
period.each(12.hours) { |time| stuff << time }
|
31
|
+
|
32
|
+
assert_equal [Time.local(2009, 3, 2, 00, 00), Time.local(2009, 3, 2, 12, 00), Time.local(2009, 3, 3, 00, 00)], stuff
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_returns_enumerator_on_each
|
36
|
+
period = (Time.local(2009, 3, 2)..Time.local(2009, 3, 3)).to_time_range
|
37
|
+
days = period.each(:day).map { |time| time }
|
38
|
+
|
39
|
+
assert_equal [Time.local(2009, 3, 2), Time.local(2009, 3, 3)], days
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_excludes_end_if_exclusive
|
43
|
+
period = TimeRange.new(Time.local(2009, 3, 2), Time.local(2009, 3, 4), true)
|
44
|
+
days = period.each(:day).map { |time| time }
|
45
|
+
|
46
|
+
assert_equal [Time.local(2009, 3, 2), Time.local(2009, 3, 3)], days
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_enumerates_with_weekday
|
50
|
+
period = (Time.local(2009, 11, 1)..Time.local(2009, 11, 30)).to_time_range
|
51
|
+
|
52
|
+
assert_equal [Time.local(2009, 11, 2),
|
53
|
+
Time.local(2009, 11, 9),
|
54
|
+
Time.local(2009, 11, 16),
|
55
|
+
Time.local(2009, 11, 23),
|
56
|
+
Time.local(2009, 11, 30)], period.each(:monday).to_a
|
57
|
+
|
58
|
+
assert_equal [Time.local(2009, 11, 3),
|
59
|
+
Time.local(2009, 11, 10),
|
60
|
+
Time.local(2009, 11, 17),
|
61
|
+
Time.local(2009, 11, 24)], period.each(:tuesday).to_a
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_previous
|
65
|
+
range = TimeRange.new(Time.local(2009, 3, 2, 11, 33), Time.local(2009, 3, 2, 12, 33))
|
66
|
+
expected = TimeRange.new(Time.local(2009, 3, 2, 10, 33), Time.local(2009, 3, 2, 11, 33), true)
|
67
|
+
|
68
|
+
assert_equal expected, range.previous
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_add_and_or_substract_interval
|
72
|
+
range = TimeRange.new(Time.local(2009, 3, 2, 11, 00), Time.local(2009, 3, 2, 12, 00))
|
73
|
+
expected = TimeRange.new(Time.local(2009, 3, 2, 13, 00), Time.local(2009, 3, 2, 14, 00))
|
74
|
+
assert_equal expected, range + 2.hours
|
75
|
+
assert_equal range, (range + 2.hours) - 2.hours
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_month_returns_true_if_the_range_is_whole_month
|
79
|
+
time = Time.local(2010, 11, 1)
|
80
|
+
range = TimeRange.new(time.beginning_of_month, time.end_of_month)
|
81
|
+
|
82
|
+
assert range.month?
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_month_return_fals_if_the_range_is_not_whole_month
|
86
|
+
range = TimeRange.new(Time.local(2010, 11, 6), Time.local(2010, 11, 17))
|
87
|
+
|
88
|
+
assert !range.month?
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_month_returns_false_if_the_begin_and_end_are_in_different_months
|
92
|
+
range = TimeRange.new(Time.local(2010, 10, 1), Time.local(2010, 11, 30).end_of_month)
|
93
|
+
|
94
|
+
assert !range.month?
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_month_returns_false_if_the_begin_and_end_are_in_the_same_month_but_different_years
|
98
|
+
range = TimeRange.new(Time.local(2010, 11, 1), Time.local(2011, 11, 30).end_of_month)
|
99
|
+
|
100
|
+
assert !range.month?
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: 3scale_time_range
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wojciech Ogrodowczyk
|
8
|
+
- Alejandro Martinez Ruiz
|
9
|
+
- Michal Macejko
|
10
|
+
- Michal Cichra
|
11
|
+
- David Ortiz Lopez
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
date: 2017-10-09 00:00:00.000000000 Z
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
18
|
+
name: activesupport
|
19
|
+
requirement: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 3.2.19
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 3.2.19
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: bundler
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.6'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '1.6'
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: rake
|
47
|
+
requirement: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - "~>"
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '10.0'
|
52
|
+
type: :development
|
53
|
+
prerelease: false
|
54
|
+
version_requirements: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - "~>"
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '10.0'
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: minitest
|
61
|
+
requirement: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - "~>"
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 5.4.0
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - "~>"
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 5.4.0
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: geminabox
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - "~>"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 0.12.4
|
80
|
+
type: :development
|
81
|
+
prerelease: false
|
82
|
+
version_requirements: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - "~>"
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 0.12.4
|
87
|
+
description: Utility class for ranges of times (time periods). It's like Range, but
|
88
|
+
has additional enumeration capabilities.
|
89
|
+
email:
|
90
|
+
- wojciech@haikuco.de
|
91
|
+
- amr@redhat.com
|
92
|
+
- mmacejko@redhat.com
|
93
|
+
- mcichra@redhat.com
|
94
|
+
- dortiz@redhat.com
|
95
|
+
executables: []
|
96
|
+
extensions: []
|
97
|
+
extra_rdoc_files: []
|
98
|
+
files:
|
99
|
+
- ".gitignore"
|
100
|
+
- 3scale_time_range.gemspec
|
101
|
+
- Gemfile
|
102
|
+
- LICENSE
|
103
|
+
- NOTICE
|
104
|
+
- README.md
|
105
|
+
- Rakefile
|
106
|
+
- lib/3scale_time_range.rb
|
107
|
+
- lib/3scale_time_range/granulate.rb
|
108
|
+
- lib/3scale_time_range/version.rb
|
109
|
+
- test/granulate_test.rb
|
110
|
+
- test/time_range_test.rb
|
111
|
+
homepage: https://github.com/3scale/3scale_time_range
|
112
|
+
licenses:
|
113
|
+
- Apache-2.0
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 2.2.5
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: Utility class for ranges of times (time periods).
|
135
|
+
test_files:
|
136
|
+
- test/granulate_test.rb
|
137
|
+
- test/time_range_test.rb
|
138
|
+
has_rdoc:
|