Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date passed in as parameter - how to add 1 day to this date?

Status
Not open for further replies.

ptmcs

Technical User
Oct 1, 2004
38
GB
hi,

a little bit of an odd one, if anyone has a neat idea.

We have a script which runs overnight using current date, in the case of failure we may need to rerun it and force the date to be a specific one.

For example, we may force a date like 20071231

Within the script, the date is used in two places -
firstly exactly as passed in like:
20071231
and then secondly needs to be + 1 date like:
20080101

Any ideas how to add 1 day to a passed in date like
20071231? so it becomes 20080101.

Thanks for any ideas!



 
I got this from one of the forum posts and it worked for me on AIX/KSH. Dont remember who posted it. There are also many posts in this forum regarding dates including some very good responses from PHV. You may search those too.
Code:
GetDate(){ # GetDate nDays [format]
  typeset -i nDays=$1; format=$2
  eval $(echo $TZ | sed 's!\([^-0-9]*\)\([-0-9]*\)\(.*\)!typeset -i localOffset=\2;zon1=\1;zon2=\3!')
  TZ=$zon1$((localOffset-24*nDays))$zon2 date $format
}
echo "Yesterday: $(GetDate -1)"
echo "Tomorrow: $(GetDate 1)"
echo "Next week: $(GetDate 7 '+%Y-%m-%d')"
 
Credit where its due - the post is PHV's FAQ80-4800

Ceci n'est pas une signature
Columb Healy
 
Sorry guys, I'm not sure you understood what was required.

It's not the date relative to today -
it's a date relative to a date passed in as a parameter to the script...

but don't worry now, a quick one line call to the database solves the issue...

never mind..
 
The short answer is that date calculations in any language are difficult and, in ksh, impossible (now just watch someone prove me wrong!).

I can be done in perl which has a module designed to do just that but it's a lot of work.

Ceci n'est pas une signature
Columb Healy
 
Columb, anything (well allmost ;-)) is possible in ksh, but I don't have the time right now to prove you wrong in this instance...


HTH,

p5wizard
 
Ok, so impossible is a bit of a strong word. As a starting point I'd do something along the lines of
Code:
#!/bin/ksh
date=$1
set -A max_month 0 31 28 31 30 31 30 31 31 30 31 30 31
eval $(echo $date|sed 's!\(....\)\(..\)\(..\)!year=\1;month=\2;day=\3!')
(( day += 1 ))
if [ $day -gt $max_month[$month] ]
then
  $day=1
  (( month += 1 ))
  if [ $month -gt 12 ]
  then
    (( year += 1 )
    month=1
  fi
fi
new_date=$(printf "%4.4d%2.2d%2.2d $year $month $day)
but it doesn't cope with leap years

Ceci n'est pas une signature
Columb Healy
 
little bit nasty, but utilised oracle:

sqlplus -s ${USERID} <<eof>tmpwk
select 'Xx;'||to_char(to_date('${enforceDate}','YYYYMMDD')+1,'DD/MM/YY') from dual;
exit
eof
dateStampInterface=`cat tmpwk|grep 'Xx'|awk 'FS=";" {print $2}'`
 
Just to be pedantic, if you're using awk you don't need grep, or the cat. Try
Code:
dateStampInterface=$(awk FS=";" '/Xx/ {print $2}' tmpwk)

Ceci n'est pas une signature
Columb Healy
 
Code:
#!/bin/ksh
date=$1
set -A max_month 0 31 28 31 30 31 30 31 31 30 31 30 31
eval $(echo $date|sed 's!\(....\)\(..\)\(..\)!year=\1;month=\2;day=\3!')
[red](( year4=year%4 ))
(( year100=year%100 ))
(( year400=year%400 ))
if [ \( $year4 -eq 0 -a \
        $year100 -ne 0 \) -o \
     $year400 -eq 0 ]
then
 set -A max_month 0 31 29 31 30 31 30 31 31 30 31 30 31
fi[/red] 
(( day += 1 ))
if [ $day -gt $max_month[$month] ]
then
  $day=1
  (( month += 1 ))
  if [ $month -gt 12 ]
  then
    (( year += 1 )
    month=1
  fi
fi
new_date=$(printf "%4.4d%2.2d%2.2d $year $month $day)

HTH,

p5wizard
 
columb said:
Just to be pedantic, if you're using awk you don't need grep, or the cat. Try
CODE
dateStampInterface=$(awk FS=";" '/Xx/ {print $2}' tmpwk)
columb,
are you saying that we have another UUOG and UUOC 'broad shouldered' person in our midst? ;)

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
gnu's date is worth installing for this.

gdate -d "12/31/2007 + 1 day"
 
Try

TZ=GMT-24 date

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top