timespan 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -1
- data/Gemfile.lock +76 -12
- data/README.md +119 -12
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/config/locales/timespan/da.yml +5 -0
- data/lib/timespan.rb +160 -166
- data/lib/timespan/compare.rb +59 -0
- data/lib/timespan/mongoid.rb +31 -0
- data/lib/timespan/printer.rb +71 -0
- data/lib/timespan/rails/engine.rb +9 -0
- data/lib/timespan/span.rb +43 -0
- data/lib/timespan/units.rb +92 -0
- data/spec/timespan/compare_spec.rb +50 -0
- data/spec/timespan/locales/duration_da.yml +20 -0
- data/spec/timespan/printer_spec.rb +24 -0
- data/spec/timespan/span_spec.rb +67 -0
- data/spec/timespan/units_spec.rb +54 -0
- data/spec/timespan_spec.rb +81 -46
- data/timespan.gemspec +22 -4
- metadata +49 -5
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Timespan::Units do
|
4
|
+
subject { timespan }
|
5
|
+
|
6
|
+
context 'From and To with 1 day apart' do
|
7
|
+
let(:timespan) { Timespan.new :from => from, :to => to}
|
8
|
+
|
9
|
+
let(:from) { Chronic.parse("1 day ago") }
|
10
|
+
let(:to) { Time.now }
|
11
|
+
|
12
|
+
describe '.weeks' do
|
13
|
+
it "spans 0 weeks" do
|
14
|
+
timespan.to_w.should == 0
|
15
|
+
end
|
16
|
+
its(:weeks) { should == 0 }
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '.days' do
|
20
|
+
it "spans 1 day" do
|
21
|
+
timespan.to_d.should == 1
|
22
|
+
end
|
23
|
+
its(:days) { seconds.should == 1 }
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '.hours' do
|
27
|
+
it "spans 24 hours" do
|
28
|
+
timespan.to_hrs.should == 24
|
29
|
+
end
|
30
|
+
its(:hours) { should == 24 }
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '.minutes' do
|
34
|
+
it "spans 60*24 minutes" do
|
35
|
+
timespan.to_hrs.should == 24
|
36
|
+
end
|
37
|
+
its(:minutes) { should == 24*60 }
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '.seconds' do
|
41
|
+
it "spans 86400 sec" do
|
42
|
+
timespan.seconds.should == 86400
|
43
|
+
end
|
44
|
+
its(:secs) { should == 86400 }
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '.milliseconds' do
|
48
|
+
it "spans 86400 sec" do
|
49
|
+
timespan.milliseconds.should be_within(10).of(86400000)
|
50
|
+
end
|
51
|
+
its(:millis) { should be_within(10).of(86400000) }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/spec/timespan_spec.rb
CHANGED
@@ -1,63 +1,98 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'chronic'
|
3
2
|
|
4
|
-
describe
|
3
|
+
describe Timespan do
|
5
4
|
subject { timespan }
|
6
5
|
|
7
|
-
|
6
|
+
let(:from) { Chronic.parse("1 day ago") }
|
7
|
+
let(:to) { Time.now }
|
8
8
|
|
9
|
-
|
10
|
-
|
9
|
+
context '2 days duration (from now - default)' do
|
10
|
+
let(:timespan) { Timespan.new :duration => "2 days"}
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
describe '.start_date' do
|
13
|
+
it 'should default to today' do
|
14
|
+
DateTime.parse(subject.start_date.to_s).strftime('%d %b %Y').should == Date.today.strftime('%d %b %Y')
|
15
|
+
end
|
16
|
+
end
|
14
17
|
end
|
15
18
|
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
+
context '3 hrs duration from now 2 days from now' do
|
20
|
+
let(:timespan) { Timespan.new("3 hrs").from(2.days.from_now) }
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
+
describe '.start_date' do
|
23
|
+
its(:start_date) { should be }
|
24
|
+
its(:end_date) { should be }
|
25
|
+
its(:duration) { should be_a Duration }
|
26
|
+
end
|
22
27
|
end
|
23
28
|
|
24
|
-
describe 'set with duration' do
|
25
|
-
let(:duration) { Duration.new(:days => 3) }
|
26
|
-
let(:timespan) { TimeSpan.new :from => from, :duration => duration }
|
27
|
-
|
28
|
-
it 'should be 3 days' do
|
29
|
-
timespan.to_d.should == 3
|
30
|
-
end
|
31
|
-
end
|
32
29
|
|
33
|
-
|
34
|
-
|
30
|
+
context 'From and To with 1 day apart' do
|
31
|
+
let(:timespan) { Timespan.new :from => from, :to => to}
|
35
32
|
|
36
|
-
|
37
|
-
|
38
|
-
|
33
|
+
describe '.convert_to_time' do
|
34
|
+
specify { subject.convert_to_time("1 day ago").to_s.should == 1.day.ago.to_s }
|
35
|
+
end
|
39
36
|
end
|
40
37
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
38
|
+
context 'From and 3 days' do
|
39
|
+
describe 'set with duration' do
|
40
|
+
let(:duration) { Duration.new(:days => 3) }
|
41
|
+
let(:timespan) { Timespan.new :from => from, :duration => duration }
|
42
|
+
|
43
|
+
it 'should be 3 days' do
|
44
|
+
timespan.to_d.should == 3
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'set duration with String' do
|
49
|
+
let(:timespan) { Timespan.new :from => from, :duration => "3 days" }
|
50
|
+
|
51
|
+
it 'should be 3 days' do
|
52
|
+
timespan.to_d.should == 3
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'set duration with Spanner String including and' do
|
57
|
+
let(:timespan) { Timespan.new :from => from, :duration => "3 days and 2 hours" }
|
58
|
+
|
59
|
+
it 'should be 3 days and 2 hrs' do
|
60
|
+
timespan.to_h.should == (24 * 3) + 2
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe 'set start_time to new' do
|
65
|
+
let(:timespan) { Timespan.new :from => from, :to => to }
|
66
|
+
|
67
|
+
before :each do
|
68
|
+
@old_timespan = timespan.clone
|
69
|
+
@new_timespan = timespan.clone
|
70
|
+
@new_timespan.start_date = Chronic.parse("2 days ago")
|
71
|
+
end
|
72
|
+
|
73
|
+
its(:duration) { should be_a Duration }
|
74
|
+
specify { subject.send(:dirty).should be_empty }
|
75
|
+
|
76
|
+
it 'should have diff timespans' do
|
77
|
+
@old_timespan.days.should_not == @new_timespan.days
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe 'set end_time to new' do
|
82
|
+
let(:timespan) { Timespan.new :from => from, :to => to }
|
83
|
+
|
84
|
+
before :each do
|
85
|
+
@old_timespan = timespan.clone
|
86
|
+
@new_timespan = timespan.clone
|
87
|
+
@new_timespan.end_date = Chronic.parse("5 days from now")
|
88
|
+
end
|
89
|
+
|
90
|
+
its(:duration) { should be_a Duration }
|
91
|
+
specify { subject.send(:dirty).should be_empty }
|
92
|
+
|
93
|
+
it 'should have diff timespans' do
|
94
|
+
@old_timespan.days.should_not == @new_timespan.days
|
95
|
+
end
|
96
|
+
end
|
47
97
|
end
|
48
|
-
|
49
|
-
|
50
|
-
describe 'set start_time to new' do
|
51
|
-
let(:timespan) { TimeSpan.new :from => from, :to => to }
|
52
|
-
|
53
|
-
before :each do
|
54
|
-
@old_timespan = timespan.clone
|
55
|
-
@new_timespan = timespan.clone
|
56
|
-
@new_timespan.start_date = Chronic.parse("2 days ago")
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'should have diff timespans' do
|
60
|
-
@old_timespan.to_d.should_not == @new_timespan.to_d
|
61
|
-
end
|
62
|
-
end
|
63
98
|
end
|
data/timespan.gemspec
CHANGED
@@ -5,12 +5,12 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "timespan"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
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
|
13
|
-
s.description = "
|
12
|
+
s.date = "2012-05-04"
|
13
|
+
s.description = "Makes it easy to calculate time distance in different units"
|
14
14
|
s.email = "kmandrup@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
@@ -25,8 +25,20 @@ Gem::Specification.new do |s|
|
|
25
25
|
"README.md",
|
26
26
|
"Rakefile",
|
27
27
|
"VERSION",
|
28
|
+
"config/locales/timespan/da.yml",
|
28
29
|
"lib/timespan.rb",
|
30
|
+
"lib/timespan/compare.rb",
|
31
|
+
"lib/timespan/mongoid.rb",
|
32
|
+
"lib/timespan/printer.rb",
|
33
|
+
"lib/timespan/rails/engine.rb",
|
34
|
+
"lib/timespan/span.rb",
|
35
|
+
"lib/timespan/units.rb",
|
29
36
|
"spec/spec_helper.rb",
|
37
|
+
"spec/timespan/compare_spec.rb",
|
38
|
+
"spec/timespan/locales/duration_da.yml",
|
39
|
+
"spec/timespan/printer_spec.rb",
|
40
|
+
"spec/timespan/span_spec.rb",
|
41
|
+
"spec/timespan/units_spec.rb",
|
30
42
|
"spec/timespan_spec.rb",
|
31
43
|
"timespan.gemspec"
|
32
44
|
]
|
@@ -34,25 +46,29 @@ Gem::Specification.new do |s|
|
|
34
46
|
s.licenses = ["MIT"]
|
35
47
|
s.require_paths = ["lib"]
|
36
48
|
s.rubygems_version = "1.8.24"
|
37
|
-
s.summary = "Use
|
49
|
+
s.summary = "Use timespans in ruby"
|
38
50
|
|
39
51
|
if s.respond_to? :specification_version then
|
40
52
|
s.specification_version = 3
|
41
53
|
|
42
54
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
43
55
|
s.add_runtime_dependency(%q<chronic>, [">= 0"])
|
56
|
+
s.add_runtime_dependency(%q<chronic_duration>, [">= 0"])
|
44
57
|
s.add_runtime_dependency(%q<spanner>, [">= 0"])
|
45
58
|
s.add_runtime_dependency(%q<ruby-duration>, [">= 0"])
|
46
59
|
s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
|
60
|
+
s.add_development_dependency(%q<rails>, ["~> 3.2"])
|
47
61
|
s.add_development_dependency(%q<rdoc>, [">= 3.12"])
|
48
62
|
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
49
63
|
s.add_development_dependency(%q<jeweler>, [">= 1.8.3"])
|
50
64
|
s.add_development_dependency(%q<simplecov>, [">= 0.5"])
|
51
65
|
else
|
52
66
|
s.add_dependency(%q<chronic>, [">= 0"])
|
67
|
+
s.add_dependency(%q<chronic_duration>, [">= 0"])
|
53
68
|
s.add_dependency(%q<spanner>, [">= 0"])
|
54
69
|
s.add_dependency(%q<ruby-duration>, [">= 0"])
|
55
70
|
s.add_dependency(%q<rspec>, [">= 2.8.0"])
|
71
|
+
s.add_dependency(%q<rails>, ["~> 3.2"])
|
56
72
|
s.add_dependency(%q<rdoc>, [">= 3.12"])
|
57
73
|
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
58
74
|
s.add_dependency(%q<jeweler>, [">= 1.8.3"])
|
@@ -60,9 +76,11 @@ Gem::Specification.new do |s|
|
|
60
76
|
end
|
61
77
|
else
|
62
78
|
s.add_dependency(%q<chronic>, [">= 0"])
|
79
|
+
s.add_dependency(%q<chronic_duration>, [">= 0"])
|
63
80
|
s.add_dependency(%q<spanner>, [">= 0"])
|
64
81
|
s.add_dependency(%q<ruby-duration>, [">= 0"])
|
65
82
|
s.add_dependency(%q<rspec>, [">= 2.8.0"])
|
83
|
+
s.add_dependency(%q<rails>, ["~> 3.2"])
|
66
84
|
s.add_dependency(%q<rdoc>, [">= 3.12"])
|
67
85
|
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
68
86
|
s.add_dependency(%q<jeweler>, [">= 1.8.3"])
|
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.
|
4
|
+
version: 0.2.0
|
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
|
12
|
+
date: 2012-05-04 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: chronic_duration
|
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: spanner
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -75,6 +91,22 @@ dependencies:
|
|
75
91
|
- - ! '>='
|
76
92
|
- !ruby/object:Gem::Version
|
77
93
|
version: 2.8.0
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: rails
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '3.2'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '3.2'
|
78
110
|
- !ruby/object:Gem::Dependency
|
79
111
|
name: rdoc
|
80
112
|
requirement: !ruby/object:Gem::Requirement
|
@@ -139,7 +171,7 @@ dependencies:
|
|
139
171
|
- - ! '>='
|
140
172
|
- !ruby/object:Gem::Version
|
141
173
|
version: '0.5'
|
142
|
-
description:
|
174
|
+
description: Makes it easy to calculate time distance in different units
|
143
175
|
email: kmandrup@gmail.com
|
144
176
|
executables: []
|
145
177
|
extensions: []
|
@@ -155,8 +187,20 @@ files:
|
|
155
187
|
- README.md
|
156
188
|
- Rakefile
|
157
189
|
- VERSION
|
190
|
+
- config/locales/timespan/da.yml
|
158
191
|
- lib/timespan.rb
|
192
|
+
- lib/timespan/compare.rb
|
193
|
+
- lib/timespan/mongoid.rb
|
194
|
+
- lib/timespan/printer.rb
|
195
|
+
- lib/timespan/rails/engine.rb
|
196
|
+
- lib/timespan/span.rb
|
197
|
+
- lib/timespan/units.rb
|
159
198
|
- spec/spec_helper.rb
|
199
|
+
- spec/timespan/compare_spec.rb
|
200
|
+
- spec/timespan/locales/duration_da.yml
|
201
|
+
- spec/timespan/printer_spec.rb
|
202
|
+
- spec/timespan/span_spec.rb
|
203
|
+
- spec/timespan/units_spec.rb
|
160
204
|
- spec/timespan_spec.rb
|
161
205
|
- timespan.gemspec
|
162
206
|
homepage: http://github.com/kristianmandrup/timespan
|
@@ -174,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
174
218
|
version: '0'
|
175
219
|
segments:
|
176
220
|
- 0
|
177
|
-
hash:
|
221
|
+
hash: 2914636864152776546
|
178
222
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
179
223
|
none: false
|
180
224
|
requirements:
|
@@ -186,5 +230,5 @@ rubyforge_project:
|
|
186
230
|
rubygems_version: 1.8.24
|
187
231
|
signing_key:
|
188
232
|
specification_version: 3
|
189
|
-
summary: Use
|
233
|
+
summary: Use timespans in ruby
|
190
234
|
test_files: []
|