Better seconds to time formatting

pull/6318/head
Chocobozzz 2024-04-03 14:50:30 +02:00
parent 9b70c8e7e8
commit 61fec4e4ef
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
4 changed files with 26 additions and 19 deletions

View File

@ -320,11 +320,11 @@ export class VideoStatsComponent implements OnInit {
this.overallStatCards = [
{
label: $localize`Average watch time`,
value: secondsToTime(overallStats.averageWatchTime)
value: secondsToTime({ seconds: overallStats.averageWatchTime, format: 'locale-string' })
},
{
label: $localize`Total watch time`,
value: secondsToTime(overallStats.totalWatchTime)
value: secondsToTime({ seconds: overallStats.totalWatchTime, format: 'locale-string' })
},
{
label: $localize`Peak viewers`,

View File

@ -40,7 +40,7 @@ export class TimestampInputComponent implements ControlValueAccessor, OnInit {
writeValue (timestamp: number) {
this.timestamp = timestamp
this.timestampString = secondsToTime({ seconds: this.timestamp, fullFormat: true, symbol: ':' })
this.timestampString = secondsToTime({ seconds: this.timestamp, format: 'full', symbol: ':' })
}
registerOnChange (fn: (_: any) => void) {

View File

@ -174,8 +174,8 @@ export class VideoPlaylistElementMiniatureComponent implements OnInit {
const start = playlistElement.startTimestamp
const stop = playlistElement.stopTimestamp
const startFormatted = secondsToTime({ seconds: start, fullFormat: true, symbol: ':' })
const stopFormatted = secondsToTime({ seconds: stop, fullFormat: true, symbol: ':' })
const startFormatted = secondsToTime({ seconds: start, format: 'full', symbol: ':' })
const stopFormatted = secondsToTime({ seconds: stop, format: 'full', symbol: ':' })
if (start === null && stop === null) return ''

View File

@ -85,48 +85,55 @@ function timeToInt (time: number | string) {
function secondsToTime (options: {
seconds: number
fullFormat?: boolean // default false
format: 'short' | 'full' | 'locale-string' // default 'short'
symbol?: string
} | number) {
let seconds: number
let fullFormat = false
let format: 'short' | 'full' | 'locale-string' = 'short'
let symbol: string
if (typeof options === 'number') {
seconds = options
} else {
seconds = options.seconds
fullFormat = options.fullFormat ?? false
format = options.format ?? 'short'
symbol = options.symbol
}
let time = ''
if (seconds === 0 && !fullFormat) return '0s'
if (seconds === 0 && format !== 'full') return '0s'
const formatNumber = (value: number) => {
if (format === 'locale-string') return value.toLocaleString()
return value
}
const hourSymbol = (symbol || 'h')
const minuteSymbol = (symbol || 'm')
const secondsSymbol = fullFormat ? '' : 's'
const secondsSymbol = format === 'full' ? '' : 's'
const hours = Math.floor(seconds / 3600)
if (hours >= 1 && hours < 10 && fullFormat) time = '0' + hours + hourSymbol
else if (hours >= 1) time = hours + hourSymbol
else if (fullFormat) time = '00' + hourSymbol
if (hours >= 1 && hours < 10 && format === 'full') time = '0' + hours + hourSymbol
else if (hours >= 1) time = formatNumber(hours) + hourSymbol
else if (format === 'full') time = '00' + hourSymbol
seconds %= 3600
const minutes = Math.floor(seconds / 60)
if (minutes >= 1 && minutes < 10 && fullFormat) time += '0' + minutes + minuteSymbol
else if (minutes >= 1) time += minutes + minuteSymbol
else if (fullFormat) time += '00' + minuteSymbol
if (minutes >= 1 && minutes < 10 && format === 'full') time += '0' + minutes + minuteSymbol
else if (minutes >= 1) time += formatNumber(minutes) + minuteSymbol
else if (format === 'full') time += '00' + minuteSymbol
seconds %= 60
if (seconds >= 1 && seconds < 10 && fullFormat) time += '0' + seconds + secondsSymbol
else if (seconds >= 1) time += seconds + secondsSymbol
else if (fullFormat) time += '00'
if (seconds >= 1 && seconds < 10 && format === 'full') time += '0' + seconds + secondsSymbol
else if (seconds >= 1) time += formatNumber(seconds) + secondsSymbol
else if (format === 'full') time += '00'
return time
}
// ---------------------------------------------------------------------------
export {