Ruby Convert time duration to Percentages
I'd like show that encoding log convert to percentage
Last log has that Current time is 00:01:07
movie file's runtime is 01:19:01
Last log has that Current time is 00:01:07
movie file's runtime is 01:19:01
Code
require 'date' start_time = DateTime.parse("00:00:00").to_time.to_i current_time = DateTime.parse("00:01:07").to_time.to_i end_time = DateTime.parse("01:19:01").to_time.to_i total_second = end_time - start_time current_second = current_time - start_time (current_second.to_f / total_second.to_f * 100).to_i
Result
irb(main):084:0> (current_second.to_f / total_second.to_f * 100).to_i => 1
Method
def percentage current, runtime start_time = DateTime.parse("00:00:00").to_time.to_i current_time = DateTime.parse(current).to_time.to_i end_time = DateTime.parse(runtime).to_time.to_i total_second = end_time - start_time current_second = current_time - start_time (current_second.to_f / total_second.to_f * 100).to_i end
Result
irb(main):094:0> percentage "00:01:07", "00:11:07" => 10