Skip to content

Commit

Permalink
feat(core): subtract Durations (#16734)
Browse files Browse the repository at this point in the history
Add a `.minus()` method to `Duration`.

Closes #16535


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jogold authored Oct 29, 2021
1 parent 0a23953 commit 7a333b0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
9 changes: 8 additions & 1 deletion packages/@aws-cdk/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions packages/@aws-cdk/core/lib/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion packages/@aws-cdk/core/test/duration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
9 changes: 8 additions & 1 deletion packages/aws-cdk-lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7a333b0

Please sign in to comment.