chronic_duration 0.10.0 → 0.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +2 -0
- data/README.md +6 -0
- data/lib/chronic_duration.rb +7 -1
- data/lib/chronic_duration/version.rb +1 -1
- data/spec/chronic_duration_spec.rb +48 -0
- metadata +2 -1
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/hpoydar/chronic_duration.png?branch=master)](https://travis-ci.org/hpoydar/chronic_duration)
|
2
|
+
|
1
3
|
# Chronic Duration
|
2
4
|
|
3
5
|
A simple Ruby natural language parser for elapsed time. (For example, 4 hours and 30 minutes, 6 minutes 4 seconds, 3 days, etc.) Returns all results in seconds. Will return an integer unless you get tricky and need a float. (4 minutes and 13.47 seconds, for example.)
|
@@ -48,6 +50,10 @@ ChronicDuration.raise_exceptions can be set to true to raise exceptions when the
|
|
48
50
|
>> ChronicDuration.parse('4 elephants and 3 Astroids')
|
49
51
|
ChronicDuration::DurationParseError: An invalid word "elephants" was used in the string to be parsed.
|
50
52
|
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
Fork and pull request after your specs are green. Add your handle to the list below.
|
56
|
+
Also looking for additional maintainers.
|
51
57
|
|
52
58
|
## Contributors
|
53
59
|
|
data/lib/chronic_duration.rb
CHANGED
@@ -28,6 +28,8 @@ module ChronicDuration
|
|
28
28
|
# Given an integer and an optional format,
|
29
29
|
# returns a formatted string representing elapsed time
|
30
30
|
def output(seconds, opts = {})
|
31
|
+
int = seconds.to_i
|
32
|
+
seconds = int if seconds - int == 0 # if seconds end with .0
|
31
33
|
|
32
34
|
opts[:format] ||= :default
|
33
35
|
|
@@ -95,7 +97,11 @@ module ChronicDuration
|
|
95
97
|
# Get rid of lead off times if they are zero
|
96
98
|
# Get rid of lead off zero
|
97
99
|
# Get rid of trailing :
|
98
|
-
|
100
|
+
divider = ':'
|
101
|
+
str.split(divider).map { |n|
|
102
|
+
# add zeros only if n is an integer
|
103
|
+
n.include?('.') ? ("%04.#{decimal_places}f" % n) : ("%02d" % n)
|
104
|
+
}.join(divider).gsub(/^(00:)+/, '').gsub(/^0/, '').gsub(/:$/, '')
|
99
105
|
end
|
100
106
|
joiner = ''
|
101
107
|
end
|
@@ -80,6 +80,54 @@ describe ChronicDuration, '.output' do
|
|
80
80
|
:long => '1 minute 20 seconds',
|
81
81
|
:chrono => '1:20'
|
82
82
|
},
|
83
|
+
(60 + 20 + 0.0) =>
|
84
|
+
{
|
85
|
+
:micro => '1m20s',
|
86
|
+
:short => '1m 20s',
|
87
|
+
:default => '1 min 20 secs',
|
88
|
+
:long => '1 minute 20 seconds',
|
89
|
+
:chrono => '1:20'
|
90
|
+
},
|
91
|
+
(60 + 20.01) =>
|
92
|
+
{
|
93
|
+
:micro => '1m20.01s',
|
94
|
+
:short => '1m 20.01s',
|
95
|
+
:default => '1 min 20.01 secs',
|
96
|
+
:long => '1 minute 20.01 seconds',
|
97
|
+
:chrono => '1:20.01'
|
98
|
+
},
|
99
|
+
(60 + 20.1) =>
|
100
|
+
{
|
101
|
+
:micro => '1m20.1s',
|
102
|
+
:short => '1m 20.1s',
|
103
|
+
:default => '1 min 20.1 secs',
|
104
|
+
:long => '1 minute 20.1 seconds',
|
105
|
+
:chrono => '1:20.1'
|
106
|
+
},
|
107
|
+
(60 + 0.1) =>
|
108
|
+
{
|
109
|
+
:micro => '1m0.1s',
|
110
|
+
:short => '1m 0.1s',
|
111
|
+
:default => '1 min 0.1 secs',
|
112
|
+
:long => '1 minute 0.1 seconds',
|
113
|
+
:chrono => '1:00.1'
|
114
|
+
},
|
115
|
+
(60*60) =>
|
116
|
+
{
|
117
|
+
:micro => '1h',
|
118
|
+
:short => '1h',
|
119
|
+
:default => '1 hr',
|
120
|
+
:long => '1 hour',
|
121
|
+
:chrono => '1:00:00'
|
122
|
+
},
|
123
|
+
(60*60 + 0.1) =>
|
124
|
+
{
|
125
|
+
:micro => '1h0.1s',
|
126
|
+
:short => '1h 0.1s',
|
127
|
+
:default => '1 hr 0.1 secs',
|
128
|
+
:long => '1 hour 0.1 seconds',
|
129
|
+
:chrono => '1:00:00.1'
|
130
|
+
},
|
83
131
|
(60 + 20.51) =>
|
84
132
|
{
|
85
133
|
:micro => '1m20.51s',
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chronic_duration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.10.
|
4
|
+
version: 0.10.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -71,6 +71,7 @@ extensions: []
|
|
71
71
|
extra_rdoc_files: []
|
72
72
|
files:
|
73
73
|
- .gitignore
|
74
|
+
- .travis.yml
|
74
75
|
- Gemfile
|
75
76
|
- MIT-LICENSE
|
76
77
|
- README.md
|