function _M.parse_time_unit()

in apisix/core/config_util.lua [146:216]


function _M.parse_time_unit(s)
    local typ = type(s)
    if typ == "number" then
        return s
    end

    if typ ~= "string" or #s == 0 then
        return nil, "invalid data: " .. tostring(s)
    end

    local size = 0
    local size_in_unit = 0
    local step = 60 * 60 * 24 * 365
    local with_ms = false
    for i = 1, #s do
        local scale
        local unit = str_byte(s, i)
        if unit == 121 then 
            scale = 60 * 60 * 24 * 365
        elseif unit == 77 then 
            scale = 60 * 60 * 24 * 30
        elseif unit == 119 then 
            scale = 60 * 60 * 24 * 7
        elseif unit == 100 then 
            scale = 60 * 60 * 24
        elseif unit == 104 then 
            scale = 60 * 60
        elseif unit == 109 then 
            unit = str_byte(s, i + 1)
            if unit == 115 then 
                size = size * 1000
                with_ms = true
                step = 0
                break
            end

            scale = 60

        elseif unit == 115 then 
            scale = 1
        elseif 48 <= unit and unit <= 57 then
            size_in_unit = size_in_unit * 10 + unit - 48
        elseif unit ~= 32 then
            return nil, "invalid data: " .. str_char(unit)
        end

        if scale ~= nil then
            if scale > step then
                return nil, "unexpected unit: " .. str_char(unit)
            end

            step = scale
            size = size + scale * size_in_unit
            size_in_unit = 0
        end
    end

    if size_in_unit > 0 then
        if step == 1 then
            return nil, "specific unit conflicts with the default unit second"
        end

        size = size + size_in_unit
    end

    if with_ms then
        size = size / 1000
    end

    return size
end