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

Date math in ksh

Status
Not open for further replies.

chipjustis

Programmer
Jun 4, 2003
1
US
Anybody know of a quick way to do date math in ksh? I know I could write my own considering leap years and 28 day months, but is there anything simple in ksh similar to Excel for instance?

I'd like to do:

date1 - date2 = days
date1 + days = date2
date1 - days = date2

I also realize Pearl, C, etc. have date routines, but I'm looking for something the within ksh, maybe awk, sed etc?

Thanks
 
Hi:

Date arithmetic has been discussed before at Tek-tips. Take a look at these threads:

thread58-543806

thread219-519109

thread822-514837

Regards,

Ed


 
This might help.

expects the 1st part of your $TZ to look like :

EDT-11EST-10

If you don't want to change the TZ in your current shell, set it in the script that calls the get_date function.

#!/usr/bin/ksh

#
# get_date
#
# function to return day month year relative to today.
# $1 = +n|-n
# $2 = y, display on tty

get_date()
{

[[ $# -lt 1 ]] &&
{
message " Usage: get_day -n|+n"
return 1
}

offset=$(print $TZ | sed -e "s/.*$(date +%Z)\([+-]*[0-9][0-9]*\).*/\1/")

[[ $(expr $1 : ^[+-]) -eq 0 ]] &&
{
message "Invalid argument "
return 1
}

if [ $(expr $1 : ^[-]) -ne 0 ]; then
n="+"$((${1#-}*24))
else
n="-"$((${1#+}*24))
fi

offset=$(($offset${n}))
day=$(TZ=XXX$offset date +%d)
mon=$(TZ=XXX$offset date +%m)
year=$(TZ=XXX$offset date +%Y)

[[ $# -eq 2 ]] && print "$day.$mon.$year"
return 0
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top