Additional time to system time

Hi All,

Is there any command to add additional time to date command. I need to add 5 hours to the present system time. I am getting the time by using date command.

WORKFLOW_START_TIME=`date +%m/%d/%Y\ %H:%M:%S`

Thanks

Horrible, but it works...

WORKFLOW_START_TIME=$(perl -e '@t= localtime(time() + (5 *(60 * 60 )));printf "%02d/%02d/%04d %02d:%02d:%02d\n", $t[4] + 1, $t[3],$t[5]+1900, @t[2,1,0]')
1 Like

Depending on what OS and date version you have, this might work:

date -d"+5hours" +%H:%M:%S

Using Korn Shell 93:

#!/bin/ksh93

printf "%(%m/%d/%Y %H:%M:%S)T\n" "exactly next 5 hour"

Hi RudiC,

Its not working,

$ date
Tue Jun 11 15:31:03 GMT 2013
$ date -d"+5hours" +%H:%M:%S
date: illegal option -- d
date: illegal option -- +
date: illegal option -- 5
date: illegal option -- h
date: illegal option -- o
date: illegal option -- r
date: illegal option -- s
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
$
$ uname
SunOS

Shell -

$ echo $0
-ksh

date -d is a feature so obvious and handy you'd think it's everywhere, but isn't. It's a GNU/Linux thing mostly.

Another perl approach:

WORKFLOW_START_TIME=$(perl -e 'use POSIX qw(strftime); print strftime "%m/%d/%Y %H:%M:%S",localtime(time()+ 3600*5);')

Hi Skrynesaver,

Can you explain the code.

WORKFLOW_START_TIME=$(perl -e '@t= localtime(time() + (5 *(60 * 60 )));printf "%02d/%02d/%04d %02d:%02d:%02d\n", $t[4] + 1, $t[3],$t[5]+1900, @t[2,1,0]')

Taking the command apart we get...

WORKFLOW_START_TIME=$(..) #assign the output of the contained command to WORKFLOW_START_TIME
perl -e # execute the following string under the perlinterpreter
@t= localtime(...); # convert the contained epoch time into a local time array ("perldoc -f localtime" for details of the array)
time() + (5 *(60 * 60 )) # get the current epoch time and add 5 hours worth of seconds
printf "%02d/%02d/%04d %02d:%02d:%02d\n",$t[4] + 1, $t[3],$t[5]+1900, @t[2,1,0]') # formatted print of time
1 Like