about summary refs log tree commit homepage
path: root/lib/dtas/parse_time.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-10-17 06:13:33 +0000
committerEric Wong <normalperson@yhbt.net>2013-10-17 06:13:33 +0000
commit605b8403bec9607b2aa818ca9e1daeea607e89fa (patch)
tree87125cd0bc710879572ad7eb70f60f6efae9f876 /lib/dtas/parse_time.rb
parent1c77d0d7c414d43f751eeb308a1a47d87de6cc1e (diff)
downloaddtas-605b8403bec9607b2aa818ca9e1daeea607e89fa.tar.gz
Only lightly tested, but this should give us some idea of where
we'll be going...
Diffstat (limited to 'lib/dtas/parse_time.rb')
-rw-r--r--lib/dtas/parse_time.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/dtas/parse_time.rb b/lib/dtas/parse_time.rb
new file mode 100644
index 0000000..c2ca777
--- /dev/null
+++ b/lib/dtas/parse_time.rb
@@ -0,0 +1,29 @@
+# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
+# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
+require_relative '../dtas'
+module DTAS::ParseTime
+  def parse_time(time)
+    case time
+    when /\A\d+\z/
+      time.to_i
+    when /\A[\d\.]+\z/
+      time.to_f
+    when /\A[:\d\.]+\z/
+      hhmmss = time.dup
+      rv = hhmmss.sub!(/\.(\d+)\z/, "") ? "0.#$1".to_f : 0
+
+      # deal with HH:MM:SS
+      t = hhmmss.split(/:/)
+      raise ArgumentError, "Bad time format: #{hhmmss}" if t.size > 3
+
+      mult = 1
+      while part = t.pop
+        rv += part.to_i * mult
+        mult *= 60
+      end
+      rv
+    else
+      raise ArgumentError, "unparseable: #{time.inspect}"
+    end
+  end
+end