DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Ruby Method To Get Seconds From Colon-separated Time String

01.11.2006
| 5438 views |
  • submit to reddit
        // converts colon-separated time to seconds
// "01:02:03" => 3723

def colon_separated_to_i(str)

  time_arr = []
  time_arr = str.split(":").reverse
  sec = 0
  secs = [1, 60, 60*60, 60*60*24]

  time_arr.size.times do {|i|
    sec += secs[i].to_i * time_arr[i].to_i
  end
  sec
end