Skip to content

Commit 8ca4c09

Browse files
authored
fix(core): Duration.parse() doesn't parse milliseconds (#25010)
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*
1 parent 2ea3e45 commit 8ca4c09

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

packages/aws-cdk-lib/core/lib/duration.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,18 @@ export class Duration {
6767
* @returns the parsed `Duration`.
6868
*/
6969
public static parse(duration: string): Duration {
70-
const matches = duration.match(/^P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/);
70+
const matches = duration.match(/^P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)\.?(\d{1,3})?S)?)?$/);
7171
if (!matches) {
7272
throw new Error(`Not a valid ISO duration: ${duration}`);
7373
}
74-
const [, days, hours, minutes, seconds] = matches;
75-
if (!days && !hours && !minutes && !seconds) {
74+
const [, days, hours, minutes, seconds, milliseconds] = matches;
75+
if (!days && !hours && !minutes && !seconds && !milliseconds) {
7676
throw new Error(`Not a valid ISO duration: ${duration}`);
7777
}
78+
const millis = milliseconds ? milliseconds.padEnd(3, '0') : '';
7879
return Duration.millis(
79-
_toInt(seconds) * TimeUnit.Seconds.inMillis
80+
_toInt(millis)
81+
+ _toInt(seconds) * TimeUnit.Seconds.inMillis
8082
+ (_toInt(minutes) * TimeUnit.Minutes.inMillis)
8183
+ (_toInt(hours) * TimeUnit.Hours.inMillis)
8284
+ (_toInt(days) * TimeUnit.Days.inMillis),
@@ -251,8 +253,8 @@ export class Duration {
251253
}
252254
}
253255

254-
// Remainder in millis
255-
if (millis > 0) {
256+
// Remainder in millis (keep only above threshold)
257+
if (millis > 0.0001) {
256258
ret.push([millis, TimeUnit.Milliseconds]);
257259
}
258260
return ret;

packages/aws-cdk-lib/core/test/duration.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,25 @@ describe('duration', () => {
9898

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

103104
test('parse', () => {
105+
expect(Duration.parse('PT0.000S').toMilliseconds()).toEqual(0);
104106
expect(Duration.parse('PT0S').toSeconds()).toEqual(0);
105107
expect(Duration.parse('PT0M').toSeconds()).toEqual(0);
106108
expect(Duration.parse('PT0H').toSeconds()).toEqual(0);
107109
expect(Duration.parse('P0D').toSeconds()).toEqual(0);
108110

111+
expect(Duration.parse('PT0.005S').toMilliseconds()).toEqual(5);
112+
expect(Duration.parse('PT0.5S').toMilliseconds()).toEqual(500);
109113
expect(Duration.parse('PT5S').toSeconds()).toEqual(5);
110114
expect(Duration.parse('PT5M').toSeconds()).toEqual(300);
111115
expect(Duration.parse('PT5H').toSeconds()).toEqual(18_000);
112116
expect(Duration.parse('P5D').toSeconds()).toEqual(432_000);
113117

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

117122
test('reject illegal parses', () => {

0 commit comments

Comments
 (0)