Skip to content

Commit f2c9d8a

Browse files
committed
fix: remove hours limit for parseDuration
- Removes 23-hour limit on duration parsing - Allows parsing of durations like "PT36H" to work correctly - Aligns behavior with Temporal.Duration spec which has no upper limit on hours
1 parent 9421c14 commit f2c9d8a

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

packages/@internationalized/date/src/string.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export function parseDuration(value: string): Required<DateTimeDuration> {
251251
months: parseDurationGroup(match.groups?.months, isNegative, 0, 12),
252252
weeks: parseDurationGroup(match.groups?.weeks, isNegative, 0, Infinity),
253253
days: parseDurationGroup(match.groups?.days, isNegative, 0, 31),
254-
hours: parseDurationGroup(match.groups?.hours, isNegative, 0, 23),
254+
hours: parseDurationGroup(match.groups?.hours, isNegative, 0, Infinity),
255255
minutes: parseDurationGroup(match.groups?.minutes, isNegative, 0, 59),
256256
seconds: parseDurationGroup(match.groups?.seconds, isNegative, 0, 59)
257257
};

packages/@internationalized/date/tests/string.test.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,32 @@ describe('string conversion', function () {
534534
});
535535
});
536536

537+
it('parses an ISO 8601 duration string that contains years, months, hours, and seconds including hours exceeding 23 and returns a DateTimeDuration object', function () {
538+
const duration = parseDuration('P18Y7MT30H15S');
539+
expect(duration).toStrictEqual({
540+
years: 18,
541+
months: 7,
542+
weeks: 0,
543+
days: 0,
544+
hours: 30,
545+
minutes: 0,
546+
seconds: 15
547+
});
548+
});
549+
550+
it('parses an ISO 8601 duration string that contains years, months, hours, and seconds including hours exceeding 23 and returns a DateTimeDuration object', function () {
551+
const duration = parseDuration('PT36H');
552+
expect(duration).toStrictEqual({
553+
years: 0,
554+
months: 0,
555+
weeks: 0,
556+
days: 0,
557+
hours: 36,
558+
minutes: 0,
559+
seconds: 0
560+
});
561+
});
562+
537563
it('throws an error when passed an improperly formatted ISO 8601 duration string', function () {
538564
expect(() => {
539565
parseDuration('+-P18Y7MT20H15S');
@@ -550,9 +576,6 @@ describe('string conversion', function () {
550576
expect(() => {
551577
parseDuration('P18Y7MT');
552578
}).toThrow('Invalid ISO 8601 Duration string: P18Y7MT');
553-
expect(() => {
554-
parseDuration('P18Y7MT30H15S');
555-
}).toThrow('Invalid ISO 8601 Duration string: P18Y7MT30H15S');
556579
expect(() => {
557580
parseDuration('7Y6D85');
558581
}).toThrow('Invalid ISO 8601 Duration string: 7Y6D85');

0 commit comments

Comments
 (0)