Peter Makowski has proposed merging ~petermakowski/maas-site-manager:fix-iso-duration-value into maas-site-manager:main.
Commit message: fix: use reduced iso duration format Requested reviews: MAAS Lander (maas-lander): unittests MAAS Committers (maas-committers) For more details, see: https://code.launchpad.net/~petermakowski/maas-site-manager/+git/site-manager/+merge/441958 -- Your team MAAS Committers is requested to review the proposed merge of ~petermakowski/maas-site-manager:fix-iso-duration-value into maas-site-manager:main.
diff --git a/frontend/src/components/TokensCreate/TokensCreate.test.tsx b/frontend/src/components/TokensCreate/TokensCreate.test.tsx index 2519f7f..550cc77 100644 --- a/frontend/src/components/TokensCreate/TokensCreate.test.tsx +++ b/frontend/src/components/TokensCreate/TokensCreate.test.tsx @@ -68,7 +68,7 @@ describe("TokensCreate", () => { expect(tokensMutationMock).toHaveBeenCalledTimes(1); expect(tokensMutationMock).toHaveBeenCalledWith({ amount: 1, - expires: "P0Y0M7DT0H0M0S", + expires: "P7DT0H0M0S", }); }); }); diff --git a/frontend/src/components/TokensCreate/utils.test.ts b/frontend/src/components/TokensCreate/utils.test.ts index 0271d0c..cbbded8 100644 --- a/frontend/src/components/TokensCreate/utils.test.ts +++ b/frontend/src/components/TokensCreate/utils.test.ts @@ -1,7 +1,13 @@ import { humanIntervalToISODuration } from "./utils"; describe("humanIntervalToISODuration", () => { + it("returns a valid ISO duration string for weeks, days, hours and seconds", () => { + expect(humanIntervalToISODuration("5 weeks 7 days 3 hours 30 seconds")).toEqual("P42DT3H0M30S"); + }); + it("returns a valid ISO duration string for weeks", () => { + expect(humanIntervalToISODuration("2 weeks")).toEqual("P14DT0H0M0S"); + }); it("returns a valid ISO duration string for hours and seconds", () => { - expect(humanIntervalToISODuration("1 week 1 days 3 hours 30 seconds")).toEqual("P0Y0M8DT3H0M30S"); + expect(humanIntervalToISODuration("1 hours 10 seconds")).toEqual("P0DT1H0M10S"); }); }); diff --git a/frontend/src/components/TokensCreate/utils.ts b/frontend/src/components/TokensCreate/utils.ts index 718d876..d23b6d2 100644 --- a/frontend/src/components/TokensCreate/utils.ts +++ b/frontend/src/components/TokensCreate/utils.ts @@ -1,9 +1,26 @@ -import { formatISODuration, intervalToDuration } from "date-fns"; import humanInterval from "human-interval"; +function intervalToDuration(ms: number) { + let seconds = Math.floor(ms / 1000); + const days = Math.floor(seconds / (24 * 3600)); + seconds %= 24 * 3600; + const hours = Math.floor(seconds / 3600); + seconds %= 3600; + const minutes = Math.floor(seconds / 60); + seconds %= 60; + return { + days, + hours, + minutes, + seconds, + }; +} + +// return ISO 8601 duration only using days, hours, minutes and seconds export const humanIntervalToISODuration = (intervalString: string) => { const intervalNumber = humanInterval(intervalString); if (intervalNumber) { - return formatISODuration(intervalToDuration({ start: 0, end: intervalNumber })); + const duration = intervalToDuration(intervalNumber); + return `P${duration.days}DT${duration.hours}H${duration.minutes}M${duration.seconds}S`; } };
-- Mailing list: https://launchpad.net/~sts-sponsors Post to : sts-sponsors@lists.launchpad.net Unsubscribe : https://launchpad.net/~sts-sponsors More help : https://help.launchpad.net/ListHelp