No, it does not work on all systems at all times.
In some it will work practically always,
on others never, on most it works sometimes
and sometimes not. I would recommend against it.
Unfortunately there is no short and sweet portable
solution. If you have or can install Gnu date it
will do it cleanly, otherwise you can find a number
of solutions posted in this group in the past.
For a general solution you could try the following,
which should work with POSIXy shells (I've only tested
it with HP's which is essentially ksh88, though):
#! /usr/bin/sh
# Date calculations using POSIX shell
# Tapani Tarvainen July 1998, February 2001 (POSIXified)
# This code is in the public domain.
# Julian Day Number from calendar date
date2julian() # day month year
{
day=$1; month=$2; year=$3
tmpmonth=$((12 * year + month - 3))
tmpyear=$((tmpmonth / 12))
print $(( (734 * tmpmonth + 15) / 24 - 2 * tmpyear + \
tmpyear/4 - tmpyear/100 + tmpyear/400 + day + 1721119 ))
}
# Calendar date from Julian Day Number
julian2date() # julianday
{
tmpday=$(($1 - 1721119))
centuries=$(( (4 * tmpday - 1) / 146097))
tmpday=$((tmpday + centuries - centuries/4))
year=$(( (4 * tmpday - 1) / 1461))
tmpday=$((tmpday - (1461 * year) / 4))
month=$(( (10 * tmpday - 5) / 306))
day=$((tmpday - (306 * month + 5) / 10))
month=$((month + 2))
year=$((year + month/12))
month=$((month % 12 + 1))
print $day $month $year
}
# Day of week, Monday=1...Sunday=7
dow() # day month year
{
print $(( $(date2julian $1 $2 $3) % 7 + 1))
}
################################# The End #################################
Those allow rather arbitrary date computations.
For example, yesterday's date can be computed like this:
julian2date $(( $(date2julian $(date +"%d %m %Y") ) - 1 ))
--
Tapani Tarvainen
> "Jean-Noêl" <Je...@freckles.de> writes:
>
>> To determine the yesterday date i do it so:
>> TZ=PST+24 date +%d
>> it work well but my question is:
>> does this work on all systems and all shells
>> or should i do it otherwise ???
>
> No, it does not work on all systems at all times.
> In some it will work practically always,
> on others never, on most it works sometimes
> and sometimes not. I would recommend against it.
>
In fact, it doesnt work in a situation I need it in at work right now. Im
using an RS6000 with AIX4.3 and /bin/ksh . We dont use Perl , or bash, so
I need a workaround. The closest Ive come so far is ( for Feb11, i need the
10th, so I need 0210) :
TODAY=`date -u +%m%d`
YESTERDAY=$(($TODAY - 1))
Which gave me 210.
Any tips ( hopefully less than 1000 lines! )
> In fact, it doesnt work in a situation I need it in at work right now. Im
> using an RS6000 with AIX4.3 and /bin/ksh . We dont use Perl , or bash, so
> I need a workaround. The closest Ive come so far is ( for Feb11, i need the
> 10th, so I need 0210) :
>
> TODAY=`date -u +%m%d`
This probably had 0211 in it..
> YESTERDAY=$(($TODAY - 1))
And THIS line deleted the leading zero for you.
> Which gave me 210.
Leaving you this...
> Any tips ( hopefully less than 1000 lines! )
You might want to experiment with 'typeset'. And AIX *does* support
the TZ trick as well.
--
Valdis Kletnieks
Computer Systems Senior Engineer
Virginia Tech
Thanks, I will look into typeset, as the TZ trick seemed to give me only
todays date
http://groups.google.com/groups?q=yesterday+date+group:comp.unix.shell+group
:comp.unix.shell&hl=en&selm=370ABBFC.C16F3EE1%40hotmail.com&rnum=1
--
Phstpok
The last surviving member of a race of mentors and idiots.
Jean-Noêl> To determine the yesterday date i do it so:
Jean-Noêl> TZ=PST+24 date +%d
Jean-Noêl> it work well but my question is:
Jean-Noêl> does this work on all systems and all shells
Jean-Noêl> or should i do it otherwise ???
Jean-Noêl> Thanks for help.
This is extremely portable:
yesterdays_date=`date`
sleep 86400
echo "Yesterday's date is $yesterdays_date"
:-)
Careful - it breaks during DST transitions. As did yours.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Hey Randal, That worked great! Im trying it on another function that
returns the previous year's date. I'll let you know how it goes when it
finishes.