File Coverage

blib/lib/Audio/MPD/Common/Time.pm
Criterion Covered Total %
statement 26 26 100.0
branch 2 2 100.0
condition n/a
subroutine 5 5 100.0
pod n/a
total 33 33 100.0


line stmt bran cond sub pod time code
1             #
2             # This file is part of Audio-MPD-Common
3             #
4             # This software is copyright (c) 2007 by Jerome Quelin.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9 2     2   622 use 5.008;
  2         6  
10 2     2   11 use strict;
  2         4  
  2         45  
11 2     2   9 use warnings;
  2         4  
  2         161  
12              
13             package Audio::MPD::Common::Time;
14             # ABSTRACT: class representing time of current song
15             $Audio::MPD::Common::Time::VERSION = '2.003';
16 2     2   859 use Moose 0.92; # need hash trait
  2         485887  
  2         17  
17              
18              
19             # -- attributes
20              
21              
22             has time => ( is=>'ro', isa=>'Str', default=>'0:0' );
23              
24              
25              
26              
27             # _cooked_values contains all the computed values.
28             has _cooked_values => (
29             traits => [ 'Hash' ],
30             is => 'ro',
31             isa => 'HashRef',
32             lazy_build => 1,
33             handles => {
34             percent => [ get => 'percent' ],
35             sofar => [ get => 'sofar' ],
36             left => [ get => 'left' ],
37             total => [ get => 'total' ],
38             sofar_secs => [ get => 'sofar_secs' ],
39             sofar_mins => [ get => 'sofar_mins' ],
40             seconds_sofar => [ get => 'seconds_sofar' ],
41             total_secs => [ get => 'total_secs' ],
42             total_mins => [ get => 'total_mins' ],
43             seconds_total => [ get => 'seconds_total' ],
44             left_secs => [ get => 'left_secs' ],
45             left_mins => [ get => 'left_mins' ],
46             seconds_left => [ get => 'seconds_left' ],
47             },
48             );
49              
50             # -- builders
51              
52             sub _build__cooked_values {
53 2     2   5 my $self = shift;
54 2         75 my $time = $self->time;
55              
56 2         10 my ($seconds_sofar, $seconds_total) = split /:/, $time;
57 2         6 my $seconds_left = $seconds_total - $seconds_sofar;
58 2 100       8 my $percent = $seconds_total ? 100*$seconds_sofar/$seconds_total : 0;
59              
60             # Parse the time so far
61 2         7 my $sofar_mins = int( $seconds_sofar / 60 );
62 2         4 my $sofar_secs = $seconds_sofar % 60;
63 2         12 my $sofar = sprintf "%d:%02d", $sofar_mins, $sofar_secs;
64              
65             # Parse the total time
66 2         4 my $total_mins = int( $seconds_total / 60 );
67 2         4 my $total_secs = $seconds_total % 60;
68 2         5 my $total = sprintf "%d:%02d", $total_mins, $total_secs;
69              
70             # Parse the time left
71 2         5 my $left_mins = int( $seconds_left / 60 );
72 2         2 my $left_secs = $seconds_left % 60;
73 2         6 my $left = sprintf "%d:%02d", $left_mins, $left_secs;
74              
75              
76             return {
77             # time elapsed in seconds
78 2         140 seconds_sofar => $seconds_sofar,
79             seconds_left => $seconds_left,
80             seconds_total => $seconds_total,
81              
82             # cooked values
83             sofar => $sofar,
84             left => $left,
85             total => $total,
86             percent => sprintf("%.1f", $percent), # 1 decimal
87              
88             # details
89             sofar_secs => $sofar_secs,
90             sofar_mins => $sofar_mins,
91             total_secs => $total_secs,
92             total_mins => $total_mins,
93             left_secs => $left_secs,
94             left_mins => $left_mins,
95             };
96             }
97              
98              
99             1;
100              
101             __END__
102              
103             =pod
104              
105             =encoding UTF-8
106              
107             =head1 NAME
108              
109             Audio::MPD::Common::Time - class representing time of current song
110              
111             =head1 VERSION
112              
113             version 2.003
114              
115             =head1 DESCRIPTION
116              
117             L<Audio::MPD::Common::Status> returns some time information with the
118             C<time()> accessor. This information relates to the elapsed time of the
119             current song, as well as the remaining and total time. This information
120             is encapsulated in an L<Audio::MPD::Common::Time> object.
121              
122             An L<Audio::MPD::Common::Time> object does B<not> update itself
123             regularly, and thus should be used immediately.
124              
125             Note: one should B<never> ever instantiate an L<Audio::MPD::Common::Time>
126             object directly - use the mpd modules instead.
127              
128             =head1 ATTRIBUTES
129              
130             =head2 $time->time;
131              
132             The time passed to the constructor, used to compute all others values
133             (see methods). It is the time value (on the "time" line) of what the MPD
134             server returns to the status command. Defaults to C<0:0>.
135              
136             =head1 METHODS
137              
138             =head2 my $str = $time->sofar;
139              
140             Return elapsed C<$time> (C<minutes:seconds> format).
141              
142             =head2 my $str = $time->left;
143              
144             Return remaining C<$time> (C<minutes:seconds> format).
145              
146             =head2 my $str = $time->left;
147              
148             Return total C<$time> (C<minutes:seconds> format).
149              
150             =head2 my $percent = $time->percent;
151              
152             Return elapsed C<$time> (percentage, 1 digit).
153              
154             =head2 my $secs = $time->seconds_sofar;
155              
156             Return elapsed C<$time> in seconds.
157              
158             =head2 my $secs = $time->seconds_left;
159              
160             Return remaining C<$time> in seconds.
161              
162             =head2 my $secs = $time->seconds_total;
163              
164             Return total C<$time> in seconds.
165              
166             =head2 my $mins = $time->sofar_mins;
167              
168             Return minutes part of elapsed C<$time>.
169              
170             =head2 my $secs = $time->sofar_secs;
171              
172             Return seconds part of elapsed C<$time>.
173              
174             =head2 my $mins = $time->left_mins;
175              
176             Return minutes part of remaining C<$time>.
177              
178             =head2 my $secs = $time->left_secs;
179              
180             Return seconds part of remaining C<$time>.
181              
182             =head2 my $mins = $time->total_mins;
183              
184             Return minutes part of total C<$time>.
185              
186             =head2 my $mins = $time->total_secs;
187              
188             Return seconds part of total C<$time>.
189              
190             =head1 AUTHOR
191              
192             Jerome Quelin
193              
194             =head1 COPYRIGHT AND LICENSE
195              
196             This software is copyright (c) 2007 by Jerome Quelin.
197              
198             This is free software; you can redistribute it and/or modify it under
199             the same terms as the Perl 5 programming language system itself.
200              
201             =cut