diff --git a/packages/@aws-cdk/core/README.md b/packages/@aws-cdk/core/README.md index 79ac6e6bb3e4d..611467116459e 100644 --- a/packages/@aws-cdk/core/README.md +++ b/packages/@aws-cdk/core/README.md @@ -172,6 +172,13 @@ Duration.days(7) // 7 days Duration.parse('PT5M') // 5 minutes ``` +Durations can be added or subtracted together: + +```ts +Duration.minutes(1).plus(Duration.seconds(60)); // 2 minutes +Duration.minutes(5).minus(Duration.seconds(10)); // 290 secondes +``` + ## Size (Digital Information Quantity) To make specification of digital storage quantities unambiguous, a class called @@ -805,7 +812,7 @@ regionTable.findInMap('us-east-2', 'regionName'); ``` On the other hand, the following code will produce the "Mappings" section shown above, -since the top-level key is an unresolved token. The call to `findInMap` will return a token that resolves to +since the top-level key is an unresolved token. The call to `findInMap` will return a token that resolves to `{ "Fn::FindInMap": [ "RegionTable", { "Ref": "AWS::Region" }, "regionName" ] }`. ```ts diff --git a/packages/@aws-cdk/core/lib/duration.ts b/packages/@aws-cdk/core/lib/duration.ts index 0061e7928e900..5c7bb804b917c 100644 --- a/packages/@aws-cdk/core/lib/duration.ts +++ b/packages/@aws-cdk/core/lib/duration.ts @@ -105,8 +105,17 @@ export class Duration { */ public plus(rhs: Duration): Duration { const targetUnit = finestUnit(this.unit, rhs.unit); - const total = convert(this.amount, this.unit, targetUnit, {}) + convert(rhs.amount, rhs.unit, targetUnit, {}); - return new Duration(total, targetUnit); + const res = convert(this.amount, this.unit, targetUnit, {}) + convert(rhs.amount, rhs.unit, targetUnit, {}); + return new Duration(res, targetUnit); + } + + /** + * Substract two Durations together + */ + public minus(rhs: Duration): Duration { + const targetUnit = finestUnit(this.unit, rhs.unit); + const res = convert(this.amount, this.unit, targetUnit, {}) - convert(rhs.amount, rhs.unit, targetUnit, {}); + return new Duration(res, targetUnit); } /** diff --git a/packages/@aws-cdk/core/test/duration.test.ts b/packages/@aws-cdk/core/test/duration.test.ts index 68da12881d3ba..c7cf230308634 100644 --- a/packages/@aws-cdk/core/test/duration.test.ts +++ b/packages/@aws-cdk/core/test/duration.test.ts @@ -170,8 +170,12 @@ describe('duration', () => { expect(Duration.minutes(1).plus(Duration.seconds(30)).toSeconds()).toEqual(Duration.seconds(90).toSeconds()); expect(Duration.minutes(1).plus(Duration.seconds(30)).toMinutes({ integral: false })) .toEqual(Duration.seconds(90).toMinutes({ integral: false })); + }); - + test('subtract two durations', () => { + expect(Duration.minutes(1).minus(Duration.seconds(30)).toSeconds()).toEqual(Duration.seconds(30).toSeconds()); + expect(Duration.minutes(1).minus(Duration.seconds(30)).toMinutes({ integral: false })) + .toEqual(Duration.seconds(30).toMinutes({ integral: false })); }); test('get unit label from duration', () => { diff --git a/packages/aws-cdk-lib/README.md b/packages/aws-cdk-lib/README.md index 40fc7c0d880af..254a58738b26d 100644 --- a/packages/aws-cdk-lib/README.md +++ b/packages/aws-cdk-lib/README.md @@ -205,6 +205,13 @@ Duration.days(7) // 7 days Duration.parse('PT5M') // 5 minutes ``` +Durations can be added or subtracted together: + +```ts +Duration.minutes(1).plus(Duration.seconds(60)); // 2 minutes +Duration.minutes(5).minus(Duration.seconds(10)); // 290 secondes +``` + ## Size (Digital Information Quantity) To make specification of digital storage quantities unambiguous, a class called @@ -838,7 +845,7 @@ regionTable.findInMap('us-east-2', 'regionName'); ``` On the other hand, the following code will produce the "Mappings" section shown above, -since the top-level key is an unresolved token. The call to `findInMap` will return a token that resolves to +since the top-level key is an unresolved token. The call to `findInMap` will return a token that resolves to `{ "Fn::FindInMap": [ "RegionTable", { "Ref": "AWS::Region" }, "regionName" ] }`. ```ts