Linux Bash 脚本:两次之间的分钟差
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14309032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash script: difference in minutes between two times
提问by gorn
I have two time strings; eg. "09:11" and "17:22" on the same day (format is hh:mm). How do I calculate the time difference in minutes between these two?
我有两个时间字符串;例如。同一天的“09:11”和“17:22”(格式为hh:mm)。如何计算这两者之间的时差(以分钟为单位)?
Can the standard date
library do this?
标准date
库可以做到这一点吗?
Example:
例子:
#!/bin/bash
MPHR=60 # Minutes per hour.
CURRENT=$(date -u -d '2007-09-01 17:30:24' '+%F %T.%N %Z')
TARGET=$(date -u -d'2007-12-25 12:30:00' '+%F %T.%N %Z')
MINUTES=$(( $(diff) / $MPHR ))
Is there a simpler way of doing this given the hour and minute in hh:mm
考虑到 hh:mm 中的小时和分钟,是否有更简单的方法来做到这一点
采纳答案by Gilles Quenot
A pure bashsolution :
一个纯粹的bash解决方案:
old=09:11
new=17:22
# feeding variables by using read and splitting with IFS
IFS=: read old_hour old_min <<< "$old"
IFS=: read hour min <<< "$new"
# convert hours to minutes
# the 10# is there to avoid errors with leading zeros
# by telling bash that we use base 10
total_old_minutes=$((10#$old_hour*60 + 10#$old_min))
total_minutes=$((10#$hour*60 + 10#$min))
echo "the difference is $((total_minutes - total_old_minutes)) minutes"
Another solution using date
(we work with hour/minutes, so the date is not important)
使用的另一个解决方案date
(我们使用小时/分钟,所以日期并不重要)
old=09:11
new=17:22
IFS=: read old_hour old_min <<< "$old"
IFS=: read hour min <<< "$new"
# convert the date "1970-01-01 hour:min:00" in seconds from Unix EPOCH time
sec_old=$(date -d "1970-01-01 $old_hour:$old_min:00" +%s)
sec_new=$(date -d "1970-01-01 $hour:$min:00" +%s)
echo "the difference is $(( (sec_new - sec_old) / 60)) minutes"
回答by Neil
MPHR=60
CURRENT=09:11
TARGET=17:22
echo $(( ( 10#${TARGET:0:2} - 10#${CURRENT:0:2} ) * MPHR + 10#${TARGET:4} - 10#${CURRENT:4} ))
回答by chepner
I would convert the dates to UNIX timestamps; you can subtract to get the difference in seconds, then divide by 60:
我会将日期转换为 UNIX 时间戳;您可以减去以获得以秒为单位的差异,然后除以 60:
#!/bin/bash
MPHR=60 # Minutes per hour.
CURRENT=$(date +%s -d '2007-09-01 17:30:24')
TARGET=$(date +%s -d'2007-12-25 12:30:00')
MINUTES=$(( ($TARGET - $CURRENT) / $MPHR ))
回答by Dorian
Here is how I did it:
这是我如何做到的:
START=$(date +%s);
sleep 1; # Your stuff
END=$(date +%s);
echo $((END-START)) | awk '{printf "%d:%02d:%02d", /3600, (/60)%60, %60}'
Really simple, take the number of seconds at the start, then take the number of seconds at the end, and print the difference in minutes:seconds.
真的很简单,在开始时取秒数,然后在结束时取秒数,并以分钟:秒为单位打印差异。
回答by Jonathan
@Dorian
If you just want to know how long a program took to run: time, man, man time!
@Dorian
如果你只想知道一个程序运行了多长时间:时间,男人,男人时间!
Trivial example:
简单的例子:
jonathan@Odin:~$ time sleep 1
real 0m1.001s
user 0m0.000s
sys 0m0.000s
OK, it doesn't give the result in seconds, but you can make it do so with a format string, or more simply with the POSIX compliance option:
好的,它不会在几秒钟内给出结果,但是您可以使用格式字符串来实现,或者更简单地使用 POSIX 合规性选项:
jonathan@Odin:~$ time -p sleep 20
real 20.00
user 0.00
sys 0.00
回答by Mark Narag
STARTTIME=$(date +%s)
YOUR CODES :
你的代码:
ENDTIME=$(date +%s)
secs=$(($ENDTIME - $STARTTIME))
printf 'Elapsed Time %dh:%dm:%ds\n' $(($secs/3600)) $(($secs%3600/60)) $(($secs%60))
回答by Ondra ?i?ka
I was looking for a solution with seconds. Found here: How to calculate time difference in bash script?
我正在寻找几秒钟的解决方案。在这里找到:如何计算 bash 脚本中的时差?
#!/bin/bash
string1="10:33:56"
string2="10:36:10"
StartDate=$(date -u -d "$string1" +"%s")
FinalDate=$(date -u -d "$string2" +"%s")
date -u -d "0 $FinalDate sec - $StartDate sec" +"%H:%M:%S"
Here I have added seconds to Gilles' solution:
在这里,我在 Gilles 的解决方案中添加了几秒钟:
function countTimeDiff() {
timeA= # 09:59:35
timeB= # 17:32:55
# feeding variables by using read and splitting with IFS
IFS=: read ah am as <<< "$timeA"
IFS=: read bh bm bs <<< "$timeB"
# Convert hours to minutes.
# The 10# is there to avoid errors with leading zeros
# by telling bash that we use base 10
secondsA=$((10#$ah*60*60 + 10#$am*60 + 10#$as))
secondsB=$((10#$bh*60*60 + 10#$bm*60 + 10#$bs))
DIFF_SEC=$((secondsB - secondsA))
echo "The difference is $DIFF_SEC seconds.";
SEC=$(($DIFF_SEC%60))
MIN=$((($DIFF_SEC-$SEC)%3600/60))
HRS=$((($DIFF_SEC-$MIN*60)/3600))
TIME_DIFF="$HRS:$MIN:$SEC";
echo $TIME_DIFF;
}