Return to Snippet

Revision: 16544
at August 7, 2009 12:13 by deepsoul


Initial Code
#!/usr/bin/perl

use strict;

print <<EOF;
Welcome to tc.pl, the time calculator.
Expressions may contain hour:minute:second or minute:second literals.
Use socat (http://www.dest-unreach.org/socat/) to add readline capability:
socat READLINE EXEC:./tc.pl,pty
EOF

while( <> )
{
  s/\s+//g;
  next if /^$/;
  if( /([^0-9+\-*\/:.()])/ ) {
    print "Illegal character `$1'.\n";
    next;
  }
  s/([0-9]+):([0-9]+):([0-9]+)/(3600*$1+60*$2+$3)/g;
  s/([0-9]+):([0-9]+)/(60*$1+$2)/g;
  # prevent interpretation as octal:
  s/\b0+([1-9][0-9]*)\b/$1/g;
  my $secs= 1*sprintf("%.3f", eval);
  print $secs;
  my $mins= int($secs/60);
  $secs -= $mins*60;
  $secs= sprintf("%02d", $secs);
  print " = $mins:$secs" if $mins;
  my $hours= int($mins/60);
  $mins -= $hours*60;
  $mins= sprintf("%02d", $mins);
  print " = $hours:$mins:$secs" if $hours;
  print "\n";
}

Initial URL

                                

Initial Description
Use this to add up your overtime.  May not be suited for processing untrusted input (e.g. as a CGI script), as the input check is rather rudimentary.

Initial Title
Time calculator

Initial Tags

                                

Initial Language
Perl