如何在Solaris中使用shell脚本将时间设置为当前时间之后的2天?

时间:2016-01-24 23:27:15

标签: linux shell solaris

我想将系统时间修改为两天,但我发现我可以使用Solaris中的date命令将时间设置为特定时间:

#date mmddhhmmYY

但不能在当前时间添加两天。有没有一种简单的方法可以使用shell命令执行此任务?如果您在linux中了解类似内容,请与我分享。

1 个答案:

答案 0 :(得分:5)

Solaris date支持逐步调整时间的语法。这可能是你最好的选择。

date -a $(( 48 * 60 * 60 * 60 ))

最终将更新您的日期提前两天。这确实是设置系统时间的最佳方式,因为它会逐渐更新而不是突然跳过(这可能搞砸了一堆正在运行的程序)。

除此之外,你可以编写一个很好的脚本来了解一个月内的日子和闰年,并自己进行计算。如果你非常懒惰(像我一样),精确度和竞争条件不会让你感到烦恼(比如我,回答StackOverflow问题),你可以这样做:

#!/bin/sh

now=$(date +%H%M.%S) # current time
date 2359.59 # Set time to 11:59:59pm
sleep 1 # Wait a second so the day rolls over
date 2359.59 # Set time to 11:59:59pm
sleep 1 # Wait a second so the day rolls over
date "$now" # Set time back to what it was, but in the new day
date -a 2 # Gradually add two seconds back to the clock

tnarik/solaris10-minimal Vagrant框上进行测试:

# ./adddaystodate
The current date is now Monday, 25 January 2016 00:46:59 GMT
Monday, 25 January 2016 23:59:59 GMT
Tuesday, 26 January 2016 23:59:59 GMT
Wednesday, 27 January 2016 00:46:59 GMT
The current date is now Wednesday, 27 January 2016 00:46:59 GMT