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

Re: Proxy support for zftp functions



On May 22,  6:04pm, Peter Stephenson wrote:
> Subject: Re: Proxy support for zftp functions
> 
> [...] a date
> and time parsing and conversion module might be a nice extension --- or, of
> course, a piece of shell code that does it neatly, if that's possible.

Here's at least a start at it.  The formats recognized are those typically
found in email Date: headers or as output of `date`; e.g. it makes no
attempt to figure out whether 3/5/7 is March 5, 1907 or May 3, 2007 etc.,
so it simply ignores dates that don't spell out the name of the month.
It also doesn't parse year-month-day (the day is assumed to be the first
number outside an HH:MM[:SS] form) but maybe there's a clever way to fit
that in unambiguously.

This expects to find one or both of the associative arrays $date and $time,
which it fills in the obvious way.  If you don't pre-declare either of them,
you can only check the return value to see whether the parse succeeded.

---- 8< ---- snip ----8< ----
[[ -z "$*" || $ZSH_VERSION < 3.1.6 ]] && return 1

setopt localoptions extendedglob noshwordsplit noksharrays

local HHMMSS='(<->):(<->):#(<->)#'
local DDmmYY='(<->)[-[:space:]]([[:alpha:]]##)[-[:space:]](<->)'
local DDmm='(<->)[[:space:]]([[:alpha:]]##)'
local ZONE='[[:space:]]#([^:[:space:]]##)'

[[ ${(t)date} == association ]] || typeset -A date ; date=()
[[ ${(t)time} == association ]] || typeset -A time ; time=()

set $=*		# Canonicalize whitespace

if [[ "$*" == (#i)(#b)${~DDmmYY}[[:space:]]${~HHMMSS}[[:space:]]#([ap]m)#${~ZONE} ]]
then
    date=(day "$match[1]" month "$match[2]" year "$match[3]")
    time=(hour "$match[4]" minute "$match[5]" second "$match[6]"
	  ampm "$match[7]" zone "$match[8]")
elif [[ "$*" == (#i)(#b)${~DDmm}[[:space:]]${~HHMMSS}[[:space:]]#([ap]m)# ]]
then
    date=(day "$match[1]" month "$match[2]" year "")
    time=(hour "$match[3]" minute "$match[4]" second "$match[5]"
	  ampm "$match[6]" zone "")
elif [[ "$*" == (#i)(#b)${~HHMMSS}[[:space:]]([ap]m)#${~ZONE}[[:space:]]${~DDmmYY} ]]
then
    time=(hour "$match[1]" minute "$match[2]" second "$match[3]"
	  ampm "$match[4]" zone "$match[5]")
    date=(day "$match[6]" month "$match[7]" year "$match[8]")
elif [[ "$*" == (#i)(#b)${~HHMMSS}[[:space:]]([ap]m)#${~ZONE}[[:space:]]${~DDmm}[[:space:]](<->) ]]
then
    time=(hour "$match[1]" minute "$match[2]" second "$match[3]"
	  ampm "$match[4]" zone "$match[5]")
    date=(day "$match[6]" month "$match[7]" year "$match[8]")
elif [[ "$*" == (#i)(#b)([[:alpha:]]##),[[:space:]]#${~DDmmYY}[[:space:]]${~HHMMSS}[[:space:]]#([ap]m)#${~ZONE} ]]
then
    # Day of the week (Mon, Tue, ...) is $match[1]
    date=(day "$match[2]" month "$match[3]" year "$match[4]")
    time=(hour "$match[5]" minute "$match[6]" second "$match[7]"
	  ampm "$match[8]" zone "$match[9]")
elif [[ "$*" == (#i)(#b)([[:alpha:]]##),[[:space:]]#${~DDmm}[[:space:]](<->)[[:space:]]${~HHMMSS}[[:space:]]#([ap]m)#${~ZONE} ]]
then
    # Day of the week (Mon, Tue, ...) is $match[1]
    date=(day "$match[2]" month "$match[3]" year "$match[4]")
    time=(hour "$match[5]" minute "$match[6]" second "$match[7]"
	  ampm "$match[8]" zone "$match[9]")
elif [[ "$*" == (#i)(#b)([[:alpha:]]##)[[:space:]]([[:alpha:]]##)[[:space:]](<->)[[:space:]]${~HHMMSS}[[:space:]]#([ap]m)#${~ZONE}[[:space:]](<->) ]]
then
    # Day of the week (Mon, Tue, ...) is $match[1]
    date=(day "$match[3]" month "$match[2]" year "$match[9]")
    time=(hour "$match[4]" minute "$match[5]" second "$match[6]"
	  ampm "$match[7]" zone "$match[8]")
elif [[ "$*" == (#i)(#b)([[:alpha:]]##)[[:space:]](<->)[[:space:]]${~HHMMSS}[[:space:]]([ap]m)# ]]
then
    date=(day "$match[2]" month "$match[1]" year "")
    time=(hour "$match[3]" minute "$match[4]" second "$match[5]"
	  ampm "$match[6]" zone "")
elif [[ "$*" == ${~DDmmYY} || "$*" == ${~DDmm}[[:space:]](<->) ]]
then
    date=(day "$match[1]" month "$match[2]" year "$match[3]")
    time=(hour "" minute "" second "" ampm "" zone "")
elif [[ "$*" == ${~HHMMSS}[[:space:]]#([ap]m)# ]]
then
    date=(day "" month "" year "")
    time=(hour "$match[1]" minute "$match[2]" second "$match[3]"
	  ampm "$match[4]" zone "")
fi

(( $#date || $#time ))
---- 8< ---- snip ----8< ----



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