calculating difference of times

2012-07-27 Thread Matthias Apitz

Hello,

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

matthias
-- 
Matthias Apitz
e g...@unixarea.de - w http://www.unixarea.de/
UNIX since V7 on PDP-11, UNIX on mainframe since ESER 1055 (IBM /370)
UNIX on x86 since SVR4.2 UnixWare 2.1.2, FreeBSD since 2.2.5
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


calculating difference of times

2012-07-27 Thread Robert Huff

Matthias Apitz writes:

  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

I don't know if there's something already available. (Sorry -
never had this problem.)
If the format is fixed, then parsing it with awk is trivial.
After that, the math should be doable with expr.


Robert Huff


___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: calculating difference of times

2012-07-27 Thread Matthew Seaman
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=08:12
t2=12:08

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

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

mdelta=$(echo $h2 * 60 + $m2 - $h1 * 60 - $m1 | bc)
hdelta=$(( $mdelta / 60 ))
mdelta=$(( $mdelta % 60 ))
tdelta=printf %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

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




signature.asc
Description: OpenPGP digital signature