# shellcheck shell=dash

xrc:mod:lib date    epochminus
___x_cmd_date___main(){
    local op="$1"
    [ -z "${op}" ] || shift
    case "$op" in
        ts|iso|str|varname|varname0|vlid|timestamp|iso2ts|counter|diff|epochminus)
            ___x_cmd_date_"$op" "$@"
        ;;
        -h|--help)
            x help -m date "$@"
        ;;
        *)
            ___x_cmd_date_iso "$@"
    esac
}

___x_cmd_date_iso(){
    command date +"%Y-%m-%d %H:%M:%S%z"
}

___x_cmd_date_str(){
    command date +"%Y-%m-%d_%H:%M:%S%z"
}

___x_cmd_date_varname(){
    command date +"%Y_%m_%d_%H_%M_%S%z" | tr '+' '_'
}

___x_cmd_date_varname0(){
    TZ=UTC0 command date +"%Y_%m_%d_%H_%M_%S%z" | tr '+' '_'
}

# TODO: Using UTC-00
# variable length ID
# In 22 century, just add 1 digit before the result. This function will be modified (possibly by AI) at that time if needed. Definitly I(L) want to live long enough to witness.
# In 100 centure, just add 2 digit before the result. Human will envolve in this way and definity don't need to worry about it.
___x_cmd_date_vlid(){
    TZ=UTC0 command date +"%y%m%d_%H%M%S"
}

___x_cmd_date_timestamp(){
    if [ 0 -eq "$#" ]; then
        if [ -n "$EPOCHSECONDS" ]; then
            printf "%s\n" "$EPOCHSECONDS"
        elif [ -n "$EPOCHREALTIME" ]; then
            printf "%s\n" "${EPOCHREALTIME%.*}"
        else
            command date +%s
        fi
    else
        ___x_cmd_date_iso2ts "$@"
    fi
}

# x date iso2ts '1990-3-25 0:0:0+8000'
___x_cmd_date_iso2ts(){
    if ! date -j 1>/dev/null 2>/dev/null; then
        ___x_cmd_date_iso2ts(){
            command date -d "$1" +%s
        }
    else
        ___x_cmd_date_iso2ts(){
            case "$1" in
                *-*-*\ *:*:*)
                    command date -j -f "%Y-%m-%d %H:%M:%S%z" "$1" +%s
                    ;;
                *-*-*)
                    command date -j -f "%Y-%m-%d" "$1" +%s
                    ;;
                *-*)
                    command date -j -f "%Y-%m" "$1" +%s
                    ;;
                *)
                    x is int $1 || return 1
                    if [ "$1" -gt 9999 ]; then
                        command date -j -f "%Y%m%d" "$1" +%s
                    else
                        command date -j -f "%m%d" "$1" +%s
                    fi
                    ;;
            esac
        }
    fi

    ___x_cmd_date_iso2ts "$@"
}

___x_cmd_date_counter(){
    # local var="${name}"
    :
}


___x_cmd_date_diff_(){
    [ -n "$1" ] || return 1
    local a; a="$(___x_cmd_date_iso2ts "$1")"
    local b;
    if [ -z "$2" ]; then    b="$(date +%s)"
    else                    b="$(___x_cmd_date_iso2ts "$2")"
    fi

    if [ "$a" -gt "$b" ]; then      x_="$(( (a - b) / 60 / 60 / 24 ))"
    else                            x_="$(( (b - a) / 60 / 60 / 24 ))"
    fi
}

# L: 20230214: To answer the embarrass question like how may days since ...
___x_cmd_date_diff(){
    local x_
    ___x_cmd_date_diff_ "$@"
    printf "%s\n" "$x_"
}

___x_cmd_date_ts(){
    local ts="$1"

    local x_; x os name_;
    case "$x_" in
        darwin)     command date -j -f "%s" "$ts" +"%Y_%m_%d_%H_%M_%S%z" ;;
        *)          command date +"%Y_%m_%d_%H_%M_%S%z" -d @"$ts" ;;
    esac
}
