# shellcheck shell=dash

___x_cmd_tz_abbr(){
    local x_
    ___x_cmd_tz_abbr_ "$@" || return
    printf "%s\n" "$x_"
}

___x_cmd_tz_abbr_(){
    [ -n "$___X_CMD_TZ_ABBR" ] || ___X_CMD_TZ_ABBR="$( command date +%Z )"
    x_="$___X_CMD_TZ_ABBR"
}

___x_cmd_tz_name(){
    local x_
    ___x_cmd_tz_name_ "$@" || return
    printf "%s\n" "$x_"
}

# consider export ___X_CMD_TZ_NAME
___x_cmd_tz_name_(){
    if [ -z "$___X_CMD_TZ_NAME" ]; then
        if [ -n "$TZ" ]; then
            ___X_CMD_TZ_NAME="$TZ"
        elif [ -r /etc/timezone ]; then
            read -r ___X_CMD_TZ_NAME <"/etc/timezone"
        elif [ -e /etc/localtime ]; then
            # For macos
            ___X_CMD_TZ_NAME="$(___x_cmd_cmds readlink /etc/localtime)"
            ___X_CMD_TZ_NAME="${___X_CMD_TZ_NAME#*"/zoneinfo/"}"
        else
            [ $# -eq 0 ] || set -- --auto
            # TODO: Use offset to assign approximate timezone
            case "$1" in
                --fail)     return 1 ;;
                --auto)     ___x_cmd_tz_offset2name0_ || return; ___X_CMD_TZ_NAME="$x_" ;;
                *)          ___X_CMD_TZ_NAME="$1"    ;;
            esac
        fi
    fi
    # if [ -n "$TZ" ] && [ -e /etc/localtime ]; then
    #   readlink /etc/localtime
    # fi
    # TZ="$(date +%Z)"
    x_="$___X_CMD_TZ_NAME"
}

___x_cmd_tz_offset_(){
    [ -n "$___X_CMD_TZ_OFFSET" ] || ___X_CMD_TZ_OFFSET="$( ___x_cmd_cmds date +%z )"
    x_="$___X_CMD_TZ_OFFSET"
}

___x_cmd_tz_offset(){
    local x_
    ___x_cmd_tz_offset_ || return
    printf "%s\n" "$x_"
}

# Should be a hardcode map ...
___x_cmd_tz_offset2name0_(){
    ___x_cmd_tz_offset_
    case "$x_" in
        "+0000")    x_=Etc/UTC  ;;
        *)          return 1 ;;
    esac
}

