fix(date-time-converter): add UTC ISO Display and JS Date Constructor

Fix #1198
This commit is contained in:
ShareVB 2024-09-01 18:14:36 +02:00
parent 88ecf60587
commit 6fd79d6e06
5 changed files with 81 additions and 8 deletions

View file

@ -2,16 +2,19 @@ import { describe, expect, test } from 'vitest';
import {
dateToExcelFormat,
excelFormatToDate,
fromJSDate,
fromTimestamp,
isExcelFormat,
isISO8601DateTimeString,
isISO9075DateString,
isJSDate,
isMongoObjectId,
isRFC3339DateString,
isRFC7231DateString,
isTimestamp,
isUTCDateString,
isUnixTimestamp,
toJSDate,
} from './date-time-converter.models';
describe('date-time-converter models', () => {
@ -218,4 +221,36 @@ describe('date-time-converter models', () => {
expect(excelFormatToDate('-1000')).toEqual(new Date('1897-04-04T00:00:00.000Z'));
});
});
describe('isJSDate', () => {
test('a JS date is a new Date()', () => {
expect(isJSDate('new Date(2000, 0)')).toBe(true);
expect(isJSDate('new Date(2000, 0, 1, 12, 12)')).toBe(true);
expect(isJSDate('new Date(2000, 0, 1, 12, 12, 12)')).toBe(true);
expect(isJSDate('new Date(2000, 0, 1, 12, 12, 12, 1)')).toBe(true);
expect(isJSDate('new Date(2000)')).toBe(false);
expect(isJSDate('')).toBe(false);
expect(isJSDate('foo')).toBe(false);
expect(isJSDate('1.1.1')).toBe(false);
});
});
describe('fromJSDate', () => {
test('convert a JS new Date() to date', () => {
expect(fromJSDate('new Date(2000, 0)')).toEqual(new Date(2000, 0));
expect(fromJSDate('new Date(2000, 0, 1, 12, 12)')).toEqual(new Date(2000, 0, 1, 12, 12));
expect(fromJSDate('new Date(2000, 0, 1, 12, 12, 12)')).toEqual(new Date(2000, 0, 1, 12, 12, 12));
expect(fromJSDate('new Date(2000, 0, 1, 12, 12, 12, 1)')).toEqual(new Date(2000, 0, 1, 12, 12, 12, 1));
});
});
describe('toJSDate', () => {
test('convert a date to JS new Date()', () => {
expect(toJSDate(new Date(2000, 0))).toEqual('new Date(2000, 0, 1, 0, 0, 0, 0);');
expect(toJSDate(new Date(2000, 0, 1, 12, 12))).toEqual('new Date(2000, 0, 1, 12, 12, 0, 0);');
expect(toJSDate(new Date(2000, 0, 1, 12, 12, 12))).toEqual('new Date(2000, 0, 1, 12, 12, 12, 0);');
expect(toJSDate(new Date(2000, 0, 1, 12, 12, 12, 1))).toEqual('new Date(2000, 0, 1, 12, 12, 12, 1);');
});
});
});

View file

@ -15,6 +15,9 @@ export {
isExcelFormat,
fromTimestamp,
isTimestampMicroSeconds,
isJSDate,
fromJSDate,
toJSDate,
};
const ISO8601_REGEX
@ -29,6 +32,8 @@ const RFC7231_REGEX = /^[A-Za-z]{3},\s[0-9]{2}\s[A-Za-z]{3}\s[0-9]{4}\s[0-9]{2}:
const EXCEL_FORMAT_REGEX = /^-?\d+(\.\d+)?$/;
const JS_DATE_REGEX = /^new\s+Date\(\s*(?:(\d+)\s*,\s*)(?:(\d|11)\s*,\s*(?:(\d+)\s*,\s*(?:(\d+)\s*,\s*(?:(\d+)\s*,\s*(?:(\d+)\s*,\s*)?)?)?)?)?(\d+)\)\s*;?$/;
function createRegexMatcher(regex: RegExp) {
return (date?: string) => !_.isNil(date) && regex.test(date);
}
@ -43,6 +48,14 @@ const isTimestampMilliSeconds = createRegexMatcher(/^[0-9]{1,13}$/);
const isTimestampMicroSeconds = createRegexMatcher(/^[0-9]{16}$/);
const isMongoObjectId = createRegexMatcher(/^[0-9a-fA-F]{24}$/);
const isJSDate = createRegexMatcher(JS_DATE_REGEX);
function fromJSDate(date: string): Date {
const res = JS_DATE_REGEX.exec(date);
const parts = (res || []).filter(p => p !== undefined).map(p => Number.parseInt(p, 10)).slice(1);
return new (Function.prototype.bind.apply(Date, [null, ...parts]))();
}
const toJSDate = (date: Date) => `new Date(${date.getFullYear()}, ${date.getMonth()}, ${date.getDate()}, ${date.getHours()}, ${date.getMinutes()}, ${date.getSeconds()}, ${date.getMilliseconds()});`;
const isExcelFormat = createRegexMatcher(EXCEL_FORMAT_REGEX);
function isUTCDateString(date?: string) {

View file

@ -11,20 +11,24 @@ import {
isValid,
parseISO,
} from 'date-fns';
import { UTCDate } from '@date-fns/utc';
import type { DateFormat, ToDateMapper } from './date-time-converter.types';
import {
dateToExcelFormat,
excelFormatToDate,
fromJSDate,
fromTimestamp,
isExcelFormat,
isISO8601DateTimeString,
isISO9075DateString,
isJSDate,
isMongoObjectId,
isRFC3339DateString,
isRFC7231DateString,
isTimestamp,
isUTCDateString,
isUnixTimestamp,
toJSDate,
} from './date-time-converter.models';
import { withDefaultOnError } from '@/utils/defaults';
import { useValidation } from '@/composable/validation';
@ -46,6 +50,12 @@ const formats: DateFormat[] = [
toDate: parseISO,
formatMatcher: date => isISO8601DateTimeString(date),
},
{
name: 'ISO 8601 UTC',
fromDate: date => (new UTCDate(date)).toISOString(),
toDate: parseISO,
formatMatcher: date => isISO8601DateTimeString(date),
},
{
name: 'ISO 9075',
fromDate: formatISO9075,
@ -94,6 +104,12 @@ const formats: DateFormat[] = [
toDate: excelFormatToDate,
formatMatcher: isExcelFormat,
},
{
name: 'JS Date',
fromDate: date => toJSDate(date),
toDate: date => fromJSDate(date),
formatMatcher: isJSDate,
},
];
const formatIndex = ref(6);