Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(core): Add Duration.minus method #16535

Closed
2 tasks
moltar opened this issue Sep 18, 2021 · 2 comments · Fixed by #16734
Closed
2 tasks

(core): Add Duration.minus method #16535

moltar opened this issue Sep 18, 2021 · 2 comments · Fixed by #16734
Labels
@aws-cdk/core Related to core CDK functionality effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2

Comments

@moltar
Copy link
Contributor

moltar commented Sep 18, 2021

There is already a Duration.plus method which allows to add two durations together.

However, there is no Duration.minus.

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);
}

Use Case

Ability to subtract one duration from another.

For example, a CloudWatch trigger that executes a lambda every 5 minutes.

At the same time, I want to avoid race conditions, so I want to set the Lambda timeout to slightly less than 5 minutes.

Proposed Solution

const ruleSchedule = Duration.minutes(5)
const lambdaTimeout = ruleSchedule.minus(Duration.seconds(10))

Other

I tried creating a Duration with a negative value, to then do simple math, but that is not allowed.

throw new Error(`Duration amounts cannot be negative. Received: ${amount}`);

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

@moltar moltar added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Sep 18, 2021
@github-actions github-actions bot added the @aws-cdk/core Related to core CDK functionality label Sep 18, 2021
@rix0rrr
Copy link
Contributor

rix0rrr commented Sep 29, 2021

Sounds like fun!

@rix0rrr rix0rrr added effort/small Small work item – less than a day of effort p2 and removed needs-triage This issue or PR still needs to be triaged. labels Sep 29, 2021
@rix0rrr rix0rrr removed their assignment Sep 29, 2021
jogold added a commit to jogold/aws-cdk that referenced this issue Sep 30, 2021
Add a `.subtract()` method to `Duration`.

Deprecate `.plus()` in favor of `.add()`, feels like a better naming.

Closes aws#16535
jogold added a commit to jogold/aws-cdk that referenced this issue Sep 30, 2021
Add a `.subtract()` method to `Duration`.

Deprecate `.plus()` in favor of `.add()`, feels like a better naming.

Closes aws#16535
@mergify mergify bot closed this as completed in #16734 Oct 29, 2021
mergify bot pushed a commit that referenced this issue Oct 29, 2021
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*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this issue Feb 21, 2022
Add a `.minus()` method to `Duration`.

Closes aws#16535


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/core Related to core CDK functionality effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants