Skip to content

Commit 2d1f8d0

Browse files
lekhmanrusmmalerba
authored andcommitted
fix(material-date-fns-adapter): parse time string containing only hours (#31978)
close #31837 (cherry picked from commit b61d884)
1 parent a327b08 commit 2d1f8d0

File tree

4 files changed

+67
-30
lines changed

4 files changed

+67
-30
lines changed

src/material-date-fns-adapter/adapter/date-fns-adapter.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,15 @@ describe('DateFnsAdapter', () => {
535535
expect(adapter.getSeconds(result)).toBe(0);
536536
});
537537

538+
it('should parse a time string containing only hours', () => {
539+
const result = adapter.parseTime('11', 'HH')!;
540+
expect(result).toBeTruthy();
541+
expect(adapter.isValid(result)).toBe(true);
542+
expect(adapter.getHours(result)).toBe(11);
543+
expect(adapter.getMinutes(result)).toBe(0);
544+
expect(adapter.getSeconds(result)).toBe(0);
545+
});
546+
538547
it('should return an invalid date when parsing invalid time string', () => {
539548
expect(adapter.isValid(adapter.parseTime('abc', 'p')!)).toBe(false);
540549
expect(adapter.isValid(adapter.parseTime('123', 'p')!)).toBe(false);

src/material-date-fns-adapter/adapter/date-fns-adapter.ts

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -162,35 +162,7 @@ export class DateFnsAdapter extends DateAdapter<Date, Locale> {
162162
}
163163

164164
parse(value: unknown, parseFormat: string | string[]): Date | null {
165-
if (typeof value == 'string' && value.length > 0) {
166-
const iso8601Date = parseISO(value);
167-
168-
if (this.isValid(iso8601Date)) {
169-
return iso8601Date;
170-
}
171-
172-
const formats = Array.isArray(parseFormat) ? parseFormat : [parseFormat];
173-
174-
if (!parseFormat.length) {
175-
throw Error('Formats array must not be empty.');
176-
}
177-
178-
for (const currentFormat of formats) {
179-
const fromFormat = parse(value, currentFormat, new Date(), {locale: this.locale});
180-
181-
if (this.isValid(fromFormat)) {
182-
return fromFormat;
183-
}
184-
}
185-
186-
return this.invalid();
187-
} else if (typeof value === 'number') {
188-
return new Date(value);
189-
} else if (value instanceof Date) {
190-
return this.clone(value);
191-
}
192-
193-
return null;
165+
return this._parse(value, parseFormat);
194166
}
195167

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

280252
override parseTime(value: unknown, parseFormat: string | string[]): Date | null {
281-
return this.parse(value, parseFormat);
253+
return this._parse(value, parseFormat, false);
282254
}
283255

284256
override addSeconds(date: Date, amount: number): Date {
285257
return addSeconds(date, amount);
286258
}
259+
260+
private _parse(
261+
value: unknown,
262+
parseFormat: string | string[],
263+
shouldTryParseIso = true,
264+
): Date | null {
265+
if (typeof value == 'string' && value.length > 0) {
266+
if (shouldTryParseIso) {
267+
const iso8601Date = parseISO(value);
268+
269+
if (this.isValid(iso8601Date)) {
270+
return iso8601Date;
271+
}
272+
}
273+
274+
const formats = Array.isArray(parseFormat) ? parseFormat : [parseFormat];
275+
276+
if (!parseFormat.length) {
277+
throw Error('Formats array must not be empty.');
278+
}
279+
280+
for (const currentFormat of formats) {
281+
const fromFormat = parse(value, currentFormat, new Date(), {locale: this.locale});
282+
283+
if (this.isValid(fromFormat)) {
284+
return fromFormat;
285+
}
286+
}
287+
288+
return this.invalid();
289+
} else if (typeof value === 'number') {
290+
return new Date(value);
291+
} else if (value instanceof Date) {
292+
return this.clone(value);
293+
}
294+
295+
return null;
296+
}
287297
}

src/material-luxon-adapter/adapter/luxon-date-adapter.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,15 @@ describe('LuxonDateAdapter', () => {
633633
expect(adapter.getSeconds(result)).toBe(0);
634634
});
635635

636+
it('should parse a time string containing only hours', () => {
637+
const result = adapter.parseTime('11', 'HH')!;
638+
expect(result).toBeTruthy();
639+
expect(adapter.isValid(result)).toBe(true);
640+
expect(adapter.getHours(result)).toBe(11);
641+
expect(adapter.getMinutes(result)).toBe(0);
642+
expect(adapter.getSeconds(result)).toBe(0);
643+
});
644+
636645
it('should parse a time string with characters around the time', () => {
637646
adapter.setLocale('bg-BG');
638647
const result = adapter.parseTime('14:52 ч.', 't')!;

src/material-moment-adapter/adapter/moment-date-adapter.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,15 @@ describe('MomentDateAdapter', () => {
614614
expect(adapter.getSeconds(result)).toBe(0);
615615
});
616616

617+
it('should parse a time string containing only hours', () => {
618+
const result = adapter.parseTime('11', 'HH')!;
619+
expect(result).toBeTruthy();
620+
expect(adapter.isValid(result)).toBe(true);
621+
expect(adapter.getHours(result)).toBe(11);
622+
expect(adapter.getMinutes(result)).toBe(0);
623+
expect(adapter.getSeconds(result)).toBe(0);
624+
});
625+
617626
it('should parse a time string with characters around the time', () => {
618627
adapter.setLocale('bg-BG');
619628
const result = adapter.parseTime('14:52 ч.', 'LT')!;

0 commit comments

Comments
 (0)