Comparison

util/human/io.lua @ 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
parent 13051:164c2787901a
child 13067:386ca97bec5b
comparison
equal deleted inserted replaced
13053:8128c4f1b08b 13054:f4d7fe919969
195 end 195 end
196 return table.concat(output, separator); 196 return table.concat(output, separator);
197 end, max_width; 197 end, max_width;
198 end 198 end
199 199
200 local day = 86400;
201 local multipliers = {
202 d = day, w = day * 7, m = 31 * day, mo = 31 * day, y = 365.2425 * day;
203 s = 1, mi = 60, h = 3600
204 };
205 local function parse_duration(duration_string)
206 local n, m = duration_string:lower():match("(%d+)%s*([dwmy]?.?)");
207 if not n then return nil; end
208 return tonumber(n) * ( multipliers[m] or 1 );
209 end
210
200 return { 211 return {
201 getchar = getchar; 212 getchar = getchar;
202 getline = getline; 213 getline = getline;
203 getpass = getpass; 214 getpass = getpass;
204 show_yesno = show_yesno; 215 show_yesno = show_yesno;
208 padleft = padleft; 219 padleft = padleft;
209 padright = padright; 220 padright = padright;
210 term_width = term_width; 221 term_width = term_width;
211 ellipsis = ellipsis; 222 ellipsis = ellipsis;
212 table = new_table; 223 table = new_table;
224 parse_duration = parse_duration;
213 }; 225 };