Convert a time span in seconds to formatted time in shell

后端 未结 7 1971
鱼传尺愫
鱼传尺愫 2020-12-28 13:25

I have a variable of $i which is seconds in a shell script, and I am trying to convert it to 24 HOUR HH:MM:SS. Is this possible in shell?

7条回答
  •  情歌与酒
    2020-12-28 14:03

    I use C shell, like this:

    #! /bin/csh -f
    
    set begDate_r = `date +%s`
    set endDate_r = `date +%s`
    
    set secs = `echo "$endDate_r - $begDate_r" | bc`
    set h = `echo $secs/3600 | bc`
    set m = `echo "$secs/60 - 60*$h" | bc`
    set s = `echo $secs%60 | bc`
    
    echo "Formatted Time: $h HOUR(s) - $m MIN(s) - $s SEC(s)"
    

提交回复
热议问题