Skip to content

Instantly share code, notes, and snippets.

@takaokouji
Created February 23, 2010 06:42
Show Gist options
  • Select an option

  • Save takaokouji/311940 to your computer and use it in GitHub Desktop.

Select an option

Save takaokouji/311940 to your computer and use it in GitHub Desktop.
class String
def -(val)
if TIME_RE =~ self && TIME_RE =~ val
if self.length == 8 || val.length == 8
with_sec = true
end
require "time"
d = Time.parse(self) - Time.parse(val)
sec = d % 60
min = (d / 60) % 60
hour = (d / 60 / 60) % 24
res = ("%02d:%02d" % [hour, min])
if with_sec
res << ":%02d" % sec
end
return res
else
raise NoMethodError, "undefined method `-' for #{self.inspect}:#{self.class}"
end
end
private
TIME_RE = /\A[0-2]\d:[0-5]\d(?::[0-5]\d)?\z/
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment