From b208bc21441144ea503ec796d01773b04e797fe0 Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Wed, 29 Mar 2023 17:42:08 +0500 Subject: [PATCH 1/2] feat: add a utility for converting seconds to Hour, minutes and seconds --- src/time/timestamp.spec.ts | 36 +++++++++++++++++++++++++++++++++++- src/time/timestamp.ts | 28 +++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/time/timestamp.spec.ts b/src/time/timestamp.spec.ts index 6953e27..30f1ef9 100644 --- a/src/time/timestamp.spec.ts +++ b/src/time/timestamp.spec.ts @@ -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}/ @@ -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') + }) +}) diff --git a/src/time/timestamp.ts b/src/time/timestamp.ts index 88d78c4..2db0971 100644 --- a/src/time/timestamp.ts +++ b/src/time/timestamp.ts @@ -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)[] = [ @@ -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 +} From 338291d770ed822f094e54b3f42002ef4f7ea8ae Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Wed, 29 Mar 2023 17:44:19 +0500 Subject: [PATCH 2/2] chore: quick fix --- src/time/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/time/index.ts b/src/time/index.ts index 6d10973..6757aa0 100644 --- a/src/time/index.ts +++ b/src/time/index.ts @@ -1 +1,5 @@ -export { timestampToYYYYMMDDHHMMSS, generateTimestamp } from './timestamp' +export { + convertSecondsToHms, + timestampToYYYYMMDDHHMMSS, + generateTimestamp +} from './timestamp'