Korn Shell 1993 Date/Time String
Manipulation and Arithmetic
Finnbarr P. Murphy
(fpm@fpmurphy.com, blog.fpmurphy.com)
A classic problem in shell scripting is the manipulation of dates and times.
Most shells do not include support for date/time string manipulation and the
user is left to roll their own routines as needed. Typically this involves parsing
date/time strings and using lookup tables and/or using a version of date with
support for formatting date/time strings other than current date.
Since 1999, when version h of ksh93 (the 1993 version of the Korn Shell) was
released, ksh93 has included such support via the printf builtin function.
However user documentation on this feature is scarce and I have written this
short article in an attempt to make more people aware of this extremely
useful feature in ksh93.
The ksh93 builtin printf (not printf(1)) includes a %T formatting option
%T Treat the string as a date/time string and format it
accordingly.
%(dateformat)T T can be preceded by dateformat, where
dateformat is any date format supported by the
date(1) command.
Some examples will illustrate the power of this feature
Example 1 Output the current date just like the date(1) command
$ printf "%T\n" now
Sat Mar 22 10:01:35 EST 2008
Example 2 Output the current hour, minute and second
$ printf "%(%H:%M:%S)T\n" now
10:02:07
Note that ksh93 does not fork/exec the date(1) command to process this
statement. It is built into ksh93. This results in faster shell script execution
and less load on the system.
Example 3 Output the number of seconds since the UNIX Epoch
$ printf “%(%s)T\n” now
1206199251
1 of 4 Copyright © Finnbarr P. Murphy 2009 7/9/2009
Example 5 If you know the number of seconds since the UNIX Epoch you
can output the corresponding date/time in ctime format
$ printf “%T\n” ‘#’1206199251
Sat Mar 22 10:22:35 EST 2008
$ printf “%T\n” ‘#’$seconds
The printf builtin also understands date/time strings like “2:00pm yesterday”,
“this Wednesday”, “23 days ago” , “next 9:30am”, “in 6 days”, “+ 5 hours
10 minutes” and lots more. Look at the source code for the printf builtin
(cmd/ksh93/bltins/print.c) in the ksh93 sources for more information on the
various date/time strings which are supported.
Example 6 Output the date/time corresponding to “2:00pm yesterday.”
$ printf "%T\n" '2:00pm yesterday'
Fri Mar 21 14:00:00 EST 2008
Example 7 Output the week day corresponding to the last day of February
2008.
$ printf '%(%a)T\n' "final day Feb 2008"
Fri
Example 8 Output the date corresponding to the third Wednesday in May
2008.
$ printf '%(%D)T\n' "3rd wednesday may 2008"
05/21/08
Example 9 Output what date it was 4 weeks ago.
$ printf '%(%D)T\n' "4 weeks ago"
02/18/08
Example 10You can assign the output of printf “%T” to a variable. Note that
“1997-198” represents the 198th day in 1997
$ datestr=$(printf '%(%D)T' "1997-198")
$ print $datestr
07/17/97
The printf builtin even understands crontab and at date/time syntax as the
following two examples demonstrate
Example 11Output the date/time the command associated with this crontab
entry will next execute.
$ printf "%T" "0 0 1,15 * 1"
2 of 4 Copyright © Finnbarr P. Murphy 2009 7/9/2009
Mon Sep 1 00:00:00 EDT 2008$
Example 12Output the date/time the command associated with this at
date/time string will execute.
$ printf "%T\n" "exactly next hour"
Sun Mar 23 14:07:31 EST 2008
Example 13Here is how to output the date for the first and last days of last
month.
Care needs to be taken in the order in which the date string is entered as not
all combinations work.
$ printf "%(%Y-%m-%d)T\n" "1st last month"
2008-05-01
$ printf "%(%Y-%m-%d)T\n" "final last month"
2008-05-31
The final example is a short shell script which tackles a common problem
associated with backing up files and deleting logs, i.e. calculate the difference
between 2 given dates.
Example 14Output the difference between two dates
#!/usr/bin/ksh93
#
# USAGE: diffdate start-date finish-date
#
# EXAMPLE: diffdate "Tue, Feb 19, 2008 08:00:02 PM" \
# "Wed, Feb 20, 2008 02:19:09 AM"
#
# Note – The version limited to a maximum of 100 hours difference
SDATE=$(printf '%(%s)T' "$1")
FDATE=$(printf '%(%s)T' "$2")
[[ $# -ne 2 ]] && {
print "Usage: diffdate start-date finish-date"
exit 1
}
DIFF=$(($FDATE-$SDATE))
SECS=$(($DIFF % 60))
MINS=$(($DIFF % (60 * 60) / 60))
HOURS=$(($DIFF / (60 * 60)))
printf "%02d:%02d:%02d\n" $HOURS $MINS $SECS
exit 0
3 of 4 Copyright © Finnbarr P. Murphy 2009 7/9/2009
Example 14 Output a range of dates
#!/bin/ksh93
#
# Example 13 – Output a range of dates in a specific format incremented by
# 1 hour each time.
#
startdate="2008-05-26 01:00:00"
count=71
for ((i=0; i < count; i++))
do
printf "%(%m%d%Y%H0000)T\n" "${startdate} + $i hour"
done
exit 0
Well, that’s about all there is to the printf %T feature in ksh93. There are a
number of other interesting features in the printf builtin which you can learn
about by entering “printf –man” at the ksh93 shell prompt. I hope that you
have found this short introduction to the power of the printf %T feature to be
informative and that you will start using it in the future shell scripts.
4 of 4 Copyright © Finnbarr P. Murphy 2009 7/9/2009