timespan 0.1.3 → 0.1.4

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.
data/Gemfile CHANGED
@@ -1,6 +1,7 @@
1
1
  source :rubygems
2
2
 
3
3
  gem 'chronic'
4
+ gem 'spanner'
4
5
  gem 'ruby-duration'
5
6
 
6
7
  group :test, :development do
data/Gemfile.lock CHANGED
@@ -45,6 +45,7 @@ GEM
45
45
  multi_json (~> 1.3)
46
46
  simplecov-html (~> 0.5.3)
47
47
  simplecov-html (0.5.3)
48
+ spanner (0.0.2)
48
49
  tzinfo (0.3.33)
49
50
 
50
51
  PLATFORMS
@@ -58,3 +59,4 @@ DEPENDENCIES
58
59
  rspec (>= 2.8.0)
59
60
  ruby-duration
60
61
  simplecov (>= 0.5)
62
+ spanner
data/README.md CHANGED
@@ -1,22 +1,30 @@
1
- = Timespan
1
+ # Timespan
2
2
 
3
3
  Use TimeSpans in Ruby :)
4
4
 
5
5
  Will calculate time diff in milliseconds between to dates, then allow you to get the time difference in some time unit as a number.
6
6
 
7
7
  ```ruby
8
- t = TimeSpan.new(Date.today, 3.days.ago)
8
+ t = TimeSpan.new(:start => Date.today, :duration => 3.days.ago)
9
9
  t.to_days # => 3
10
10
  t.to_weeks # => 0
11
11
  t.to_secs # => 259200
12
12
  t.to_hours = 10800
13
+
14
+ t = TimeSpan.new(:from => Date.today, :to => "6 weeks from now")
15
+
16
+ t = TimeSpan.new(:from => Date.today, :duration => "7 weeks 3 days")
17
+ t = TimeSpan.new(:from => 2.days.ago, :duration => "5 months and 2 weeks")
13
18
  ``
14
19
 
15
- == TODO
20
+ See specs for more examples of usage
21
+
22
+ ## TODO
16
23
 
17
- Should use Duration for duration calculations!
24
+ * use Duration for duration calculations!
25
+ * Calculate start_time and end_time based on duration if not set explicitly!
18
26
 
19
- == Contributing to timespan
27
+ ## Contributing to timespan
20
28
 
21
29
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
22
30
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
data/lib/timespan.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'duration'
2
+ require 'spanner'
2
3
 
3
4
  class TimeSpan
5
+ class ParseError < StandardError; end
6
+
4
7
  attr_reader :start_time, :end_time, :seconds
5
8
 
6
9
  alias_method :start_date, :start_time
@@ -36,8 +39,20 @@ class TimeSpan
36
39
  end
37
40
 
38
41
  def duration= duration
39
- raise ArgumentError, "the duration option must be set to a Duration instance" unless duration.kind_of? Duration
40
- @duration = duration
42
+ @duration = case duration
43
+ when Duration
44
+ duration
45
+ when Integer, Hash
46
+ Duration.new duration
47
+ when String
48
+ begin
49
+ Duration.new Spanner.parse(duration.gsub /and/, '')
50
+ rescue Exception => e
51
+ raise ParseError, "Internal error: Spanner couldn't parse String '#{duration}'"
52
+ end
53
+ else
54
+ raise ArgumentError, "the duration option must be set to any of: #{valid_duration_types}"
55
+ end
41
56
  refresh! unless is_new?
42
57
  end
43
58
 
@@ -80,39 +95,57 @@ class TimeSpan
80
95
  def to_milliseconds
81
96
  @to_seconds ||= (seconds * 1000.0).round
82
97
  end
83
- alias_method :to_mils, :to_milliseconds
84
- alias_method :millis, :to_mils
98
+ alias_method :to_mils, :to_milliseconds
99
+ alias_method :millis, :to_mils
100
+ alias_method :milliseconds, :to_mils
85
101
 
86
102
  def to_minutes
87
103
  @to_minutes ||= (to_seconds / 60.0).round
88
104
  end
89
105
  alias_method :to_m, :to_minutes
90
106
  alias_method :to_mins, :to_minutes
107
+ alias_method :minutes, :to_minutes
91
108
 
92
109
  def to_hours
93
110
  @to_hours ||= (to_minutes / 60.0).round
94
111
  end
95
- alias_method :to_h, :to_hours
112
+ alias_method :to_h, :to_hours
113
+ alias_method :hrs, :to_hours
114
+ alias_method :hours, :to_hours
96
115
 
97
116
  def to_days
98
- @to_days ||= (to_hours / 24).round
117
+ @to_days ||= (to_hours / 24.0).round
99
118
  end
100
119
  alias_method :to_d, :to_days
120
+ alias_method :days, :to_days
101
121
 
102
122
  def to_weeks
103
- @to_weeks ||= (to_days / 7).round
123
+ @to_weeks ||= (to_days / 7.0).round
104
124
  end
105
- alias_method :to_w, :to_weeks
125
+ alias_method :to_w, :to_weeks
126
+ alias_method :weeks, :to_days
106
127
 
107
128
  def to_months
108
- @to_months ||= (to_days / 30).round
129
+ @to_months ||= (to_days / 30.0).round
109
130
  end
110
131
  alias_method :to_mon, :to_months
132
+ alias_method :months, :to_months
111
133
 
112
134
  def to_years
113
135
  @to_years ||= (to_days.to_f / 365.25).round
114
136
  end
115
- alias_method :to_y, :to_years
137
+ alias_method :to_y, :to_years
138
+ alias_method :years, :to_years
139
+
140
+ def to_decades
141
+ @to_decades ||= (to_years / 10.0).round
142
+ end
143
+ alias_method :decades, :to_decades
144
+
145
+ def to_centuries
146
+ @to_centuries ||= (to_decades / 10.0).round
147
+ end
148
+ alias_method :centuries, :to_centuries
116
149
 
117
150
  def units
118
151
  %w{seconds minutes hours days weeks months years}
@@ -120,13 +153,14 @@ class TimeSpan
120
153
 
121
154
  protected
122
155
 
123
- def set_with_options options = {}
156
+ def set_with_options options = {}
124
157
  case options
125
158
  when Hash
126
159
  duration = options[:duration]
127
- @start_time = options[:from]
128
- @end_time = options[:to]
129
- when Integer
160
+ @start_time = options[:from] || options[:start]
161
+ @end_time = options[:to] || options[:end]
162
+ when Integer, String
163
+ duration = options
130
164
  else
131
165
  raise ArgumentError, "Timespan must take Hash or Integer, was: #{options}"
132
166
  end
@@ -169,6 +203,10 @@ class TimeSpan
169
203
  set_seconds
170
204
  end
171
205
 
206
+ def valid_duration_types
207
+ [Duration, String, Integer, Hash]
208
+ end
209
+
172
210
  def valid_compare? time
173
211
  valid_compare_types.any? {|type| time.kind_of? type }
174
212
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,3 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
3
1
  require 'rspec'
4
2
  require 'timespan'
5
3
 
@@ -30,6 +30,23 @@ describe "Timespan" do
30
30
  end
31
31
  end
32
32
 
33
+ describe 'set duration with String' do
34
+ let(:timespan) { TimeSpan.new :from => from, :duration => "3 days" }
35
+
36
+ it 'should be 3 days' do
37
+ timespan.to_d.should == 3
38
+ end
39
+ end
40
+
41
+ describe 'set duration with Spanner String including and' do
42
+ let(:timespan) { TimeSpan.new :from => from, :duration => "3 days and 2 hours" }
43
+
44
+ it 'should be 3 days and 2 hrs' do
45
+ timespan.to_h.should == (24 * 3) + 2
46
+ end
47
+ end
48
+
49
+
33
50
  describe 'set start_time to new' do
34
51
  let(:timespan) { TimeSpan.new :from => from, :to => to }
35
52
 
data/timespan.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "timespan"
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = "2012-04-26"
12
+ s.date = "2012-04-30"
13
13
  s.description = "Easy to calculate time distance in different units"
14
14
  s.email = "kmandrup@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -33,7 +33,7 @@ Gem::Specification.new do |s|
33
33
  s.homepage = "http://github.com/kristianmandrup/timespan"
34
34
  s.licenses = ["MIT"]
35
35
  s.require_paths = ["lib"]
36
- s.rubygems_version = "1.8.22"
36
+ s.rubygems_version = "1.8.24"
37
37
  s.summary = "Use TimeSpans in ruby"
38
38
 
39
39
  if s.respond_to? :specification_version then
@@ -41,6 +41,7 @@ Gem::Specification.new do |s|
41
41
 
42
42
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
43
  s.add_runtime_dependency(%q<chronic>, [">= 0"])
44
+ s.add_runtime_dependency(%q<spanner>, [">= 0"])
44
45
  s.add_runtime_dependency(%q<ruby-duration>, [">= 0"])
45
46
  s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
46
47
  s.add_development_dependency(%q<rdoc>, [">= 3.12"])
@@ -49,6 +50,7 @@ Gem::Specification.new do |s|
49
50
  s.add_development_dependency(%q<simplecov>, [">= 0.5"])
50
51
  else
51
52
  s.add_dependency(%q<chronic>, [">= 0"])
53
+ s.add_dependency(%q<spanner>, [">= 0"])
52
54
  s.add_dependency(%q<ruby-duration>, [">= 0"])
53
55
  s.add_dependency(%q<rspec>, [">= 2.8.0"])
54
56
  s.add_dependency(%q<rdoc>, [">= 3.12"])
@@ -58,6 +60,7 @@ Gem::Specification.new do |s|
58
60
  end
59
61
  else
60
62
  s.add_dependency(%q<chronic>, [">= 0"])
63
+ s.add_dependency(%q<spanner>, [">= 0"])
61
64
  s.add_dependency(%q<ruby-duration>, [">= 0"])
62
65
  s.add_dependency(%q<rspec>, [">= 2.8.0"])
63
66
  s.add_dependency(%q<rdoc>, [">= 3.12"])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timespan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-26 00:00:00.000000000 Z
12
+ date: 2012-04-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chronic
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: spanner
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: ruby-duration
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -158,7 +174,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
158
174
  version: '0'
159
175
  segments:
160
176
  - 0
161
- hash: 2204981981511614855
177
+ hash: 4198297860052493168
162
178
  required_rubygems_version: !ruby/object:Gem::Requirement
163
179
  none: false
164
180
  requirements:
@@ -167,7 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
183
  version: '0'
168
184
  requirements: []
169
185
  rubyforge_project:
170
- rubygems_version: 1.8.22
186
+ rubygems_version: 1.8.24
171
187
  signing_key:
172
188
  specification_version: 3
173
189
  summary: Use TimeSpans in ruby