tick-tock 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +33 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +181 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/tick-tock.rb +17 -0
- data/lib/tick-tock/duration.rb +105 -0
- data/lib/tick-tock/formatter.rb +58 -0
- data/spec/duration_spec.rb +429 -0
- data/spec/formatter_spec.rb +107 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/tick-tock_spec.rb +22 -0
- data/tick-tock.gemspec +67 -0
- metadata +157 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.8.3)
|
7
|
+
bundler (~> 1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rdoc
|
11
|
+
json (1.6.5)
|
12
|
+
rake (0.9.2.2)
|
13
|
+
rcov (1.0.0)
|
14
|
+
rdoc (3.12)
|
15
|
+
json (~> 1.4)
|
16
|
+
rspec (2.8.0)
|
17
|
+
rspec-core (~> 2.8.0)
|
18
|
+
rspec-expectations (~> 2.8.0)
|
19
|
+
rspec-mocks (~> 2.8.0)
|
20
|
+
rspec-core (2.8.0)
|
21
|
+
rspec-expectations (2.8.0)
|
22
|
+
diff-lcs (~> 1.1.2)
|
23
|
+
rspec-mocks (2.8.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.0.0)
|
30
|
+
jeweler (~> 1.8.3)
|
31
|
+
rcov
|
32
|
+
rdoc (~> 3.12)
|
33
|
+
rspec (~> 2.8.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Ian Goodrich
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
= tick-tock
|
2
|
+
|
3
|
+
== DESCRIPTION
|
4
|
+
|
5
|
+
A gem that provides a simple mechanism for timing and reporting how long your code takes to run. Benchmark is great but, for simple timing needs, I prefer a narrower API. Often, I just want to know how long something took without having to add temporary variables and fuss over the output.
|
6
|
+
|
7
|
+
== INSTALL
|
8
|
+
|
9
|
+
gem install tick-tock
|
10
|
+
|
11
|
+
== FEATURES
|
12
|
+
|
13
|
+
* Easy API
|
14
|
+
* Flexible API
|
15
|
+
* Simple, easy to read output
|
16
|
+
* Handles milliseconds, seconds, minutes, hours, days, weeks, and years
|
17
|
+
|
18
|
+
== USAGE
|
19
|
+
|
20
|
+
=== Usage Scenario 0
|
21
|
+
$> TickTock.time { sleep 4 }
|
22
|
+
=> 4s 0ms
|
23
|
+
|
24
|
+
=== Usage Scenario 1
|
25
|
+
|
26
|
+
For simple, everyday use.
|
27
|
+
|
28
|
+
t = TickTock.time do
|
29
|
+
# do stuff
|
30
|
+
sleep 10
|
31
|
+
end
|
32
|
+
puts "That took #{t}"
|
33
|
+
|
34
|
+
That took 10s 0ms
|
35
|
+
|
36
|
+
t = TickTock.time do
|
37
|
+
sleep 314503737247018
|
38
|
+
end
|
39
|
+
puts "That took #{t}"
|
40
|
+
|
41
|
+
That took 10000y 12w 5d 13h 14m 7s 18ms
|
42
|
+
|
43
|
+
|
44
|
+
=== Usage Scenario 2
|
45
|
+
|
46
|
+
TickTock.time returns a TickTock::Duration. These can be added together like so:
|
47
|
+
|
48
|
+
thing1 = TickTock.time do
|
49
|
+
sleep 10
|
50
|
+
end
|
51
|
+
puts "Thing 1 took: #{thing1}"
|
52
|
+
|
53
|
+
thing2 = TickTock.time do
|
54
|
+
sleep 55
|
55
|
+
end
|
56
|
+
puts "Thing 2 took: #{thing2}"
|
57
|
+
|
58
|
+
puts "In total, that took #{thing1 + thing2}"
|
59
|
+
|
60
|
+
Thing 1 took: 10s 0ms
|
61
|
+
Thing 2 took: 55s 0ms
|
62
|
+
In total, that took 1m 5s 0ms
|
63
|
+
|
64
|
+
|
65
|
+
=== Usage Scenario 3 - collecting parameter
|
66
|
+
|
67
|
+
If you'd rather, you can get a fresh instance of a TickTock::Duration with a call to #fresh. Then, you can pass the duration into TickTock::time. This might be useful if you want to pass around a duration to other methods and accumualte timings. Do this, like so:
|
68
|
+
|
69
|
+
total = TickTock.fresh
|
70
|
+
thing1 = TickTock.time(total) do
|
71
|
+
sleep 10
|
72
|
+
end
|
73
|
+
puts "Thing 1 took #{thing1}"
|
74
|
+
|
75
|
+
thing2 = TickTock.time(total) do
|
76
|
+
sleep 55
|
77
|
+
end
|
78
|
+
puts "Thing 2 took #{thing2}"
|
79
|
+
|
80
|
+
puts "In total, that took #{t}"
|
81
|
+
|
82
|
+
Thing 1 took: 10s 0ms
|
83
|
+
Thing 2 took: 55s 0ms
|
84
|
+
In total, that took 1m 5s 0ms
|
85
|
+
|
86
|
+
|
87
|
+
=== Usage Scenario 4 - running total
|
88
|
+
|
89
|
+
Or, you might want to accumulate a running total like so:
|
90
|
+
|
91
|
+
total = TickTock.fresh
|
92
|
+
total += TickTock.time do
|
93
|
+
sleep 4
|
94
|
+
end
|
95
|
+
total += TickTock.time do
|
96
|
+
sleep 15
|
97
|
+
end
|
98
|
+
total += TickTock.time do
|
99
|
+
sleep 19
|
100
|
+
end
|
101
|
+
puts "In total, that took #{t}"
|
102
|
+
|
103
|
+
In total, that took 38s 0ms
|
104
|
+
|
105
|
+
=== Usage Scenario 5 - using duration on it's own
|
106
|
+
|
107
|
+
duraton = TickTock::Duration.new(314503737247018)
|
108
|
+
puts "That's a long time: #{duration}"
|
109
|
+
|
110
|
+
That's a long time: 10000y 12w 5d 13h 14m 7s 18ms
|
111
|
+
|
112
|
+
|
113
|
+
duration = TickTock::Duration.from_start_to_finish(4.years.ago, Time.now)
|
114
|
+
puts "That's not so long: #{duration}"
|
115
|
+
|
116
|
+
That's not so long: 4y 0w 0d 0h 0m 0s 0s
|
117
|
+
|
118
|
+
|
119
|
+
duration = TickTock::Duration.from_pieces(10, 20, 30)
|
120
|
+
puts "Pieces example 1: #{duration}"
|
121
|
+
|
122
|
+
Pieces example 1: 30m 20s 10ms
|
123
|
+
|
124
|
+
|
125
|
+
duration = TickTock::Duration.from_pieces(5, 10, 15, 20, 25, 30, 35)
|
126
|
+
puts "Pieces example 2: #{duration}"
|
127
|
+
|
128
|
+
Pieces example 2: 35y 30w 25d 20h 15m 10s 5ms
|
129
|
+
|
130
|
+
|
131
|
+
== Example Duration Output
|
132
|
+
|
133
|
+
#to_s method only outputs as much as needed and in a brief, easy to understand format
|
134
|
+
|
135
|
+
With a duration of 5 milliseconds:
|
136
|
+
|
137
|
+
5ms
|
138
|
+
|
139
|
+
With a duration of 45 second, 5 milliseconds:
|
140
|
+
|
141
|
+
45s 5ms
|
142
|
+
|
143
|
+
With a duration of 5 minutes, 45 seconds, and 15 milliseconds
|
144
|
+
|
145
|
+
5m 45s 15ms
|
146
|
+
|
147
|
+
With a duration of 4 hours, 5 minutes, 45 seconds, and 15 milliseconds
|
148
|
+
|
149
|
+
4h 5m 45s 15ms
|
150
|
+
|
151
|
+
With a duration of 6 days, 4 hours, 5 minutes, 45 seconds, and 15 milliseconds
|
152
|
+
|
153
|
+
6d 4h 5m 45s 15ms
|
154
|
+
|
155
|
+
With a duration of 9 weeks, 6 days, 4 hours, 5 minutes, 45 seconds, and 15 milliseconds
|
156
|
+
|
157
|
+
9w 6d 4h 5m 45s 15ms
|
158
|
+
|
159
|
+
With a duration of 1014 years, 9 weeks, 6 days, 4 hours, 5 minutes, 45 seconds, and 15 milliseconds
|
160
|
+
|
161
|
+
1014y 9w 6d 4h 5m 45s 15ms
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
== Contributing to tick-tock
|
166
|
+
|
167
|
+
Feel free to request a feature, point out a bug, or fork/etc.
|
168
|
+
|
169
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
170
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
171
|
+
* Fork the project.
|
172
|
+
* Start a feature/bugfix branch.
|
173
|
+
* Commit and push until you are happy with your contribution.
|
174
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
175
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
176
|
+
|
177
|
+
== Copyright
|
178
|
+
|
179
|
+
Copyright (c) 2012 Ian Goodrich. See LICENSE.txt for
|
180
|
+
further details.
|
181
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "tick-tock"
|
18
|
+
gem.homepage = "http://github.com/igoodrich/tick-tock"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{A gem that provides a simple mechanism for timing and reporting how long your code takes to run}
|
21
|
+
gem.description = %Q{A gem that provides a simple mechanism for timing and reporting how long your code takes to run. Benchmark is great but, for simple timing needs, I prefer a narrower API. Often, I just want to know how long something took without having to add temporary variables and fuss over the output.}
|
22
|
+
gem.email = "igoodrich@gmail.com"
|
23
|
+
gem.authors = ["Ian Goodrich"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rdoc/task'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "tick-tock #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/lib/tick-tock.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'tick-tock/duration'
|
2
|
+
require 'tick-tock/formatter'
|
3
|
+
|
4
|
+
module TickTock
|
5
|
+
|
6
|
+
def self.fresh
|
7
|
+
TickTock::Duration.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.time(duration = nil)
|
11
|
+
start = Time.now
|
12
|
+
yield
|
13
|
+
this_timing = TickTock::Duration.from_times(start, Time.now)
|
14
|
+
duration.nil? ? this_timing : duration + this_timing
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
module TickTock
|
2
|
+
|
3
|
+
class Duration
|
4
|
+
|
5
|
+
MILLIS_IN_SECOND = 1000
|
6
|
+
MILLIS_IN_MINUTE = MILLIS_IN_SECOND * 60
|
7
|
+
MILLIS_IN_HOUR = MILLIS_IN_MINUTE * 60
|
8
|
+
MILLIS_IN_DAY = MILLIS_IN_HOUR * 24
|
9
|
+
MILLIS_IN_WEEK = MILLIS_IN_DAY * 7
|
10
|
+
MILLIS_IN_YEAR = MILLIS_IN_WEEK * 52
|
11
|
+
|
12
|
+
attr_reader :raw_millis
|
13
|
+
|
14
|
+
def self.from_times(start, finish)
|
15
|
+
Duration.new(((finish - start) * 1000).to_i)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.from_pieces(millis=0, seconds=0, minutes=0, hours=0, days=0, weeks=0, years=0)
|
19
|
+
Duration.new(millis +
|
20
|
+
seconds * MILLIS_IN_SECOND +
|
21
|
+
minutes * MILLIS_IN_MINUTE +
|
22
|
+
hours * MILLIS_IN_HOUR +
|
23
|
+
days * MILLIS_IN_DAY +
|
24
|
+
weeks * MILLIS_IN_WEEK +
|
25
|
+
years * MILLIS_IN_YEAR)
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize(millis = 0)
|
29
|
+
@raw_millis = millis
|
30
|
+
@formatter = TickTock::Formatter.new(self)
|
31
|
+
end
|
32
|
+
|
33
|
+
def +(other)
|
34
|
+
Duration.new(raw_millis + other.raw_millis)
|
35
|
+
end
|
36
|
+
|
37
|
+
def -(other)
|
38
|
+
Duration.new(raw_millis - other.raw_millis)
|
39
|
+
end
|
40
|
+
|
41
|
+
def years
|
42
|
+
raw_millis / MILLIS_IN_YEAR
|
43
|
+
end
|
44
|
+
|
45
|
+
def weeks
|
46
|
+
(raw_millis - years_in_millis) / MILLIS_IN_WEEK
|
47
|
+
end
|
48
|
+
|
49
|
+
def days
|
50
|
+
(raw_millis - years_in_millis - weeks_in_millis) / MILLIS_IN_DAY
|
51
|
+
end
|
52
|
+
|
53
|
+
def hours
|
54
|
+
(raw_millis - years_in_millis - weeks_in_millis - days_in_millis) / MILLIS_IN_HOUR
|
55
|
+
end
|
56
|
+
|
57
|
+
def minutes
|
58
|
+
(raw_millis - years_in_millis - weeks_in_millis - days_in_millis - hours_in_millis) / MILLIS_IN_MINUTE
|
59
|
+
end
|
60
|
+
|
61
|
+
def seconds
|
62
|
+
(raw_millis - years_in_millis - weeks_in_millis - days_in_millis - hours_in_millis - minutes_in_millis) / MILLIS_IN_SECOND
|
63
|
+
end
|
64
|
+
|
65
|
+
def millis
|
66
|
+
raw_millis - years_in_millis - weeks_in_millis - days_in_millis - hours_in_millis - minutes_in_millis - seconds_in_millis
|
67
|
+
end
|
68
|
+
|
69
|
+
def to_s
|
70
|
+
@formatter.to_s
|
71
|
+
end
|
72
|
+
|
73
|
+
def inspect
|
74
|
+
to_s
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def years_in_millis
|
80
|
+
MILLIS_IN_YEAR * years
|
81
|
+
end
|
82
|
+
|
83
|
+
def weeks_in_millis
|
84
|
+
MILLIS_IN_WEEK * weeks
|
85
|
+
end
|
86
|
+
|
87
|
+
def days_in_millis
|
88
|
+
MILLIS_IN_DAY * days
|
89
|
+
end
|
90
|
+
|
91
|
+
def hours_in_millis
|
92
|
+
MILLIS_IN_HOUR * hours
|
93
|
+
end
|
94
|
+
|
95
|
+
def minutes_in_millis
|
96
|
+
MILLIS_IN_MINUTE * minutes
|
97
|
+
end
|
98
|
+
|
99
|
+
def seconds_in_millis
|
100
|
+
MILLIS_IN_SECOND * seconds
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module TickTock
|
2
|
+
class Formatter
|
3
|
+
|
4
|
+
attr_reader :duration
|
5
|
+
|
6
|
+
def initialize(duration)
|
7
|
+
@duration = duration
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
"".tap do |s|
|
12
|
+
s << " #{duration.years}y" if include_years?
|
13
|
+
s << " #{duration.weeks}w" if include_weeks?
|
14
|
+
s << " #{duration.days}d" if include_days?
|
15
|
+
s << " #{duration.hours}h" if include_hours?
|
16
|
+
s << " #{duration.minutes}m" if include_minutes?
|
17
|
+
s << " #{duration.seconds}s" if include_seconds?
|
18
|
+
s << " #{duration.millis}ms" if include_millis?
|
19
|
+
s.lstrip!
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def include_millis?
|
26
|
+
true
|
27
|
+
end
|
28
|
+
|
29
|
+
def include_seconds?
|
30
|
+
at_or_beyond? TickTock::Duration::MILLIS_IN_SECOND
|
31
|
+
end
|
32
|
+
|
33
|
+
def include_minutes?
|
34
|
+
at_or_beyond? TickTock::Duration::MILLIS_IN_MINUTE
|
35
|
+
end
|
36
|
+
|
37
|
+
def include_hours?
|
38
|
+
at_or_beyond? TickTock::Duration::MILLIS_IN_HOUR
|
39
|
+
end
|
40
|
+
|
41
|
+
def include_days?
|
42
|
+
at_or_beyond? TickTock::Duration::MILLIS_IN_DAY
|
43
|
+
end
|
44
|
+
|
45
|
+
def include_weeks?
|
46
|
+
at_or_beyond? TickTock::Duration::MILLIS_IN_WEEK
|
47
|
+
end
|
48
|
+
|
49
|
+
def include_years?
|
50
|
+
at_or_beyond? TickTock::Duration::MILLIS_IN_YEAR
|
51
|
+
end
|
52
|
+
|
53
|
+
def at_or_beyond?(boundary)
|
54
|
+
@duration.raw_millis >= boundary
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,429 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Duration" do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
@ms_in_a_second = 1000
|
7
|
+
@ms_in_a_minute = @ms_in_a_second * 60
|
8
|
+
@ms_in_an_hour = @ms_in_a_minute * 60
|
9
|
+
@ms_in_a_day = @ms_in_an_hour * 24
|
10
|
+
@ms_in_a_week = @ms_in_a_day * 7
|
11
|
+
@ms_in_a_year = @ms_in_a_week * 52
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "creation" do
|
15
|
+
|
16
|
+
it "should know how many millis it was made with" do
|
17
|
+
d = TickTock::Duration.new(4321)
|
18
|
+
d.raw_millis.should == 4321
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should use zero if no millis specified" do
|
22
|
+
d = TickTock::Duration.new
|
23
|
+
d.raw_millis.should == 0
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#years" do
|
29
|
+
|
30
|
+
it "should have 0 years when the duration has fewer than the number of millis in a year" do
|
31
|
+
TickTock::Duration.new.years.should == 0
|
32
|
+
TickTock::Duration.new(@ms_in_a_week * 51).years.should == 0
|
33
|
+
TickTock::Duration.new(@ms_in_a_year - 1).years.should == 0
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should have 1 year at the boundary" do
|
37
|
+
TickTock::Duration.new(@ms_in_a_year).years.should == 1
|
38
|
+
TickTock::Duration.new(@ms_in_a_year + @ms_in_a_week * 17).years.should == 1
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should have 11 years if it's got a lot of millis" do
|
42
|
+
TickTock::Duration.new(@ms_in_a_year * 11).years.should == 11
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#weeks" do
|
48
|
+
|
49
|
+
it "should have 0 weeks when the duration has fewer than the number of millis in a week" do
|
50
|
+
TickTock::Duration.new.weeks.should == 0
|
51
|
+
TickTock::Duration.new(@ms_in_a_day * 4).weeks.should == 0
|
52
|
+
TickTock::Duration.new(@ms_in_a_week - 1).weeks.should == 0
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should have 1 week when the duration is at the boundary" do
|
56
|
+
TickTock::Duration.new(@ms_in_a_week).weeks.should == 1
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should be able to handle multiple weeks less than a year" do
|
60
|
+
TickTock::Duration.new(@ms_in_a_week * 30).weeks.should == 30
|
61
|
+
TickTock::Duration.new(@ms_in_a_week * 51).weeks.should == 51
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should account for a year if the duration is more than a year" do
|
65
|
+
TickTock::Duration.new(@ms_in_a_year).weeks.should == 0
|
66
|
+
TickTock::Duration.new(@ms_in_a_year + @ms_in_a_week - 1).weeks.should == 0
|
67
|
+
TickTock::Duration.new(@ms_in_a_year + @ms_in_a_week).weeks.should == 1
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should work with multiple years and multiple weeks" do
|
71
|
+
tick_tock_duration = TickTock::Duration.new(@ms_in_a_year * 8 + @ms_in_a_week * 40)
|
72
|
+
tick_tock_duration.weeks.should == 40
|
73
|
+
tick_tock_duration.years.should == 8
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#days" do
|
79
|
+
|
80
|
+
it "should have 0 days when the duration has fewer than the number of millis in a day" do
|
81
|
+
TickTock::Duration.new.weeks.should == 0
|
82
|
+
TickTock::Duration.new(@ms_in_an_hour * 10).days.should == 0
|
83
|
+
TickTock::Duration.new(@ms_in_a_day - 1).days.should == 0
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should should have 1 day when at the day boundary" do
|
87
|
+
TickTock::Duration.new(@ms_in_a_day).days.should == 1
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should should have 1 day when just under the 2 day boundary" do
|
91
|
+
TickTock::Duration.new(@ms_in_a_day * 2 - 1).days.should == 1
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should work when just over a week" do
|
95
|
+
duration = TickTock::Duration.new(@ms_in_a_week + 1)
|
96
|
+
duration.weeks.should == 1
|
97
|
+
duration.days.should == 0
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should work with multiple weeks when just over a week" do
|
101
|
+
duration = TickTock::Duration.new((@ms_in_a_week * 2) + (@ms_in_a_day * 3))
|
102
|
+
duration.weeks.should == 2
|
103
|
+
duration.days.should == 3
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should work with multiple years, weeks, and days" do
|
107
|
+
duration = TickTock::Duration.new((@ms_in_a_year * 5) + (@ms_in_a_week * 2) + (@ms_in_a_day * 3))
|
108
|
+
duration.years.should == 5
|
109
|
+
duration.weeks.should == 2
|
110
|
+
duration.days.should == 3
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "#hours" do
|
116
|
+
|
117
|
+
it "should have 0 hours when the duration has fewer millis than there are in an hour" do
|
118
|
+
duration = TickTock::Duration.new(@ms_in_an_hour - 1)
|
119
|
+
duration.hours.should == 0
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should have 1 hour when the duration is exactly one hour in millis" do
|
123
|
+
duration = TickTock::Duration.new(@ms_in_an_hour)
|
124
|
+
duration.hours.should == 1
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should have 23 hours when the duration is just under a day" do
|
128
|
+
duration = TickTock::Duration.new(@ms_in_a_day - 1)
|
129
|
+
duration.hours.should == 23
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should have 0 hours when the duration is exactly a day" do
|
133
|
+
duration = TickTock::Duration.new(@ms_in_a_day)
|
134
|
+
duration.hours.should == 0
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should have 0 hours when the duration is a day and up to 1 hour" do
|
138
|
+
duration = TickTock::Duration.new(@ms_in_a_day + (@ms_in_an_hour - 1))
|
139
|
+
duration.hours.should == 0
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should have 1 hours when the duration is exactly a day and an hour" do
|
143
|
+
duration = TickTock::Duration.new(@ms_in_a_day + (@ms_in_an_hour))
|
144
|
+
duration.hours.should == 1
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
describe "#minutes" do
|
150
|
+
|
151
|
+
it "should have 0 minutes if millis are 0" do
|
152
|
+
duration = TickTock::Duration.new(0)
|
153
|
+
duration.minutes.should == 0
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should have 0 minutes when just less than the boundary between 0 minutes and 1 minute" do
|
157
|
+
duration = TickTock::Duration.new(@ms_in_a_minute - 1)
|
158
|
+
duration.minutes.should == 0
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should have 1 minute when it has exactly one minute of millis" do
|
162
|
+
duration = TickTock::Duration.new(@ms_in_a_minute)
|
163
|
+
duration.minutes.should == 1
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should have 1 minute up to the 2 minute boundary" do
|
167
|
+
duration = TickTock::Duration.new(@ms_in_a_minute * 2 - 1)
|
168
|
+
duration.minutes.should == 1
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should have 59 minutes up to the hour boundary" do
|
172
|
+
duration = TickTock::Duration.new(@ms_in_an_hour - 1)
|
173
|
+
duration.minutes.should == 59
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should have 0 minutes at the hour boundary" do
|
177
|
+
duration = TickTock::Duration.new(@ms_in_an_hour)
|
178
|
+
duration.minutes.should == 0
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should cross the hour boundary gracefully" do
|
182
|
+
duration = TickTock::Duration.new(@ms_in_an_hour + 1)
|
183
|
+
duration.hours.should == 1
|
184
|
+
duration.minutes.should == 0
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
describe "#seconds" do
|
190
|
+
|
191
|
+
it "should have 0 seconds if millis are 0" do
|
192
|
+
duration = TickTock::Duration.new(0)
|
193
|
+
duration.seconds.should == 0
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should have 0 seconds up until 1 second of millis" do
|
197
|
+
duration = TickTock::Duration.new(999)
|
198
|
+
duration.seconds.should == 0
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should have 1 second at exactly 1 second of millis" do
|
202
|
+
duration = TickTock::Duration.new(1000)
|
203
|
+
duration.seconds.should == 1
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should have 1 second up to 2 seconds of millis" do
|
207
|
+
duration = TickTock::Duration.new(1999)
|
208
|
+
duration.seconds.should == 1
|
209
|
+
end
|
210
|
+
|
211
|
+
it "should have 2 seconds after 2 seconds of millis" do
|
212
|
+
duration = TickTock::Duration.new(2001)
|
213
|
+
duration.seconds.should == 2
|
214
|
+
end
|
215
|
+
|
216
|
+
end
|
217
|
+
|
218
|
+
describe "#millis" do
|
219
|
+
|
220
|
+
it "should have 0 seconds if millis are 0" do
|
221
|
+
duration = TickTock::Duration.new(0)
|
222
|
+
duration.millis.should == 0
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should count millis up to 1 second" do
|
226
|
+
duration = TickTock::Duration.new(999)
|
227
|
+
duration.millis.should == 999
|
228
|
+
end
|
229
|
+
|
230
|
+
it "should reset at 1 second" do
|
231
|
+
duration = TickTock::Duration.new(1000)
|
232
|
+
duration.millis.should == 0
|
233
|
+
end
|
234
|
+
|
235
|
+
it "should count between 1000 and 2000 millis" do
|
236
|
+
duration = TickTock::Duration.new(1457)
|
237
|
+
duration.millis.should == 457
|
238
|
+
end
|
239
|
+
|
240
|
+
end
|
241
|
+
|
242
|
+
describe "all the bits at once" do
|
243
|
+
|
244
|
+
it "should account for milliseconds, seconds, minutes, hours, days, weeks, and years" do
|
245
|
+
duration = TickTock::Duration.new((@ms_in_a_year * 6) + (@ms_in_a_week * 17) + (@ms_in_a_day * 5) + (@ms_in_an_hour * 4) + (@ms_in_a_minute * 14) + (@ms_in_a_second * 45) + 973)
|
246
|
+
duration.years.should == 6
|
247
|
+
duration.weeks.should == 17
|
248
|
+
duration.days.should == 5
|
249
|
+
duration.hours.should == 4
|
250
|
+
duration.minutes.should == 14
|
251
|
+
duration.seconds.should == 45
|
252
|
+
duration.millis.should == 973
|
253
|
+
end
|
254
|
+
|
255
|
+
end
|
256
|
+
|
257
|
+
describe ".start_to_finish" do
|
258
|
+
|
259
|
+
it "should allow creation from two times" do
|
260
|
+
start = Time.utc(2012, 12, 10)
|
261
|
+
finish = Time.utc(2012, 12, 10, 4)
|
262
|
+
duration = TickTock::Duration.from_times(start, finish)
|
263
|
+
duration.years.should == 0
|
264
|
+
duration.weeks.should == 0
|
265
|
+
duration.days.should == 0
|
266
|
+
duration.hours.should == 4
|
267
|
+
duration.minutes.should == 0
|
268
|
+
duration.seconds.should == 0
|
269
|
+
duration.millis.should == 0
|
270
|
+
end
|
271
|
+
|
272
|
+
end
|
273
|
+
|
274
|
+
|
275
|
+
describe ".from_pieces" do
|
276
|
+
|
277
|
+
it "should allow creation from just millis" do
|
278
|
+
duration = TickTock::Duration.from_pieces(10)
|
279
|
+
duration.years.should == 0
|
280
|
+
duration.weeks.should == 0
|
281
|
+
duration.days.should == 0
|
282
|
+
duration.hours.should == 0
|
283
|
+
duration.minutes.should == 0
|
284
|
+
duration.seconds.should == 0
|
285
|
+
duration.millis.should == 10
|
286
|
+
end
|
287
|
+
|
288
|
+
it "should allow creation from millis and seconds" do
|
289
|
+
duration = TickTock::Duration.from_pieces(10, 40)
|
290
|
+
duration.years.should == 0
|
291
|
+
duration.weeks.should == 0
|
292
|
+
duration.days.should == 0
|
293
|
+
duration.hours.should == 0
|
294
|
+
duration.minutes.should == 0
|
295
|
+
duration.seconds.should == 40
|
296
|
+
duration.millis.should == 10
|
297
|
+
end
|
298
|
+
|
299
|
+
it "should allow creation from millis, seconds, and minutes" do
|
300
|
+
duration = TickTock::Duration.from_pieces(10, 40, 20)
|
301
|
+
duration.years.should == 0
|
302
|
+
duration.weeks.should == 0
|
303
|
+
duration.days.should == 0
|
304
|
+
duration.hours.should == 0
|
305
|
+
duration.minutes.should == 20
|
306
|
+
duration.seconds.should == 40
|
307
|
+
duration.millis.should == 10
|
308
|
+
end
|
309
|
+
|
310
|
+
it "should allow creation from millis, seconds, minutes, and hours" do
|
311
|
+
duration = TickTock::Duration.from_pieces(10, 40, 20, 5)
|
312
|
+
duration.years.should == 0
|
313
|
+
duration.weeks.should == 0
|
314
|
+
duration.days.should == 0
|
315
|
+
duration.hours.should == 5
|
316
|
+
duration.minutes.should == 20
|
317
|
+
duration.seconds.should == 40
|
318
|
+
duration.millis.should == 10
|
319
|
+
end
|
320
|
+
|
321
|
+
it "should allow creation from millis, seconds, minutes, hours, and days" do
|
322
|
+
duration = TickTock::Duration.from_pieces(10, 40, 20, 5, 2)
|
323
|
+
duration.years.should == 0
|
324
|
+
duration.weeks.should == 0
|
325
|
+
duration.days.should == 2
|
326
|
+
duration.hours.should == 5
|
327
|
+
duration.minutes.should == 20
|
328
|
+
duration.seconds.should == 40
|
329
|
+
duration.millis.should == 10
|
330
|
+
end
|
331
|
+
|
332
|
+
it "should allow creation from millis, seconds, minutes, hours, days, and weeks" do
|
333
|
+
duration = TickTock::Duration.from_pieces(10, 40, 20, 5, 2, 40)
|
334
|
+
duration.years.should == 0
|
335
|
+
duration.weeks.should == 40
|
336
|
+
duration.days.should == 2
|
337
|
+
duration.hours.should == 5
|
338
|
+
duration.minutes.should == 20
|
339
|
+
duration.seconds.should == 40
|
340
|
+
duration.millis.should == 10
|
341
|
+
end
|
342
|
+
|
343
|
+
it "should allow creation from millis, seconds, minutes, hours, days, weeks, and years" do
|
344
|
+
duration = TickTock::Duration.from_pieces(10, 40, 20, 5, 2, 40, 1023)
|
345
|
+
duration.years.should == 1023
|
346
|
+
duration.weeks.should == 40
|
347
|
+
duration.days.should == 2
|
348
|
+
duration.hours.should == 5
|
349
|
+
duration.minutes.should == 20
|
350
|
+
duration.seconds.should == 40
|
351
|
+
duration.millis.should == 10
|
352
|
+
end
|
353
|
+
|
354
|
+
it "should overflow lesser bits into more significant bits" do
|
355
|
+
duration = TickTock::Duration.from_pieces(2223, 70, 65, 5, 2, 40, 1023)
|
356
|
+
duration.years.should == 1023
|
357
|
+
duration.weeks.should == 40
|
358
|
+
duration.days.should == 2
|
359
|
+
duration.hours.should == 6
|
360
|
+
duration.minutes.should == 6
|
361
|
+
duration.seconds.should == 12
|
362
|
+
duration.millis.should == 223
|
363
|
+
end
|
364
|
+
|
365
|
+
end
|
366
|
+
|
367
|
+
describe "addition" do
|
368
|
+
|
369
|
+
it "should support simple addition" do
|
370
|
+
duration1 = TickTock::Duration.from_pieces(823)
|
371
|
+
duration2 = TickTock::Duration.from_pieces(455)
|
372
|
+
duration = duration1 + duration2
|
373
|
+
duration.years.should == 0
|
374
|
+
duration.weeks.should == 0
|
375
|
+
duration.days.should == 0
|
376
|
+
duration.hours.should == 0
|
377
|
+
duration.minutes.should == 0
|
378
|
+
duration.seconds.should == 1
|
379
|
+
duration.millis.should == 278
|
380
|
+
end
|
381
|
+
|
382
|
+
it "should support adding a bunch together" do
|
383
|
+
duration1 = TickTock::Duration.from_pieces(10)
|
384
|
+
duration2 = TickTock::Duration.from_pieces(0, 34)
|
385
|
+
duration3 = TickTock::Duration.from_pieces(0, 0, 4)
|
386
|
+
duration4 = TickTock::Duration.from_pieces(0, 0, 0, 10)
|
387
|
+
duration = duration1 + duration2 + duration3 + duration4
|
388
|
+
duration.years.should == 0
|
389
|
+
duration.weeks.should == 0
|
390
|
+
duration.days.should == 0
|
391
|
+
duration.hours.should == 10
|
392
|
+
duration.minutes.should == 4
|
393
|
+
duration.seconds.should == 34
|
394
|
+
duration.millis.should == 10
|
395
|
+
end
|
396
|
+
|
397
|
+
it "should work with +=" do
|
398
|
+
duration = TickTock::Duration.from_pieces(10)
|
399
|
+
extra = TickTock::Duration.from_pieces(0, 34)
|
400
|
+
duration += extra
|
401
|
+
duration.years.should == 0
|
402
|
+
duration.weeks.should == 0
|
403
|
+
duration.days.should == 0
|
404
|
+
duration.hours.should == 0
|
405
|
+
duration.minutes.should == 0
|
406
|
+
duration.seconds.should == 34
|
407
|
+
duration.millis.should == 10
|
408
|
+
end
|
409
|
+
|
410
|
+
end
|
411
|
+
|
412
|
+
describe "subtraction" do
|
413
|
+
|
414
|
+
it "should support simple subtraction" do
|
415
|
+
duration1 = TickTock::Duration.from_pieces(823)
|
416
|
+
duration2 = TickTock::Duration.from_pieces(455)
|
417
|
+
duration = duration1 - duration2
|
418
|
+
duration.years.should == 0
|
419
|
+
duration.weeks.should == 0
|
420
|
+
duration.days.should == 0
|
421
|
+
duration.hours.should == 0
|
422
|
+
duration.minutes.should == 0
|
423
|
+
duration.seconds.should == 0
|
424
|
+
duration.millis.should == 368
|
425
|
+
end
|
426
|
+
|
427
|
+
end
|
428
|
+
|
429
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Formatter" do
|
4
|
+
|
5
|
+
# "1s 0ms"
|
6
|
+
|
7
|
+
|
8
|
+
describe "progressive output" do
|
9
|
+
|
10
|
+
it "should output only millis if zero" do
|
11
|
+
duration = TickTock::Duration.new(0)
|
12
|
+
formatter = TickTock::Formatter.new(duration)
|
13
|
+
formatter.to_s.should == "0ms"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should output millis" do
|
17
|
+
duration = TickTock::Duration.new(1)
|
18
|
+
formatter = TickTock::Formatter.new(duration)
|
19
|
+
formatter.to_s.should == "1ms"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should output only millis if less than second" do
|
23
|
+
duration = TickTock::Duration.new(999)
|
24
|
+
formatter = TickTock::Formatter.new(duration)
|
25
|
+
formatter.to_s.should == "999ms"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should output seconds and millis if exactly 1 second" do
|
29
|
+
duration = TickTock::Duration.new(1000)
|
30
|
+
formatter = TickTock::Formatter.new(duration)
|
31
|
+
formatter.to_s.should == "1s 0ms"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should output seconds and millis only up to 1 minute" do
|
35
|
+
duration = TickTock::Duration.new(59999)
|
36
|
+
formatter = TickTock::Formatter.new(duration)
|
37
|
+
formatter.to_s.should == "59s 999ms"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should output minutes, seconds, and millis at exactly 1 minute" do
|
41
|
+
duration = TickTock::Duration.new(60000)
|
42
|
+
formatter = TickTock::Formatter.new(duration)
|
43
|
+
formatter.to_s.should == "1m 0s 0ms"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should output minutes, seconds, and millis up to 1 hour" do
|
47
|
+
duration = TickTock::Duration.new(TickTock::Duration::MILLIS_IN_HOUR - 1)
|
48
|
+
formatter = TickTock::Formatter.new(duration)
|
49
|
+
formatter.to_s.should == "59m 59s 999ms"
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should output hours, minutes, seconds, and millis at exactly hour" do
|
53
|
+
duration = TickTock::Duration.new(TickTock::Duration::MILLIS_IN_HOUR)
|
54
|
+
formatter = TickTock::Formatter.new(duration)
|
55
|
+
formatter.to_s.should == "1h 0m 0s 0ms"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should output hours, minutes, seconds, and millis up to 1 day" do
|
59
|
+
duration = TickTock::Duration.new(TickTock::Duration::MILLIS_IN_DAY - 1)
|
60
|
+
formatter = TickTock::Formatter.new(duration)
|
61
|
+
formatter.to_s.should == "23h 59m 59s 999ms"
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should output days, hours, minutes, seconds, and millis at exactly 1 day" do
|
65
|
+
duration = TickTock::Duration.new(TickTock::Duration::MILLIS_IN_DAY)
|
66
|
+
formatter = TickTock::Formatter.new(duration)
|
67
|
+
formatter.to_s.should == "1d 0h 0m 0s 0ms"
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should output days, hours, minutes, seconds, and millis up to 1 week" do
|
71
|
+
duration = TickTock::Duration.new(TickTock::Duration::MILLIS_IN_WEEK - 1)
|
72
|
+
formatter = TickTock::Formatter.new(duration)
|
73
|
+
formatter.to_s.should == "6d 23h 59m 59s 999ms"
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should output weeks, days, hours, minutes, seconds, and millis at exactly 1 week" do
|
77
|
+
duration = TickTock::Duration.new(TickTock::Duration::MILLIS_IN_WEEK)
|
78
|
+
formatter = TickTock::Formatter.new(duration)
|
79
|
+
formatter.to_s.should == "1w 0d 0h 0m 0s 0ms"
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should output weeks, days, hours, minutes, seconds, and millis up to 1 year" do
|
83
|
+
duration = TickTock::Duration.new(TickTock::Duration::MILLIS_IN_YEAR - 1)
|
84
|
+
formatter = TickTock::Formatter.new(duration)
|
85
|
+
formatter.to_s.should == "51w 6d 23h 59m 59s 999ms"
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should output years, weeks, days, hours, minutes, seconds, and millis at exactly 1 year" do
|
89
|
+
duration = TickTock::Duration.new(TickTock::Duration::MILLIS_IN_YEAR)
|
90
|
+
formatter = TickTock::Formatter.new(duration)
|
91
|
+
formatter.to_s.should == "1y 0w 0d 0h 0m 0s 0ms"
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should output years, weeks, days, hours, minutes, seconds, and millis for a long time" do
|
95
|
+
duration = TickTock::Duration.new((TickTock::Duration::MILLIS_IN_YEAR * 10000) +
|
96
|
+
(TickTock::Duration::MILLIS_IN_WEEK * 12) +
|
97
|
+
(TickTock::Duration::MILLIS_IN_DAY * 5) +
|
98
|
+
(TickTock::Duration::MILLIS_IN_HOUR * 13) +
|
99
|
+
(TickTock::Duration::MILLIS_IN_MINUTE * 14) +
|
100
|
+
(TickTock::Duration::MILLIS_IN_SECOND * 7) + 18)
|
101
|
+
formatter = TickTock::Formatter.new(duration)
|
102
|
+
formatter.to_s.should == "10000y 12w 5d 13h 14m 7s 18ms"
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'tick-tock'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "TickTock" do
|
4
|
+
|
5
|
+
it "calls block and returns duration" do
|
6
|
+
flubber = 10
|
7
|
+
duration = TickTock.time do
|
8
|
+
flubber = 12
|
9
|
+
end
|
10
|
+
duration.should_not be_nil
|
11
|
+
flubber.should == 12
|
12
|
+
end
|
13
|
+
|
14
|
+
it "can use the same duration if passed in" do
|
15
|
+
duration1 = double("duration1")
|
16
|
+
duration1.should_receive(:+)
|
17
|
+
duration2 = TickTock.time(duration1) do
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/tick-tock.gemspec
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "tick-tock"
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Ian Goodrich"]
|
12
|
+
s.date = "2012-07-09"
|
13
|
+
s.description = "A gem that provides a simple mechanism for timing and reporting how long your code takes to run. Benchmark is great but, for simple timing needs, I prefer a narrower API. Often, I just want to know how long something took without having to add temporary variables and fuss over the output."
|
14
|
+
s.email = "igoodrich@gmail.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/tick-tock.rb",
|
29
|
+
"lib/tick-tock/duration.rb",
|
30
|
+
"lib/tick-tock/formatter.rb",
|
31
|
+
"spec/duration_spec.rb",
|
32
|
+
"spec/formatter_spec.rb",
|
33
|
+
"spec/spec_helper.rb",
|
34
|
+
"spec/tick-tock_spec.rb",
|
35
|
+
"tick-tock.gemspec"
|
36
|
+
]
|
37
|
+
s.homepage = "http://github.com/igoodrich/tick-tock"
|
38
|
+
s.licenses = ["MIT"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = "1.8.15"
|
41
|
+
s.summary = "A gem that provides a simple mechanism for timing and reporting how long your code takes to run"
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
|
48
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
49
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
50
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
|
51
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
54
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
55
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
56
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
57
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
58
|
+
end
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
61
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
62
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
63
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
64
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tick-tock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ian Goodrich
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-07-09 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
22
|
+
none: false
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
hash: 47
|
27
|
+
segments:
|
28
|
+
- 2
|
29
|
+
- 8
|
30
|
+
- 0
|
31
|
+
version: 2.8.0
|
32
|
+
name: rspec
|
33
|
+
prerelease: false
|
34
|
+
type: :development
|
35
|
+
requirement: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 31
|
43
|
+
segments:
|
44
|
+
- 3
|
45
|
+
- 12
|
46
|
+
version: "3.12"
|
47
|
+
name: rdoc
|
48
|
+
prerelease: false
|
49
|
+
type: :development
|
50
|
+
requirement: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ~>
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 23
|
58
|
+
segments:
|
59
|
+
- 1
|
60
|
+
- 0
|
61
|
+
- 0
|
62
|
+
version: 1.0.0
|
63
|
+
name: bundler
|
64
|
+
prerelease: false
|
65
|
+
type: :development
|
66
|
+
requirement: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ~>
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 49
|
74
|
+
segments:
|
75
|
+
- 1
|
76
|
+
- 8
|
77
|
+
- 3
|
78
|
+
version: 1.8.3
|
79
|
+
name: jeweler
|
80
|
+
prerelease: false
|
81
|
+
type: :development
|
82
|
+
requirement: *id004
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
name: rcov
|
94
|
+
prerelease: false
|
95
|
+
type: :development
|
96
|
+
requirement: *id005
|
97
|
+
description: A gem that provides a simple mechanism for timing and reporting how long your code takes to run. Benchmark is great but, for simple timing needs, I prefer a narrower API. Often, I just want to know how long something took without having to add temporary variables and fuss over the output.
|
98
|
+
email: igoodrich@gmail.com
|
99
|
+
executables: []
|
100
|
+
|
101
|
+
extensions: []
|
102
|
+
|
103
|
+
extra_rdoc_files:
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.rdoc
|
106
|
+
files:
|
107
|
+
- .document
|
108
|
+
- .rspec
|
109
|
+
- Gemfile
|
110
|
+
- Gemfile.lock
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.rdoc
|
113
|
+
- Rakefile
|
114
|
+
- VERSION
|
115
|
+
- lib/tick-tock.rb
|
116
|
+
- lib/tick-tock/duration.rb
|
117
|
+
- lib/tick-tock/formatter.rb
|
118
|
+
- spec/duration_spec.rb
|
119
|
+
- spec/formatter_spec.rb
|
120
|
+
- spec/spec_helper.rb
|
121
|
+
- spec/tick-tock_spec.rb
|
122
|
+
- tick-tock.gemspec
|
123
|
+
homepage: http://github.com/igoodrich/tick-tock
|
124
|
+
licenses:
|
125
|
+
- MIT
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
|
129
|
+
require_paths:
|
130
|
+
- lib
|
131
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
132
|
+
none: false
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
hash: 3
|
137
|
+
segments:
|
138
|
+
- 0
|
139
|
+
version: "0"
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
none: false
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
hash: 3
|
146
|
+
segments:
|
147
|
+
- 0
|
148
|
+
version: "0"
|
149
|
+
requirements: []
|
150
|
+
|
151
|
+
rubyforge_project:
|
152
|
+
rubygems_version: 1.8.15
|
153
|
+
signing_key:
|
154
|
+
specification_version: 3
|
155
|
+
summary: A gem that provides a simple mechanism for timing and reporting how long your code takes to run
|
156
|
+
test_files: []
|
157
|
+
|