zsh-users
 help / color / mirror / code / Atom feed
* s2hms
@ 2017-10-18 22:21 Emanuel Berg
  2017-10-19  8:49 ` s2hms Marc Chantreux
  0 siblings, 1 reply; 5+ messages in thread
From: Emanuel Berg @ 2017-10-18 22:21 UTC (permalink / raw)
  To: zsh-users

I just wrote a small interface to units(1).
Does it look good to you?

Try for example

    $ s2hms 500
    8m 20s

Sounds about right...

s2hms () {
    local secs=$1

    local time_string
    time_string=$(units -t "$secs s" hms)

    local -a data
    data=("${(@s/;/)time_string}")
    local h=$data[1]
    local m=$data[2]
    local s=$data[3]

    local out
    [[ $h > 0 ]] && out ="${h}h "
    [[ $m > 0 ]] && out+="${m}m "
    [[ $s > 0 ]] && out+="${s}s"
    echo $out
}

-- 
underground experts united
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: s2hms
  2017-10-18 22:21 s2hms Emanuel Berg
@ 2017-10-19  8:49 ` Marc Chantreux
  2017-10-19 13:03   ` s2hms Emanuel Berg
  0 siblings, 1 reply; 5+ messages in thread
From: Marc Chantreux @ 2017-10-19  8:49 UTC (permalink / raw)
  To: zsh-users

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: s2hms
  2017-10-19  8:49 ` s2hms Marc Chantreux
@ 2017-10-19 13:03   ` Emanuel Berg
  2017-10-20 10:26     ` s2hms Marc Chantreux
  0 siblings, 1 reply; 5+ messages in thread
From: Emanuel Berg @ 2017-10-19 13:03 UTC (permalink / raw)
  To: zsh-users

Marc Chantreux wrote:

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

local secs=${1?duration in seconds} it= out=

unknown file attribute

Also, what's with the strange chars: \240 two times?

> 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
> }

-- 
underground experts united
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: s2hms
  2017-10-19 13:03   ` s2hms Emanuel Berg
@ 2017-10-20 10:26     ` Marc Chantreux
  2017-10-20 11:27       ` s2hms Emanuel Berg
  0 siblings, 1 reply; 5+ messages in thread
From: Marc Chantreux @ 2017-10-20 10:26 UTC (permalink / raw)
  To: zsh-users

> local secs=${1?duration in seconds} it= out=
> 
> unknown file attribute
> Also, what's with the strange chars: \240 two times?

those are equal signs. (ascii 61) maybe that explains the
file attribute error ? 


-- 
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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: s2hms
  2017-10-20 10:26     ` s2hms Marc Chantreux
@ 2017-10-20 11:27       ` Emanuel Berg
  0 siblings, 0 replies; 5+ messages in thread
From: Emanuel Berg @ 2017-10-20 11:27 UTC (permalink / raw)
  To: zsh-users

Marc Chantreux wrote:

>> local secs=${1?duration in seconds} it= out=
>> 
>> unknown file attribute Also, what's with the
>> strange chars: \240 two times?
>
> those are equal signs. (ascii 61)

ASCII chars are printable. Besides, you use
equal sings for example in the first line with
no problem. Even I can use them: =

\240 (not the char, now I just type the digits)
is:

             position: 2683 of 3139 (85%), column: 17
            character:  (displayed as ) (codepoint 160, #o240, #xa0)
    preferred charset: eight-bit (Raw bytes 128-255)
code point in charset: 0xA0
               script: latin
               syntax: . 	which means: punctuation
          buffer code: #xA0
            file code: #xA0
              display: terminal code #xC2 #xA0

> maybe that explains the file attribute error?

Correct!

-- 
underground experts united
http://user.it.uu.se/~embe8573


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-10-20 11:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-18 22:21 s2hms Emanuel Berg
2017-10-19  8:49 ` s2hms Marc Chantreux
2017-10-19 13:03   ` s2hms Emanuel Berg
2017-10-20 10:26     ` s2hms Marc Chantreux
2017-10-20 11:27       ` s2hms Emanuel Berg

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).