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

Need korn script to get date - 1 1

Status
Not open for further replies.

Tison

Programmer
May 12, 1999
216
CH
Does anyone have a script to get yesterdays date (catering for yesterday being last month or even 31 12 yy)?
 
To deal with dates in the shell, it is useful to convert to Julian Day values (
The listing below provides two functions for Julian Day conversions that I implemented in KornShell based on C routines appearing in The C Users Journal.

Save the listing to a file, source it ( [tt]. <filename>[/tt] ), and then yesterday's date can be calculated like so:

[tt]
today=$(ToJul `date +&quot;%m %e %Y&quot;`)
(( yesterday = today -1 ))
set `FromJul $yesterday`
month=$1
day=$2
year=$3
echo &quot;$month/$day/$year&quot;
[/tt]

Hope this helps.

**begin script**
[tt]
#!/bin/ksh

# KornShell implementation of Julian Day functions


# ToJul and FromJul functions based on C routines presented by
# David Burki, The C Users Journal, Vol. 11 , No 2, February, 1993,
# page 30.
#
# Burki's algorithm was an adaptation of the FORTRAN code used to
# implement the algorithm presented by H. Fliegl and T. Van Flanders,
# Communications of the ACM, Vol. 11, No. 10, October, 1968, page 657.


function ToJul #month day year
{
# ToJul prints the Julian Day value for the date passed

integer lmonth=${1}
integer lday=${2}
integer lyear=${3}

# set to defaults if null
if [[ ${lmonth}&quot;&quot; -eq &quot;&quot; ]]
then
lmonth=$(date +&quot;%m&quot;)
fi

if [[ ${lday}&quot;&quot; -eq &quot;&quot; ]]
then
lday=$(date +&quot;%d&quot;)
fi

if [[ ${lyear}&quot;&quot; -eq &quot;&quot; ]]
then
lyear=$(date +&quot;%Y&quot;)
fi


jul_day=$(( lday - 32075 + 1461 *
(lyear + 4800 + (lmonth - 14) /12) /
4 + 367 * (lmonth - 2 - (lmonth -14) /
12 * 12 ) / 12 - 3 * ((lyear + 4900 +
(lmonth - 14) / 12 ) / 100) /4 ))

echo ${jul_day}
}


function FromJul #julian_day
{
# FromJul prints the date represented by the passed Julian Day value in the
# form &quot;month day year&quot;

integer t1
integer t2
integer yr
integer mo

integer jul_date=${1}
integer month
integer day
integer year

t1=$((jul_date + 68569))
t2=$(( 4 * t1 / 146097 ))
t1=$(( t1 - (146097 * t2 + 3)/4))
yr=$(( 4000 * ( t1 + 1) /1461001 ))
t1=$(( t1 - 1461 * yr /4 + 31 ))
mo=$(( 80 * t1 / 2447 ))
day=$(( t1 - 2447 * mo / 80 ))
t1=$(( mo / 11 ))
month=$(( mo + 2 - 12 * t1 ))
year=$(( 100 * (t2 - 49) + yr + t1 ))
echo &quot;${month} ${day} ${year}&quot;
}

function DayOfWeek #julian_day
{
integer julian_day=${1}
echo $(( julian_day % 7 ))
}
[/tt]
**end script**

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L
 
That's what I have used to establish the date 1 week back in the format that errpt uses:
===================================

#Establish the date 1 week back in the format of errpt:
CURRENT_DATE=`date +%m%d%H%M%y`
integer MONTH=`echo $CURRENT_DATE |cut -c 1-2`
integer DAY=`echo $CURRENT_DATE |cut -c 3-4`
REMINDER=`echo $CURRENT_DATE |cut -c 5-10`
MONTH=$MONTH+12
if (( ($DAY + 23) <= 30 )) ;then
(( DAY=$DAY+23 ))
(( MONTH=$MONTH-13 ))
else
(( DAY=$DAY-7 ))
(( MONTH=$MONTH-12 ))
fi
if (( $MONTH ==0 )) ;then
MONTH_S=”12”
elif (( $MONTH <= 9 )) ;then
MONTH_S=&quot;0$MONTH&quot;
else
MONTH_S=$MONTH
fi
if (( $DAY <= 9 )) ;then
DAY_S=&quot;0$DAY&quot;
else
DAY_S=$DAY
fi


&quot;Long live king Moshiach !&quot;
 
star for Rod... even though i use perl for such things. =)

IBM Certified -- AIX 4.3 Obfuscation
 
Thanks, Yegolev.

Perl's my choice these days, as well.

That script was so dusty, I'm suprised it didn't clog my net connection. :)



Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
the most simple solution :
imagine you're in GMT-2

do the following :

export OLDTZ=$TZ
export TZ=GMT+22 # (-2 + 24) (if your're in GMT+6 do GMT+30)
DATE=`date`
export TZ=$OLDTZ

#this tip allow your to decrease or increase minutes too. (by GMT-2:30 you'll decrase time by 30 min)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top