Format times like hh:mm:ss

This commit is contained in:
Nils Maier 2013-07-29 17:20:08 +02:00
parent 39ecd3a3d6
commit 6027f1ad7c

View File

@ -20,14 +20,21 @@ angular
}; };
}]) }])
.filter('time', function() { .filter('time', function() {
function pad(f) {
return ("0" + f).substr(-2);
}
return function(time) { return function(time) {
time = parseFloat(time); time = parseInt(time, 10);
if (isNaN(time) || !isFinite(time)) return " infinite"; if (!time || !isFinite(time)) return "∞";
if (!time) return " infinite"; var secs = time % 60;
if (time < 60) return time.toFixed(2) + " s"; if (time < 60) return secs + "s";
else if (time < 60*60) return (time/60).toFixed(2) + " min"; var mins = Math.floor((time % 3600) / 60)
else if (time < 60*60*24) return (time/(60*60)).toFixed(2) + " hours"; if (time < 3600) return pad(mins) + ":" + pad(secs);
else return (time/(60*60*24)).toFixed(2) + " days!"; var hrs = Math.floor((time % 86400) / 3600);
if (time < 86400) return pad(hrs) + ":" + pad(mins) + ":" + pad(secs);
var days = Math.floor(time / 86400);
return days + "::" + pad(hrs) + ":" + pad(mins) + ":" + pad(secs);
}; };
}); });