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

aws-cdk: Duration.parse() does not handle units larger then days #29572

Closed
mattaschmann opened this issue Mar 21, 2024 · 2 comments · Fixed by #29576
Closed

aws-cdk: Duration.parse() does not handle units larger then days #29572

mattaschmann opened this issue Mar 21, 2024 · 2 comments · Fixed by #29576
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort p2 package/tools Related to AWS CDK Tools or CLI

Comments

@mattaschmann
Copy link

Describe the bug

While trying to use cdk.Duration.parse() with the strings P1Y and P1M it threw errors. Looking at the source (

public static parse(duration: string): Duration {
const matches = duration.match(/^P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/);
if (!matches) {
throw new Error(`Not a valid ISO duration: ${duration}`);
}
const [, days, hours, minutes, seconds] = matches;
if (!days && !hours && !minutes && !seconds) {
throw new Error(`Not a valid ISO duration: ${duration}`);
}
return Duration.millis(
_toInt(seconds) * TimeUnit.Seconds.inMillis
+ (_toInt(minutes) * TimeUnit.Minutes.inMillis)
+ (_toInt(hours) * TimeUnit.Hours.inMillis)
+ (_toInt(days) * TimeUnit.Days.inMillis),
);
function _toInt(str: string): number {
if (!str) { return 0; }
return Number(str);
}
}
) it's obvious that any unit larger then days are not handled.

Expected Behavior

As the documentation for this message states:

    /**
     * Parse a period formatted according to the ISO 8601 standard
     *
     * @see https://www.iso.org/standard/70907.html
     * @param duration an ISO-formatted duration to be parsed.
     * @returns the parsed `Duration`.
     */

It should correctly handle all valid ISO 8601 strings. Either that, or the documentation needs to indicate it doesn't handled anything larger then days.

Current Behavior

Throws an error on any unit larger then a day.

Reproduction Steps

import * as cdk from 'aws-cdk-lib'

cdk.Duration.parse('P1Y')
cdk.Duration.parse('P1M')

Possible Solution

Add larger units to the regular expression and handle them, or change the documentation to indicate that they are not handled.

Additional Information/Context

No response

CDK CLI Version

2.115.0 (build 58027ee)

Framework Version

No response

Node.js Version

v18.15.0

OS

macOS: 14.3.1 (23D60)

Language

TypeScript

Language Version

No response

Other information

No response

@mattaschmann mattaschmann added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 21, 2024
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Mar 21, 2024
@pahud
Copy link
Contributor

pahud commented Mar 22, 2024

Thank you for the report.

And @msambol thanks for your PR as always!

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Mar 22, 2024
@mergify mergify bot closed this as completed in #29576 Mar 22, 2024
mergify bot pushed a commit that referenced this issue Mar 22, 2024
Closes #29572.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
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.

jun1-t pushed a commit to jun1-t/aws-cdk that referenced this issue Mar 23, 2024
Closes aws#29572.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
ahammond pushed a commit to ahammond/aws-cdk that referenced this issue Mar 26, 2024
Closes aws#29572.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@aws aws locked as resolved and limited conversation to collaborators Jul 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort p2 package/tools Related to AWS CDK Tools or CLI
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants