root/misc/stopwatch

Revision 2062 (checked in by miyagawa, 14 years ago)

support h:mm:ss.ms format. Ugly code!

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl
2 use strict;
3 use POSIX;
4 use Time::HiRes qw( gettimeofday usleep );
5
6 $| = 1;
7
8 my $start = gettimeofday;
9 while (1) {
10     my $time = gettimeofday - $start;
11     my($hour, $min, $sec, $ss) = hms($time);
12     printf "%d:%02d:%02d%s\r", $hour, $min, $sec, $ss;
13     usleep 0.01;
14 }
15
16 sub hms {
17     my $sec = shift;
18
19     my($hour, $min, $ss);
20     $min  = POSIX::floor($sec / 60);
21     $sec  = $sec - $min * 60; # don't use %
22     $hour = $min / 60;
23     $min  = $min % 60;
24     my $tmp = $sec;
25     $sec  = POSIX::floor($sec);
26     $ss   = substr(sprintf('%.02f', $tmp - $sec), 1);
27
28     return ($hour, $min, $sec, $ss);
29 }
30
31
Note: See TracBrowser for help on using the browser.