Skip to content

Instantly share code, notes, and snippets.

@koki-h
Last active January 22, 2018 02:26
Show Gist options
  • Select an option

  • Save koki-h/64a57ba3e0bc5241c42ff07ea63371e5 to your computer and use it in GitHub Desktop.

Select an option

Save koki-h/64a57ba3e0bc5241c42ff07ea63371e5 to your computer and use it in GitHub Desktop.
時間のhms表記を全て秒に換算する
times = [
"02h 37m 59s",
"18m 43s",
"09s",
"09s",
"09s",
"09s",
"09s",
"09s",
"09s",
"24s",
"04m 45s",
"50s",
"04m 38s",
"04m 28s",
"04m 31s",
"04m 56s",
"04m 32s",
"05m 02s",
"04m 40s",
"20s"
]
def hms2s(time)
nums = time.split(" ")
hour = 0
minute = 0
second = 0
nums.each do |n|
h = n.scan(/(.+)h/)[0]
m = n.scan(/(.+)m/)[0]
s = n.scan(/(.+)s/)[0]
hour = h[0].to_i if h
minute = m[0].to_i if m
second = s[0].to_i if s
end
puts "#{hour}h #{minute}m #{second}s"
return hour * 60 * 60 + minute * 60 + second
end
times.each do |time|
puts time
puts hms2s(time)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment