在 bash / shell 脚本中增加时间(分钟和秒)

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

Incrementing time (minutes and seconds) in bash / shell script

bashtimeshell

提问by cbros2008

I need to increment minutes and seconds (in relation to time) in a variable.

我需要在变量中增加分钟和秒(相对于时间)。

First, I'm not sure whether declaring a 'time' variable is written as

首先,我不确定声明“时间”变量是否写为

time="00:00:00" 

or

或者

 time=$(date +00:00:00)?

From there, I want to increment this variable by 10 minutes and seconds resulting in

01:00:00 increased to
01:10:10 to
01:20:20 etc (all the way up to midnight - 00:00:00)

What would be the best way to achieve this?

从那里,我想将这个变量增加 10 分和秒,导致01:00:00

增加到
01:10:10 到 01:20:20
等等(一直到午夜 - 00:00:00)

什么将是实现这一目标的最佳方式?

I understand that doing $ date -d "2010-07-07 200 days"adds (200) days but I don't know how to apply this example to time (minutes and seconds) and not date?

我知道这样做 $ date -d "2010-07-07 200 days"会增加(200)天,但我不知道如何将此示例应用于时间(分钟和秒)而不是日期?

All replies are much appreciated.

非常感谢所有答复。

采纳答案by Chris J

Note that this is Linux only. date -don BSD unixes (and possibly others) does something significantly different (and untoward).

请注意,这仅适用于 Linux。date -d在 BSD unixes(可能还有其他)上做了一些明显不同的事情(并且令人不快)。

You could use epoch time - i.e., seconds since 1 Jan 1970 00:00:00, for example:

您可以使用纪元时间 - 即自 1970 年 1 月 1 日 00:00:00 以来的秒数,例如:

#!/bin/bash

time=0
echo `date -d "1970-01-01 00:00:00 UTC $time seconds" +"%H:%M:%S"`
time=$((time + 600))
echo `date -d "1970-01-01 00:00:00 UTC $time seconds" +"%H:%M:%S"`
time=$((time + 600))
echo `date -d "1970-01-01 00:00:00 UTC $time seconds" +"%H:%M:%S"`

gives this output:

给出这个输出:

$ /tmp/datetest.sh
00:00:00
00:10:00
00:20:00
$

回答by ghoti

The datecommand is not part of bash. It's provided by your operating system, and its behaviour differs in different operating systems. Notably, GNU coreutils date (in most Linux distros) has a -doption used for interpreting source dates, whereas BSD date has a -foption for interpreting specifically formatted input dates and a -voption for adjusting times by various units.

date命令不是 bash 的一部分。它由您的操作系统提供,其行为在不同的操作系统中有所不同。值得注意的是,GNU coreutils date(在大多数 Linux 发行版中)有一个-d用于解释源日期的选项,而 BSD date 有一个-f用于解释特定格式的输入日期的-v选项和一个用于按不同单位调整时间的选项。

You've already got an accepted answer for the Linux option. To be complete about this, here's a BSD (and macOS) option:

对于 Linux 选项,您已经得到了公认的答案。为了完成这一点,这里有一个 BSD(和 macOS)选项:

$ for (( s=0; s<86400; s+=610 )); do date -j -v+${s}S -f '%H:%M:%S' "$time" '+%T'; done | head -5
00:00:00
00:10:10
00:20:20
00:30:30
00:40:40

Output here is obviously trimmed to 5 lines.

这里的输出显然被修剪为 5 行。

Of course, this is still platform-specific. If you want something that will work with bash anywhere, then as long as you're using bash >4.2, the following might do:

当然,这仍然是特定于平台的。如果你想要一些可以在任何地方与 bash 一起使用的东西,那么只要你使用 bash > 4.2,以下可能会做:

$ offset=$(printf '%(%z)T\n' 0)
$ for (( s=$(( ${offset:1:2}*3600 + ${offset:3}*60 )); s<86400; s+=610 )); do printf '%(%T)T\n' "$s"; done | head -5
00:00:00
00:10:10
00:20:20
00:30:30
00:40:40

The $offsetvariable allows us to compensate for the fact that you may not be in UTC. :) Like the datesolution, this steps through increments of 610 seconds, but it uses printf's %Tformat to generate output. You may with to add or subtract the timezone offset to the 86400 end point to get a full day of times. Handling that is left as an exercise for the reader. :-)

$offset变量允许我们补偿您可能不在 UTC 中的事实。:) 与date解决方案一样,此步骤以 610 秒为增量,但它使用printf%T格式来生成输出。您可以添加或减去 86400 端点的时区偏移量以获得一整天的时间。处理它留给读者作为练习。:-)