Change Date Input :-

I have Below Input :-

X1=03 ### Hour
Y1=20160405 ## Date
Z1=3 ## I want to Back 3 Hour

Output

List=03 02 01
Y1=20160405

Input:-

X1=02 ### Hour
Y1=20160405 ## Date
Z1=4 ## I want to Back 4 Hour

Output:-

List=02 01 24 23
Y1=20160404 ### Date Will Change

Any attempts/ideas/thoughts from your side?

And, be aware that switching back in dates can be seriously involved.

HI

I am still Trying....

shell approach:

. file             # source input file to set all variables
printf "List="; for ((i=0; i<Z1; i++)); do T=$(((23 + X1 - i) % 24 + 1)); [ $T = 24 ] && ((Y1--)); printf "%02d " $T; done; printf "\nY1=%s\n" $Y1
List=02 01 24 23 
Y1=20160404

Thanks ...

Error :-

syntax error at line 12 : `((' unexpected

How come that's somewhere near line 12?
What OS and shell version do you use?

Solaris and Shell Script #ksh

in your script change #!/bin/ksh to #!/bin/ksh93

Command not Found

./test.sh

must be in a diff path - probably /usr/bin/ksh93 - don't have Solaris in front of me.
Try it with /usr/bin/bash ...

What Solaris version are you on? Do uname -a

1 Like

Now its Working

Thanks Guys

IMO it'd be better to perform such calculations in perl as it has the built in libraries for handling date/time requirements...

ksh93 using builtin time conversion and calculation:

#!/usr/bin/ksh93

X1=02 ### Hour
Y1=20160405 ## Date
Z1=4 ## I want to Back 4 Hour

day=$(printf "%(%Y-%m-%d)T" $Y1)
timestamp="$day $X1:00:00"
epoc=$(printf "%(%#)T" "$timestamp")


cnt=1
str=""
while ((cnt<=Z1))
do

        h=$(printf "%(%H)T" "#$epoc")
        ((h==0)) && h=24
        str="$str$h "
        ((epoc-=3600))  # 1 hour, 3600 s
        ((cnt+=1))
done

endday=$(printf "%(%Y%m%d)T" "#$epoc")
echo List=$str
echo Y1=$endday

Hi.

I agree with shamrock, and there is a perl version of date right here: General Purpose Date Script -- I call it date.pl on my system.

Here's a demo snippet:

pl " Standard date:"
/usr/bin/date

pl " Standard date with arithmetic correction of -15 hours (expect fail):"
/usr/bin/date --date="15 hours ago"

pl " Standard date with arithmetic correction of -15 hours (expect fail):"
/usr/xpg4/bin/date --date="15 hours ago"

pl " perl version of date:"
date.pl --date="-15 hours"

producing:

 Standard date:
Sun Apr 10 03:25:53 CDT 2016

-----
 Standard date with arithmetic correction of -15 hours (expect fail):
/usr/bin/date: illegal option -- date=15 hours ago
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]

-----
 Standard date with arithmetic correction of -15 hours (expect fail):
/usr/xpg4/bin/date: illegal option -- date=15 hours ago
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]

-----
 perl version of date:
2016-04-09 12:25:54

For a sysem like:

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: SunOS, 5.11, i86pc
Distribution        : Oracle Solaris 11.3 X86
bash GNU bash 4.1.17
date - ( /usr/bin/date, 2016-04-10 )
date.pl - ( local: RepRev 1.4, ~/bin/date.pl, 2014-02-18 )

Bedst wishes ... cheers, drl