chronic_duration 0.10.2 → 0.10.3

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db9c9caba8412e38577177bed6d7a36e21a5ca86
4
+ data.tar.gz: b0d3a2a4f52825ef58698c314d5e33ca3a2cb145
5
+ SHA512:
6
+ metadata.gz: 8b9ac709e3c106593f91f0272b48b3a593181e821b1207f05c157afe4cafbd14fda60dbe1e7a5f8d24ffd9f155f61cac9b29622d49a39c6936b4af41f2180f87
7
+ data.tar.gz: e1e8bb348ca38af8cbe0c5f7f94cb5094b1839d3456b4da0f071612d5cf2ea620ffdc9b231e3558db5031db996c7ed13ceeab620671c84e5cec4195a7ff0155e
data/README.md CHANGED
@@ -65,7 +65,7 @@ Also looking for additional maintainers.
65
65
 
66
66
  ## Contributors
67
67
 
68
- pdf, brianjlandau, jduff, olauzon, roboman, ianlevesque
68
+ errm,pdf, brianjlandau, jduff, olauzon, roboman, ianlevesque
69
69
 
70
70
  ## TODO
71
71
 
@@ -8,6 +8,8 @@ module ChronicDuration
8
8
  end
9
9
 
10
10
  @@raise_exceptions = false
11
+ @@hours_per_day = 24
12
+ @@days_per_week = 7
11
13
 
12
14
  def self.raise_exceptions
13
15
  !!@@raise_exceptions
@@ -17,6 +19,22 @@ module ChronicDuration
17
19
  @@raise_exceptions = !!value
18
20
  end
19
21
 
22
+ def self.hours_per_day
23
+ @@hours_per_day
24
+ end
25
+
26
+ def self.hours_per_day=(value)
27
+ @@hours_per_day = value
28
+ end
29
+
30
+ def self.days_per_week
31
+ @@days_per_week
32
+ end
33
+
34
+ def self.days_per_week=(value)
35
+ @@days_per_week = value
36
+ end
37
+
20
38
  # Given a string representation of elapsed time,
21
39
  # return an integer (or float, if fractions of a
22
40
  # second are input)
@@ -38,19 +56,32 @@ module ChronicDuration
38
56
 
39
57
  decimal_places = seconds.to_s.split('.').last.length if seconds.is_a?(Float)
40
58
 
41
- if seconds >= 60
59
+ minute = 60
60
+ hour = 60 * minute
61
+ day = ChronicDuration.hours_per_day * hour
62
+ month = 30 * day
63
+ year = 31557600
64
+
65
+ if seconds >= 31557600 && seconds%year < seconds%month
66
+ years = seconds / year
67
+ months = seconds % year / month
68
+ days = seconds % year % month / day
69
+ hours = seconds % year % month % day / hour
70
+ minutes = seconds % year % month % day % hour / minute
71
+ seconds = seconds % year % month % day % hour % minute
72
+ elsif seconds >= 60
42
73
  minutes = (seconds / 60).to_i
43
74
  seconds = seconds % 60
44
75
  if minutes >= 60
45
76
  hours = (minutes / 60).to_i
46
77
  minutes = (minutes % 60).to_i
47
- if hours >= 24
48
- days = (hours / 24).to_i
49
- hours = (hours % 24).to_i
78
+ if hours >= ChronicDuration.hours_per_day
79
+ days = (hours / ChronicDuration.hours_per_day).to_i
80
+ hours = (hours % ChronicDuration.hours_per_day).to_i
50
81
  if opts[:weeks]
51
- if days >= 7
52
- weeks = (days / 7).to_i
53
- days = (days % 7).to_i
82
+ if days >= ChronicDuration.days_per_week
83
+ weeks = (days / ChronicDuration.days_per_week).to_i
84
+ days = (days % ChronicDuration.days_per_week).to_i
54
85
  if weeks >= 4
55
86
  months = (weeks / 4).to_i
56
87
  weeks = (weeks % 4).to_i
@@ -62,11 +93,6 @@ module ChronicDuration
62
93
  days = (days % 30).to_i
63
94
  end
64
95
  end
65
- if months >= 12
66
- years = (months / 12).to_i
67
- months = (months % 12).to_i
68
- days = days - (5 * years)
69
- end
70
96
  end
71
97
  end
72
98
  end
@@ -166,20 +192,16 @@ private
166
192
  def duration_units_seconds_multiplier(unit)
167
193
  return 0 unless duration_units_list.include?(unit)
168
194
  case unit
169
- when 'years'; 31536000 # doesn't accounts for leap years
170
- when 'months'; 3600 * 24 * 30
171
- when 'weeks'; 3600 * 24 * 7
172
- when 'days'; 3600 * 24
195
+ when 'years'; 31557600
196
+ when 'months'; 3600 * ChronicDuration.hours_per_day * 30
197
+ when 'weeks'; 3600 * ChronicDuration.hours_per_day * ChronicDuration.days_per_week
198
+ when 'days'; 3600 * ChronicDuration.hours_per_day
173
199
  when 'hours'; 3600
174
200
  when 'minutes'; 60
175
201
  when 'seconds'; 1
176
202
  end
177
203
  end
178
204
 
179
- def error_message
180
- 'Sorry, that duration could not be parsed'
181
- end
182
-
183
205
  # Parse 3:41:59 and return 3 hours 41 minutes 59 seconds
184
206
  def filter_by_type(string)
185
207
  chrono_units_list = duration_units_list.reject {|v| v == "weeks"}
@@ -244,6 +266,8 @@ private
244
266
  'd' => 'days',
245
267
  'weeks' => 'weeks',
246
268
  'week' => 'weeks',
269
+ 'wks' => 'weeks',
270
+ 'wk' => 'weeks',
247
271
  'w' => 'weeks',
248
272
  'months' => 'months',
249
273
  'mo' => 'months',
@@ -260,9 +284,4 @@ private
260
284
  def join_words
261
285
  ['and', 'with', 'plus']
262
286
  end
263
-
264
- def white_list
265
- self.mappings.keys
266
- end
267
-
268
287
  end
@@ -1,3 +1,3 @@
1
1
  module ChronicDuration
2
- VERSION = '0.10.2'
2
+ VERSION = '0.10.3'
3
3
  end
@@ -14,9 +14,9 @@ describe ChronicDuration do
14
14
  '2 hrs 20 min' => 2 * 3600 + 20 * 60,
15
15
  '2h20min' => 2 * 3600 + 20 * 60,
16
16
  '6 mos 1 day' => 6 * 30 * 24 * 3600 + 24 * 3600,
17
- '1 year 6 mos 1 day' => 1 * 31536000 + 6 * 30 * 24 * 3600 + 24 * 3600,
17
+ '1 year 6 mos 1 day' => 1 * 31557600 + 6 * 30 * 24 * 3600 + 24 * 3600,
18
18
  '2.5 hrs' => 2.5 * 3600,
19
- '47 yrs 6 mos and 4.5d' => 47 * 31536000 + 6 * 30 * 24 * 3600 + 4.5 * 24 * 3600,
19
+ '47 yrs 6 mos and 4.5d' => 47 * 31557600 + 6 * 30 * 24 * 3600 + 4.5 * 24 * 3600,
20
20
  'two hours and twenty minutes' => 2 * 3600 + 20 * 60,
21
21
  'four hours and forty minutes' => 4 * 3600 + 40 * 60,
22
22
  'four hours, and fourty minutes' => 4 * 3600 + 40 * 60,
@@ -24,7 +24,11 @@ describe ChronicDuration do
24
24
  '3 weeks, plus 2 days' => 3600 * 24 * 7 * 3 + 3600 * 24 * 2,
25
25
  '3 weeks with 2 days' => 3600 * 24 * 7 * 3 + 3600 * 24 * 2,
26
26
  '1 month' => 3600 * 24 * 30,
27
- '2 months' => 3600 * 24 * 30 * 2
27
+ '2 months' => 3600 * 24 * 30 * 2,
28
+ '18 months' => 3600 * 24 * 30 * 18,
29
+ '1 year 6 months' => (3600 * 24 * (365.25 + 6 * 30)).to_i,
30
+ 'day' => 3600 * 24,
31
+ 'minute 30s' => 90
28
32
  }
29
33
 
30
34
  context "when string can't be parsed" do
@@ -33,11 +37,15 @@ describe ChronicDuration do
33
37
  ChronicDuration.parse('gobblygoo').should be_nil
34
38
  end
35
39
 
40
+ it "cannot parse zero" do
41
+ ChronicDuration.parse('0').should be_nil
42
+ end
43
+
36
44
  context "when @@raise_exceptions set to true" do
37
45
 
38
46
  it "raises with ChronicDuration::DurationParseError" do
39
47
  ChronicDuration.raise_exceptions = true
40
- lambda { ChronicDuration.parse('23 gobblygoos') }.should raise_exception(ChronicDuration::DurationParseError)
48
+ expect { ChronicDuration.parse('23 gobblygoos') }.to raise_error(ChronicDuration::DurationParseError)
41
49
  ChronicDuration.raise_exceptions = false
42
50
  end
43
51
 
@@ -45,6 +53,10 @@ describe ChronicDuration do
45
53
 
46
54
  end
47
55
 
56
+ it "should return zero if the string parses as zero and the keep_zero option is true" do
57
+ ChronicDuration.parse('0', :keep_zero => true).should == 0
58
+ end
59
+
48
60
  it "should return a float if seconds are in decimals" do
49
61
  ChronicDuration.parse('12 mins 3.141 seconds').is_a?(Float).should be_true
50
62
  end
@@ -124,7 +136,7 @@ describe ChronicDuration do
124
136
  :long => '6 months 1 day',
125
137
  :chrono => '6:01:00:00:00' # Yuck. FIXME
126
138
  },
127
- (365 * 24 * 3600 + 24 * 3600 ) =>
139
+ (365.25 * 24 * 3600 + 24 * 3600 ).to_i =>
128
140
  {
129
141
  :micro => '1y1d',
130
142
  :short => '1y 1d',
@@ -132,7 +144,7 @@ describe ChronicDuration do
132
144
  :long => '1 year 1 day',
133
145
  :chrono => '1:00:01:00:00:00'
134
146
  },
135
- (3 * 365 * 24 * 3600 + 24 * 3600 ) =>
147
+ (3 * 365.25 * 24 * 3600 + 24 * 3600 ).to_i =>
136
148
  {
137
149
  :micro => '3y1d',
138
150
  :short => '3y 1d',
@@ -140,6 +152,14 @@ describe ChronicDuration do
140
152
  :long => '3 years 1 day',
141
153
  :chrono => '3:00:01:00:00:00'
142
154
  },
155
+ (3600 * 24 * 30 * 18) =>
156
+ {
157
+ :micro => '18mo',
158
+ :short => '18mo',
159
+ :default => '18 mos',
160
+ :long => '18 months',
161
+ :chrono => '18:00:00:00:00'
162
+ }
143
163
  }
144
164
 
145
165
  @exemplars.each do |k, v|
@@ -150,8 +170,35 @@ describe ChronicDuration do
150
170
  end
151
171
  end
152
172
 
173
+ @keep_zero_exemplars = {
174
+ (true) =>
175
+ {
176
+ :micro => '0s',
177
+ :short => '0s',
178
+ :default => '0 secs',
179
+ :long => '0 seconds',
180
+ :chrono => '0'
181
+ },
182
+ (false) =>
183
+ {
184
+ :micro => nil,
185
+ :short => nil,
186
+ :default => nil,
187
+ :long => nil,
188
+ :chrono => '0'
189
+ },
190
+ }
191
+
192
+ @keep_zero_exemplars.each do |k, v|
193
+ v.each do |key, val|
194
+ it "should properly output a duration of 0 seconds as #{val.nil? ? "nil" : val} using the #{key.to_s} format option, if the keep_zero option is #{k.to_s}" do
195
+ ChronicDuration.output(0, :format => key, :keep_zero => k).should == val
196
+ end
197
+ end
198
+ end
199
+
153
200
  it "returns weeks when needed" do
154
- ChronicDuration.output(15*24*60*60, :weeks => true).should =~ /.*wk.*/
201
+ ChronicDuration.output(45*24*60*60, :weeks => true).should =~ /.*wk.*/
155
202
  end
156
203
 
157
204
  it "returns the specified number of units if provided" do
@@ -209,4 +256,21 @@ describe ChronicDuration do
209
256
 
210
257
  end
211
258
 
259
+ describe "work week" do
260
+ before(:all) do
261
+ ChronicDuration.hours_per_day = 8
262
+ ChronicDuration.days_per_week = 5
263
+ end
264
+
265
+ after(:all) do
266
+ ChronicDuration.hours_per_day = 24
267
+ ChronicDuration.days_per_week = 7
268
+ end
269
+
270
+ it "should parse knowing the work week" do
271
+ week = ChronicDuration.parse('5d')
272
+ ChronicDuration.parse('40h').should == week
273
+ ChronicDuration.parse('1w').should == week
274
+ end
275
+ end
212
276
  end
metadata CHANGED
@@ -1,62 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chronic_duration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.2
5
- prerelease:
4
+ version: 0.10.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - hpoydar
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-24 00:00:00.000000000 Z
11
+ date: 2014-02-24 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: numerizer
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: 0.1.1
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: 0.1.1
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
33
  version: 10.0.3
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
40
  version: 10.0.3
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ~>
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
47
  version: 2.12.0
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ~>
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
54
  version: 2.12.0
62
55
  description: A simple Ruby natural language parser for elapsed time. (For example,
@@ -70,8 +63,8 @@ executables: []
70
63
  extensions: []
71
64
  extra_rdoc_files: []
72
65
  files:
73
- - .gitignore
74
- - .travis.yml
66
+ - ".gitignore"
67
+ - ".travis.yml"
75
68
  - Gemfile
76
69
  - MIT-LICENSE
77
70
  - README.md
@@ -79,34 +72,31 @@ files:
79
72
  - chronic_duration.gemspec
80
73
  - lib/chronic_duration.rb
81
74
  - lib/chronic_duration/version.rb
82
- - spec/chronic_duration_spec.rb
83
75
  - spec/lib/chronic_duration_spec.rb
84
76
  - spec/spec_helper.rb
85
77
  homepage: https://github.com/hpoydar/chronic_duration
86
78
  licenses: []
79
+ metadata: {}
87
80
  post_install_message:
88
81
  rdoc_options: []
89
82
  require_paths:
90
83
  - lib
91
84
  required_ruby_version: !ruby/object:Gem::Requirement
92
- none: false
93
85
  requirements:
94
- - - ! '>='
86
+ - - ">="
95
87
  - !ruby/object:Gem::Version
96
88
  version: '0'
97
89
  required_rubygems_version: !ruby/object:Gem::Requirement
98
- none: false
99
90
  requirements:
100
- - - ! '>='
91
+ - - ">="
101
92
  - !ruby/object:Gem::Version
102
93
  version: '0'
103
94
  requirements: []
104
95
  rubyforge_project:
105
- rubygems_version: 1.8.23
96
+ rubygems_version: 2.2.0
106
97
  signing_key:
107
- specification_version: 3
98
+ specification_version: 4
108
99
  summary: A simple Ruby natural language parser for elapsed time
109
100
  test_files:
110
- - spec/chronic_duration_spec.rb
111
101
  - spec/lib/chronic_duration_spec.rb
112
102
  - spec/spec_helper.rb
@@ -1,300 +0,0 @@
1
- require 'chronic_duration'
2
-
3
- describe ChronicDuration, '.parse' do
4
-
5
- @exemplars = {
6
- '1:20' => 60 + 20,
7
- '1:20.51' => 60 + 20.51,
8
- '4:01:01' => 4 * 3600 + 60 + 1,
9
- '3 mins 4 sec' => 3 * 60 + 4,
10
- '3 Mins 4 Sec' => 3 * 60 + 4,
11
- 'three mins four sec' => 3 * 60 + 4,
12
- '2 hrs 20 min' => 2 * 3600 + 20 * 60,
13
- '2h20min' => 2 * 3600 + 20 * 60,
14
- '6 mos 1 day' => 6 * 30 * 24 * 3600 + 24 * 3600,
15
- '1 year 6 mos 1 day' => 1 * 31536000 + 6 * 30 * 24 * 3600 + 24 * 3600,
16
- '2.5 hrs' => 2.5 * 3600,
17
- '47 yrs 6 mos and 4.5d' => 47 * 31536000 + 6 * 30 * 24 * 3600 + 4.5 * 24 * 3600,
18
- 'two hours and twenty minutes' => 2 * 3600 + 20 * 60,
19
- 'four hours and forty minutes' => 4 * 3600 + 40 * 60,
20
- 'four hours, and fourty minutes' => 4 * 3600 + 40 * 60,
21
- '3 weeks and, 2 days' => 3600 * 24 * 7 * 3 + 3600 * 24 * 2,
22
- '3 weeks, plus 2 days' => 3600 * 24 * 7 * 3 + 3600 * 24 * 2,
23
- '3 weeks with 2 days' => 3600 * 24 * 7 * 3 + 3600 * 24 * 2,
24
- '1 month' => 3600 * 24 * 30,
25
- '2 months' => 3600 * 24 * 30 * 2,
26
- 'day' => 3600 * 24,
27
- 'minute 30s' => 90
28
- }
29
-
30
- it "should return nil if the string can't be parsed" do
31
- ChronicDuration.parse('gobblygoo').should be_nil
32
- end
33
-
34
- it "should return nil if the string parses as zero" do
35
- ChronicDuration.parse('0').should be_nil
36
- end
37
-
38
- it "should return zero if the string parses as zero and the keep_zero option is true" do
39
- ChronicDuration.parse('0', :keep_zero => true).should == 0
40
- end
41
-
42
- it "should raise an exception if the string can't be parsed and @@raise_exceptions is set to true" do
43
- ChronicDuration.raise_exceptions = true
44
- lambda { ChronicDuration.parse('23 gobblygoos') }.should raise_exception(ChronicDuration::DurationParseError)
45
- ChronicDuration.raise_exceptions = false
46
- end
47
-
48
- it "should return a float if seconds are in decimals" do
49
- ChronicDuration.parse('12 mins 3.141 seconds').is_a?(Float).should be_true
50
- end
51
-
52
- it "should return an integer unless the seconds are in decimals" do
53
- ChronicDuration.parse('12 mins 3 seconds').is_a?(Integer).should be_true
54
- end
55
-
56
- it "should be able to parse minutes by default" do
57
- ChronicDuration.parse('5', :default_unit => "minutes").should == 300
58
- end
59
-
60
- @exemplars.each do |k, v|
61
- it "should properly parse a duration like #{k}" do
62
- ChronicDuration.parse(k).should == v
63
- end
64
- end
65
-
66
- end
67
-
68
- describe ChronicDuration, '.output' do
69
-
70
- it "should return nil if the input can't be parsed" do
71
- ChronicDuration.parse('gobblygoo').should be_nil
72
- end
73
-
74
- @exemplars = {
75
- #(0) =>
76
- #{
77
- #:micro => '0s',
78
- #:short => '0s',
79
- #:default => '0 secs',
80
- #:long => '0 seconds',
81
- #:chrono => '0'
82
- #},
83
- (60 + 20) =>
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 + 0.0) =>
92
- {
93
- :micro => '1m20s',
94
- :short => '1m 20s',
95
- :default => '1 min 20 secs',
96
- :long => '1 minute 20 seconds',
97
- :chrono => '1:20'
98
- },
99
- (60 + 20.01) =>
100
- {
101
- :micro => '1m20.01s',
102
- :short => '1m 20.01s',
103
- :default => '1 min 20.01 secs',
104
- :long => '1 minute 20.01 seconds',
105
- :chrono => '1:20.01'
106
- },
107
- (60 + 20.1) =>
108
- {
109
- :micro => '1m20.1s',
110
- :short => '1m 20.1s',
111
- :default => '1 min 20.1 secs',
112
- :long => '1 minute 20.1 seconds',
113
- :chrono => '1:20.1'
114
- },
115
- (60 + 0.1) =>
116
- {
117
- :micro => '1m0.1s',
118
- :short => '1m 0.1s',
119
- :default => '1 min 0.1 secs',
120
- :long => '1 minute 0.1 seconds',
121
- :chrono => '1:00.1'
122
- },
123
- (60*60) =>
124
- {
125
- :micro => '1h',
126
- :short => '1h',
127
- :default => '1 hr',
128
- :long => '1 hour',
129
- :chrono => '1:00:00'
130
- },
131
- (60*60 + 0.1) =>
132
- {
133
- :micro => '1h0.1s',
134
- :short => '1h 0.1s',
135
- :default => '1 hr 0.1 secs',
136
- :long => '1 hour 0.1 seconds',
137
- :chrono => '1:00:00.1'
138
- },
139
- (60 + 20.51) =>
140
- {
141
- :micro => '1m20.51s',
142
- :short => '1m 20.51s',
143
- :default => '1 min 20.51 secs',
144
- :long => '1 minute 20.51 seconds',
145
- :chrono => '1:20.51'
146
- },
147
- (60 + 20.51928) =>
148
- {
149
- :micro => '1m20.51928s',
150
- :short => '1m 20.51928s',
151
- :default => '1 min 20.51928 secs',
152
- :long => '1 minute 20.51928 seconds',
153
- :chrono => '1:20.51928'
154
- },
155
- (4 * 3600 + 60 + 1) =>
156
- {
157
- :micro => '4h1m1s',
158
- :short => '4h 1m 1s',
159
- :default => '4 hrs 1 min 1 sec',
160
- :long => '4 hours 1 minute 1 second',
161
- :chrono => '4:01:01'
162
- },
163
- (2 * 3600 + 20 * 60) =>
164
- {
165
- :micro => '2h20m',
166
- :short => '2h 20m',
167
- :default => '2 hrs 20 mins',
168
- :long => '2 hours 20 minutes',
169
- :chrono => '2:20'
170
- },
171
- (2 * 3600 + 20 * 60) =>
172
- {
173
- :micro => '2h20m',
174
- :short => '2h 20m',
175
- :default => '2 hrs 20 mins',
176
- :long => '2 hours 20 minutes',
177
- :chrono => '2:20:00'
178
- },
179
- (6 * 30 * 24 * 3600 + 24 * 3600) =>
180
- {
181
- :micro => '6mo1d',
182
- :short => '6mo 1d',
183
- :default => '6 mos 1 day',
184
- :long => '6 months 1 day',
185
- :chrono => '6:01:00:00:00' # Yuck. FIXME
186
- },
187
- (365 * 24 * 3600 + 24 * 3600 ) =>
188
- {
189
- :micro => '1y1d',
190
- :short => '1y 1d',
191
- :default => '1 yr 1 day',
192
- :long => '1 year 1 day',
193
- :chrono => '1:00:01:00:00:00'
194
- },
195
- (3 * 365 * 24 * 3600 + 24 * 3600 ) =>
196
- {
197
- :micro => '3y1d',
198
- :short => '3y 1d',
199
- :default => '3 yrs 1 day',
200
- :long => '3 years 1 day',
201
- :chrono => '3:00:01:00:00:00'
202
- },
203
- }
204
-
205
- @exemplars.each do |k, v|
206
- v.each do |key, val|
207
- it "should properly output a duration of #{k} seconds as #{val} using the #{key.to_s} format option" do
208
- ChronicDuration.output(k, :format => key).should == val
209
- end
210
- end
211
- end
212
-
213
- @keep_zero_exemplars = {
214
- (true) =>
215
- {
216
- :micro => '0s',
217
- :short => '0s',
218
- :default => '0 secs',
219
- :long => '0 seconds',
220
- :chrono => '0'
221
- },
222
- (false) =>
223
- {
224
- :micro => nil,
225
- :short => nil,
226
- :default => nil,
227
- :long => nil,
228
- :chrono => '0'
229
- },
230
- }
231
-
232
- @keep_zero_exemplars.each do |k, v|
233
- v.each do |key, val|
234
- it "should properly output a duration of 0 seconds as #{val.nil? ? "nil" : val} using the #{key.to_s} format option, if the keep_zero option is #{k.to_s}" do
235
- ChronicDuration.output(0, :format => key, :keep_zero => k).should == val
236
- end
237
- end
238
- end
239
-
240
- it "should show weeks when needed" do
241
- ChronicDuration.output(15*24*60*60, :weeks => true).should =~ /.*wk.*/
242
- end
243
-
244
- it "should show the specified number of units if provided" do
245
- ChronicDuration.output(4 * 3600 + 60 + 1, units: 2).should == '4 hrs 1 min'
246
- ChronicDuration.output(6 * 30 * 24 * 3600 + 24 * 3600 + 3600 + 60 + 1, units: 3, format: :long).should == '6 months 1 day 1 hour'
247
- end
248
-
249
- it "should use the default format when the format is not specified" do
250
- ChronicDuration.output(2 * 3600 + 20 * 60).should == '2 hrs 20 mins'
251
- end
252
-
253
- @exemplars.each do |seconds,format_spec|
254
- format_spec.each do |format,_|
255
- it "it should properly output a duration for #{seconds} that parses back to the same thing when using the #{format.to_s} format" do
256
- ChronicDuration.parse(ChronicDuration.output(seconds, :format => format)).should == seconds
257
- end
258
- end
259
- end
260
- end
261
-
262
-
263
- # Some of the private methods deserve some spec'ing to aid
264
- # us in development...
265
-
266
- describe ChronicDuration, "private methods" do
267
-
268
- describe ".filter_by_type" do
269
-
270
- it "should take a chrono-formatted time like 3:14 and return a human time like 3 minutes 14 seconds" do
271
- ChronicDuration.instance_eval("filter_by_type('3:14')").should == '3 minutes 14 seconds'
272
- end
273
-
274
- it "should take a chrono-formatted time like 12:10:14 and return a human time like 12 hours 10 minutes 14 seconds" do
275
- ChronicDuration.instance_eval("filter_by_type('12:10:14')").should == '12 hours 10 minutes 14 seconds'
276
- end
277
-
278
- it "should return the input if it's not a chrono-formatted time" do
279
- ChronicDuration.instance_eval("filter_by_type('4 hours')").should == '4 hours'
280
- end
281
-
282
- end
283
-
284
- describe ".cleanup" do
285
-
286
- it "should clean up extraneous words" do
287
- ChronicDuration.instance_eval("cleanup('4 days and 11 hours')").should == '4 days 11 hours'
288
- end
289
-
290
- it "should cleanup extraneous spaces" do
291
- ChronicDuration.instance_eval("cleanup(' 4 days and 11 hours')").should == '4 days 11 hours'
292
- end
293
-
294
- it "should insert spaces where there aren't any" do
295
- ChronicDuration.instance_eval("cleanup('4m11.5s')").should == '4 minutes 11.5 seconds'
296
- end
297
-
298
- end
299
-
300
- end