我使用日期+“%T”打印开始和结束时间,结果如下:
10:33:56
10:36:10
我如何计算并打印这两者之间的差值呢?
我想要的是:
2m 14s
我使用日期+“%T”打印开始和结束时间,结果如下:
10:33:56
10:36:10
我如何计算并打印这两者之间的差值呢?
我想要的是:
2m 14s
当前回答
如果你已经计算出了时间差值,并且它们小于1天,这里有一个非常边缘的BC用例,可以将输出格式化为
HH MM SS.xxxx
24小时格式,请记住小数点右边的数字是以60为基数打印的
Jot -w 'obase = 60;%。3f' - 1.3219567 300 73.6543211 | BC
01.19 19
01 14.58 33
02 28.37 51
03 42.17 06 # 3 mins 42 secs
04 55.56 20
...
...
19 38 10.54 32 # 19 hrs 38 mins 10 secs
#
# (or 7:38pm, if it's representing absolute time)
但这绝对是一个快速得到大致数字的方法
其他回答
Bash有一个方便的SECONDS内建变量,用于跟踪自shell启动以来已经过的秒数。此变量在赋值时保留其属性,赋值后返回的值为自赋值后的秒数加上赋值。
因此,您可以在启动计时事件之前将SECONDS设置为0,在事件发生后读取SECONDS,并在显示之前进行时间算术。
#!/usr/bin/env bash
SECONDS=0
# do some work
duration=$SECONDS
echo "$(($duration / 60)) minutes and $(($duration % 60)) seconds elapsed."
由于这个解决方案不依赖于date +%s(这是一个GNU扩展),所以它可以移植到Bash支持的所有系统。
以下是我的做法:
START=$(date +%s);
sleep 1; # Your stuff
END=$(date +%s);
echo $((END-START)) | awk '{print int($1/60)":"int($1%60)}'
非常简单,取开始时的秒数,然后取结束时的秒数,打印出以分钟为单位的差值:秒。
我想提出另一种避免召回日期命令的方法。如果你已经收集了%T日期格式的时间戳,这可能会有帮助:
ts_get_sec()
{
read -r h m s <<< $(echo $1 | tr ':' ' ' )
echo $(((h*60*60)+(m*60)+s))
}
start_ts=10:33:56
stop_ts=10:36:10
START=$(ts_get_sec $start_ts)
STOP=$(ts_get_sec $stop_ts)
DIFF=$((STOP-START))
echo "$((DIFF/60))m $((DIFF%60))s"
我们甚至可以用同样的方法处理毫秒。
ts_get_msec()
{
read -r h m s ms <<< $(echo $1 | tr '.:' ' ' )
echo $(((h*60*60*1000)+(m*60*1000)+(s*1000)+ms))
}
start_ts=10:33:56.104
stop_ts=10:36:10.102
START=$(ts_get_msec $start_ts)
STOP=$(ts_get_msec $stop_ts)
DIFF=$((STOP-START))
min=$((DIFF/(60*1000)))
sec=$(((DIFF%(60*1000))/1000))
ms=$(((DIFF%(60*1000))%1000))
echo "${min}:${sec}.$ms"
下面是Daniel Kamil Kozar的回答,表示小时/分钟/秒:
echo "Duration: $(($DIFF / 3600 )) hours $((($DIFF % 3600) / 60)) minutes $(($DIFF % 60)) seconds"
所以完整的脚本是:
date1=$(date +"%s")
date2=$(date +"%s")
DIFF=$(($date2-$date1))
echo "Duration: $(($DIFF / 3600 )) hours $((($DIFF % 3600) / 60)) minutes $(($DIFF % 60)) seconds"
我需要一个时差脚本用于mencoder(它的——endpos是相对的),我的解决方案是调用一个Python脚本:
$ ./timediff.py 1:10:15 2:12:44
1:02:29
还支持秒的分数:
$ echo "diff is `./timediff.py 10:51.6 12:44` (in hh:mm:ss format)"
diff is 0:01:52.4 (in hh:mm:ss format)
它可以告诉你200和120的差值是1h 20m:
$ ./timediff.py 120:0 200:0
1:20:0
并且可以将任何(可能是分数)秒、分或小时数转换为hh:mm:ss
$ ./timediff.py 0 3600
1:00:0
$ ./timediff.py 0 3.25:0:0
3:15:0
timediff.py:
#!/usr/bin/python
import sys
def x60(h,m):
return 60*float(h)+float(m)
def seconds(time):
try:
h,m,s = time.split(':')
return x60(x60(h,m),s)
except ValueError:
try:
m,s = time.split(':')
return x60(m,s)
except ValueError:
return float(time)
def difftime(start, end):
d = seconds(end) - seconds(start)
print '%d:%02d:%s' % (d/3600,d/60%60,('%02f' % (d%60)).rstrip('0').rstrip('.'))
if __name__ == "__main__":
difftime(sys.argv[1],sys.argv[2])