Browse Source

+ Subtitle::TimePoint

master
Alex 'AdUser' Z 6 years ago
parent
commit
6bbc5e1824
  1. 37
      lib/Subtitle/TimePoint.pm
  2. 28
      t/timepoint.t

37
lib/Subtitle/TimePoint.pm

@ -0,0 +1,37 @@
package Subtitle::TimePoint;
use strict;
use warnings;
use Subtitle::Utils ':timing';
sub new {
my ($class, %args) = @_;
my $self = {
shift => $args{shift} || 0.0,
time => $args{time} || 0.0,
};
return bless($self, $class);
}
sub parse {
my ($self, $timespec) = @_;
return 'empty timespec, nothing to do'
unless $timespec;
return 'wrong timespec, expected <time>/[+-]<time>, abort'
unless $timespec =~ m<^([0-9:.]+)/([+-]?[0-9:.]+)$>o;
my ($t1, $t2) = ($1, $2);
$self->{time} = parse_timeshift($t1);
$self->{shift} = parse_timeshift($t2);
return "can't parse time: $t1"
unless defined $self->{time};
return "can't parse time: $t2"
unless defined $self->{shift};
return;
}
sub shift { return CORE::shift->{'shift'}; }
sub time { return CORE::shift->{'time'}; }
1;

28
t/timepoint.t

@ -0,0 +1,28 @@
use strict;
use warnings;
use Test::More tests => 19;
use Subtitle::TimePoint;
my $p = Subtitle::TimePoint->new;
is(ref $p, 'Subtitle::TimePoint');
can_ok($p, qw(new parse time shift));
# valid timespec
is($p->parse('0/0'), undef);
is($p->time, 0.0), is($p->shift, 0.0);
is($p->parse('0.3/0'), undef);
is($p->time, 0.3), is($p->shift, 0.0);
is($p->parse('1:17/-4.1'), undef);
is($p->time, 77), is($p->shift, -4.1);
is($p->parse('2:15:03.515/+3:02.7'), undef);
is($p->time, 8103.515), is($p->shift, 182.7);
# invalid timespec
is($p->parse('0.3/-'), "wrong timespec, expected <time>/[+-]<time>, abort");
is($p->parse('1:17/-4.'), "can't parse time: -4.");
is($p->parse(':47/-5.4'), "can't parse time: :47");
is($p->parse('1:1.2/-5'), "can't parse time: 1:1.2");
is($p->parse('5:31./7'), "can't parse time: 5:31.");
1;
Loading…
Cancel
Save