bash 在bash脚本中减去两个时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41793634/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:38:35  来源:igfitidea点击:

Subtracting two timestamps in bash script

bashshelldatemathtimestamp

提问by Ali

I have a file that contains a set of Timestamps in format of H:M:S.MS, I can read and print all of the saved Timestamps but when I do some arithmetic operation like Subtract (say Timestamp2 - Timestamp1) it doesn't give me the answer, I do not have experience work with bash script programming.

我有一个文件,其中包含一组格式为 的时间戳H:M:S.MS,我可以读取和打印所有保存的时间戳,但是当我执行一些算术运算时,例如减去(例如Timestamp2 - Timestamp1)它没有给我答案,我没有经验使用 bash 脚本编程。

Any suggestions, recommendations will be much appreciated.

任何建议,建议将不胜感激。

Here is the screen shotof my problem.

这是我的问题的屏幕截图

Here is an example :

这是一个例子:

Start Time = 17:53:01.166721
End Time = 17:53:01.369787

The expected result for End Time - Start Timeis either 0:00:0.203066or 0.203066.

的预期结果End Time - Start Time0:00:0.2030660.203066

回答by Grisha Levit

If you want to process the date using simple command line tools, you need to convert the timestamps into some easy-to-deal-with format, like epoch-based.

如果您想使用简单的命令行工具处理日期,则需要将时间戳转换为某种易于处理的格式,例如基于 epoch 的格式。

You can do this with the datecommand, which you can wrap in a function like so:

您可以使用date命令执行此操作,您可以将其包装在一个函数中,如下所示:

timestamp() { 
   date '+%s%N' --date=""
}
# timestamp "12:20:45.12345"  ==>  1485105645123450000

Then you can subtract them directly:

然后你可以直接减去它们:

echo $(( $(timestamp "${Stime[$i]}") - $(timestamp "${Rtime[$i]}") ))

Note that since you are not keeping track of the day but only of the time, you will have errors when, for example, the start time is 23:59:59 and the end time is 00:00:01.

请注意,由于您不是在跟踪当天,而只是在跟踪时间,例如,当开始时间为 23:59:59 和结束时间为 00:00:01 时,您将出现错误。

回答by glenn Hymanman

If you need to use bash, you need to convert the timestamp into an integer value:

如果需要使用bash,则需要将时间戳转换为整数值:

$ time="12:34:56.789"
$ IFS=":." read -r h m s ms <<<"$time"
$ echo $h $m $s $ms
12 34 56 789
$ milliseconds=$(( (h*3600 + m*60 + s)*1000 + ms ))
$ echo $milliseconds
45296789

Then you can subtract to get a number of milliseconds representing the diff.

然后您可以减去以获得表示差异的毫秒数。

Convert to seconds with some arithmetic and string construction:

使用一些算术和字符串构造转换为秒:

$ seconds="$((milliseconds / 1000)).$((milliseconds % 1000))"
$ echo $seconds
45296.789


To address gniourf_gniourf's valid point (in a clunky way):

要解决 gniourf_gniourf 的有效观点(以笨拙的方式):

$ time="12:34:56.7"
$ IFS=":." read -r h m s ms <<<"$time"
$ ms="${ms}000"; ms=${ms:0:3}
$ echo $h $m $s $ms
12 34 56 700
# .......^^^


Also, strings that are invalid octal numbers like "08" and "09" will throw errors in bash arithmetic, so explicitly declare they are base 10

此外,像 "08" 和 "09" 这样的无效八进制数的字符串会在 bash 算术中抛出错误,因此明确声明它们是以 10 为基数的

milliseconds=$(( (10#$h*3600 + 10#$m*60 + 10#$s)*1000 + 10#$ms ))

回答by Fred

Assuming your variables are integers, you need to enclose your calculation in an arithmetic evaluation for that to work.

假设您的变量是整数,您需要将您的计算包含在算术评估中才能使其工作。

echo $(( VAR1 - VAR2 ))

Or, if you want to assign the value :

或者,如果要分配值:

VAR3=$(( VAR1 - VAR2 ))