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!

Date maths in ksh script

Status
Not open for further replies.

Mthales

Technical User
Feb 27, 2004
1,181
GB
I have a script and in this I want to be able to take a date and work on the dates of the seven days before this date.

If the date is today then I can use the date command like this:
Code:
x=7
while [ $x -ge 0 ]
do
        DAY="$( date --date="${x} days ago" +%D)"
        ## work with the date I've found
        x=$(($x-1))
done

but how can I best do it for any other date?
Thanks for any help
Emma

--------------------------------------
For animals, the entire universe has been neatly divided into things to (a) mate with, (b) eat, (c) run away from, and (d) rocks.
-- Terry Pratchett, Equal Rites
 
This is how I get days gone by. Hope it helps.
# days 1 which is 1 x 24 + 5 = 29
a1=`env TZ=CST6CDT+29 date`
echo $a1

this is 24 hours ago
formula I use is
1 x 24 +5 =29 or 24 hours ago
2 x 24 +5 =53 or 48 hours ago
3 x 24 +5 =77 or 3 days ago etc ......

$env TZ=CST6CDT+29 date
Tue Oct 17 09:39:00 CDT 2006
$env TZ=CST6CDT+53 date
Mon Oct 16 09:39:07 CDT 2006
$env TZ=CST6CDT+77 date
Sun Oct 15 09:39:51 CDT 2006
$env TZ=CST6CDT+101 date
Sat Oct 14 09:40:13 CDT 2006

 
Thanks for your answer lpblauen but I'm not quite sure I understand what you mean.

I've managed to coble together a messy way of doing what I want so maybe you can offer some advice on that:

Code:
## get today's date
TODAY=$( date +%D)
## get the seconds since epoc for today
TODAY=$( date --date=${TODAY} +%s)

## get the seconds since epoc for the date I want to work from
DIFF=$( date --date=${DAY} +%s)

## subtract the old date from today
DIFF=$((${TODAY}-${DIFF}))
## devide by the number of seconds in a day
DIFF=$((${DIFF}/86400))
## so I know how many days ago that date was 

## ... so THATDAY should match DAY
THATDAY=$( date --date="${DIFF} days ago" +%D)

## then for the whole of the week before that day
k=6
while [ $k -ge 0 ]
do
       THISDAY=$(date --date="$((${DIFF}+${k})) days ago" +%d%m%Y)
       ## work on THISDAY
done

Does that make my question any clearer?
Emma

--------------------------------------
For animals, the entire universe has been neatly divided into things to (a) mate with, (b) eat, (c) run away from, and (d) rocks.
-- Terry Pratchett, Equal Rites
 
You can't subtract a date form a date that I know. Here is a script to go back up to 10 days. Try it maybe you can use it to make your script.

#set -x
if [ $# -ne 1 ]; then
echo " Valid args are numbers 1 thur 10 for # of days."
exit 1
fi
# here we set the days. we want to go back up to 10.
# this is based on eastern time. day x 24 +5 = 24hr ago.
case "$1" in
1) x=29;;
2) x=53;;
3) x=77;;
4) x=101;;
5) x=125;;
6) x=149;;
7) x=173;;
8) x=193;;
9) x=221;;
10) x=245;;
*) echo " Valid args are numbers 1 thur 10 for days."; exit 1;;
esac
ee=`env TZ=+$x date +"%D"`
echo $ee
 
She's not subtracting dates as such, but seconds from the epoch, which should be fine.

That script looks good to me Emma, if it works fine for you, use it!

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top