Reputation: 427
I would like to modify the system time to two days after but I only find that I can set the time to a specific time with the date
command in Solaris:
#date mmddhhmmYY
but cannot add two days on the current time. Is there an easy way to do this task with a shell command? If you know something similar in linux, please also share with me.
Upvotes: 2
Views: 540
Reputation: 77137
Solaris date supports a syntax that gradually adjusts the time. This may be your best bet.
date -a $(( 48 * 60 * 60 * 60 ))
should eventually update your date to two days ahead. That is really the best way to set the system time as it will gradually update instead of jumping ahead suddenly (which can screw up a bunch of running programs).
Barring that, you could write up a nice script that's aware of the days in a month, and leap years, and do the calculations yourself. If you're exceptionally lazy (like me) and precision and race conditions don't bug you that much (like me, answering StackOverflow questions), you could just do a hack like this:
#!/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
Test run on a tnarik/solaris10-minimal
Vagrant box:
# ./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
Upvotes: 5