zsh-users
 help / color / mirror / code / Atom feed
From: Emanuel Berg <moasen@zoho.com>
To: zsh-users@zsh.org
Subject: DWIM interface to date args; reverse array
Date: Wed, 15 Nov 2017 15:37:44 +0100	[thread overview]
Message-ID: <86375f1ubr.fsf@zoho.com> (raw)

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


             reply	other threads:[~2017-11-15 14:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20171115143925epcas2p39a7bfefe245b1b93d1d19dcf210770f3@epcas2p3.samsung.com>
2017-11-15 14:37 ` Emanuel Berg [this message]
2017-11-15 14:57   ` Peter Stephenson
2017-11-15 15:56     ` Emanuel Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86375f1ubr.fsf@zoho.com \
    --to=moasen@zoho.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).