Skip to content

Commit

Permalink
fix(core): Duration.parse() doesn't parse milliseconds (#25010)
Browse files Browse the repository at this point in the history
Added millisecond parsing to [Duration](https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/core/lib/duration.ts).
`Duration.parse('PT0.005S').toMilliseconds()` is now correctly resolved to `5`.

Closes #24971.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
lpizzinidev committed Apr 18, 2023
1 parent 2ea3e45 commit 8ca4c09
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/aws-cdk-lib/core/lib/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,18 @@ export class Duration {
* @returns the parsed `Duration`.
*/
public static parse(duration: string): Duration {
const matches = duration.match(/^P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/);
const matches = duration.match(/^P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)\.?(\d{1,3})?S)?)?$/);
if (!matches) {
throw new Error(`Not a valid ISO duration: ${duration}`);
}
const [, days, hours, minutes, seconds] = matches;
if (!days && !hours && !minutes && !seconds) {
const [, days, hours, minutes, seconds, milliseconds] = matches;
if (!days && !hours && !minutes && !seconds && !milliseconds) {
throw new Error(`Not a valid ISO duration: ${duration}`);
}
const millis = milliseconds ? milliseconds.padEnd(3, '0') : '';
return Duration.millis(
_toInt(seconds) * TimeUnit.Seconds.inMillis
_toInt(millis)
+ _toInt(seconds) * TimeUnit.Seconds.inMillis
+ (_toInt(minutes) * TimeUnit.Minutes.inMillis)
+ (_toInt(hours) * TimeUnit.Hours.inMillis)
+ (_toInt(days) * TimeUnit.Days.inMillis),
Expand Down Expand Up @@ -251,8 +253,8 @@ export class Duration {
}
}

// Remainder in millis
if (millis > 0) {
// Remainder in millis (keep only above threshold)
if (millis > 0.0001) {
ret.push([millis, TimeUnit.Milliseconds]);
}
return ret;
Expand Down
5 changes: 5 additions & 0 deletions packages/aws-cdk-lib/core/test/duration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,25 @@ describe('duration', () => {

expect(Duration.seconds(65).toIsoString()).toEqual('PT1M5S');
expect(Duration.seconds(1 + 60 * (1 + 60 * (1 + 24))).toIsoString()).toEqual('P1DT1H1M1S');
expect(Duration.millis(1 + (1000 * (1 + 60 * (1 + 60 * (1 + 24))))).toIsoString()).toEqual('P1DT1H1M1.001S');
});

test('parse', () => {
expect(Duration.parse('PT0.000S').toMilliseconds()).toEqual(0);
expect(Duration.parse('PT0S').toSeconds()).toEqual(0);
expect(Duration.parse('PT0M').toSeconds()).toEqual(0);
expect(Duration.parse('PT0H').toSeconds()).toEqual(0);
expect(Duration.parse('P0D').toSeconds()).toEqual(0);

expect(Duration.parse('PT0.005S').toMilliseconds()).toEqual(5);
expect(Duration.parse('PT0.5S').toMilliseconds()).toEqual(500);
expect(Duration.parse('PT5S').toSeconds()).toEqual(5);
expect(Duration.parse('PT5M').toSeconds()).toEqual(300);
expect(Duration.parse('PT5H').toSeconds()).toEqual(18_000);
expect(Duration.parse('P5D').toSeconds()).toEqual(432_000);

expect(Duration.parse('P1DT1H1M1S').toSeconds()).toEqual(1 + 60 * (1 + 60 * (1 + 24)));
expect(Duration.parse('P1DT1H1M1.001S').toMilliseconds()).toEqual(1 + (1000 * (1 + 60 * (1 + 60 * (1 + 24)))));
});

test('reject illegal parses', () => {
Expand Down

0 comments on commit 8ca4c09

Please sign in to comment.