diff --git a/lib/util/pretty-time.spec.ts b/lib/util/pretty-time.spec.ts index dff57198a077362984972f15e7e852b329983d02..a9f291e467a9c6e19e0a9ec85e359b727d82675d 100644 --- a/lib/util/pretty-time.spec.ts +++ b/lib/util/pretty-time.spec.ts @@ -16,6 +16,14 @@ describe('util/pretty-time', () => { ${'1hour 1 min 1s'} | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1000} ${'1h 1m 1s 1ms'} | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1000 + 1} ${'3 days'} | ${3 * 24 * 60 * 60 * 1000} + ${'1 week'} | ${7 * 24 * 60 * 60 * 1000} + ${'1 month'} | ${30 * 24 * 60 * 60 * 1000} + ${'1 M'} | ${30 * 24 * 60 * 60 * 1000} + ${'2 months'} | ${2 * 30 * 24 * 60 * 60 * 1000} + ${'1month'} | ${30 * 24 * 60 * 60 * 1000} + ${'1M'} | ${30 * 24 * 60 * 60 * 1000} + ${'2months'} | ${2 * 30 * 24 * 60 * 60 * 1000} + ${'1 year'} | ${365.25 * 24 * 60 * 60 * 1000} ${'0'.repeat(100)} | ${0} ${'0'.repeat(101)} | ${null} ${'1 whatever'} | ${null} diff --git a/lib/util/pretty-time.ts b/lib/util/pretty-time.ts index 63d88a598c71ad75b777291348f274e24d7e5a97..c34e9098119338a159136ac3333c989e7f0d4ae9 100644 --- a/lib/util/pretty-time.ts +++ b/lib/util/pretty-time.ts @@ -7,15 +7,19 @@ const splitRegex = regEx(/(.*?[a-z]+)/); function split(time: string): string[] { return time - .toLocaleLowerCase() .split(splitRegex) .map((x) => x.trim()) .filter(is.nonEmptyString); } +function applyCustomFormat(spec: string): string { + const monthRegex = regEx(/^(\d+)\s*(?:months?|M)$/); + return spec.replace(monthRegex, (_, months) => `${months * 30} days`); +} + export function toMs(time: string): number | null { try { - const specs = split(time); + const specs = split(time).map(applyCustomFormat); if (!specs.length) { logger.debug({ time }, `Invalid time specifier: '${time}'`); return null;