Software /
code /
prosody
Changeset
13054:f4d7fe919969
util.human.io: Add parse_duration() method to parse a duration string
Similar logic occurs throughout various modules in the codebase. We might even
want a module:get_option_duration()??
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 07 Apr 2023 14:14:53 +0100 |
parents | 13053:8128c4f1b08b |
children | 13055:e732f9dfdfc8 |
files | spec/util_human_io_spec.lua util/human/io.lua |
diffstat | 2 files changed, 30 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/spec/util_human_io_spec.lua Fri Apr 07 14:03:24 2023 +0200 +++ b/spec/util_human_io_spec.lua Fri Apr 07 14:14:53 2023 +0100 @@ -42,6 +42,24 @@ assert.equal("räksmörgås", human_io.ellipsis("räksmörgås", 10)); end); end); + + describe("parse_duration", function () + local function test(expected, duration) + assert.equal(expected, human_io.parse_duration(duration)); + end + it("works", function () + test(1, "1s"); + test(60, "1mi"); + test(60, "1min"); + test(60, "1 min"); + test(60, "1 minute"); + test(120, "2min"); + test(86400, "1d"); + test(2678400, "1m"); + test(2678400, "1month"); + test(2678400, "1 month"); + end); + end); end);
--- a/util/human/io.lua Fri Apr 07 14:03:24 2023 +0200 +++ b/util/human/io.lua Fri Apr 07 14:14:53 2023 +0100 @@ -197,6 +197,17 @@ end, max_width; end +local day = 86400; +local multipliers = { + d = day, w = day * 7, m = 31 * day, mo = 31 * day, y = 365.2425 * day; + s = 1, mi = 60, h = 3600 +}; +local function parse_duration(duration_string) + local n, m = duration_string:lower():match("(%d+)%s*([dwmy]?.?)"); + if not n then return nil; end + return tonumber(n) * ( multipliers[m] or 1 ); +end + return { getchar = getchar; getline = getline; @@ -210,4 +221,5 @@ term_width = term_width; ellipsis = ellipsis; table = new_table; + parse_duration = parse_duration; };