time_sentence 0.0.2
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/.gitignore +14 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/Rakefile +17 -0
- data/Readme.md +1 -0
- data/lib/ext/numeric.rb +5 -0
- data/lib/ext/time.rb +35 -0
- data/lib/time_sentence.rb +4 -0
- data/lib/time_sentence/version.rb +3 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/time_sentence/sentence_spec.rb +39 -0
- data/time_sentence.gemspec +27 -0
- metadata +125 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake'
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
|
5
|
+
namespace :spec do
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new(:normal) do |t|
|
8
|
+
t.pattern ='spec/**/*_spec.rb'
|
9
|
+
t.rcov = false
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "RSpec tests"
|
15
|
+
task "spec" => "spec:normal"
|
16
|
+
|
17
|
+
task "default" => "spec"
|
data/Readme.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
= Time Sentence =
|
data/lib/ext/numeric.rb
ADDED
data/lib/ext/time.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
class Time
|
2
|
+
def self.sentence seconds, specificity = 3
|
3
|
+
|
4
|
+
return 'now' if seconds == 0
|
5
|
+
|
6
|
+
ago = seconds < 0
|
7
|
+
|
8
|
+
time = [
|
9
|
+
{ value: seconds.abs, unit: 'second', conversion: 60 },
|
10
|
+
{ value: 0, unit: 'minute', conversion: 60 },
|
11
|
+
{ value: 0, unit: 'hour', conversion: 24 },
|
12
|
+
{ value: 0, unit: 'day', conversion: 7 },
|
13
|
+
{ value: 0, unit: 'week', conversion: 4 },
|
14
|
+
{ value: 0, unit: 'month', conversion: 12 },
|
15
|
+
{ value: 0, unit: 'year', conversion: 10 },
|
16
|
+
{ value: 0, unit: 'decade', conversion: 100 },
|
17
|
+
{ value: 0, unit: 'millennium', conversion: nil },
|
18
|
+
]
|
19
|
+
|
20
|
+
time.each_cons(2) do |this_time, next_time|
|
21
|
+
next_time[:value], this_time[:value] = this_time[:value].divmod this_time[:conversion]
|
22
|
+
end
|
23
|
+
|
24
|
+
time = time.delete_if { |t| t[:value] == 0 }
|
25
|
+
|
26
|
+
out = []
|
27
|
+
time.reverse.each do |t|
|
28
|
+
out << "#{t[:value]} #{t[:unit].pluralize t[:value]}"
|
29
|
+
break if out.count == specificity
|
30
|
+
end
|
31
|
+
|
32
|
+
out.join(', ') + (ago ? ' ago' : '')
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Time.sentence' do
|
4
|
+
|
5
|
+
it "should accept a positive integer" do
|
6
|
+
Time.sentence(100000000).should eql "3 years, 5 months, 1 week"
|
7
|
+
100000000.time_sentence.should eql "3 years, 5 months, 1 week"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should accept a negative integer" do
|
11
|
+
Time.sentence(-3600).should eql "1 hour ago"
|
12
|
+
-3600.time_sentence.should eql "1 hour ago"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should accept 0" do
|
16
|
+
Time.sentence(0).should eql "now"
|
17
|
+
0.time_sentence.should eql "now"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should accept a very large number" do
|
21
|
+
Time.sentence(100000000000000000000000).should be_kind_of String
|
22
|
+
100000000000000000000000.time_sentence.should be_kind_of String
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should accept specificity" do
|
26
|
+
(1..10).each do |specificity|
|
27
|
+
Time.sentence(123456789123456789, specificity).should be_kind_of String
|
28
|
+
123456789123456789.time_sentence(specificity).should be_kind_of String
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should ignore a specificity that is out of range" do
|
33
|
+
Time.sentence(123456789123456789, -5).should be_kind_of String
|
34
|
+
123456789123456789.time_sentence(-5).should be_kind_of String
|
35
|
+
Time.sentence(123456789123456789, 20).should be_kind_of String
|
36
|
+
123456789123456789.time_sentence(20).should be_kind_of String
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "time_sentence/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "time_sentence"
|
7
|
+
s.version = TimeSentence::VERSION
|
8
|
+
s.authors = ["Pete Michaud"]
|
9
|
+
s.email = ["me@petermichaud.com"]
|
10
|
+
s.homepage = "http://github.com/PeteMichaud/time-sentence"
|
11
|
+
s.summary = "Translates Seconds into Human Readable Sentences"
|
12
|
+
s.description = "Translates Seconds into Human Readable Sentences"
|
13
|
+
s.rubyforge_project = "time_sentence"
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["config", "lib"]
|
18
|
+
|
19
|
+
#s.add_dependency 'activesupport', '~> 3.2'
|
20
|
+
#s.add_dependency 'actionpack', '~> 3.2'
|
21
|
+
|
22
|
+
s.add_development_dependency 'rake', '~> 0.9.2'
|
23
|
+
s.add_development_dependency 'rspec', '~> 2.10'
|
24
|
+
s.add_development_dependency 'yard'
|
25
|
+
s.add_development_dependency 'minitest-rails', '~> 0.2'
|
26
|
+
s.add_development_dependency 'minitest', '~> 3.0' if RUBY_PLATFORM == "java"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time_sentence
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Pete Michaud
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.9.2
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.9.2
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.10'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.10'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: yard
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: minitest-rails
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.2'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0.2'
|
78
|
+
description: Translates Seconds into Human Readable Sentences
|
79
|
+
email:
|
80
|
+
- me@petermichaud.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- Gemfile
|
88
|
+
- Rakefile
|
89
|
+
- Readme.md
|
90
|
+
- lib/ext/numeric.rb
|
91
|
+
- lib/ext/time.rb
|
92
|
+
- lib/time_sentence.rb
|
93
|
+
- lib/time_sentence/version.rb
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
- spec/time_sentence/sentence_spec.rb
|
96
|
+
- time_sentence.gemspec
|
97
|
+
homepage: http://github.com/PeteMichaud/time-sentence
|
98
|
+
licenses: []
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- config
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project: time_sentence
|
118
|
+
rubygems_version: 1.8.24
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Translates Seconds into Human Readable Sentences
|
122
|
+
test_files:
|
123
|
+
- spec/spec_helper.rb
|
124
|
+
- spec/time_sentence/sentence_spec.rb
|
125
|
+
has_rdoc:
|