add support for HH:mm:ss format for input time fields

This commit is contained in:
Gregor Vostrak
2025-10-22 13:28:09 +02:00
parent f8f708a664
commit 9fe0ea5a0f
3 changed files with 20 additions and 12 deletions

View File

@@ -29,7 +29,7 @@ const open = ref(false);
function updateTimerAndStartLiveTimerUpdate() { function updateTimerAndStartLiveTimerUpdate() {
const defaultUnit = const defaultUnit =
organizationSettings?.value?.intervalFormat === 'decimal' ? 'hours' : 'minutes'; organizationSettings?.value?.intervalFormat === 'decimal' ? 'hours' : 'minutes';
const { seconds } = parseTimeInput(temporaryCustomTimerEntry.value, defaultUnit); const seconds = parseTimeInput(temporaryCustomTimerEntry.value, defaultUnit);
if (seconds && seconds > 0) { if (seconds && seconds > 0) {
let newEndDate = props.end; let newEndDate = props.end;
let newStartDate = props.start; let newStartDate = props.start;

View File

@@ -56,7 +56,7 @@ const currentTime = computed({
}); });
function updateTimerAndStartLiveTimerUpdate() { function updateTimerAndStartLiveTimerUpdate() {
const { seconds } = parseTimeInput(temporaryCustomTimerEntry.value, 'minutes'); const seconds = parseTimeInput(temporaryCustomTimerEntry.value, 'minutes');
if (seconds && seconds > 0) { if (seconds && seconds > 0) {
const newStartDate = dayjs().subtract(seconds, 's'); const newStartDate = dayjs().subtract(seconds, 's');

View File

@@ -208,22 +208,30 @@ export function formatStartEnd(
export function parseTimeInput( export function parseTimeInput(
input: string, input: string,
defaultUnit: TimeInputUnit = 'minutes' defaultUnit: TimeInputUnit = 'minutes'
): { ): number | null {
seconds: number | null;
isHHMM: boolean;
} {
// Check if input is a decimal number (hours) // Check if input is a decimal number (hours)
const decimalRegex = /^-?\d+[.,]\d+$/; const decimalRegex = /^-?\d+[.,]\d+$/;
if (decimalRegex.test(input)) { if (decimalRegex.test(input)) {
const hours = parseFloat(input.replace(',', '.')); const hours = parseFloat(input.replace(',', '.'));
return { seconds: Math.round(hours * 3600), isHHMM: false }; return Math.round(hours * 3600);
} }
// Check if input is just a number (minutes or hours based on defaultUnit) // Check if input is just a number (minutes or hours based on defaultUnit)
if (/^-?\d+$/.test(input)) { if (/^-?\d+$/.test(input)) {
const value = parseInt(input); const value = parseInt(input);
const seconds = defaultUnit === 'minutes' ? value * 60 : value * 3600; return defaultUnit === 'minutes' ? value * 60 : value * 3600;
return { seconds, isHHMM: false }; }
// Check if input is in HH:MM:SS format
const HHMMSStimeRegex = /^([0-9]{1,2}):([0-5]?[0-9]):([0-5]?[0-9])$/;
if (HHMMSStimeRegex.test(input)) {
const match = input.match(HHMMSStimeRegex);
if (match) {
const hours = parseInt(match[1]);
const minutes = parseInt(match[2]);
const seconds = parseInt(match[3]);
return hours * 3600 + minutes * 60 + seconds;
}
} }
// Check if input is in HH:MM format // Check if input is in HH:MM format
@@ -233,15 +241,15 @@ export function parseTimeInput(
if (match) { if (match) {
const hours = parseInt(match[1]); const hours = parseInt(match[1]);
const minutes = parseInt(match[2]); const minutes = parseInt(match[2]);
return { seconds: (hours * 60 + minutes) * 60, isHHMM: true }; return (hours * 60 + minutes) * 60;
} }
} }
// Try to parse natural language like "1h 30m" // Try to parse natural language like "1h 30m"
const parsedDuration = parse(input, 's'); const parsedDuration = parse(input, 's');
if (parsedDuration && parsedDuration > 0) { if (parsedDuration && parsedDuration > 0) {
return { seconds: parsedDuration, isHHMM: false }; return parsedDuration;
} }
return { seconds: null, isHHMM: false }; return null;
} }