Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

yesterday's date under the shell

89 views
Skip to first unread message

Jean-Noêl

unread,
Feb 12, 2002, 9:44:37 AM2/12/02
to
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 ???
Thanks for help.


Tapani Tarvainen

unread,
Feb 12, 2002, 10:33:11 AM2/12/02
to
"Jean-Noêl" <Je...@freckles.de> writes:

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

chris

unread,
Feb 12, 2002, 1:15:45 PM2/12/02
to
Tapani Tarvainen gobbled:

> "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! )

Valdis Kletnieks

unread,
Feb 12, 2002, 1:39:52 PM2/12/02
to
chris <chrisS...@gnubin.com> writes:


> 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

chris

unread,
Feb 12, 2002, 9:39:14 PM2/12/02
to
Valdis Kletnieks gobbled:

Thanks, I will look into typeset, as the TZ trick seemed to give me only
todays date

phstpok

unread,
Feb 12, 2002, 10:09:55 PM2/12/02
to
"Jean-Noêl" <Je...@freckles.de> wrote in message
news:a4b9ir$78s$1...@penthesilea.materna.de...
One solution (not mine) from a search of google. I've tried it on d-unix4.0f
and hp-ux10

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.


Randal L. Schwartz

unread,
Feb 19, 2002, 12:48:56 PM2/19/02
to
>>>>> "Jean-Noêl" == Jean-Noêl <Je...@freckles.de> writes:

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!

cjvo

unread,
Feb 22, 2002, 1:52:15 PM2/22/02
to

"Randal L. Schwartz" <mer...@stonehenge.com> wrote in message
news:m17kp95...@halfdome.holdit.com...

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.

0 new messages