<<-doc 对于传递给此方法的任意数量的时间值,修改“时间总和”方法。 例如:(“ 11:23:07”,“ 22:53:45”,“ 0:23:23”,“ 23:45:56”)->“ 2天&10:26:11” (“ 0:45:34”,“ 0:15:58”)-> 01:01:32; (“ 11:23:07”,“ 22:53:45”)-> 1天&10:16:52 doc
但是我希望它是2 [/名称] [输入] “ 24:01:10”“ 10:30:50” [/输入] [输出] “无效的24小时时间值” [/输出]
===
[名称] 3 [/名称] [输入] “ 0:45:34”“ 0:15:58” [/输入] [输出] “ 01:01:32” [/输出]
放入sum_time(“ 11:23:07”,“ 22:53:45”,“ 0:23:23”,“ 23:45:56 enter code here
”)
答案 0 :(得分:0)
如何从脚本中使用ARGV:
require 'time'
def to_seconds(timestamp)
timestamp.hour * 3600 + timestamp.min * 60 + timestamp.sec
end
def sum_time(*time)
# initialize the variables
total_seconds = 0
time.each do |time_item|
timestamp = Time.parse(time_item)
total_seconds += to_seconds(timestamp)
end
sum_time_string = ""
days = (total_seconds / (24 * 3600)).to_i
sum_time_string = "#{days} day & " if days > 0
sum_time_string += Time.at(total_seconds).utc.strftime("%H:%M:%S")
end
puts sum_time(*ARGV)
通话:
ruby your_script.rb 11:23:07 22:53:45 0:23:23 23:45:56