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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting date one month before today using Unix shell Script 2

Status
Not open for further replies.

RajShekar

Programmer
May 27, 2004
14
0
0
US
I want to get date one month before today using unix shell script in YYYYMMDD format?
 
And what is displayed when type this at your shell prompt:
(TZ=X-19; export TZ; date; echo $TZ)
Don't forgot the parens

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Obviously the date command executed in your environment is not the one described in your man page !
What is the result of:
type date

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Check for alias or function named date.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
prompt>type date
date is a tracked alias for /usr/bin/date
prompt>date
Thu Jun 10 16:06:29 EDT 2004
 
Ask your sysadmin why the date command you use doesn't complies the man page.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I think I can clear this up. HP-UX has a default timezone of EST5EDT when TZ is unset or invalid. It does not recognize "X" as a valid timezone.
[tt]
$ echo $TZ
EST5EDT
$ date
Fri Jun 11 06:06:22 EDT 2004
$ TZ=X+24 date
Fri Jun 11 06:06:27 EDT 2004
$ TZ=X-24 date
Fri Jun 11 06:06:30 EDT 2004
[/tt]
To use the "TZ trick" you need to retain the time zone names...
[tt]
$ TZ=EST+24EDT date
Thu Jun 10 11:06:49 EDT 2004
$ TZ=EST-24EDT date
Sat Jun 12 11:06:56 EDT 2004
[/tt]
PHV: maybe update the FAQ?
 
I got the following with my date function:
Code:
bash-2.05a$ date    
Fri Jun 11 13:22:24 [COLOR=blue]CEST[/color] 2004

bash-2.05a$ TZ=[COLOR=red]X[/color]+1 date
Fri Jun 11 11:21:56 [COLOR=red]GMT[/color] 2004

bash-2.05a$ TZ=[COLOR=blue]CEST+167[/color] date
Fri Jun  [COLOR=blue]4[/color] 12:21:44 CEST 2004

bash-2.05a$ TZ=[COLOR=red]CEST+168[/color] date
Fri Jun [COLOR=red]11[/color] 11:21:47 [COLOR=red]GMT[/color] 2004

So:
1) the date defaults to GMT if the time zone is not valid.
2) there is a limit to the offset (in hours) to the timezone, it must be lower than 7 days (168 hours).

No way to get the date of a month ago with this method, at least no portable way.

I'm using the bsd date of Mac OSX ( man page release January 20, 1998).

--------------------

Denis
 
dchoulette: Good post. It would appear that the "TZ" trick does not work (or is not reliable) on all UNIX systems.
PHV: maybe update/delete the FAQ?
vgersh99: maybe create new FAQ?
 
Could you please test this new function ?
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')
 
The results are:-
[tt]
Yesterday: Tue Jun 15 16:15:05 BST 2004
Tomorrow: Thu Jun 17 16:15:05 BST 2004
Next week: 2004-06-16
[/tt]
It fails because of reason 2 given by dchoulette above.
 
Here are what I got:

Yesterday: Wed Jun 16 18:04:55 EDT 2004
Tomorrow: Thu Jun 17 18:04:55 EDT 2004
Next week: 2004-06-16
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top