From owner-freebsd-questions@FreeBSD.ORG Fri Jul 27 13:02:38 2012 Return-Path: Delivered-To: freebsd-questions@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id CD038106566B for ; Fri, 27 Jul 2012 13:02:38 +0000 (UTC) (envelope-from matthew@FreeBSD.org) Received: from smtp.infracaninophile.co.uk (smtp6.infracaninophile.co.uk [IPv6:2001:8b0:151:1:3cd3:cd67:fafa:3d78]) by mx1.freebsd.org (Postfix) with ESMTP id 34BEA8FC0C for ; Fri, 27 Jul 2012 13:02:38 +0000 (UTC) Received: from seedling.local (seedling.black-earth.co.uk [81.187.76.163]) (authenticated bits=0) by smtp.infracaninophile.co.uk (8.14.5/8.14.5) with ESMTP id q6RD2YCr054687 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO) for ; Fri, 27 Jul 2012 14:02:34 +0100 (BST) (envelope-from matthew@FreeBSD.org) X-DKIM: OpenDKIM Filter v2.5.2 smtp.infracaninophile.co.uk q6RD2YCr054687 Authentication-Results: smtp.infracaninophile.co.uk/q6RD2YCr054687; dkim=none (no signature); dkim-adsp=none X-Authentication-Warning: lucid-nonsense.infracaninophile.co.uk: Host seedling.black-earth.co.uk [81.187.76.163] claimed to be seedling.local Message-ID: <5012915F.6020906@FreeBSD.org> Date: Fri, 27 Jul 2012 14:02:23 +0100 From: Matthew Seaman User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 To: freebsd-questions@FreeBSD.org References: <20120727123429.GA2094@tiny.Sisis.de> In-Reply-To: <20120727123429.GA2094@tiny.Sisis.de> X-Enigmail-Version: 1.4.3 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enigD07B248322A172A542AD641D" X-Virus-Scanned: clamav-milter 0.97.5 at lucid-nonsense.infracaninophile.co.uk X-Virus-Status: Clean X-Spam-Status: No, score=-2.6 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on lucid-nonsense.infracaninophile.co.uk Cc: Subject: Re: calculating difference of times X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 27 Jul 2012 13:02:38 -0000 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--