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

John Harrold jmh17 at pitt.edu
Mon Dec 20 09:54:54 EST 2004


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

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]);
      # 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>


-- 
---------------------------------------------------------- 
                            | /"\                         
 john harrold               | \ / ASCII ribbon campaign   
 jmh at member.fsf.org      |  X  against HTML mail       
 the most useful idiot      | / \                         
----------------------------------------------------------
 What difference does it make to the dead, the orphans, 
 and the homeless, whether the mad destruction is brought 
 under the name of totalitarianism or the holy name of 
 liberty and democracy?
 --Gandhi
----------------------------------------------------------
  gpg --keyserver keys.indymedia.org --recv-key F65A739E
----------------------------------------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://penguin.wplug.org/pipermail/wplug/attachments/20041220/123fc691/attachment.bin


More information about the wplug mailing list