feat(parse): add parse_duration_to_days for duration string conversion
Problem: The archive command accepted only a bare integer for days, inconsistent with the `+Nd`/`+Nw`/`+Nm` duration syntax used elsewhere. Solution: Add `parse_duration_to_days()` supporting `Nd`, `Nw`, `Nm`, and bare integers. Returns nil on invalid input for caller error handling.
This commit is contained in:
parent
c5d55e5e92
commit
8cfdafe464
2 changed files with 67 additions and 0 deletions
|
|
@ -667,4 +667,29 @@ function M.is_today(due)
|
|||
return time_part >= current_time
|
||||
end
|
||||
|
||||
---@param s? string
|
||||
---@return integer?
|
||||
function M.parse_duration_to_days(s)
|
||||
if s == nil or s == '' then
|
||||
return nil
|
||||
end
|
||||
local n = s:match('^(%d+)d$')
|
||||
if n then
|
||||
return tonumber(n) --[[@as integer]]
|
||||
end
|
||||
n = s:match('^(%d+)w$')
|
||||
if n then
|
||||
return tonumber(n) --[[@as integer]] * 7
|
||||
end
|
||||
n = s:match('^(%d+)m$')
|
||||
if n then
|
||||
return tonumber(n) --[[@as integer]] * 30
|
||||
end
|
||||
n = s:match('^(%d+)$')
|
||||
if n then
|
||||
return tonumber(n) --[[@as integer]]
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue