# shellcheck    shell=sh

# NOTICE: In Docker, this will be the kernel uptime. Not the container uptime.
___x_cmd_os_uptime() {
    local x_=; ___x_cmd_os_uptime_ || return
    printf "%s\n" "$___X_CMD_OS_UPTIME_"
}

___x_cmd_os_uptime_() {
    x_=""
    if [ -z "$___X_CMD_OS_UPTIME_" ]; then
        ___x_cmd_os_name_
        case "$___X_CMD_OS_NAME_" in
            darwin | freebsd)
                eval "$({
                    command sysctl -q -n kern.boottime
                    command date +%s
                } | ___x_cmd_cmds_awk '
                    NR==1{ boot = $4; }
                    NR==2{ now  = $0; }
                    END{ print "___X_CMD_OS_UPTIME_=" ( now - boot ) }
                ')"
                ;;
            linux | win)
                ___X_CMD_OS_UPTIME_=$(cut -d' ' -f1 </proc/uptime)
                ;;
            openbsd)
                eval "$({
                    command sysctl -q -n kern.boottime
                    command date +%s
                } | ___x_cmd_cmds_awk '
                    NR==1{ boot = $0; }
                    NR==2{ now  = $0; }
                    END{ print "___X_CMD_OS_UPTIME_=" ( now - boot ) }
                ')"
                ;;
        esac
    fi

    x_="$___X_CMD_OS_UPTIME_"
}
