Add tests for timeformat "parseDate"

This commit is contained in:
Matthias 2023-04-10 17:40:56 +02:00
parent 9984052688
commit fabe44293e
2 changed files with 16 additions and 2 deletions

View File

@ -1,10 +1,12 @@
import { parse, toDate } from 'date-fns'; import { parse, toDate } from 'date-fns';
import { format, utcToZonedTime } from 'date-fns-tz'; import { format, utcToZonedTime, zonedTimeToUtc } from 'date-fns-tz';
import humanizeDuration from 'humanize-duration'; import humanizeDuration from 'humanize-duration';
import { isUndefined } from './numberformat'; import { isUndefined } from './numberformat';
/** Parse date from string, returns date in UTC! */
export function dateFromString(datestring: string, format: string): Date { export function dateFromString(datestring: string, format: string): Date {
return parse(datestring, format, 0); const localTime = parse(datestring, format, 0);
return zonedTimeToUtc(localTime, 'UTC');
} }
let locTimeZone = 'UTC'; let locTimeZone = 'UTC';

View File

@ -6,6 +6,7 @@ import {
timestampToDateString, timestampToDateString,
dateStringToTimeRange, dateStringToTimeRange,
timestampHour, timestampHour,
dateFromString,
} from '@/shared/formatters'; } from '@/shared/formatters';
const { getTimeZone } = exportForTesting; const { getTimeZone } = exportForTesting;
@ -58,4 +59,15 @@ describe('timeformatter.ts', () => {
setTimezone('CET'); setTimezone('CET');
expect(timestampHour(1651057500000)).toEqual(13); expect(timestampHour(1651057500000)).toEqual(13);
}); });
it('dateFromString converts correctly', () => {
expect(dateFromString('2022-04-04', 'yyyy-MM-dd')).toEqual(
new Date('2022-04-04T00:00:00.000Z'),
);
expect(dateFromString('2023-02-08', 'yyyy-MM-dd')).toEqual(
new Date('2023-02-08T00:00:00.000Z'),
);
expect(dateFromString('2022-12-31', 'yyyy-MM-dd')).toEqual(
new Date('2022-12-31T00:00:00.000Z'),
);
});
}); });