feat: convert-time-to-seconds

This commit is contained in:
Chesterkxng
2025-03-26 17:40:24 +00:00
parent 890005f77c
commit e081a5ee9c
5 changed files with 231 additions and 1 deletions

View File

@@ -0,0 +1,57 @@
import { describe, it, expect } from 'vitest';
import { convertTimetoSeconds } from './service';
describe('convertSecondsToTime', () => {
it('should convert valid time strings to seconds', () => {
const input = '01:02:03\n00:45:30\n12:00';
const expectedOutput = '3723\n2730\n43200';
expect(convertTimetoSeconds(input)).toBe(expectedOutput);
});
it('should handle single-line input', () => {
const input = '00:01:30';
const expectedOutput = '90';
expect(convertTimetoSeconds(input)).toBe(expectedOutput);
});
it('should throw an error for invalid time format', () => {
const input = '01:02:03\n01:02:03:04';
expect(() => convertTimetoSeconds(input)).toThrow(
'Time contains more than 3 parts on line 2'
);
});
it('should throw an error for non-numeric values (minutes)', () => {
const input = '01:XX:03';
expect(() => convertTimetoSeconds(input)).toThrow(
"Time doesn't contain valid minutes on line 1"
);
});
it('should throw an error for non-numeric values (hours)', () => {
const input = '0x:00:03';
expect(() => convertTimetoSeconds(input)).toThrow(
"Time doesn't contain valid hours on line 1"
);
});
it('should throw an error for non-numeric values (seconds)', () => {
const input = '01:00:0s';
expect(() => convertTimetoSeconds(input)).toThrow(
"Time doesn't contain valid seconds on line 1"
);
});
it('should throw an error for non-numeric values multi lines (seconds)', () => {
const input = '01:00:00\n01:00:0s';
expect(() => convertTimetoSeconds(input)).toThrow(
"Time doesn't contain valid seconds on line 2"
);
});
it('should handle empty input', () => {
const input = '';
const expectedOutput = '';
expect(convertTimetoSeconds(input)).toBe(expectedOutput);
});
});

View File

@@ -0,0 +1,99 @@
import React, { useState } from 'react';
import ToolContent from '@components/ToolContent';
import { ToolComponentProps } from '@tools/defineTool';
import ToolTextInput from '@components/input/ToolTextInput';
import ToolTextResult from '@components/result/ToolTextResult';
import { CardExampleType } from '@components/examples/ToolExamples';
import { convertTimetoSeconds } from './service';
const initialValues = {};
type InitialValuesType = typeof initialValues;
const exampleCards: CardExampleType<InitialValuesType>[] = [
{
title: 'Multiple Clock Times',
description:
'In this example, we convert multiple clock times to seconds. Each clock time is listed on a new line and the spacing between input times is preserved in the output.',
sampleText: `00:00:00
00:00:01
00:01:00
01:00:00
01:59:59
12:00:00
18:30:30
23:59:59
24:00:00`,
sampleResult: `0
1
60
3600
7199
43200
66630
86399
86400`,
sampleOptions: {}
},
{
title: 'Partial Clock Times',
description:
'This example finds how many seconds there are in clock times that are partially written. Some of the clock times contain just the hours and some others contain just hours and minutes.',
sampleText: `1
1:10
14:44
23`,
sampleResult: `3600
4200
53040
82800`,
sampleOptions: {}
},
{
title: 'Time Beyond 24 Hours',
description:
'In this example, we go beyond the regular 24-hour clock. In fact, we even go beyond 60 minutes and 60 seconds.',
sampleText: `24:00:01
48:00:00
72
00:100:00
100:100:100`,
sampleResult: `86401
172800
259200
6000
366100`,
sampleOptions: {}
}
];
export default function TimeToSeconds({
title,
longDescription
}: ToolComponentProps) {
const [input, setInput] = useState<string>('');
const [result, setResult] = useState<string>('');
const compute = (optionsValues: typeof initialValues, input: string) => {
setResult(convertTimetoSeconds(input));
};
return (
<ToolContent
title={title}
input={input}
inputComponent={<ToolTextInput value={input} onChange={setInput} />}
resultComponent={<ToolTextResult value={result} />}
initialValues={initialValues}
getGroups={null}
setInput={setInput}
compute={compute}
toolInfo={{ title: `What is a ${title}?`, description: longDescription }}
exampleCards={exampleCards}
/>
);
}

View File

@@ -0,0 +1,15 @@
import { defineTool } from '@tools/defineTool';
import { lazy } from 'react';
export const tool = defineTool('time', {
path: 'convert-time-to-seconds',
name: 'Convert Time to Seconds',
icon: 'ic:round-timer-10-select',
description:
'With this browser-based application, you can convert clock time provided in hours, minutes, and seconds into just seconds. Given a time in HH:MM:SS format, it calculates HH*3600 + MM*60 + SS and prints this value in the output box. It supports AM/PM time formats as well as clock times beyond 24 hours.',
shortDescription: 'Quickly convert clock time in H:M:S format to seconds.',
keywords: ['convert', 'seconds', 'time', 'clock'],
longDescription:
'This is a quick online utility for calculating how many seconds there are in the given time. When you input a full clock time in the input box (in format H:M:S), it gets split into hours, minutes, and seconds, and using the math formula hours×60×60 plus minutes×60 plus seconds, it finds the seconds. If seconds are missing (format is H:M), then the formula becomes hours×60×60 plus minutes×60. If minutes are also missing, then the formula becomes hours×60×60. As an extra feature, hours, minutes, and seconds are not limited to just 24 hours, 60 minutes, and 60 seconds. You can use any hours value, any minutes value, and any seconds value. For example, the input time "72:00:00" will find the number of seconds in three days (72 hours is 3×24 hours) and the input time "0:1000:0" will find seconds in 1000 minutes. Timeabulous!',
component: lazy(() => import('./index'))
});

View File

@@ -0,0 +1,53 @@
import { containsOnlyDigits } from '@utils/string';
function recursiveTimeToSeconds(
timeArray: string[],
index: number = 0
): number {
if (index >= timeArray.length) {
return 0;
}
const multipliers = [3600, 60, 1];
return (
Number(timeArray[index]) * multipliers[index] +
recursiveTimeToSeconds(timeArray, index + 1)
);
}
function compute(timeArray: string[], lineNumber: number): string {
if (timeArray[0] == '') {
return '';
}
if (timeArray.length > 3) {
throw new Error(`Time contains more than 3 parts on line ${lineNumber}`);
}
const normalizedArray = [...timeArray, '0', '0'];
normalizedArray.forEach((time, index) => {
if (!containsOnlyDigits(time)) {
throw new Error(
`Time doesn't contain valid ${
['hours', 'minutes', 'seconds'][index]
} on line ${lineNumber}`
);
}
});
return recursiveTimeToSeconds(timeArray).toString();
}
export function convertTimetoSeconds(input: string): string {
const result: string[] = [];
const lines = input.split('\n');
lines.forEach((line, index) => {
const timeArray = line.split(':');
const seconds = compute(timeArray, index + 1);
result.push(seconds);
});
return result.join('\n');
}

View File

@@ -1,5 +1,11 @@
import { tool as daysDoHours } from './convert-days-to-hours/meta'; import { tool as daysDoHours } from './convert-days-to-hours/meta';
import { tool as hoursToDays } from './convert-hours-to-days/meta'; import { tool as hoursToDays } from './convert-hours-to-days/meta';
import { tool as convertSecondsToTime } from './convert-seconds-to-time/meta'; import { tool as convertSecondsToTime } from './convert-seconds-to-time/meta';
import { tool as convertTimetoSeconds } from './convert-time-to-seconds/meta';
export const timeTools = [daysDoHours, hoursToDays, convertSecondsToTime]; export const timeTools = [
daysDoHours,
hoursToDays,
convertSecondsToTime,
convertTimetoSeconds
];