Add human-friendly formatting to runtime

This commit is contained in:
Michael Miller 2018-09-15 15:45:17 -06:00
parent d1527d8c0d
commit 196187a98a

View file

@ -64,7 +64,25 @@ module Spectator
end
private def human_time(span : Time::Span)
span.to_s
millis = span.total_milliseconds
return "#{(millis * 1000).round.to_i} microseconds" if millis < 1
seconds = span.total_seconds
return "#{millis.round(2)} milliseconds" if seconds < 1
return "#{seconds.round(2)} seconds" if seconds < 60
int_seconds = seconds.to_i
minutes = int_seconds / 60
int_seconds %= 60
return sprintf("%i:%02i", minutes, int_seconds) if minutes < 60
hours = minutes / 60
minutes %= 60
return sprintf("%i:%02i:%02i", hours, minutes, int_seconds) if hours < 24
days = hours / 24
hours %= 24
return sprintf("%i days %i:%02i:%02i", days, hours, minutes, int_seconds)
end
end
end