Audio-MPD-Common
view release on metacpan or search on metacpan
lib/Audio/MPD/Common/Time.pm view on Meta::CPAN
# _cooked_values contains all the computed values.
has _cooked_values => (
traits => [ 'Hash' ],
is => 'ro',
isa => 'HashRef',
lazy_build => 1,
handles => {
percent => [ get => 'percent' ],
sofar => [ get => 'sofar' ],
left => [ get => 'left' ],
total => [ get => 'total' ],
sofar_secs => [ get => 'sofar_secs' ],
sofar_mins => [ get => 'sofar_mins' ],
seconds_sofar => [ get => 'seconds_sofar' ],
total_secs => [ get => 'total_secs' ],
total_mins => [ get => 'total_mins' ],
seconds_total => [ get => 'seconds_total' ],
left_secs => [ get => 'left_secs' ],
lib/Audio/MPD/Common/Time.pm view on Meta::CPAN
);
# -- builders
sub _build__cooked_values {
my $self = shift;
my $time = $self->time;
my ($seconds_sofar, $seconds_total) = split /:/, $time;
my $seconds_left = $seconds_total - $seconds_sofar;
my $percent = $seconds_total ? 100*$seconds_sofar/$seconds_total : 0;
# Parse the time so far
my $sofar_mins = int( $seconds_sofar / 60 );
my $sofar_secs = $seconds_sofar % 60;
my $sofar = sprintf "%d:%02d", $sofar_mins, $sofar_secs;
# Parse the total time
my $total_mins = int( $seconds_total / 60 );
my $total_secs = $seconds_total % 60;
my $total = sprintf "%d:%02d", $total_mins, $total_secs;
lib/Audio/MPD/Common/Time.pm view on Meta::CPAN
return {
# time elapsed in seconds
seconds_sofar => $seconds_sofar,
seconds_left => $seconds_left,
seconds_total => $seconds_total,
# cooked values
sofar => $sofar,
left => $left,
total => $total,
percent => sprintf("%.1f", $percent), # 1 decimal
# details
sofar_secs => $sofar_secs,
sofar_mins => $sofar_mins,
total_secs => $total_secs,
total_mins => $total_mins,
left_secs => $left_secs,
left_mins => $left_mins,
};
}
lib/Audio/MPD/Common/Time.pm view on Meta::CPAN
Return elapsed C<$time> (C<minutes:seconds> format).
=head2 my $str = $time->left;
Return remaining C<$time> (C<minutes:seconds> format).
=head2 my $str = $time->left;
Return total C<$time> (C<minutes:seconds> format).
=head2 my $percent = $time->percent;
Return elapsed C<$time> (percentage, 1 digit).
=head2 my $secs = $time->seconds_sofar;
Return elapsed C<$time> in seconds.
=head2 my $secs = $time->seconds_left;
Return remaining C<$time> in seconds.
=head2 my $secs = $time->seconds_total;
use Audio::MPD::Common::Time;
use Test::More tests => 14;
#
# formatted output
my $time = Audio::MPD::Common::Time->new( time => '126:225' );
is( $time->sofar, '2:06', 'sofar() formats time so far' );
is( $time->left, '1:39', 'left() formats remaining time' );
is( $time->total, '3:45', 'sofar() formats time so far' );
is( $time->percent, '56.0', 'percent() gives percentage elapsed' );
#
# so far
is( $time->sofar_secs, 6, 'sofar_secs() gives seconds so far' );
is( $time->sofar_mins, 2, 'sofar_mins() gives minutes so far' );
is( $time->seconds_sofar, 126, 'seconds_sofar() gives time so far in secs' );
#
# left details
#
# total details
is( $time->total_secs, 45, 'total_secs() gives seconds total' );
is( $time->total_mins, 3, 'total_mins() gives minutes total' );
is( $time->seconds_total, 225, 'seconds_total() gives time total in secs' );
#
# testing null time
$time = Audio::MPD::Common::Time->new( time => '126:0' );
is( $time->percent, '0.0', 'percent() defaults to 0' );
( run in 0.340 second using v1.01-cache-2.11-cpan-709fd43a63f )