From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20145 invoked by alias); 15 Nov 2017 14:38:04 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 22981 Received: (qmail 29502 invoked by uid 1010); 15 Nov 2017 14:38:04 -0000 X-Qmail-Scanner-Diagnostics: from 195.159.176.226 by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(195.159.176.226):SA:0(-0.9/5.0):. Processed in 5.470233 secs); 15 Nov 2017 14:38:04 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RDNS_NONE autolearn=no autolearn_force=no version=3.4.1 X-Envelope-From: gcszu-zsh-users@m.gmane.org X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: zsh-users@zsh.org To: zsh-users@zsh.org From: Emanuel Berg Subject: DWIM interface to date args; reverse array Date: Wed, 15 Nov 2017 15:37:44 +0100 Message-ID: <86375f1ubr.fsf@zoho.com> Mime-Version: 1.0 Content-Type: text/plain X-Complaints-To: usenet@blaine.gmane.org User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Mail-Copies-To: never Cancel-Lock: sha1:IqqKSHtKLBj3VvGfMMQTQF4zvTA= 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