epitools 0.5.42 → 0.5.43

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e79467768b0f577f8f5abc4759c803db80c09ef
4
- data.tar.gz: 3f7dd61e9c7e55ebfa7229f96ebc33eb3e3d7b00
3
+ metadata.gz: 18aedb7dadd9b9a8cdfe9b619d228dc250ff2c0b
4
+ data.tar.gz: 41feba1a86b88e3f0790151ef815e5f1c1ebf680
5
5
  SHA512:
6
- metadata.gz: 062d1d28880dc3912788142ec6d9b8e83be4690bb371940c82b68fd8c71986a3b9a14476f2e204b1370e30c42ba82985ea5771b32e97ad10a1a767b8474ec734
7
- data.tar.gz: 09e27bd437b0dc4a7a1ae64a6c87972251aa10def9b2e34d2a5283e885a95a058a3d7afe711c16db90068a6e98f7d7ea9826a535455763f1f9a70f479ac6343f
6
+ metadata.gz: abf447f70c70e0b79f3b949a1faca4569f45325244dce43fffd1c139d94ff31c03151aec3368a6b18b8e528dec558572db31bc0385a993589297063fd7f6bf4f
7
+ data.tar.gz: 55c84b31bb1f3182ea56354649036df86add2c3d72c4a80aba726f8db7ee4ad01692fb523dd80d353fe3a0d10c0652f8b5a941f64f22cc642ead7a567a32e439
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.42
1
+ 0.5.43
@@ -168,6 +168,25 @@ class Numeric
168
168
 
169
169
  alias_method :human_size, :human_bytes
170
170
 
171
+ #
172
+ # Convert seconds to hours:minutes:seconds (hours is dropped if it's zero)
173
+ #
174
+ def to_hms
175
+ seconds = self
176
+
177
+ days, seconds = seconds.divmod(86400)
178
+ hours, seconds = seconds.divmod(3600)
179
+ minutes, seconds = seconds.divmod(60)
180
+ seconds, frac = seconds.divmod(1)
181
+
182
+ result = "%0.2d:%0.2d" % [minutes,seconds]
183
+ result = ("%0.2d:" % hours) + result if hours > 0 or days > 0
184
+ result = ("%0.2d:" % days) + result if days > 0
185
+ result += ("." + frac.round(2).to_s.split(".").last[0..1]) if frac > 0
186
+
187
+ result
188
+ end
189
+
171
190
  end
172
191
 
173
192
 
@@ -252,23 +271,6 @@ class Integer
252
271
  end
253
272
  alias_method :fibonacci, :fib
254
273
 
255
- #
256
- # Convert seconds to hours:minutes:seconds (hours is dropped if it's zero)
257
- #
258
- def to_hms
259
- seconds = self
260
-
261
- days, seconds = seconds.divmod(86400)
262
- hours, seconds = seconds.divmod(3600)
263
- minutes, seconds = seconds.divmod(60)
264
-
265
- result = "%0.2d:%0.2d" % [minutes,seconds]
266
- result = ("%0.2d:" % hours) + result if hours > 0 or days > 0
267
- result = ("%0.2d:" % days) + result if days > 0
268
-
269
- result
270
- end
271
-
272
274
  end
273
275
 
274
276
  #
@@ -336,11 +336,15 @@ class String
336
336
  end
337
337
 
338
338
  #
339
- # Converts time duration strings (mm:ss, hh:mm:ss, or dd:hh:mm:ss) to seconds.
339
+ # Converts time duration strings (mm:ss, mm:ss.dd, hh:mm:ss, or dd:hh:mm:ss) to seconds.
340
340
  # (The reverse of Integer#to_hms)
341
341
  #
342
342
  def from_hms
343
- nums = split(':').map(&:to_i)
343
+ nums = split(':')
344
+
345
+ nums[-1] = nums[-1].to_f if nums[-1] =~ /\d+\.\d+/ # convert fractional seconds to a float
346
+ nums.map! { |n| n.is_a?(String) ? n.to_i : n } # convert the rest to integers
347
+
344
348
  nums_and_units = nums.reverse.zip %w[seconds minutes hours days]
345
349
  nums_and_units.map { |num, units| num.send(units) }.sum
346
350
  end
@@ -764,6 +764,13 @@ end
764
764
  describe "to_hms and from_hms" do
765
765
  60.to_hms.should == "01:00"
766
766
  60.to_hms.from_hms.should == 60
767
+
768
+ 60.1.to_hms.should == "01:00.1"
769
+ 60.1.to_hms.from_hms.should == 60.1
770
+
771
+ 60.15.to_hms.should == "01:00.15"
772
+ 60.2892.to_hms.should == "01:00.29"
773
+
767
774
  "1:20:33".from_hms.to_hms.should == "01:20:33"
768
775
  "5:01:20:33".from_hms.to_hms.should == "05:01:20:33"
769
776
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epitools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.42
4
+ version: 0.5.43
5
5
  platform: ruby
6
6
  authors:
7
7
  - epitron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-11 00:00:00.000000000 Z
11
+ date: 2013-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec