[wplug] [SOLVED] converting HH:MM:SS to count of seconds

Alexandros Papadopoulos apapadop at alumni.cmu.edu
Fri Dec 24 03:51:51 EST 2004


On Monday 20 December 2004 16:54, John Harrold wrote:
> Sometime in December Alexandros Papadopoulos assaulted the keyboard 
and produced:
> | 01.11.04%13:35:51%1%847%00:00:02%141                  % 2
> | 01.11.04%13:36:33%5%841%00:00:27%2102319811           % 1
> | 01.11.04%13:36:35%1%847%00:00:03%141                  % 2
> | 01.11.04%13:37:16%1%841%00:00:11%2103645318           % 1
> | 01.11.04%13:37:32%5%837%00:00:58%2102729811           %35
>
> ...
>
> | 00:00:02 becomes 2
> | 00:04:31 becomes 271  (0*60^2 + 4*60^1 + 31*60^0)
> | 02:00:05 becomes 7205 (2*60^2 + 0*60^1 + 5*60^0)
>
>                          ^^^^^^
>                          sure about this, shouldn't it be 60*24

Hm, aren't there 3600 (60*60) seconds in an hour?

> This is my first-pass cheap ass perl solution which seems to work:
>
>
> <script>
> #! /usr/bin/perl
>
> use strict;
> use warnings;
>
> MAIN:
> {
>   my $str  = '';
>   my @arr  = ();
>   my @tarr = ();
>   my $sec  = 0;
>
>   open(FH,"<test.txt");
>
>   while($str =<FH>){
>     chomp($str);
>
>     @arr = split(/%/,$str);
>     if(defined($arr[4])){
>       @tarr = split(/:/, $arr[4]);
>       $sec = int($tarr[0])*60*24 + int($tarr[1])*60 + int($tarr[2]);

I changed this to 
 $sec = int($tarr[0])*3600 + int($tarr[1])*60 + int($tarr[2]);

>       # do something more useful here
>       print $sec."\n";
>     }
>   }
>
>   close(FH);
>   exit 0;
> }
> </script>
>
>
> <test.txt>
> 01.11.04%13:35:51%1%847%01:00:02%141                  % 2
> 01.11.04%13:36:33%5%841%00:15:27%2102319811           % 1
> 01.11.04%13:36:35%1%847%03:00:03%141                  % 2
> 01.11.04%13:37:16%1%841%04:29:11%2103645318           % 1
> 01.11.04%13:37:32%5%837%00:00:58%2102729811           %35
> </test.txt>

It works perfectly, thanks John!

-A

PS: Thank you's also to Patrick and Bryon for their scripts. (I went for 
the Perl version since the server that will do this processing doesn't 
have Python installed)
PPS: Merry Christmas to all! :-)

-A


More information about the wplug mailing list