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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8cebcccddb6b2339e8e86a5bbfd742daf3d693d9
4
- data.tar.gz: 2292c5e560b68117b99730a480c09b82675a4ef5
3
+ metadata.gz: a06b8ee61c99dec515cff3b45778aed0ff303883
4
+ data.tar.gz: 3fa56d4beafb1ab48cde76da66f122f722875139
5
5
  SHA512:
6
- metadata.gz: eab154fb90d0fe3e08f256d3fdbd4b8ed90c9dc760dfbf6e8abead7e80d84af9b7d23b484710b7f7fce6ee1e6aad2c05a9468c865d255ad13f01159c947354d7
7
- data.tar.gz: ea1c9bfcac179dc9021a33d7780ec7c1e59281ee60746536d177f7db6b981f50fb4df6ac9ea8d2fd843f182bc73b3fd5712a59ef31cbb9979ac4cf6af2f38734
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
@@ -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
- e = e.in_time_zone(time_zone)
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 = Date.today
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 = Date.yesterday
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)
@@ -1,3 +1,3 @@
1
1
  class TimeRange < Range
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -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.3
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-06-23 00:00:00.000000000 Z
11
+ date: 2014-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tzinfo