use*_*302 8 bash date arithmetic variable
是否可以使用 bash 减去包含 24 小时时间的变量?
#!/bin/bash
var1="23:30" # 11:30pm
var2="20:00" # 08:00pm
echo "$(expr $var1 - $var2)"
Run Code Online (Sandbox Code Playgroud)
运行它会产生以下错误。
./test
expr: non-integer argument
Run Code Online (Sandbox Code Playgroud)
我需要输出以十进制形式出现,例如:
./test
3.5
Run Code Online (Sandbox Code Playgroud)
Hax*_*iel 13
该date
命令的输入非常灵活。你可以利用它来发挥你的优势:
#!/bin/bash
var1="23:30"
var2="20:00"
# Convert to epoch time and calculate difference.
difference=$(( $(date -d "$var1" "+%s") - $(date -d "$var2" "+%s") ))
# Divide the difference by 3600 to calculate hours.
echo "scale=2 ; $difference/3600" | bc
Run Code Online (Sandbox Code Playgroud)
输出:
$ ./test.bash
3.50
Run Code Online (Sandbox Code Playgroud)
使用 only bash
,没有外部程序,你可以这样做:
#!/bin/bash
# first time is the first argument, or 23:30
var1=${1:-23:30}
# second time is the second argument, or 20:00
var2=${2:-20:00}
# Split variables on `:` and insert pieces into arrays
IFS=':' read -r -a t1 <<< "$var1"
IFS=':' read -r -a t2 <<< "$var2"
# strip leading zeros (so it's not interpreted as octal
t1=("${t1[@]##0}")
t2=("${t2[@]##0}")
# check if the first time is before the second one
if (( t1[0] > t2[0] || ( t1[0] == t2[0] && t1[1] > t2[1]) ))
then
# if the minutes on the first time are less than the ones on the second time
if (( t1[1] < t2[1] ))
then
# add 60 minutes to time 1
(( t1[1] += 60 ))
# and subtract an hour
(( t1[0] -- ))
fi
# now subtract the hours and the minutes
echo $((t1[0] -t2[0] )):$((t1[1] - t2[1]))
# to get a decimal result, multiply the minutes by 100 and divide by 60
echo $((t1[0] -t2[0] )).$(((t1[1] - t2[1])*100/60))
else
echo "Time 1 should be after time 2" 2>&1
fi
Run Code Online (Sandbox Code Playgroud)
测试:
$ ./script.sh
3:30
3.50
$ ./script.sh 12:10 11:30
0:40
0.66
$ ./script.sh 12:00 11:30
0:30
0.50
Run Code Online (Sandbox Code Playgroud)
如果您想要更复杂的时差,可能跨越不同的日期等,那么最好使用 GNU 日期。