From 605b8403bec9607b2aa818ca9e1daeea607e89fa Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 17 Oct 2013 06:13:33 +0000 Subject: trimfx: flesh out parsing of commands Only lightly tested, but this should give us some idea of where we'll be going... --- lib/dtas/parse_time.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lib/dtas/parse_time.rb (limited to 'lib/dtas/parse_time.rb') 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 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 -- cgit v1.2.3-24-ge0c7