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

using ksh, need yesterday's date

409 views
Skip to first unread message

mr_pota...@my-deja.com

unread,
Jul 29, 2000, 3:00:00 AM7/29/00
to
Hi,
I'm using korn shell and I need to find out what yesterday's date
was. I need this in YYMMDD format. I can output todays date to file
and read the file tomorrow to get yesterday's date but I really don't
want to do that. I'd like to do like a (date - 1) inside my ksh script
to get yesterdays date. This way I can specify any date simply by
changing the "1" value. Thanks in advance.


Sent via Deja.com http://www.deja.com/
Before you buy.

Adam Price

unread,
Jul 29, 2000, 3:00:00 AM7/29/00
to

mr_pota...@my-deja.com wrote in message <8lte90$gtv$1...@nnrp1.deja.com>...

>Hi,
> I'm using korn shell and I need to find out what yesterday's date
>was. I need this in YYMMDD format. I can output todays date to file
>and read the file tomorrow to get yesterday's date but I really don't
>want to do that. I'd like to do like a (date - 1) inside my ksh script
>to get yesterdays date. This way I can specify any date simply by
>changing the "1" value. Thanks in advance.
I see you use deja for posting already, go to comp.unix.shell and do
a deja search for the information, this comes up all the time there.

Summary: you can't do it easily portably and accurately just using the shell,
it's easy using gnu date or mktime and some other interesting hacks exist
for doing it.

Some bad methods include messing with the TZ variable (24 hours ago isn't always
yesterday if your country uses summertime) and the one you posted of creating a
yesterday file in cron (them machine may have been down for three days).
All of this and more will come up in the threads you find.

Hope this helps
Adam Price


Logan Shaw

unread,
Jul 29, 2000, 3:00:00 AM7/29/00
to
In article <964860790.10610.1...@news.demon.co.uk>,

Adam Price <adam+rep...@pappnase.demon.co.uk> wrote:
>mr_pota...@my-deja.com wrote in message <8lte90$gtv$1...@nnrp1.deja.com>...
>> I'm using korn shell and I need to find out what yesterday's date
>>was. I need this in YYMMDD format.

>Summary: you can't do it easily portably and accurately just using the shell,

I guess that depends on your definition of "easily". Here's my quick
implementation:

#! /bin/sh

year=`date +%Y`
month=`date +%m`
day=`date +%d`

# figure out what yesterday was.

day=`expr "$day" - 1`

case "$day" in
0)
month=`expr "$month" - 1`
case "$month" in
0)
month=12
year=`expr "$year" - 1`
;;
esac

case "$month" in
1|3|5|7|8|10|12)
day=31
;;
4|6|9|11)
day=30
;;
2)
if [ `expr "$year" % 4` = 0 \
-a `expr "$year" % 100` != 0 \
-o `expr "$year" % 400` = 0 ]
then
day=29
else
day=28
fi
;;
esac
;;
esac

echo "Year $year month $month day $day"

Formatting the results into YYMMDD format is left as an exercise. I
would probably do it with "sed", personally.

Also note that this Bourne shell, not Korn shell. With Korn shell, you
could probably do a cleaner implementation, or at least one that
invokes fewer external programs, although this rarely ever invokes more
than one external program besides "date".

- Logan

Logan Shaw

unread,
Jul 29, 2000, 3:00:00 AM7/29/00
to
[ can ksh compute yesterday's date? ]

In article <8lv3r5$2cv$1...@provolone.cs.utexas.edu>,

Oh, I just realized that my simple implmentation was far too
complicated. What was I thinking? The Unix philosophy is to combine
tools together to get what you need, and there's already a tool that
solves the most difficult part of the problem for you: finding the last
day of a given month. It's called "cal". Well, O.K., it doesn't quite
tell you directly. You have to ask it nicely:

cal 2 2000 | grep . | fmt -1 | tail -1

So anyway, the *simple* way to do it is this:

#! /bin/sh

year=`date +%Y`
month=`date +%m`
day=`date +%d`

# figure out what yesterday was.

day=`expr "$day" - 1`

case "$day" in
0)
month=`expr "$month" - 1`
case "$month" in
0)
month=12
year=`expr "$year" - 1`
;;
esac

# figure out the last day of the month
day=`cal $month $year | grep . | fmt -1 | tail -1`
esac

echo "Year $year month $month day $day"

Actually, this is less efficient than my previous solution because it
has to run the external programs "cal" and "grep" and "fmt" and "tail"
and "expr", whereas the previous one only had to run "expr". But it's
certainly short and easy.

- Logan

Philip Brown

unread,
Jul 31, 2000, 3:00:00 AM7/31/00
to
On Sat, 29 Jul 2000 08:53:35 +0100, adam+...@pappnase.demon.co.uk wrote:
>
>Summary: you can't do it easily portably and accurately just using the shell,
>it's easy using gnu date or mktime and some other interesting hacks exist
>for doing it.
>
>Some bad methods include messing with the TZ variable (24 hours ago isn't always
>yesterday if your country uses summertime)

bah. it's fairly easy to put in a check for that if you care about that
sort of thing. Just get the "old" value, and see if it is the same as the
current value. If it is the same, then add 1 to 24 and try again.

I more often just want to fake a timestamp for (approximately) 24 hours ago
so I use the TZ method.


--
[Trim the no-bots from my address to reply to me by email!]
[ Do NOT email-CC me on posts. Pick one or the other.]
S.1618 http://thomas.loc.gov/cgi-bin/bdquery/z?d105:SN01618:@@@D
The word of the day is mispergitude

0 new messages