
___x_cmd_rand_uuidv7(){
    local x_
    ___x_cmd_rand_uuidv7_
    printf "%s\n" "$x_"
}

___x_cmd_rand_uuidv7_(){
    x_=""
    ___x_cmd_rand_uuidv7_ts_

    local randstr; randstr=$(dd if=/dev/urandom bs=1 count=16 2>/dev/null | xxd -p)
    local tmp="${randstr#???}"
    fst=${randstr%"$tmp"}
    randstr="${tmp#???}"
    sec=${tmp%"$randstr"}

    randstr="${randstr%"${randstr#????????????}"}"

    # TODO: a could b "$___X_CMD_LOCALE_DEF_C" d
    x_="${x_}$fst-a$sec-$randstr"
}


# Consider using awk ...
___x_cmd_rand_uuidv7_ts_(){
    ___x_cmd_rand_uuidv7_mill3_
    x_="$(printf "%012x" "$x_")"
    local prefix="${x_%????}"
    x_="${prefix}-${x_#"${prefix}"}-7"
}


___x_cmd_rand_uuidv7_mill3_(){
    x_=$EPOCHREALTIME
    if [ -n "$x_" ]; then
        x_="$(___x_cmd_cmds awk "BEGIN { printf \"%.0f\", $x_ * 1000 }")"
        x_="${x_%.*}"
        return
    fi

    x_="$(date +%s%3N)"
    case "$x_" in
        *3N)
            x_="${x_%3N}000"
            ;;
    esac
}

