Skip to content

Commit

Permalink
Merge pull request #1510 from Ekep-Obasi/fix/video-player-timestamp-f…
Browse files Browse the repository at this point in the history
…ormat

Format Video Player Timestamp for Accurate Display
  • Loading branch information
Rassl authored May 23, 2024
2 parents c3232f3 + b62f8c9 commit 9032530
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
28 changes: 20 additions & 8 deletions src/utils/secondsToMediaTime/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import { secondsToMediaTime } from '..'

describe('secondsToMediaTime', () => {
it('converts 0 seconds to "00:00"', () => {
expect(secondsToMediaTime(0)).toEqual('00:00')
it('converts 0 seconds to "0:00"', () => {
expect(secondsToMediaTime(0)).toEqual('0:00')
})

it('converts 65 seconds to "01:05"', () => {
expect(secondsToMediaTime(65)).toEqual('01:05')
it('converts 65 seconds to "1:05"', () => {
expect(secondsToMediaTime(65)).toEqual('1:05')
})

it('converts 3600 seconds to "60:00"', () => {
expect(secondsToMediaTime(3600)).toEqual('60:00')
it('converts 3600 seconds to "1:00:00"', () => {
expect(secondsToMediaTime(3600)).toEqual('1:00:00')
})

it('converts 3665 seconds to "61:05"', () => {
expect(secondsToMediaTime(3665)).toEqual('61:05')
it('converts 3665 seconds to "1:01:05"', () => {
expect(secondsToMediaTime(3665)).toEqual('1:01:05')
})

it('converts 5025 seconds to "1:23:45"', () => {
expect(secondsToMediaTime(5025)).toEqual('1:23:45')
})

it('converts 7261 seconds to "2:01:01"', () => {
expect(secondsToMediaTime(7261)).toEqual('2:01:01')
})

it('handles large values correctly', () => {
expect(secondsToMediaTime(123456)).toEqual('34:17:36')
})
})
8 changes: 5 additions & 3 deletions src/utils/secondsToMediaTime/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export const secondsToMediaTime = (seconds: number): string => {
const minutes = Math.floor(seconds / 60)
const hours = Math.floor(seconds / 3600)
const minutes = Math.floor((seconds % 3600) / 60)
const remainingSeconds = Math.floor(seconds % 60)

const formattedMinutes = minutes.toString().padStart(2, '0')
const formattedHours = hours > 0 ? `${hours}:` : ''
const formattedMinutes = hours > 0 ? minutes.toString().padStart(2, '0') : minutes.toString()
const formattedSeconds = remainingSeconds.toString().padStart(2, '0')

return `${formattedMinutes}:${formattedSeconds}`
return `${formattedHours}${formattedMinutes}:${formattedSeconds}`
}

0 comments on commit 9032530

Please sign in to comment.