Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
DWIM interface to date args; reverse array
- X-seq: zsh-users 22981
- From: Emanuel Berg <moasen@xxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: DWIM interface to date args; reverse array
- Date: Wed, 15 Nov 2017 15:37:44 +0100
- Cancel-lock: sha1:IqqKSHtKLBj3VvGfMMQTQF4zvTA=
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mail-copies-to: never
- Mail-followup-to: zsh-users@xxxxxxx
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
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