2014-10-29 73 views
0

在我的脚本中,我将日期中的当前日期存储在一个变量中,但是我希望变量能够提前5分钟存储日期。在bash中将当前日期递增5分钟

29/10/2014-10:47:06 to 29/10/2014-10:52:06 

在Bash中有这样一个快速简单的方法吗?例如,如果时间是10:59:00(例如识别到进入下一小时),那么寻找一个也能正常工作的解决方案。

我发现了一些Perl解决方案,但没有为Bash。

+0

这可能适合你:'date -d'2014/10/29 10:47:06 + 5 minutes'''。或者如果它是从当前日期开始的,那么'date -d'5分钟''。 – 2014-10-29 10:52:34

回答

2

可以使用-d选项:

~$ date 
mercredi 29 octobre 2014, 11:45:45 (UTC+0100) 
~$ date -d '5 minutes' 
mercredi 29 octobre 2014, 11:50:55 (UTC+0100) 

如果你想使用一个变量:

~$ curr=$(date +%d/%m/%Y-%H:%M:%S) 
~$ echo $curr 
29/10/2014-11:59:50 
~$ date -d "($cur) +5minutes" +%d/%m/%Y-%H:%M:%S 
29/10/2014-12:04:54 
0

date看一看: date +%s让你从1.1.1970秒,添加5 * 60并使用 date --date='@<newTime>'将其转换回来。

请参阅man date bash。

1

你需要使用-d选项,与任何用户定义的日期:

date -d '2014/10/29 10:58:06 5 minutes' 
Wed Oct 29 11:03:06 IST 2014 

为当前日期

date -d '5 minutes' 
0

在bash,下列选项可用于date命令:

-a [-]sss.fff Slowly adjust the time by sss.fff seconds 
       (fff represents fractions of a second). This 
       adjustment can be positive or negative. The 
       system's clock is sped up or slowed down 
       until it has drifted by the number of 
       seconds specified. Only the super-user may 
       adjust the time. 

因此,使用以下内容添加5分钟(= 300秒):

date -a 300