Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,15 @@ describe('DateFnsAdapter', () => {
expect(adapter.getSeconds(result)).toBe(0);
});

it('should parse a time string containing only hours', () => {
const result = adapter.parseTime('11', 'HH')!;
expect(result).toBeTruthy();
expect(adapter.isValid(result)).toBe(true);
expect(adapter.getHours(result)).toBe(11);
expect(adapter.getMinutes(result)).toBe(0);
expect(adapter.getSeconds(result)).toBe(0);
});

it('should return an invalid date when parsing invalid time string', () => {
expect(adapter.isValid(adapter.parseTime('abc', 'p')!)).toBe(false);
expect(adapter.isValid(adapter.parseTime('123', 'p')!)).toBe(false);
Expand Down
70 changes: 40 additions & 30 deletions src/material-date-fns-adapter/adapter/date-fns-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,35 +162,7 @@ export class DateFnsAdapter extends DateAdapter<Date, Locale> {
}

parse(value: unknown, parseFormat: string | string[]): Date | null {
if (typeof value == 'string' && value.length > 0) {
const iso8601Date = parseISO(value);

if (this.isValid(iso8601Date)) {
return iso8601Date;
}

const formats = Array.isArray(parseFormat) ? parseFormat : [parseFormat];

if (!parseFormat.length) {
throw Error('Formats array must not be empty.');
}

for (const currentFormat of formats) {
const fromFormat = parse(value, currentFormat, new Date(), {locale: this.locale});

if (this.isValid(fromFormat)) {
return fromFormat;
}
}

return this.invalid();
} else if (typeof value === 'number') {
return new Date(value);
} else if (value instanceof Date) {
return this.clone(value);
}

return null;
return this._parse(value, parseFormat);
}

format(date: Date, displayFormat: string): string {
Expand Down Expand Up @@ -278,10 +250,48 @@ export class DateFnsAdapter extends DateAdapter<Date, Locale> {
}

override parseTime(value: unknown, parseFormat: string | string[]): Date | null {
return this.parse(value, parseFormat);
return this._parse(value, parseFormat, false);
}

override addSeconds(date: Date, amount: number): Date {
return addSeconds(date, amount);
}

private _parse(
value: unknown,
parseFormat: string | string[],
shouldTryParseIso = true,
): Date | null {
if (typeof value == 'string' && value.length > 0) {
if (shouldTryParseIso) {
const iso8601Date = parseISO(value);

if (this.isValid(iso8601Date)) {
return iso8601Date;
}
}

const formats = Array.isArray(parseFormat) ? parseFormat : [parseFormat];

if (!parseFormat.length) {
throw Error('Formats array must not be empty.');
}

for (const currentFormat of formats) {
const fromFormat = parse(value, currentFormat, new Date(), {locale: this.locale});

if (this.isValid(fromFormat)) {
return fromFormat;
}
}

return this.invalid();
} else if (typeof value === 'number') {
return new Date(value);
} else if (value instanceof Date) {
return this.clone(value);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,15 @@ describe('LuxonDateAdapter', () => {
expect(adapter.getSeconds(result)).toBe(0);
});

it('should parse a time string containing only hours', () => {
const result = adapter.parseTime('11', 'HH')!;
expect(result).toBeTruthy();
expect(adapter.isValid(result)).toBe(true);
expect(adapter.getHours(result)).toBe(11);
expect(adapter.getMinutes(result)).toBe(0);
expect(adapter.getSeconds(result)).toBe(0);
});

it('should parse a time string with characters around the time', () => {
adapter.setLocale('bg-BG');
const result = adapter.parseTime('14:52 ч.', 't')!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,15 @@ describe('MomentDateAdapter', () => {
expect(adapter.getSeconds(result)).toBe(0);
});

it('should parse a time string containing only hours', () => {
const result = adapter.parseTime('11', 'HH')!;
expect(result).toBeTruthy();
expect(adapter.isValid(result)).toBe(true);
expect(adapter.getHours(result)).toBe(11);
expect(adapter.getMinutes(result)).toBe(0);
expect(adapter.getSeconds(result)).toBe(0);
});

it('should parse a time string with characters around the time', () => {
adapter.setLocale('bg-BG');
const result = adapter.parseTime('14:52 ч.', 'LT')!;
Expand Down
Loading