diff options
author | Matthew Wild <mwild1@gmail.com> | 2023-11-30 13:42:44 +0000 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2023-11-30 13:42:44 +0000 |
commit | a669ffb5a2ea3d73e7f7c460deef52579d5ba57b (patch) | |
tree | 93196b86d1773a8310a596d4a1e9d709c2cd87e8 /util/human | |
parent | 56d45091bdcc75292a73a789a017b36721efa2e6 (diff) | |
download | prosody-a669ffb5a2ea3d73e7f7c460deef52579d5ba57b.tar.gz prosody-a669ffb5a2ea3d73e7f7c460deef52579d5ba57b.zip |
util.human.io: Don't accept ambiguous durations by default
The new method parse_duration_lax() exports the old behaviour, mainly for
compatibility purposes.
Diffstat (limited to 'util/human')
-rw-r--r-- | util/human/io.lua | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/util/human/io.lua b/util/human/io.lua index 5af8d13e..d6112b3b 100644 --- a/util/human/io.lua +++ b/util/human/io.lua @@ -200,13 +200,26 @@ 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, ho = 3600 + d = day, w = day * 7, mon = 31 * day, y = 365.2425 * day; + s = 1, min = 60, h = 3600, ho = 3600 }; + local function parse_duration(duration_string) + local n, m = duration_string:lower():match("(%d+)%s*([smhdwy]?[io]?n?)"); + if not n or not multipliers[m] then return nil; end + return tonumber(n) * ( multipliers[m] or 1 ); +end + +local multipliers_lax = setmetatable({ + m = multipliers.mon; + mo = multipliers.mon; + mi = multipliers.min; +}, { __index = multipliers }); + +local function parse_duration_lax(duration_string) local n, m = duration_string:lower():match("(%d+)%s*([smhdwy]?[io]?)"); if not n then return nil; end - return tonumber(n) * ( multipliers[m] or 1 ); + return tonumber(n) * ( multipliers_lax[m] or 1 ); end return { @@ -223,4 +236,5 @@ return { ellipsis = ellipsis; table = new_table; parse_duration = parse_duration; + parse_duration_lax = parse_duration_lax; }; |