Time calculator


/ Published in: Perl
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. print <<EOF;
  6. Welcome to tc.pl, the time calculator.
  7. Expressions may contain hour:minute:second or minute:second literals.
  8. Use socat (http://www.dest-unreach.org/socat/) to add readline capability:
  9. socat READLINE EXEC:./tc.pl,pty
  10. EOF
  11.  
  12. while( <> )
  13. {
  14. s/\s+//g;
  15. next if /^$/;
  16. if( /([^0-9+\-*\/:.()])/ ) {
  17. print "Illegal character `$1'.\n";
  18. next;
  19. }
  20. s/([0-9]+):([0-9]+):([0-9]+)/(3600*$1+60*$2+$3)/g;
  21. s/([0-9]+):([0-9]+)/(60*$1+$2)/g;
  22. # prevent interpretation as octal:
  23. s/\b0+([1-9][0-9]*)\b/$1/g;
  24. my $secs= 1*sprintf("%.3f", eval);
  25. print $secs;
  26. my $mins= int($secs/60);
  27. $secs -= $mins*60;
  28. $secs= sprintf("%02d", $secs);
  29. print " = $mins:$secs" if $mins;
  30. my $hours= int($mins/60);
  31. $mins -= $hours*60;
  32. $mins= sprintf("%02d", $mins);
  33. print " = $hours:$mins:$secs" if $hours;
  34. print "\n";
  35. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.