Manipulating dates in KornShell

Sometimes you need to shift the current date and time by 24 hours, but unfortunately one of the things that KornShell is very poor at is the handling of dates and times. Here are some tips for getting and manipulating dates:

Yesterday’s date:

TEMP_TZ=$TZ
TZ=GMT+24
YESTERDAY=`date`
TZ=$TEMP_TZ

Tomorrow’s date:

TEMP_TZ=$TZ
TZ=GMT-24
TOMORROW=`date`
TZ=$TEMP_TZ

If you need to get the current date and time for use within a SQL statement, then one example of how to do this is shown below:

SYSDATE=`date '+%Y/%m/%d %H:%M:%S'`
select to_date('$SYSDATE','YYYY/MM/DD HH24:MI:SS') from dual;

Advanced date operations

For more advanced date handling, or where you need to perform mathematical operations on dates you could use The KornShell King’s date library. Some examples of it’s use are shown below:

# Results are displayed as a string in the form"YEAR MON DAY HOUR MIN SEC"

NOW=`FN_timeout`

echo "Yesterday is : `FN_unix2date $(( $NOW - 86400 ))` "
echo "4 Hours from now is : `FN_unix2date $(( $NOW + 14400 ))` "
echo "Tomorrow is : `FN_unix2date $(( $NOW + 86400 ))` "
echo "10 Days from now is : `FN_unix2date $(( $NOW + 864000 ))` "

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: