Refined the convertTime function

This commit is contained in:
Core 2022-02-28 13:57:48 +00:00
parent d2cc8c5573
commit bc31a30a66
3 changed files with 57 additions and 15 deletions

View file

@ -1334,24 +1334,65 @@ const app = new Vue({
return this.playerLCD.playbackDuration
}
},
convertTime(time) {
/**
* Converts seconds to dd:hh:mm:ss
* @param time (in seconds)
* @param format (short, long)
* @returns {string}
*/
convertTime(time, format = 'short') {
if (typeof time !== "number") {
time = parseInt(time)
}
const timeGates = {
600: 15,
3600: 14,
36000: 12,
600: 15, // 10 Minutes
3600: 14, // Hour
36000: 12, // 10 Hours
}
const datetime = new Date(time * 1000)
let returnTime = datetime.toISOString().substring(11, 19);
for (let key in timeGates) {
if (time < key) {
return new Date(time * 1000).toISOString().substring(timeGates[key], 19)
returnTime = datetime.toISOString().substring(timeGates[key], 19)
break
}
}
return new Date(time * 1000).toISOString().substring(11, 19)
// Add the days on the front
let day;
if (time >= 86400) {
day = datetime.toISOString().substring(8, 10)
day = parseInt(day) - 1
returnTime = day + ":" + returnTime
}
if (format === 'long') {
const longFormat = []
// Seconds
longFormat.push(`${datetime.getSeconds()} ${app.getLz('term.time.second', options = {count: datetime.getSeconds()})}`)
// Minutes
if (time >= 60) {
longFormat.push(`${datetime.getMinutes()} ${app.getLz('term.time.minute', options = {count: datetime.getMinutes()})}`)
}
// Hours
if (time >= 3600) {
longFormat.push(`${datetime.getHours()} ${app.getLz('term.time.hour', options = {count: datetime.getHours()})}`)
}
// Days
if (time >= 86400) {
longFormat.push(`${day} ${app.getLz('term.time.day', options = {count: day})}`)
}
returnTime = longFormat.reverse().join(', ')
}
return returnTime
},
hashCode(str) {
let hash = 0,
@ -2314,14 +2355,8 @@ const app = new Vue({
getTotalTime() {
try {
if (app.showingPlaylist.relationships.tracks.data.length > 0) {
let time = Math.round([].concat(...app.showingPlaylist.relationships.tracks.data).reduce((a, {attributes: {durationInMillis}}) => a + durationInMillis, 0) / 1000);
let hours = Math.floor(time / 3600)
let mins = Math.floor(time / 60) % 60
let secs = time % 60
return app.showingPlaylist.relationships.tracks.data.length + " " + app.getLz('term.tracks', options = {count: app.showingPlaylist.relationships.tracks.data.length}) + ", "
+ ((hours > 0) ? (hours + (" " + (app.getLz('term.time.hour', options = {count: hours}) + ", "))) : "") +
((mins > 0) ? (mins + (" " + app.getLz('term.time.minute', options = {count: mins}) + ", ")) : "") +
secs + (" " + app.getLz('term.time.second', options = {count: secs}) + ".");
const timeInSeconds = Math.round([].concat(...app.showingPlaylist.relationships.tracks.data).reduce((a, {attributes: {durationInMillis}}) => a + durationInMillis, 0) / 1000);
return `${app.showingPlaylist.relationships.tracks.data.length} ${app.getLz('term.tracks', options = {count: app.showingPlaylist.relationships.tracks.data.length})}, ${this.convertTime(timeInSeconds, 'long')}`
} else return ""
} catch (err) {
return ""