Operating System - HP-UX
1824149 Members
4738 Online
109668 Solutions
New Discussion юеВ

Shell script: How to obtain future date

 
SOLVED
Go to solution
Michael Hemmingsen
Occasional Advisor

Shell script: How to obtain future date

Hi:

Does anyone have a suggestion to an easy way of generating the following output:

THE DATE AND TIME 3 DAYS LATER THAN THE PRESENT DATE AND TIME.

I need it for producing a script, that creates a user which expires date 3 days later. One approch would be to obtain the date, via the 'date' command, but it would then be nescessary to somehow convert the string '07' (e.g. output of 'date +%m') to the integer 7.

Your help is appreciated!
Why take your-/myself so seriously?
11 REPLIES 11
Alexander M. Ermes
Honored Contributor

Re: Shell script: How to obtain future date

Hi there.
Try this :

#
TAG_=`date +'%y%m%d'`; export TAG_


export TAG1="000000"
TAG1=`expr $TAG_ + 1`
echo $TAG1

Rgds
Alexander M. Ermes
.. and all these memories are going to vanish like tears in the rain! final words from Rutger Hauer in "Blade Runner"
Michael Hemmingsen
Occasional Advisor

Re: Shell script: How to obtain future date

Nope, the expr $TAG... doesn't work. The output echoed to screen is: 010705+1
Why take your-/myself so seriously?
federico_3
Honored Contributor

Re: Shell script: How to obtain future date

Try:
let TAG1=$TAG_+1
Ralph Grothe
Honored Contributor

Re: Shell script: How to obtain future date

Micheal,

as date calculations sometimes can get a bit tricky, and since most standard shells (except for bash, zsh etc.) have only limited arithmetic functionality, I would suggest doing it with Perl.
(Perl should be on any decent Unix box ;-)

e.g.

$ perl -e 'printf "%s\n",scalar localtime(time + 3*86400)'

If you need some fancier formatting you would have to invoke localtime() in list context.
See the POD for what localtime returns (i.e. $ perldoc -f localtime)
Madness, thy name is system administration
federico_3
Honored Contributor

Re: Shell script: How to obtain future date

or
TAG1=`expr $TAG_ + 1`
Michael Hemmingsen
Occasional Advisor

Re: Shell script: How to obtain future date

Nope, both let TAG1=$TAG_+1 and
TAG1=`expr $TAG_ + 1` result in syntax errors.
Why take your-/myself so seriously?
Andreas Voss
Honored Contributor
Solution

Re: Shell script: How to obtain future date

Hi,

put you a C-Source at attachment.
Save it ie: under /usr/local/src/fdate.c
Compile it ie:
cc /usr/local/src/fdate.c -o /usr/local/bin/fdate

Use it ie: 3 days forward:
fdate 3

You can also compute backwards with ie:

fdate -3

Regards
A. Clay Stephenson
Acclaimed Contributor

Re: Shell script: How to obtain future date

Hi Micheal:

I has a technique that you will find very useful. It involves converting a date into a truncated Julian Day (more or less the number of days since Jan 1, 4712 BCE; it's used by astronomers to make life simple). In your case, it takes care of month,year,leap year boundaries with ease.

The script I've attached will convert a calender date into a Julian Day and also do the inverse. It also has an argument to calculate the day of the week. Invoke the script as caljd.sh -u for full usage.

In your case I would do it like this:

INC=3
JD=`caljd.sh`
JD=$((${JD} + ${INC}))
CAL_DATE=`caljd.sh ${JD}`

This will calculate 3 days after the current date. If you supply the first call to caljd.sh
with MM DD YYYY it will calculate 3 days after that date. The number of args is how caljd.sh knows to convert from calendar to julian day or julian day to calendar date.

Regards, Clay
If it ain't broke, I can fix that.
federico_3
Honored Contributor

Re: Shell script: How to obtain future date

Andrea, your c script does not work. I get the following:
cc: "fdate.c", line 2: error 1000: Unexpected symbol: "i".
cc: "fdate.c", line 2: error 1002: Unexpected character: 'u'.
cc: "fdate.c", line 2: error 1000: Unexpected symbol: "<".
cc: "fdate.c", line 2: error 1000: Unexpected symbol: ".".
cc: "fdate.c", line 2: error 1000: Unexpected symbol: ">".
cc: "fdate.c", line 16: error 1588: "stdout" undefined.


How come?
Andreas Voss
Honored Contributor

Re: Shell script: How to obtain future date

Hi,

this may be a bug when putting something at attachment.
The first line of the file is inserted with tabs.

So delete the inserted tabs so that it looks like:

#include
#include
.
.
.
.

Regards
Michael Hemmingsen
Occasional Advisor

Re: Shell script: How to obtain future date

The perl thing does work (however a bit elaborate). I created the below script to obtain the date 5 days later in the format dd/mm/yy. It itsn't wery elegant, but does work:

#!/usr/bin/sh
#
# futuredate.sh
#
###############################################################################

NINETH=`perl -e 'printf "%s",scalar localtime(time + 5*86400)'|cut -c9`

if [ "$NINETH" = " " ]
then
NEWDATE=0`perl -e 'printf "%s",scalar localtime(time + 5*86400)'|cut -c10`
else
NEWDATE=`perl -e 'printf "%s",scalar localtime(time + 5*86400)'|cut -c9-10`
fi

MONTH=`perl -e 'printf "%s",scalar localtime(time + 5*86400)'|cut -c5-7`

case $MONTH in
Jan) NEWMONTH=01 ;;
Feb) NEWMONTH=02 ;;
Mar) NEWMONTH=03 ;;
Apr) NEWMONTH=04 ;;
May) NEWMONTH=05 ;;
Jun) NEWMONTH=06 ;;
Jul) NEWMONTH=07 ;;
Aug) NEWMONTH=08 ;;
Sep) NEWMONTH=09 ;;
Oct) NEWMONTH=10 ;;
Now) NEWMONTH=11 ;;
Dec) NEWMONTH=12 ;;
esac

NEWYEAR=`perl -e 'printf "%s",scalar localtime(time + 5*86400)'|cut -c23-24`

echo ${NEWDATE}/${NEWMONTH}/${NEWYEAR}
Why take your-/myself so seriously?