Operating System - HP-UX
1821609 Members
3475 Online
109633 Solutions
New Discussion юеВ

Reg Previous date calculation

 
harikrishna
Advisor

Reg Previous date calculation

hi friends,
i am using the following command for calculating the previous date in hp unix server.

$(sh -c "TZ=$(date +%Z)+24; export TZ; date '+%Y%m%d'")

But it is not working now.i dont know why. so if any body plz let me know any command to calculate the prevoius date.it is very urgent to me.

Thanks in Advance

Regards,
HariKrishna
7 REPLIES 7
Sridhar Bhaskarla
Honored Contributor

Re: Reg Previous date calculation

Hi HariKrishna,

Try the following simple script

NOW=$(/usr/contrib/bin/perl -e "printf("%d\n",time())")
(( YES = $NOW - 86400 ))
echo "0d${YES}=Y" |adb

86400 is the number of seconds since yesterday. If you want 'n' days back, then you calculate it by doing n*24*60*60

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
harikrishna
Advisor

Re: Reg Previous date calculation

hi sri,
thanks for giving the reply.but i want the code in unix script only .

plz let me know if you know it in the unix

Thanking you

regards,
HariKrishna
Sridhar Bhaskarla
Honored Contributor

Re: Reg Previous date calculation

Hi Harikrishna,

That is unix script. Perl is available in /usr/contrib/bin on any HP-UX and it is used only once to get the epoch seconds. Rest are all HP commands.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
A. Clay Stephenson
Acclaimed Contributor

Re: Reg Previous date calculation

Tricks that alter TZ only work over a limited range of TZ's because the offset must remain in the range 0-23. Scripts that violate this only work by accident and can break without warning (e.g. a patch to the date command of libC). Man 5 environ for details. The Perl solution is a valid one but here's a method that will work over any date range --- including those that will kill the Perl solution.

YESTERDAY=$(caljd.sh -s -y $(calhd.sh -p 1))
echo "Yesterday = ${YESTERDAY}"

If you like, you can yank out the cal_jdate and jdate_cal functions and use them inside your own script for a "pure" shell solution.


If it ain't broke, I can fix that.
Dave La Mar
Honored Contributor

Re: Reg Previous date calculation

The following is used quite a bit here and differs only in the variable value read in.
TZ=PST8PDT+24 date +%A | read Yesterday

But for a robust and solid date routine, A.Clay's has been noted by many to be the top choice.

We also use his. Shucks, now I owe royalties.

Best of luck.

Regards,

dl
"I'm not dumb. I just have a command of thoroughly useless information."
A. Clay Stephenson
Acclaimed Contributor

Re: Reg Previous date calculation

Ooops, you'd think I could get my own command right, should be:

YESTERDAY=$(caljd.sh -s -y $(caljd.sh -p 1))
echo "Yesterday = ${YESTERDAY}"

Invoke as caljd.sh -u for full usage and examples.

If it ain't broke, I can fix that.
Jdamian
Respected Contributor

Re: Reg Previous date calculation

I miss tools and utilities to handle dates and times.