timerange 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/lib/timerange.rb +17 -8
- data/lib/timerange/version.rb +1 -1
- data/test/time_range_test.rb +18 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a06b8ee61c99dec515cff3b45778aed0ff303883
|
4
|
+
data.tar.gz: 3fa56d4beafb1ab48cde76da66f122f722875139
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84e5b5b48c4d20302733ca4b93b5020eaac047ff1ea6556b051ccee821bba459c390372fb5201e89b378ab450035c522713bb61c40496644257dfef8b4c93fce
|
7
|
+
data.tar.gz: de978230ff3be0a8849f2b5e1e30e10ca9a1508812688aa5dff014be938d6669f6bb7a47885604dd148169761644b6baa6c8abdfdaeff2508712ab88f6696aa5
|
data/README.md
CHANGED
@@ -19,6 +19,7 @@ time_range.expand(:week).step(1.day)
|
|
19
19
|
|
20
20
|
TimeRange.new("2014-06-01", "2014-06-07")
|
21
21
|
TimeRange.new("2014-06-01", duration: 1.week)
|
22
|
+
TimeRange.new("2014-06-01", within: 5.minutes)
|
22
23
|
TimeRange.new(4.weeks.ago).expand_start(:week) # last 4 weeks
|
23
24
|
|
24
25
|
TimeRange.today
|
data/lib/timerange.rb
CHANGED
@@ -8,6 +8,10 @@ class TimeRange < Range
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def initialize(b = nil, e = Time.now, exclude_end = false, options = {})
|
11
|
+
if !b
|
12
|
+
raise TypeError, "no implicit conversion of nil into Time"
|
13
|
+
end
|
14
|
+
|
11
15
|
if b.is_a?(Range)
|
12
16
|
b, e, exclude_end = b.begin, b.end, b.exclude_end?
|
13
17
|
end
|
@@ -18,7 +22,7 @@ class TimeRange < Range
|
|
18
22
|
options, e, exclude_end = e, Time.now, false
|
19
23
|
end
|
20
24
|
|
21
|
-
time_zone = options[:time_zone] || TimeRange.time_zone || Time.zone || "Etc/UTC"
|
25
|
+
time_zone = options[:time_zone] || (b.respond_to?(:time_zone) && b.time_zone) || TimeRange.time_zone || Time.zone || "Etc/UTC"
|
22
26
|
if time_zone.is_a?(ActiveSupport::TimeZone) or (time_zone = ActiveSupport::TimeZone[time_zone])
|
23
27
|
# do nothing
|
24
28
|
else
|
@@ -28,12 +32,17 @@ class TimeRange < Range
|
|
28
32
|
b = time_zone.parse(b) if b.is_a?(String)
|
29
33
|
e = time_zone.parse(e) if e.is_a?(String)
|
30
34
|
b = b.in_time_zone(time_zone)
|
35
|
+
e = e.in_time_zone(time_zone)
|
31
36
|
|
32
37
|
if options[:duration]
|
33
38
|
e = b + options[:duration]
|
34
39
|
exclude_end = true
|
35
40
|
end
|
36
|
-
|
41
|
+
if options[:within]
|
42
|
+
e = b + options[:within]
|
43
|
+
b -= options[:within]
|
44
|
+
exclude_end = false
|
45
|
+
end
|
37
46
|
|
38
47
|
@options = options.merge(time_zone: time_zone)
|
39
48
|
|
@@ -110,14 +119,14 @@ class TimeRange < Range
|
|
110
119
|
time + day_start.hours
|
111
120
|
end
|
112
121
|
|
113
|
-
def self.today
|
114
|
-
date =
|
115
|
-
new(date, date).expand(:day)
|
122
|
+
def self.today(options = {})
|
123
|
+
date = Time.now
|
124
|
+
new(date, date, options).expand(:day)
|
116
125
|
end
|
117
126
|
|
118
|
-
def self.yesterday
|
119
|
-
date =
|
120
|
-
new(date, date).expand(:day)
|
127
|
+
def self.yesterday(options = {})
|
128
|
+
date = Time.now - 1.day
|
129
|
+
new(date, date, options).expand(:day)
|
121
130
|
end
|
122
131
|
|
123
132
|
def +(period)
|
data/lib/timerange/version.rb
CHANGED
data/test/time_range_test.rb
CHANGED
@@ -7,12 +7,22 @@ class TestTimeRange < Minitest::Test
|
|
7
7
|
assert_equal 7, TimeRange.new(day..day).expand(:week).step(:day).size
|
8
8
|
end
|
9
9
|
|
10
|
+
def test_nil_range
|
11
|
+
assert_raises(TypeError){ TimeRange.new(nil) }
|
12
|
+
end
|
13
|
+
|
10
14
|
def test_time_zone
|
11
15
|
day = Time.parse("2014-06-01")
|
12
16
|
time_zone = "Eastern Time (US & Canada)"
|
13
17
|
assert_equal time_zone, TimeRange.new(day, time_zone: time_zone).expand(:week).first.time_zone.name
|
14
18
|
end
|
15
19
|
|
20
|
+
def test_default_time_zone
|
21
|
+
time_zone = "Central Time (US & Canada)"
|
22
|
+
day = ActiveSupport::TimeZone[time_zone].parse("2014-06-01")
|
23
|
+
assert_equal time_zone, TimeRange.new(day).expand(:week).first.time_zone.name
|
24
|
+
end
|
25
|
+
|
16
26
|
def test_today
|
17
27
|
day = Time.now.midnight
|
18
28
|
tr = TimeRange.today
|
@@ -49,4 +59,12 @@ class TestTimeRange < Minitest::Test
|
|
49
59
|
assert_equal TimeRange.yesterday, TimeRange.today - 1.day
|
50
60
|
end
|
51
61
|
|
62
|
+
def test_within
|
63
|
+
tr = TimeRange.new("2014-06-01", within: 5.minutes)
|
64
|
+
day = Time.zone.parse("2014-06-01")
|
65
|
+
assert_equal day - 5.minutes, tr.begin
|
66
|
+
assert_equal day + 5.minutes, tr.end
|
67
|
+
assert !tr.exclude_end?
|
68
|
+
end
|
69
|
+
|
52
70
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timerange
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tzinfo
|