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

Re: s2hms



hello,

you don't need the $data buffer as you can read directly in varibles.
my (untested) version should be more like

s2hms () {
	# clean error message when no argument passed
	local secs=${1?duration in seconds} it= out=
	local -a fields=( h m s )
	local -A duration

	# duration\[$^fields] is a short for
	# duration\[h] duration\[m] duration\[s]

	units -t $secs\s hms |
		IFS=\; read duration\[$^fields]


	# use mathematical context to do math
	# also: a value is 0 or >0 so just test if it's >0

	for it ($fields)
		(( duration[it] )) && out+="$duration[$it]$it"

	print $out
}

HTH
regards

-- 
Marc Chantreux (eiro on github and freenode)
http://eiro.github.com/
http://eiro.github.com/atom.xml
"Don't believe everything you read on the Internet"
    -- Abraham Lincoln



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