# HG changeset patch # User Matthew Wild # Date 1680873293 -3600 # Node ID f4d7fe919969d81ff42311a58dbe77c23eb9bda1 # Parent 8128c4f1b08b6fb96873ab9512c0d41ed1e788de 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()?? diff -r 8128c4f1b08b -r f4d7fe919969 spec/util_human_io_spec.lua --- 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); diff -r 8128c4f1b08b -r f4d7fe919969 util/human/io.lua --- 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; };