Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Better seconds to time formatting

Chocobozzz61fec4e4efa6
diff --git a/client/src/app/+stats/video/video-stats.component.ts b/client/src/app/+stats/video/video-stats.component.ts
index 0267cfb352..0630906ec1 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 631709bc8a..dab907b484 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 c2ba405e22..0966484752 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 f53a2c816c..8a9224dc4a 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 {