From 61fec4e4efa6678f815ac58957b23609b7aad19f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 3 Apr 2024 14:50:30 +0200 Subject: [PATCH] Better seconds to time formatting --- .../app/+stats/video/video-stats.component.ts | 4 +-- .../shared-forms/timestamp-input.component.ts | 2 +- ...eo-playlist-element-miniature.component.ts | 4 +-- packages/core-utils/src/common/date.ts | 35 +++++++++++-------- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/client/src/app/+stats/video/video-stats.component.ts b/client/src/app/+stats/video/video-stats.component.ts index 0267cfb35..0630906ec 100644 --- a/client/src/app/+stats/video/video-stats.component.ts +++ b/client/src/app/+stats/video/video-stats.component.ts @@ -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`, diff --git a/client/src/app/shared/shared-forms/timestamp-input.component.ts b/client/src/app/shared/shared-forms/timestamp-input.component.ts index 631709bc8..dab907b48 100644 --- a/client/src/app/shared/shared-forms/timestamp-input.component.ts +++ b/client/src/app/shared/shared-forms/timestamp-input.component.ts @@ -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) { diff --git a/client/src/app/shared/shared-video-playlist/video-playlist-element-miniature.component.ts b/client/src/app/shared/shared-video-playlist/video-playlist-element-miniature.component.ts index c2ba405e2..096648475 100644 --- a/client/src/app/shared/shared-video-playlist/video-playlist-element-miniature.component.ts +++ b/client/src/app/shared/shared-video-playlist/video-playlist-element-miniature.component.ts @@ -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 '' diff --git a/packages/core-utils/src/common/date.ts b/packages/core-utils/src/common/date.ts index f53a2c816..8a9224dc4 100644 --- a/packages/core-utils/src/common/date.ts +++ b/packages/core-utils/src/common/date.ts @@ -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 {