"bnj.s" schreef:
> I have a file with lots of times in the format "01:22:33,456", where
> the numbers after the comma represent the milliseconds.
>
> I would like to reduce all these times by a given offset. I first
> thought to convert the string to a sort of date format, and then to
> do a minus operation, and then to convert the result back to a string
> of the original form. However, I have great difficulties finding how
> to handle times and dates. I am sure, however, that there exist a
> simple way to do so in perl. Could you please help me and tell me how
> you would do it?
What is the smallest (earliest) time value, and is it bigger than the
offset?
If yes:
#!/usr/bin/perl
use strict ;
use warnings ;
use integer ;
sub t2ms
{
$_[3] ||= '' ;
$_[3] .= '0' while length $_[3] < 3 ;
return ( ( $_[0] * 60 + $_[1] ) * 60 + $_[2] ) * 1000 + $_[3] ;
}
sub ms2t
{
$_[3] = $_[0] % ( 1000) ;
$_[2] = $_[0] % ( 60 * 1000) / ( 1000) ;
$_[1] = $_[0] % (60 * 60 * 1000) / ( 60 * 1000) ;
$_[0] = $_[0] / (60 * 60 * 1000) ;
return @_ ;
}
my $qr = qr/(\d\d):(\d\d):(\d\d)(?:,(\d*))?/ ;
my $offset = - t2ms( '00:00:01,234' =~ m/$qr/ ) ;
while ( <DATA> )
{
printf "%02d:%02d:%02d,%03d\n", ms2t( t2ms( m/$qr/ ) + $offset ) ;
}
__DATA__
01:22:33,456
12:34:56,7
23:45:19,001
23:45:19,01
23:45:19,0
23:45:19,
23:45:19
But please check out DateTime::Duration too.
--
Affijn, Ruud
"Gewoon is een tijger."
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>