Convert seconds to hours, minutes, seconds

前端 未结 13 1010
梦谈多话
梦谈多话 2020-12-02 14:08

How can I convert seconds to hours, minutes and seconds?

show_time() {
  ?????
}

show_time 36 # 00:00:36
show_time 1036 # 00:17:26
show_time 91925 # 25:32:0         


        
13条回答
  •  眼角桃花
    2020-12-02 14:40

    on MacOSX 10.13 Slight edit from @eMPee584 's code to get it all in one GO (put the function in some .bashrc like file and source it, use it as myuptime. For non-Mac OS, replace the T formula by one that gives the seconds since last boot.

    myuptime () 
    { 
        local T=$(($(date +%s)-$(sysctl -n kern.boottime | awk '{print $4}' | sed 's/,//g')));
        local D=$((T/60/60/24));
        local H=$((T/60/60%24));
        local M=$((T/60%60));
        local S=$((T%60));
        printf '%s' "UpTime: ";
        [[ $D > 0 ]] && printf '%d days ' $D;
        [[ $H > 0 ]] && printf '%d hours ' $H;
        [[ $M > 0 ]] && printf '%d minutes ' $M;
        [[ $D > 0 || $H > 0 || $M > 0 ]] && printf '%d seconds\n' $S
    }
    

提交回复
热议问题