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

Re: time command with shell builtins



On 1/25/23, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Tue, Jan 24, 2023 at 2:48 AM Dominik Vogt <dominik.vogt@xxxxxx> wrote:
>>
>> [...]  This is what I use now:
>>
>> -- snip --
>> autoload -Uz add-zsh-hook
>> zmodload zsh/datetime
>>
>> function preexec_recordtime() {
>>         typeset -g _zsh_time
>>         _zsh_time="$EPOCHSECONDS"
>> }
>> add-zsh-hook preexec preexec_recordtime
>
> Unless you're already using psvar for something else, I would suggest
>
> function preexec_recordtime() {
>   if [[ -n $REPORTTIME ]]; then
>     psvar[1]="$EPOCHSECONDS"
>   else
>     psvar[1]=''
>   fi
> }
> precmd_reporttime () {
>   if [[ -n $REPORTTIME && -n $psvar[1] ]]
>   then
>     psvar[1]=$(( $EPOCHSECONDS - $psvar[1] ))
>     if (( $psvar[1] <= $REPORTTIME ))
>     then
>       psvar[1]=''
>     else
>       psvar[1]=" $psvar[1]s "
>     fi
>   fi
> }
> add-zsh-hook preexec preexec_recordtime
> add-zsh-hook precmd precmd_reporttime
>
> PS1="<...>%1v<...>"
>
> Out of curiosity, why are you doing math on $EPOCHSECONDS rather than
> just use $SECONDS?

I also do this in precmd/exec, so I figured I may as well use the same
thing for all of it, also if I assign to SECONDS interactively maybe
weird things happen wrt float formatting etc,
  function pre{exec,cmd}_datechange() {
    local newdate
    newdate=$(strftime %x $EPOCHSECONDS)
    if [[ $_zsh_date != $newdate ]]; then
      printf "Date changed from %s to %s\n" $_zsh_date $newdate
      _zsh_date=$newdate
    fi
  }


-- 
Mikael Magnusson




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