// checkup is the global namespace for all checkup variables (except time). var checkup = checkup || {}; // time provides simple nanosecond-based unit measurements. var time = (function() { // now gets the current time with millisecond accuracy, // but as a unit of nanoseconds. var now = function() { return new Date().getTime() * 1e6; }; var ns = 1, us = 1000 * ns, ms = 1000 * us, second = 1000 * ms, minute = 60 * second, hour = 60 * minute, day = 24 * hour, week = 7 * day; return { Now: now, Nanosecond: ns, Microsecond: us, Millisecond: ms, Second: second, Minute: minute, Hour: hour, Day: day, Week: week }; })(); // formatDuration formats d (in nanoseconds) with // a proper unit suffix based on its value. checkup.formatDuration = function(d) { if (d == 0) return d+"ms"; else if (d < time.Millisecond) return Math.round(d*1e-3)+"µs"; else if (d < 10 * time.Second) return Math.round(d*1e-6)+"ms"; else if (d < 90 * time.Second) return Math.round(d*1e-9)+"s"; else if (d < 90 * time.Minute) return Math.round(d*1e-9/60)+" minutes"; else if (d < 48 * time.Hour) return Math.round(d*1e-9/60/60)+" hours"; else return Math.round(d*1e-9 / 60/60/24)+" days"; }; // I'm not even joking checkup.leftpad = function(str, len, ch) { str = String(str); var i = -1; if (!ch && ch !== 0) ch = ' '; len = len - str.length; while (++i < len) str = ch + str; return str; } // timeSince renders the duration ms (in milliseconds) in human-friendly form. checkup.timeSince = function(ms) { var seconds = Math.floor((new Date() - ms) / 1000); var interval = Math.floor(seconds / 31536000); if (interval > 1) return interval + " years"; interval = Math.floor(seconds / 2592000); if (interval > 1) return interval + " months"; interval = Math.floor(seconds / 86400); if (interval > 1) return interval + " days"; interval = Math.floor(seconds / 3600); if (interval > 1) return interval + " hours"; interval = Math.floor(seconds / 60); if (interval > 1) return interval + " minutes"; return Math.floor(seconds) + " seconds"; }; // makeTimeTag returns a