summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2008-03-24 22:58:00 +0100
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2008-03-24 22:58:00 +0100
commitab5703bcdf888e0ea9d65ba194eaeea922f72146 (patch)
tree65413c5355dfc5295fcf650c4ba75e2246fc2bf7
parent78ccfb89403dc2fd2c06d4bbaca2129e2ba4c968 (diff)
utils: Utils.secs_to_short convert seconds into hour:minute:seconds format etc
-rw-r--r--data/rbot/plugins/youtube.rb20
-rw-r--r--lib/rbot/core/utils/utils.rb16
2 files changed, 18 insertions, 18 deletions
diff --git a/data/rbot/plugins/youtube.rb b/data/rbot/plugins/youtube.rb
index 947156bc..5f1aa822 100644
--- a/data/rbot/plugins/youtube.rb
+++ b/data/rbot/plugins/youtube.rb
@@ -71,15 +71,7 @@ class YouTubePlugin < Plugin
vid[:desc].gsub!(/\s+/m, " ")
end
if secs = vid[:seconds]
- mins, secs = secs.divmod 60
- hours, mins = mins.divmod 60
- if hours > 0
- vid[:duration] = "%s:%s:%s" % [hours, mins, secs]
- elsif mins > 0
- vid[:duration] = "%s'%s\"" % [mins, secs]
- else
- vid[:duration] = "%ss" % [secs]
- end
+ vid[:duration] = Utils.secs_to_short(secs)
else
vid[:duration] = _("unknown duration")
end
@@ -109,15 +101,7 @@ class YouTubePlugin < Plugin
:format => fmt
}.delete_if { |k, v| v.nil? }
if seconds
- mins, secs = seconds.divmod 60
- hours, mins = mins.divmod 60
- if hours > 0
- vid[:formats].last[:duration] = "%s:%s:%s" % [hours, mins, secs]
- elsif mins > 0
- vid[:formats].last[:duration] = "%s'%s\"" % [mins, secs]
- else
- vid[:formats].last[:duration] = "%ss" % [secs]
- end
+ vid[:formats].last[:duration] = Utils.secs_to_short(seconds)
else
vid[:formats].last[:duration] = _("unknown duration")
end
diff --git a/lib/rbot/core/utils/utils.rb b/lib/rbot/core/utils/utils.rb
index 529c37bd..7b1ff7f1 100644
--- a/lib/rbot/core/utils/utils.rb
+++ b/lib/rbot/core/utils/utils.rb
@@ -223,6 +223,22 @@ module ::Irc
end
end
+ # Turn a number of seconds into a hours:minutes:seconds e.g.
+ # 3:18:10 or 5'12" or 7s
+ #
+ def Utils.secs_to_short(seconds)
+ secs = seconds.to_i # make sure it's an integer
+ mins, secs = secs.divmod 60
+ hours, mins = mins.divmod 60
+ if hours > 0
+ return ("%s:%s:%s" % [hours, mins, secs])
+ elsif mins > 0
+ return ("%s'%s\"" % [mins, secs])
+ else
+ return ("%ss" % [secs])
+ end
+ end
+
# Execute an external program, returning a String obtained by redirecting
# the program's standards errors and output