time_sentence 0.0.4 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme.md +27 -24
- data/lib/ext/ts_numeric.rb +3 -2
- data/lib/ext/ts_time.rb +5 -5
- data/lib/time_sentence/version.rb +1 -1
- data/spec/time_sentence/sentence_spec.rb +15 -15
- metadata +2 -2
data/Readme.md
CHANGED
@@ -5,46 +5,49 @@ into joy.
|
|
5
5
|
|
6
6
|
## Basic Usage
|
7
7
|
|
8
|
-
You can use the `
|
8
|
+
You can use the `to_time_sentence` method on any number or the `Time.to_sentence` class method to get the same result
|
9
9
|
|
10
10
|
```ruby
|
11
|
-
1.hour.
|
12
|
-
168.hours.
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
Time.
|
18
|
-
Time.
|
19
|
-
Time.
|
11
|
+
1.hour.to_time_sentence #=> "1 hour"
|
12
|
+
168.hours.to_time_sentence #=> "1 week"
|
13
|
+
525600.minutes.to_time_sentence #=> "1 year, 1 month, 1 day"
|
14
|
+
9999.seconds.to_time_sentence #=> "2 hours, 46 minutes, 39 seconds"
|
15
|
+
123456876543.seconds.to_time_sentence #=> "4 millennia, 25 decades, 2 years"
|
16
|
+
|
17
|
+
Time.to_sentence(1.hour) #=> "1 hour"
|
18
|
+
Time.to_sentence(168.hours) #=> "1 week"
|
19
|
+
Time.to_sentence(525600.minutes) #=> "1 year, 1 month, 1 day"
|
20
|
+
Time.to_sentence(9999.seconds) #=> "2 hours, 46 minutes, 39 seconds"
|
21
|
+
Time.to_sentence(123456876543.seconds) #=> "4 millennia, 25 decades, 2 years"
|
20
22
|
```
|
21
23
|
|
22
24
|
## Advanced Usage
|
23
25
|
|
24
26
|
### Specificity
|
25
27
|
|
26
|
-
|
28
|
+
`time_sentence` assumes that you won't want more than 3 terms in your sentence, so it chooses the largest unit it can,
|
27
29
|
then displays the next smaller 2 (that don't equal 0) as well. For example:
|
28
30
|
|
29
31
|
```ruby
|
30
|
-
123456877.seconds.
|
32
|
+
123456877.seconds.to_time_sentence #=> "4 years, 3 months, 21 hours"
|
31
33
|
```
|
32
34
|
|
33
35
|
Normally if you have a number on the scale of years, it doesn't matter how many, say, seconds there are. The number
|
34
|
-
above represents 4 years, 3 months, 0 weeks, 0 days, 21 hours, and probably some number
|
36
|
+
above represents 4 years, 3 months, 0 weeks, 0 days, 21 hours, and probably some number of minutes and seconds.
|
35
37
|
|
36
38
|
If we actually *do* care about those minutes and seconds, we can pass a specificity argument in to make the sentence
|
37
39
|
more (or less specific). For example:
|
38
40
|
|
39
41
|
```ruby
|
40
|
-
123456877.seconds.
|
41
|
-
123456877.seconds.
|
42
|
-
123456877.seconds.
|
43
|
-
123456877.seconds.
|
44
|
-
123456877.seconds.
|
45
|
-
123456877.seconds.
|
46
|
-
|
47
|
-
|
42
|
+
123456877.seconds.to_time_sentence #=> "4 years, 3 months, 21 hours"
|
43
|
+
123456877.seconds.to_time_sentence(1) #=> "4 years"
|
44
|
+
123456877.seconds.to_time_sentence(2) #=> "4 years, 3 months"
|
45
|
+
123456877.seconds.to_time_sentence(3) #=> "4 years, 3 months, 21 hours"
|
46
|
+
123456877.seconds.to_time_sentence(4) #=> "4 years, 3 months, 21 hours, 34 minutes"
|
47
|
+
123456877.seconds.to_time_sentence(5) #=> "4 years, 3 months, 21 hours, 34 minutes, 37 seconds"
|
48
|
+
|
49
|
+
# Works the same way by calling Time.to_sentence
|
50
|
+
Time.to_sentence(123456877.seconds, 5) #=> "4 years, 3 months, 21 hours, 34 minutes, 37 seconds"
|
48
51
|
```
|
49
52
|
|
50
53
|
### Past Times
|
@@ -52,7 +55,7 @@ more (or less specific). For example:
|
|
52
55
|
Passing a negative number into the method will generate a sentence about the past:
|
53
56
|
|
54
57
|
```ruby
|
55
|
-
-123456877.seconds.
|
58
|
+
-123456877.seconds.to_time_sentence(5) #=> "4 years, 3 months, 21 hours, 34 minutes, 37 seconds ago"
|
56
59
|
```
|
57
60
|
|
58
61
|
### Now
|
@@ -60,12 +63,12 @@ Passing a negative number into the method will generate a sentence about the pas
|
|
60
63
|
Passing 0 into the method will simply return "now"
|
61
64
|
|
62
65
|
```ruby
|
63
|
-
0.seconds.
|
66
|
+
0.seconds.to_time_sentence #=> "now"
|
64
67
|
```
|
65
68
|
|
66
69
|
# Contributors
|
67
70
|
|
68
|
-
time_sentence is solely Pete Michaud's (me@petermichaud.com) fault, so blame him for everything.
|
71
|
+
`time_sentence` is solely Pete Michaud's (me@petermichaud.com) fault, so blame him for everything.
|
69
72
|
|
70
73
|
# License
|
71
74
|
|
data/lib/ext/ts_numeric.rb
CHANGED
data/lib/ext/ts_time.rb
CHANGED
@@ -4,17 +4,17 @@ class Time
|
|
4
4
|
#
|
5
5
|
# == Examples
|
6
6
|
#
|
7
|
-
# Time.
|
8
|
-
# Time.
|
9
|
-
# Time.
|
10
|
-
# Time.
|
7
|
+
# Time.to_sentence(1.hour) #=> "1 hour"
|
8
|
+
# Time.to_sentence(168.hours) #=> "1 week"
|
9
|
+
# Time.to_sentence(9999.seconds) #=> "2 hours, 46 minutes, 39 seconds"
|
10
|
+
# Time.to_sentence(123456876543.seconds) #=> "4 millennia, 25 decades, 2 years"
|
11
11
|
#
|
12
12
|
# == Params
|
13
13
|
#
|
14
14
|
# @param [Numeric] Number of seconds to convert to a sentence
|
15
15
|
# @param [Integer] Specificity of the sentence (how many clauses are included)
|
16
16
|
# @return [String] Generated sentence
|
17
|
-
def self.
|
17
|
+
def self.to_sentence seconds, specificity = 3
|
18
18
|
|
19
19
|
return 'now' if seconds == 0
|
20
20
|
|
@@ -1,39 +1,39 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe 'Time.
|
3
|
+
describe 'Time.to_sentence' do
|
4
4
|
|
5
5
|
it "should accept a positive integer" do
|
6
|
-
Time.
|
7
|
-
100000000.
|
6
|
+
Time.to_sentence(100000000).should eql "3 years, 5 months, 1 week"
|
7
|
+
100000000.to_time_sentence.should eql "3 years, 5 months, 1 week"
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should accept a negative integer" do
|
11
|
-
Time.
|
12
|
-
-3600.
|
11
|
+
Time.to_sentence(-3600).should eql "1 hour ago"
|
12
|
+
-3600.to_time_sentence.should eql "1 hour ago"
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should accept 0" do
|
16
|
-
Time.
|
17
|
-
0.
|
16
|
+
Time.to_sentence(0).should eql "now"
|
17
|
+
0.to_time_sentence.should eql "now"
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should accept a very large number" do
|
21
|
-
Time.
|
22
|
-
100000000000000000000000.
|
21
|
+
Time.to_sentence(100000000000000000000000).should be_kind_of String
|
22
|
+
100000000000000000000000.to_time_sentence.should be_kind_of String
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should accept specificity" do
|
26
26
|
(1..10).each do |specificity|
|
27
|
-
Time.
|
28
|
-
123456789123456789.
|
27
|
+
Time.to_sentence(123456789123456789, specificity).should be_kind_of String
|
28
|
+
123456789123456789.to_time_sentence(specificity).should be_kind_of String
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should ignore a specificity that is out of range" do
|
33
|
-
Time.
|
34
|
-
123456789123456789.
|
35
|
-
Time.
|
36
|
-
123456789123456789.
|
33
|
+
Time.to_sentence(123456789123456789, -5).should be_kind_of String
|
34
|
+
123456789123456789.to_time_sentence(-5).should be_kind_of String
|
35
|
+
Time.to_sentence(123456789123456789, 20).should be_kind_of String
|
36
|
+
123456789123456789.to_time_sentence(20).should be_kind_of String
|
37
37
|
end
|
38
38
|
|
39
39
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_sentence
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.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-10-
|
12
|
+
date: 2012-10-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|