[Turn off line numbering]1: arg string, @units; 2: var words, len, nsec, n, i, entry, unit; 3: 4: [(units ?= time_units)] = units; 5: words = string.explode(); 6: words = words.setremove_all(["and"]); 7: if (listlen(words) == 1) { 8: if (!(words = regexp(words[1], "^([0-9]+)(.*)$"))) 9: throw(~args, "Invalid time."); 10: } 11: len = listlen(words); 12: if (len % 2) 13: throw(~args, "Unrecognized time '" + string + "', no descriptor."); 14: nsec = (n = 0); 15: for i in [1 .. len] { 16: if (i % 2 == 1) { 17: if (words[i].is_numeric()) 18: n = words[i].to_number(); 19: else if (words[i] in ["a", "an"]) 20: n = 1; 21: else if (words[i] == "no") 22: n = 0; 23: else 24: throw(~invarg, "Invalid time element '" + n + "'."); 25: } else { 26: unit = words[i]; 27: unit = unit.strip(","); 28: nsec += (> units[.parse_unit(unit, units)][1] <) * n; 29: } 30: } 31: return nsec;
// Created 19-Nov-1996 as a part of ColdCore, see: @help Credit