timerange 0.0.2 → 0.0.3

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: 17b671b71b8905bfdd65ac8e0c3f620eb278f51f
4
- data.tar.gz: 132d36c026b2da0f57cf3817228647eb74f210b9
3
+ metadata.gz: 8cebcccddb6b2339e8e86a5bbfd742daf3d693d9
4
+ data.tar.gz: 2292c5e560b68117b99730a480c09b82675a4ef5
5
5
  SHA512:
6
- metadata.gz: eb12938aa430be463edff9b7b763406c464677cf3309f2d6b32f0444c75ae04017903fa78275cec1992d99ca671d73ce0553141ffefdea7fae8447a528fc4ac5
7
- data.tar.gz: fc04948e87a7d35967f8c0b2cd3aa24e546b5818b163903bfcc5ad6aad0578c327efaaf51b4605238e1eaebfd7fdb4bcd83a034e5bc1555895e00778f03d058d
6
+ metadata.gz: eab154fb90d0fe3e08f256d3fdbd4b8ed90c9dc760dfbf6e8abead7e80d84af9b7d23b484710b7f7fce6ee1e6aad2c05a9468c865d255ad13f01159c947354d7
7
+ data.tar.gz: ea1c9bfcac179dc9021a33d7780ec7c1e59281ee60746536d177f7db6b981f50fb4df6ac9ea8d2fd843f182bc73b3fd5712a59ef31cbb9979ac4cf6af2f38734
@@ -0,0 +1,3 @@
1
+ class TimeRange < Range
2
+ VERSION = "0.0.3"
3
+ end
data/lib/timerange.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  require "time"
2
2
  require "active_support/time"
3
- require "active_support/core_ext/module/attribute_accessors"
3
+ require "timerange/version"
4
4
 
5
5
  class TimeRange < Range
6
- VERSION = "0.0.2"
7
-
8
- mattr_accessor :time_zone
6
+ class << self
7
+ attr_accessor :time_zone
8
+ end
9
9
 
10
10
  def initialize(b = nil, e = Time.now, exclude_end = false, options = {})
11
11
  if b.is_a?(Range)
@@ -15,26 +15,27 @@ class TimeRange < Range
15
15
  if b.is_a?(Hash)
16
16
  options, b, e, exclude_end = b, nil, nil, false
17
17
  elsif e.is_a?(Hash)
18
- options, e, exclude_end = e, nil, false
18
+ options, e, exclude_end = e, Time.now, false
19
19
  end
20
20
 
21
- time_zone = options[:time_zone] || self.class.time_zone || Time.zone || "Etc/UTC"
21
+ time_zone = options[:time_zone] || TimeRange.time_zone || Time.zone || "Etc/UTC"
22
22
  if time_zone.is_a?(ActiveSupport::TimeZone) or (time_zone = ActiveSupport::TimeZone[time_zone])
23
23
  # do nothing
24
24
  else
25
25
  raise "Unrecognized time zone"
26
26
  end
27
+
27
28
  b = time_zone.parse(b) if b.is_a?(String)
28
29
  e = time_zone.parse(e) if e.is_a?(String)
29
- if options[:time_zone]
30
- b = b.in_time_zone(b)
31
- e = e.in_time_zone(e)
32
- end
30
+ b = b.in_time_zone(time_zone)
33
31
 
34
32
  if options[:duration]
35
33
  e = b + options[:duration]
36
34
  exclude_end = true
37
35
  end
36
+ e = e.in_time_zone(time_zone)
37
+
38
+ @options = options.merge(time_zone: time_zone)
38
39
 
39
40
  super(b, e, exclude_end)
40
41
  end
@@ -59,21 +60,21 @@ class TimeRange < Range
59
60
  else
60
61
  bucket(period, self.end + 1.send(period), options)
61
62
  end
62
- self.class.new(bucket(period, self.begin, options), e, true)
63
+ self.class.new(bucket(period, self.begin, options), e, true, @options.merge(options))
63
64
  end
64
65
 
65
66
  def expand_start(period, options = {})
66
67
  e = self.end
67
68
  e = e.in_time_zone(options[:time_zone]) if options[:time_zone]
68
- self.class.new(bucket(period, self.begin, options), e, exclude_end?)
69
+ self.class.new(bucket(period, self.begin, options), e, exclude_end?, @options.merge(options))
69
70
  end
70
71
 
71
72
  def bucket(period, time, options = {})
72
- self.class.bucket(period, time, options)
73
+ self.class.bucket(period, time, @options.merge(options))
73
74
  end
74
75
 
75
76
  def self.bucket(period, time, options = {})
76
- time_zone = options[:time_zone] || Time.zone
77
+ time_zone = options[:time_zone] || TimeRange.time_zone || Time.zone || "Etc/UTC"
77
78
  day_start = options[:day_start] || 0
78
79
  week_start = options[:week_start] || 6
79
80
 
@@ -84,9 +85,8 @@ class TimeRange < Range
84
85
 
85
86
  time = time.to_time.in_time_zone(time_zone) - day_start.hours
86
87
 
87
- period = period.to_sym
88
88
  time =
89
- case period
89
+ case period.to_sym
90
90
  when :second
91
91
  time.change(usec: 0)
92
92
  when :minute
@@ -7,6 +7,12 @@ 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_time_zone
11
+ day = Time.parse("2014-06-01")
12
+ time_zone = "Eastern Time (US & Canada)"
13
+ assert_equal time_zone, TimeRange.new(day, time_zone: time_zone).expand(:week).first.time_zone.name
14
+ end
15
+
10
16
  def test_today
11
17
  day = Time.now.midnight
12
18
  tr = TimeRange.today
data/timerange.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'timerange'
4
+ require 'timerange/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "timerange"
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "tzinfo"
21
22
  spec.add_dependency "activesupport"
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.6"
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timerange
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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-22 00:00:00.000000000 Z
11
+ date: 2014-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tzinfo
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: activesupport
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -93,6 +107,7 @@ files:
93
107
  - README.md
94
108
  - Rakefile
95
109
  - lib/timerange.rb
110
+ - lib/timerange/version.rb
96
111
  - test/test_helper.rb
97
112
  - test/time_range_test.rb
98
113
  - timerange.gemspec