Skip to content

Commit

Permalink
Merge pull request #236 from sasjs/convertSecondsToHms
Browse files Browse the repository at this point in the history
feat: add a utility for converting seconds to Hour, minutes and seconds
  • Loading branch information
allanbowe authored Mar 29, 2023
2 parents 48e3fe7 + 338291d commit 127875d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/time/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export { timestampToYYYYMMDDHHMMSS, generateTimestamp } from './timestamp'
export {
convertSecondsToHms,
timestampToYYYYMMDDHHMMSS,
generateTimestamp
} from './timestamp'
36 changes: 35 additions & 1 deletion src/time/timestamp.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { timestampToYYYYMMDDHHMMSS, generateTimestamp } from './timestamp'
import {
convertSecondsToHms,
timestampToYYYYMMDDHHMMSS,
generateTimestamp
} from './timestamp'

describe('timestampToYYYYMMDDHHMMSS', () => {
const regExp = /^\d{4}\/\d{1,2}\/\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}/
Expand Down Expand Up @@ -74,3 +78,33 @@ describe('generateTimestamp', () => {
global.Date = realDate
})
})

describe('convertSecondsToHms', () => {
const hour = 3600 // 3600 secs = 1 hour
const minute = 60 // 60 secs = 1 min

test('should convert seconds to Hms', () => {
expect(convertSecondsToHms(1 * hour)).toEqual('1 hour')
expect(convertSecondsToHms(2 * hour)).toEqual('2 hours')
expect(convertSecondsToHms(1 * hour + 1 * minute)).toEqual(
'1 hour, 1 minute'
)
expect(convertSecondsToHms(1 * hour + 2 * minute)).toEqual(
'1 hour, 2 minutes'
)
expect(convertSecondsToHms(2 * hour + 1 * minute)).toEqual(
'2 hours, 1 minute'
)
expect(convertSecondsToHms(2 * hour + 2 * minute)).toEqual(
'2 hours, 2 minutes'
)
expect(convertSecondsToHms(2 * hour + 2 * minute + 30)).toEqual(
'2 hours, 2 minutes, 30 seconds'
)
expect(convertSecondsToHms(2 * hour + 30)).toEqual('2 hours, 30 seconds')
expect(convertSecondsToHms(2 * minute + 30)).toEqual(
'2 minutes, 30 seconds'
)
expect(convertSecondsToHms(0)).toEqual('0 second')
})
})
28 changes: 27 additions & 1 deletion src/time/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const timestampToYYYYMMDDHHMMSS = (timestamp: number = Date.now()) => {
)
}

export function generateTimestamp(sep = '', sepIndex?: number): string {
export const generateTimestamp = (sep = '', sepIndex?: number): string => {
const date = new Date()

let timestamp: string | (number | string)[] = [
Expand All @@ -40,3 +40,29 @@ export function generateTimestamp(sep = '', sepIndex?: number): string {

return timestamp
}

export const convertSecondsToHms = (totalSeconds: number) => {
if (!totalSeconds) return '0 second'

const totalMinutes = Math.floor(totalSeconds / 60)

const hours = Math.floor(totalMinutes / 60)
const minutes = totalMinutes % 60
const seconds = totalSeconds % 60

let result = ''

if (hours > 0) {
result = hours + ` hour${hours > 1 ? 's' : ''}`
}

if (minutes > 0) {
result += `${result ? ', ' : ''}${minutes} minute${minutes > 1 ? 's' : ''}`
}

if (seconds > 0) {
result += `${result ? ', ' : ''}${seconds} second${seconds > 1 ? 's' : ''}`
}

return result
}

0 comments on commit 127875d

Please sign in to comment.