Skip to content

Commit

Permalink
fix: remove hours limit for parseDuration
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
limkhl committed Sep 22, 2024
1 parent 9421c14 commit f2c9d8a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/@internationalized/date/src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export function parseDuration(value: string): Required<DateTimeDuration> {
months: parseDurationGroup(match.groups?.months, isNegative, 0, 12),
weeks: parseDurationGroup(match.groups?.weeks, isNegative, 0, Infinity),
days: parseDurationGroup(match.groups?.days, isNegative, 0, 31),
hours: parseDurationGroup(match.groups?.hours, isNegative, 0, 23),
hours: parseDurationGroup(match.groups?.hours, isNegative, 0, Infinity),
minutes: parseDurationGroup(match.groups?.minutes, isNegative, 0, 59),
seconds: parseDurationGroup(match.groups?.seconds, isNegative, 0, 59)
};
Expand Down
29 changes: 26 additions & 3 deletions packages/@internationalized/date/tests/string.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,32 @@ describe('string conversion', function () {
});
});

it('parses an ISO 8601 duration string that contains years, months, hours, and seconds including hours exceeding 23 and returns a DateTimeDuration object', function () {
const duration = parseDuration('P18Y7MT30H15S');
expect(duration).toStrictEqual({
years: 18,
months: 7,
weeks: 0,
days: 0,
hours: 30,
minutes: 0,
seconds: 15
});
});

it('parses an ISO 8601 duration string that contains years, months, hours, and seconds including hours exceeding 23 and returns a DateTimeDuration object', function () {
const duration = parseDuration('PT36H');
expect(duration).toStrictEqual({
years: 0,
months: 0,
weeks: 0,
days: 0,
hours: 36,
minutes: 0,
seconds: 0
});
});

it('throws an error when passed an improperly formatted ISO 8601 duration string', function () {
expect(() => {
parseDuration('+-P18Y7MT20H15S');
Expand All @@ -550,9 +576,6 @@ describe('string conversion', function () {
expect(() => {
parseDuration('P18Y7MT');
}).toThrow('Invalid ISO 8601 Duration string: P18Y7MT');
expect(() => {
parseDuration('P18Y7MT30H15S');
}).toThrow('Invalid ISO 8601 Duration string: P18Y7MT30H15S');
expect(() => {
parseDuration('7Y6D85');
}).toThrow('Invalid ISO 8601 Duration string: 7Y6D85');
Expand Down

0 comments on commit f2c9d8a

Please sign in to comment.