Convert Minutes to Hours

I often use both Winamp and my iPhone to listen to music. These two, unfortunately, show the time differently in the songs. Winamp displays the time in minutes (mm) while the iPhone does it hour/minutes (hh:mm). Here’s a quick little script I whipped together because I’m too lazy to do this in my head, especially for audio books where an audio book can be over 500 minutes, and I need to convert to iPhone time because I want to continue listening where I had just left off on Winamp.

use POSIX qw(ceil floor); # used for the floor function
 
sub GetToken {
  # @_ = flatten args list from an array
  # @_[0] = first argument
 
  $data      = @_[0];
  $delimiter = @_[1];
  $token     = @_[2] - 1;
 
  @tokens_array = split($delimiter, $data);   
 
  return @tokens_array[$token]; 
}
 
sub chr_conver_min {  
  if (length(@_[0]) == 1) {
    return "0".@_[0];
  }
  else {
    return @_[0];
  }   
}
 
 
sub iphone_time_convert {
 
  # converts winamp time to iphone - winamp stores time only in minutes.  
  # @_[0]   =  winamp_time, e.g. 124:34
  # $hour   = floor($winamp_time/60);
  # $minute = $winamp_time % 60;
 
  $winamp_hour_min = GetToken(@_[0], ":", 1);  
  $winamp_seconds  = GetToken(@_[0], ":", 2);  
 
  return floor($winamp_hour_min/60).":".chr_conver_min( ($winamp_hour_min % 60) ).":".$winamp_seconds;
 
}
 
 
sub winamp_time_convert {  
 
  # converts iphone time to winamp  
  # @_[0] = iphone_time, e.g. 3:43:34    
  $iphone_hour     = GetToken(@_[0], ":", 1);  
  $iphone_min      = GetToken(@_[0], ":", 2);    
  $iphone_seconds  = GetToken(@_[0], ":", 3);
 
  return (($iphone_hour * 60) + $iphone_min).":".$iphone_seconds;
 
}
 
sub show_help {
  print "\nDisplays the conversion of time between winamp and iPhone.\n\n";
  print "   winamptime [-w2i|-i2p] [mm:ss][hh:mm:ss]\n\n";
  print "Example to convert winamp time to iPhone: \n\n";
  print "   winamptime -w2i 212:41\n\n";
  print "Example to convert iPhone time to winamp, seconds being optional: \n\n";
  print "   winamptime -i2w 2:31:41\n";
  print "   winamptime -i2w 2:31\n\n";
}
 
 
# START
 
# Optimize this:
if( $ARGV[0] eq "-w2i" ) 
{
  # winamp to iphone time
  if ( length($ARGV[1]) > 0 ) {
    print "iPhone Time: ".iphone_time_convert( $ARGV[1] )."\n";
  }
}
elsif( $ARGV[0] eq "-i2w" ) 
{
  # iphone to winamp time
  if ( length($ARGV[1]) > 0 ) {
    print "Winamp Time: ".winamp_time_convert( $ARGV[1] )."\n";
  }
}
else 
{
  show_help();
}

Output:

Leave a Reply