clever_duration 0.1.0
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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +24 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/clever_duration.gemspec +55 -0
- data/lib/clever_duration.rb +5 -0
- data/lib/clever_duration/clever_duration.rb +54 -0
- data/test/helper.rb +10 -0
- data/test/test_clever_duration.rb +73 -0
- metadata +76 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Ilya Sabanin
|
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,24 @@
|
|
1
|
+
= Clever Duration
|
2
|
+
|
3
|
+
Super tiny Ruby library for parsing human format of time durations to numbers.
|
4
|
+
|
5
|
+
It's far for complete, but it's a good base for future development. Test suit included.
|
6
|
+
|
7
|
+
Check tests for a list of supported formats.
|
8
|
+
|
9
|
+
== Example
|
10
|
+
|
11
|
+
duration = CleverDuration.new("1 hour 30 minutes")
|
12
|
+
duration.hours # => 1.5
|
13
|
+
duration.seconds # => 5400
|
14
|
+
duration.minutes # => 90
|
15
|
+
|
16
|
+
== History
|
17
|
+
|
18
|
+
The library was extracted from Beanstalk during the awesome Wildbit Open Source Fridays.
|
19
|
+
|
20
|
+
Read more about Open Source Fridays here:
|
21
|
+
|
22
|
+
http://wildbit.com/blog/2010/01/15/open-source-fridays-at-wildbit/
|
23
|
+
|
24
|
+
Copyright (c) 2010 Ilya Sabanin, Wildbit; see LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "clever_duration"
|
8
|
+
gem.summary = "Human format time duration parsing."
|
9
|
+
gem.description = "A tiny Ruby lib for parsing human format of time duration to numbers."
|
10
|
+
gem.email = "ilya.sabanin@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/iSabanin/clever_duration"
|
12
|
+
gem.authors = ["Ilya Sabanin"]
|
13
|
+
gem.add_development_dependency "shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "clever_duration #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{clever_duration}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Ilya Sabanin"]
|
12
|
+
s.date = %q{2010-04-02}
|
13
|
+
s.description = %q{A tiny Ruby lib for parsing human format of time duration to numbers.}
|
14
|
+
s.email = %q{ilya.sabanin@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"clever_duration.gemspec",
|
27
|
+
"lib/clever_duration.rb",
|
28
|
+
"lib/clever_duration/clever_duration.rb",
|
29
|
+
"test/helper.rb",
|
30
|
+
"test/test_clever_duration.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/iSabanin/clever_duration}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.5}
|
36
|
+
s.summary = %q{Human format time duration parsing.}
|
37
|
+
s.test_files = [
|
38
|
+
"test/helper.rb",
|
39
|
+
"test/test_clever_duration.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
50
|
+
end
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
class CleverDuration
|
2
|
+
|
3
|
+
attr_reader :input
|
4
|
+
|
5
|
+
def initialize(input)
|
6
|
+
@input = input
|
7
|
+
end
|
8
|
+
|
9
|
+
def hours
|
10
|
+
minutes.to_f / 60
|
11
|
+
end
|
12
|
+
|
13
|
+
def minutes
|
14
|
+
seconds.to_f / 60
|
15
|
+
end
|
16
|
+
|
17
|
+
def seconds
|
18
|
+
if colon_format?
|
19
|
+
regular_format_to_seconds
|
20
|
+
else
|
21
|
+
tokens_to_seconds
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def colon_format?
|
28
|
+
!!(input =~ /[:\.]/)
|
29
|
+
end
|
30
|
+
|
31
|
+
def tokens
|
32
|
+
input.scan(/(\d{1,3}) ?(hours?|hrs?|h|minutes?|mins?|m|seconds?|secs?|s)/i)
|
33
|
+
end
|
34
|
+
|
35
|
+
def regular_format_to_seconds
|
36
|
+
hours, minutes = *input.scan(/(\d{0,2})[:\.](\d{0,2})/).flatten
|
37
|
+
(hours.to_i * 60 * 60) + (minutes.to_i * 60)
|
38
|
+
end
|
39
|
+
|
40
|
+
def tokens_to_seconds
|
41
|
+
seconds = 0
|
42
|
+
tokens.each do |t|
|
43
|
+
if t.last =~ /(hours?|hrs?|h)/i
|
44
|
+
seconds += t.first.to_i * 60 * 60
|
45
|
+
elsif t.last =~ /(minutes?|mins?|m)/i
|
46
|
+
seconds += t.first.to_i * 60
|
47
|
+
elsif t.last =~ /(seconds?|secs?|s)/i
|
48
|
+
seconds += t.first.to_i
|
49
|
+
end
|
50
|
+
end
|
51
|
+
seconds
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class CleverDurationTest < Test::Unit::TestCase
|
4
|
+
context "Duration" do
|
5
|
+
should "translate hours" do
|
6
|
+
assert_equal 1, duration("1h").hours
|
7
|
+
assert_equal 4, duration("4 h").hours
|
8
|
+
|
9
|
+
assert_equal 6, duration("6hours").hours
|
10
|
+
assert_equal 3, duration("3 hours").hours
|
11
|
+
|
12
|
+
assert_equal 8, duration("8hrs").hours
|
13
|
+
assert_equal 8, duration("8 hrs").hours
|
14
|
+
end
|
15
|
+
|
16
|
+
should "translate minutes" do
|
17
|
+
assert_equal 10, duration("10m").minutes
|
18
|
+
assert_equal 10, duration("10 m").minutes
|
19
|
+
|
20
|
+
assert_equal 54, duration("54 minutes").minutes
|
21
|
+
assert_equal 54, duration("54minutes").minutes
|
22
|
+
|
23
|
+
assert_equal 22, duration("22min").minutes
|
24
|
+
assert_equal 22, duration("22 min").minutes
|
25
|
+
|
26
|
+
assert_equal 52, duration("52mins").minutes
|
27
|
+
assert_equal 52, duration("52 mins").minutes
|
28
|
+
end
|
29
|
+
|
30
|
+
should "translate seconds" do
|
31
|
+
assert_equal 10, duration("10s").seconds
|
32
|
+
assert_equal 40, duration("40 s").seconds
|
33
|
+
|
34
|
+
assert_equal 50, duration("50sec").seconds
|
35
|
+
assert_equal 50, duration("50 sec").seconds
|
36
|
+
|
37
|
+
assert_equal 140, duration("140seconds").seconds
|
38
|
+
assert_equal 140, duration("140 seconds").seconds
|
39
|
+
|
40
|
+
assert_equal 78, duration("78secs").seconds
|
41
|
+
assert_equal 78, duration("78 secs").seconds
|
42
|
+
end
|
43
|
+
|
44
|
+
should "translate colon" do
|
45
|
+
assert_equal 3, duration("3:00").hours
|
46
|
+
assert_equal 4, duration("04:00").hours
|
47
|
+
assert_equal 30, duration("0:30").minutes
|
48
|
+
assert_equal 2.5, duration("2:30").hours
|
49
|
+
assert_equal 10, duration(":10").minutes
|
50
|
+
end
|
51
|
+
|
52
|
+
should "translate dot" do
|
53
|
+
assert_equal 3, duration("3.00").hours
|
54
|
+
assert_equal 4, duration("04.00").hours
|
55
|
+
assert_equal 30, duration("0.30").minutes
|
56
|
+
assert_equal 2.5, duration("2.30").hours
|
57
|
+
assert_equal 10, duration(".10").minutes
|
58
|
+
end
|
59
|
+
|
60
|
+
should "translate compounds" do
|
61
|
+
assert_equal 1.5, duration("1 hour 30 minutes").hours
|
62
|
+
assert_equal 5.75, duration("5h45m").hours
|
63
|
+
assert_equal 1, duration("59 mins 60s").hours
|
64
|
+
assert_equal 1850, duration("30m 50s").seconds
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def duration(str)
|
71
|
+
CleverDuration.new(str)
|
72
|
+
end
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: clever_duration
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ilya Sabanin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-04-02 00:00:00 +08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: A tiny Ruby lib for parsing human format of time duration to numbers.
|
26
|
+
email: ilya.sabanin@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- clever_duration.gemspec
|
42
|
+
- lib/clever_duration.rb
|
43
|
+
- lib/clever_duration/clever_duration.rb
|
44
|
+
- test/helper.rb
|
45
|
+
- test/test_clever_duration.rb
|
46
|
+
has_rdoc: true
|
47
|
+
homepage: http://github.com/iSabanin/clever_duration
|
48
|
+
licenses: []
|
49
|
+
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options:
|
52
|
+
- --charset=UTF-8
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.5
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Human format time duration parsing.
|
74
|
+
test_files:
|
75
|
+
- test/helper.rb
|
76
|
+
- test/test_clever_duration.rb
|