Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

DWIM interface to date args; reverse array



time-from () {
    local from=$1
    local to=${2:-now}

    local -a order

    ### from
    local -a from_items
    from_items=(${(s:-:)from})

    # instead of doing this one could reverse the original data array
    # I Googled "zsh reverse array" and found ${(Oa)array}
    # but seems to only work when I echo it,
    # not assign it to another array
    (( $from_items[1] < $from_items[3] )) && order=(d m y) || order=(y m d)

    local from_y=$from_items[${order[(i)y]}]
    local from_m=$from_items[${order[(i)m]}] # always 2
    local from_d=$from_items[${order[(i)d]}]
    from=${from_y}-${from_m}-${from_d}

    ### to
    if [[ $to != now ]]; then
        local -a to_items
        to_items=(${(s:-:)to})

        (( $to_items[1] < $to_items[3] )) && order=(d m y) || order=(y m d)

        local to_y=$to_items[${order[(i)y]}]
        local to_m=$to_items[${order[(i)m]}] # ditto
        local to_d=$to_items[${order[(i)d]}]
        to=${to_y}-${to_m}-${to_d}
    fi

    ### compute
    local ymd
    local day
    ymd=$(dateutils.ddiff $from $to -f '%Yy %mm %dd')
    day=$(dateutils.ddiff $from $to -f 'total %dd')

    ### output
    echo "${ymd// 0(y|m|d)/} ($day)"
}

-- 
underground experts united
http://user.it.uu.se/~embe8573



Messages sorted by: Reverse Date, Date, Thread, Author