Instantly share code, notes, and snippets.

# thiagofm/thiagomassa.rbSecret Created Oct 10, 2009

 require 'time' def how_much_seconds(d) total=(d.day*24*3_600)+(d.hour*3_600)+(d.min*60) end def print_time(s,f) days=s / 86400; hours=(s / 3600) - (days * 24); minutes=(s / 60) - (days * 1440) - (hours * 60); if days>f hours+=12 end print hours print ":" if minutes <=9 print "0" end print minutes if hours < 12 print "am" else print "pm" end print "\n" end def average_time_of_day(d) total=0 loop=0 loop2=0 firstday= Time.parse(d[0]).day seconds=Array.new(d) while loop < d.size #puts Time.parse(d[loop]) seconds[loop]=how_much_seconds(Time.parse(d[loop])) loop+=1 end loop=0 while loop < d.size-1 if Time.parse(d[loop+1]) < Time.parse(d[loop]) loop2=loop+1 while (loop2) < d.size seconds[loop2]+=24*3600 loop2+=1 end loop2=0 end loop+=1 end loop=0 while loop < d.size total += seconds[loop] loop+=1 end total /= d.size print_time(total,firstday) end average_time_of_day(["11:51pm", "11:56pm", "12:01am", "12:06am", "12:11am"]) average_time_of_day(["6:41am", "6:51am", "7:01am"]) average_time_of_day(["1:00am", "1:30am", "2:00am"]) average_time_of_day(["1:00am", "1:30am"]) average_time_of_day(["1:00am"]) average_time_of_day(["6:00pm","6:00am"])