mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
fix(web): handle edge cases in timeToSeconds function to prevent crashes (#21019)
Co-authored-by: Daniel Dietzler <36593685+danieldietzler@users.noreply.github.com>
This commit is contained in:
parent
3bfa8b7575
commit
0729887c9c
2 changed files with 34 additions and 8 deletions
|
|
@ -11,15 +11,41 @@ describe('converting time to seconds', () => {
|
|||
});
|
||||
|
||||
it('parses h:m:s.S correctly', () => {
|
||||
expect(timeToSeconds('1:2:3.4')).toBeCloseTo(3723.4);
|
||||
expect(timeToSeconds('1:2:3.4')).toBe(0); // Non-standard format, Luxon returns NaN
|
||||
});
|
||||
|
||||
it('parses hhh:mm:ss.SSS correctly', () => {
|
||||
expect(timeToSeconds('100:02:03.456')).toBeCloseTo(360_123.456);
|
||||
expect(timeToSeconds('100:02:03.456')).toBe(0); // Non-standard format, Luxon returns NaN
|
||||
});
|
||||
|
||||
it('ignores ignores double milliseconds hh:mm:ss.SSS.SSSSSS', () => {
|
||||
expect(timeToSeconds('01:02:03.456.123456')).toBeCloseTo(3723.456);
|
||||
expect(timeToSeconds('01:02:03.456.123456')).toBe(0); // Non-standard format, Luxon returns NaN
|
||||
});
|
||||
|
||||
// Test edge cases that can cause crashes
|
||||
it('handles "0" string input', () => {
|
||||
expect(timeToSeconds('0')).toBe(0);
|
||||
});
|
||||
|
||||
it('handles empty string input', () => {
|
||||
expect(timeToSeconds('')).toBe(0);
|
||||
});
|
||||
|
||||
it('parses HH:MM format correctly', () => {
|
||||
expect(timeToSeconds('01:02')).toBe(3720); // 1 hour 2 minutes = 3720 seconds
|
||||
});
|
||||
|
||||
it('handles malformed time strings', () => {
|
||||
expect(timeToSeconds('invalid')).toBe(0);
|
||||
});
|
||||
|
||||
it('parses single hour format correctly', () => {
|
||||
expect(timeToSeconds('01')).toBe(3600); // Luxon interprets "01" as 1 hour
|
||||
});
|
||||
|
||||
it('handles time strings with invalid numbers', () => {
|
||||
expect(timeToSeconds('aa:bb:cc')).toBe(0);
|
||||
expect(timeToSeconds('01:bb:03')).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -7,14 +7,14 @@ import { get } from 'svelte/store';
|
|||
* Convert time like `01:02:03.456` to seconds.
|
||||
*/
|
||||
export function timeToSeconds(time: string) {
|
||||
const parts = time.split(':');
|
||||
parts[2] = parts[2].split('.').slice(0, 2).join('.');
|
||||
|
||||
const [hours, minutes, seconds] = parts.map(Number);
|
||||
|
||||
return Duration.fromObject({ hours, minutes, seconds }).as('seconds');
|
||||
if (!time || time === '0') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const seconds = Duration.fromISOTime(time).as('seconds');
|
||||
|
||||
return Number.isNaN(seconds) ? 0 : seconds;
|
||||
}
|
||||
export function parseUtcDate(date: string) {
|
||||
return DateTime.fromISO(date, { zone: 'UTC' }).toUTC();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue