First, I don't think you can solve this problem with just the date command unless you take the first suggestion. Second, I'd use perl to solve this problem. If you don't have perl, but you have access to GNU's awk, gawk, the following script provides yesterday's
date:
time.sh -1
You'll have to change the gawk internal strftime function formatting to fit your own fromatting needs.
#!/bin/bash
# time.sh
# Author: Julian Cates
#
# Figure what a date was/will be, given an offset from current day:
#./time.sh -7
# Thu May 17 10:49:06 CDT 2001
#======================================================================
ME=`basename ${0}`
if [ $# -ne 1 ]
then echo "Usage is $ME [+/-] # of days"
else
gawk -v days=$1 'BEGIN {
target_date = systime()+(86400*days)
datestring = strftime("%c", target_date)
printf "%s\n\n",datestring }'
fi
Third, if you don't want to use the GNU tools, then you need to do some programming. I choose to manipulate the Julian Date. Not the modern julian date which counts the number of days from January 1 of the current year, but the astronomer's algorithm that tracks days from Jan. 1, 4713 BC.
There are well known algorithms that are easily implemented. The following ksh script gets the Julian Date of the system date, subtracts 1 to get yesterday's JD, and
converts yesterday's JD to the gregorian date:
#!/bin/ksh
# The Julian date (JD) is a continuous count of days from 1 January 4713 BC. The following
# algorithm is good from years 1801 to 2099
# See URL:
for more information
get_greg_from_JD ()
{
typeset -i L
typeset -i N
typeset -i I
typeset -i J
typeset -i DAY
typeset -i MON
typeset -i YR
L=$(($1+68569)) # $1 is the julian date
N=$((4*L/146097))
L=$((L-(146097*N+3)/4))
I=$((4000*(L+1)/1461001))
L=$((L-1461*I/4+31))
J=$((80*L/2447))
DAY=$((L-2447*J/80))
L=$((J/11))
MON=$((J+2-12*L))
YR=$((100*(N-49)+I+L))
echo $MON $DAY $YR
}
TY="20"$(date '+%y') # Year for today
TM=$(date '+%m') # Month for today
TD=$(date '+%d') # Day for today
JD=$(get_JD TD TM TY) # today's Julian date
YJD=$((JD-1)) # yesterday's julian date
date_str=$(get_greg_from_JD YJD)
# parse yesterday's date string
set - $(echo $date_str)
echo $1 # month
echo $2 # day
echo $3 # year
yesterday (0-6); date --date="-1 day" +"%w"
tomorrow (0-6); date --date="+1 day" +"%w"
yesterday (00-31); date --date="-1 day" +"%d"
tomorrow (00-31); date --date="+1 day" +"%d"
yesterday (000-366); date --date="-1 day" +"%j"
tomorrow (000-366); date --date="+1 day" +"%j"
next month (01-12); date --date="+1 month" +"%m"
last month (01-12); date --date="-1 month" +"%m"
next year (00-99); date --date="+1 year" +"%y"
last year (00-99); date --date="-1 year" +"%y"
"week", "year", "minute", "second" also work.
I believe someone pointed out that the shell solution in your FAQ80-953 only works in the GMT timezone. Tek-tips search is down at the moment so I couldn't search for it. CaKiwi
I got this script from some site and am using it. Hope this helps.
----------------------------
TODAY=`date "+%y %m %d" | nawk '{split("31,31,28,31,30,31,30,31,31,30,31,30",month,","
if ($1 % 4 == 0)
{
month[3]=29;
};
day=$3-1;
mon=$2+0;
year=$1+0;
if (day == 0)
{
day=month[$2-0];
mon=$2-1;
if (mon == 0)
{
mon=12;
year=$1-1;
}
}
printf("%02d/%02d/%02d",mon,day, year);}'`
echo $TODAY
-----------------------------
Also, awk isn't "C". The semi-colons are totally unnecessary. That could be your problem.
Not that it probably matters, but your leap year function is incorrect. Leap year is a year evenly divisible by 4, provided the year is not divisible by 100, except when divisible by 400. Thus, 2000 is a leap year whereas 1900 is not.
Ed, Thanks for the Leap year corrections. I try to include those in my script. As i said I dint write the script myself and am not too good at script. Shall try to work without those semicolns. Thanks
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.