Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 27 Jul 2012 14:02:23 +0100
From:      Matthew Seaman <matthew@FreeBSD.org>
To:        freebsd-questions@FreeBSD.org
Subject:   Re: calculating difference of times
Message-ID:  <5012915F.6020906@FreeBSD.org>
In-Reply-To: <20120727123429.GA2094@tiny.Sisis.de>
References:  <20120727123429.GA2094@tiny.Sisis.de>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help
This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enigD07B248322A172A542AD641D
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On 27/07/2012 13:34, Matthias Apitz wrote:
> Do we have something (in the ports) to calculate easy the difference of=

> two times given as hh:mm - hh:mm? Some hack in bc(1) or something like
> this? Well, I could translate the times into UNIX seconds of epoche,
> build the diff and reconvert, but something more easy (and not in Perl
> or C, just shell); thanks

Not as such.  Generic toolkits for doing time differences are fairly
common, but they tend to be a) quite large and b) written in higher
level languages than shell.  However they usually account for all the
annoying corner cases like switching to daylight savings time.

If your times are always going to be strictly hh:mm (24h clock) and you
aren't worried about time differences over more than one day, then
something like this in shell:

t1=3D08:12
t2=3D12:08

h1=3D${t1%:*}
h2=3D${t2%:*}

m1=3D${t1#*:}
m2=3D${t2#*:}

mdelta=3D$(echo "$h2 * 60 + $m2 - $h1 * 60 - $m1" | bc)
hdelta=3D$(( $mdelta / 60 ))
mdelta=3D$(( $mdelta % 60 ))
tdelta=3Dprintf "%02d:%02d" $hdelta $mdelta"

This will calculate the duration from 23:59 to 00:01 as -23:58; ie. it
assumes both times are on the same calendar day.  Coming up with the
answer 00:02 is left as an exercise for the student.

	Cheers,

	Matthew

--=20
Dr Matthew J Seaman MA, D.Phil.
PGP: http://www.infracaninophile.co.uk/pgpkey



--------------enigD07B248322A172A542AD641D
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.16 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlASkWoACgkQ8Mjk52CukIzF3gCffs/Nw9DmlFoiK5CcoOYTaZ2L
XLQAnjC6Vwqg9b8y9ISzOQtCA34p56/g
=vRCC
-----END PGP SIGNATURE-----

--------------enigD07B248322A172A542AD641D--



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?5012915F.6020906>