Skip to content

Commit

Permalink
Add a shortDuration function to DateUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
iBelieve committed Jan 10, 2015
1 parent d1282ff commit 76278ab
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions modules/Material/Extras/js/dateutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,38 @@ function friendlyTime(time, standalone) {
return standalone ? Qt.formatDate(time) : ("on %1").arg(Qt.formatDate(time))
}

function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

function shortDuration(duration, type) {
var hours = Math.floor(duration/(1000 * 60 * 60))
var minutes = Math.floor(duration/(1000 * 60) - 60 * hours)
var seconds = Math.floor(duration/1000 - 60 * minutes - 60 * 60 * hours)

if (type === undefined)
type = '?'

var str = ''
if (type === 's' || type === '?')
str = "%1".arg(pad(seconds, 2))
if (type === 's' || type === 'm' || (type === '?' && (minutes >= 1 || hours >= 1))) {
if (str.length > 0)
str = "%1:%2".arg(pad(minutes, 2)).arg(str)
else
str = "%1".arg(pad(minutes, 2))
}
if (type === 's' || type === 'm' || type === 'h' || (type === '?' && hours >= 1)) {
if (str.length > 0)
str = "%1:%2".arg(hours).arg(str)
else
str = "%1".arg(hours)
}
return str.trim()
}

function friendlyDuration(duration, type) {
var hours = Math.floor(duration/(1000 * 60 * 60))
var minutes = Math.floor(duration/(1000 * 60) - 60 * hours)
Expand Down

0 comments on commit 76278ab

Please sign in to comment.