The READIN Family Album
Me and Gary, brooding (September 2004)

READIN

Jeremy's journal

The alternatives are not placid servitude on the one hand and revolt against servitude on the other. There is a third way, chosen by thousands and millions of people every day. It is the way of quietism, of willed obscurity, of inner emigration.

J.M. Coetzee


(This is a page from my archives)
Front page
More recent posts
Older posts
More posts about:
Programming
Programming Projects
Projects

Archives index
Subscribe to RSS

This page renders best in Firefox (or Safari, or Chrome)

🦋 Interval

Here is a bash script to determine the interval between two date/times. Parameters are two dates, specified using any format the date utility can recognize; if the second parameter is omitted, "now" is assumed. Output is the number of seconds between the two, followed by "d h:m:s" format.

 #!/bin/bash

if [ $# -eq 0 ]
then
echo Usage: `basename $0` \ \[\\ default \"now\"\] >&2
exit -1
fi

start=`date +%s -d "$1"`
if [ $# -eq 1 ]
then
fin=`date +%s`
else
fin=`date +%s -d "$2"`
fi

res=`expr $fin - $start`
if [ $res -lt 0 ]
then
res=`expr 0 - $res`
fi

echo $res sec
d=`expr $res / 86400`
t=`expr $res % 86400`
h=`expr $t / 3600`
ms=`expr $t % 3600`
m=`expr $ms / 60`
s=`expr $ms % 60`
if [ $d -gt 0 ]
then
echo -n $d day
if [ $d -gt 1 ]
then
echo -n s
fi
echo -n \
fi
if [ $t -gt 0 ]
then
echo -n $h\:
if [ $m -lt 10 ]
then
echo -n 0
fi
echo -n $m
if [ $s -gt 0 ]
then
echo -n \:
if [ $s -lt 10 ]
then
echo -n 0
fi
echo -n $s
fi
fi
echo

posted evening of Sunday, March 11th, 2007
➳ More posts about Programming
➳ More posts about Programming Projects
➳ More posts about Projects

Respond:

Name:
E-mail:
(will not be displayed)
Link:
Remember info

Drop me a line! or, sign my Guestbook.
    •
Check out Ellen's writing at Patch.com.

Where to go from here...

Friends and Family
Programming
Texts
Music
Woodworking
Comix
Blogs
South Orange
readinsinglepost