Home > Archive > PERL Beginners > June 2006 > converting between time and string
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
converting between time and string
|
|
|
| Hello,
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?
Thank you and best regards,
Benjamin
| |
| Muma W. 2006-06-24, 8:01 am |
| bnj.s wrote:
> Hello,
>
> 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. [...]
You can use POSIX::mktime to convert the string into a time value, but
the milliseconds must be left out.
| |
| Dr.Ruud 2006-06-24, 8:01 am |
| "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."
| |
| Mark Harris 2006-06-24, 8:01 am |
| bnj.s wrote:
> Hello,
>
> 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?
>
> Thank you and best regards,
>
> Benjamin
Unless I don't quite get what you're asking this seems pretty simple
# string to milliseconds
$refTime = "01:22:33,456"; #as in your example
$refTime =~ /(\d+):(\d+):(\d+),(\d+)/; #capture the elements and assign
$hours = $1;
$minutes = $2;
$secs = $3;
$mili = $4;
#calc milliseconds if needed
$millisec = $mili+($secs*1000)+($minutes*60*1000)+($
hours*60*60*1000);
#reduce minutes by 5
$minutes-= 5;
#format for printing
$outStr = sprintf("%02d:%02d:%02d,%03d",$hours,$minutes,$secs,$mili);
print $outStr;
| |
| JupiterHost.Net 2006-06-24, 8:01 am |
| Hello,
> $refTime = "01:22:33,456"; #as in your example
> $refTime =~ /(\d+):(\d+):(\d+),(\d+)/; #capture the elements and assign
> $hours = $1;
> $minutes = $2;
> $secs = $3;
> $mili = $4;
or much simpler:
my($hours, $minutes, $secs, $mili) = $ref_time =~ m{(\d+)}g;
Note that if it doesn't match then the variable will be undef...
This doesn't look strict or wanrings safe either, please always use
use strict;
use warnings;
Please buy Perl best Practices, you'll love it and your code will be
much more readable (like $ref_time vs $refTime is clearer and less
problem prone...)
|
|
|
|
|