|
|
|
@ -7,13 +7,13 @@ use base 'Exporter';
|
|
|
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
|
|
|
|
chomp_all strip_bom trim |
|
|
|
|
make_timing parse_timing |
|
|
|
|
make_timing parse_timeshift parse_timing |
|
|
|
|
round |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
|
|
|
|
string => [qw(chomp_all strip_bom trim)], |
|
|
|
|
timing => [qw(make_timing parse_timing)], |
|
|
|
|
timing => [qw(make_timing parse_timeshift parse_timing)], |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
## string functions |
|
|
|
@ -47,11 +47,36 @@ sub make_timing {
|
|
|
|
|
return ($hrs, $min, $sec, $msec); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
# recognized format [+-][[hh:]mm:]ss[.ms] |
|
|
|
|
sub parse_timeshift { |
|
|
|
|
my ($str) = @_; |
|
|
|
|
my $ts = { sign => '+', hrs => 0, min => 0, sec => 0 }; |
|
|
|
|
|
|
|
|
|
return unless defined $str; |
|
|
|
|
|
|
|
|
|
while(1) { |
|
|
|
|
if ($str =~ s<^([+-])><>o) { $ts->{sign} = $1; } |
|
|
|
|
if ($str =~ s<^([0-9](?:\.\d{1,3})?)$><>o) { $ts->{sec} = $1; } |
|
|
|
|
if ($str =~ s<([0-5][0-9](?:\.\d{1,3})?)$><>o) { $ts->{sec} = $1; } |
|
|
|
|
if ($str =~ s<^([0-9]):$><>o) { $ts->{min} = $1; } |
|
|
|
|
if ($str =~ s<([0-5][0-9]):$><>o) { $ts->{min} = $1; } |
|
|
|
|
if ($str =~ s<^(\d+):$><>o) { $ts->{hrs} = $1; } |
|
|
|
|
last; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if ($str or $ts->{min} >= 60 or $ts->{sec} >= 60.0) { |
|
|
|
|
return; # wrong time format |
|
|
|
|
} |
|
|
|
|
my $time = $ts->{sec} + $ts->{min} * 60 + $ts->{hrs} * 3600; |
|
|
|
|
$time *= -1 if $ts->{sign} eq '-'; |
|
|
|
|
return $time; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sub parse_timing { |
|
|
|
|
my ($str) = @_; |
|
|
|
|
my $time = 0.0; |
|
|
|
|
return unless $str =~ m/(\d+):(\d+):(\d+)([.,])(\d+)/oi; |
|
|
|
|
my ($hrs, $min, $sec, $delim, $msec) = ($1, $2, $3, $4, $5); |
|
|
|
|
return unless $str =~ m/(\d+):(\d+):(\d+)[.,](\d+)/o; |
|
|
|
|
my ($hrs, $min, $sec, $msec) = ($1, $2, $3, $4); |
|
|
|
|
if ($msec < 0 or $msec > 999) { |
|
|
|
|
return -1; # wrong mseconds part of timing |
|
|
|
|
} |
|
|
|
|