2 # This file is part of Audio::MPD::Common
3 # Copyright (c) 2007 Jerome Quelin, all rights reserved.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the same terms as Perl itself.
10 package Audio
::MPD
::Common
::Time
;
15 use base qw
[ Class
::Accessor
::Fast
];
16 __PACKAGE__
->mk_accessors
17 ( qw
[ percent sofar left total
18 sofar_secs sofar_mins seconds_sofar
19 total_secs total_mins seconds_total
20 left_secs left_mins seconds_left
23 #our ($VERSION) = '$Rev$' =~ /(\d+)/;
30 # my $status = Audio::MPD::Common::Time->new( $time )
32 # The constructor for the class Audio::MPD::Common::Time. $time is
33 # the time value (on the "time" line) of what the output MPD server
34 # returns to the status command.
37 my ($class, $time) = @_;
39 my ($seconds_sofar, $seconds_total) = split /:/, $time;
40 my $seconds_left = $seconds_total - $seconds_sofar;
41 my $percent = $seconds_total ?
100*$seconds_sofar/$seconds_total : 0;
43 # Parse the time so far
44 my $sofar_mins = int( $seconds_sofar / 60 );
45 my $sofar_secs = $seconds_sofar % 60;
46 my $sofar = sprintf "%d:%02d", $sofar_mins, $sofar_secs;
48 # Parse the total time
49 my $total_mins = int( $seconds_total / 60 );
50 my $total_secs = $seconds_total % 60;
51 my $total = sprintf "%d:%02d", $total_mins, $total_secs;
54 my $left_mins = int( $seconds_left / 60 );
55 my $left_secs = $seconds_left % 60;
56 my $left = sprintf "%d:%02d", $left_mins, $left_secs;
61 # time elapsed in seconds
62 seconds_sofar
=> $seconds_sofar,
63 seconds_left
=> $seconds_left,
64 seconds_total
=> $seconds_total,
70 percent
=> sprintf("%.1f", $percent), # 1 decimal
73 sofar_secs
=> $sofar_secs,
74 sofar_mins
=> $sofar_mins,
75 total_secs
=> $total_secs,
76 total_mins
=> $total_mins,
77 left_secs
=> $left_secs,
78 left_mins
=> $left_mins,
91 Audio::MPD::Common::Time - class representing time of current song
96 my $time = $status->time;
102 C<Audio::MPD::Common::Status> returns some time information with the C<time()>
103 accessor. This information relates to the elapsed time of the current song,
104 as well as the remaining and total time. This information is encapsulated
105 in an C<Audio::MPD::Common::Time> object.
107 Note that an C<Audio::MPD::Common::Time> object does B<not> update itself
108 regularly, and thus should be used immediately.
119 The C<new()> method is the constructor for the C<Audio::MPD::Common::Time>
122 Note: one should B<never> ever instantiate an C<Audio::MPD::Common::Time>
123 object directly - use the mpd modules instead.
130 Once created, one can access the following members of the object:
136 The C<sofar()>, C<left()> and C<total()> methods return the according values
137 under the form C<minutes:seconds>. Note the existence of a C<percent()>
138 method returning a percentage complete. (one decimal)
141 =item values in seconds:
143 The C<seconds_sofar()>, C<seconds_left()> and C<seconds_total()> return the
144 according values in seconds.
147 =item detailled values:
149 If you want to cook your own value, then the following methods can help:
150 C<sofar_secs()> and C<sofar_mins()> return the seconds and minutes elapsed.
151 Same for C<left_secs()> and C<left_mins()> (time remaining), C<total_secs()>
152 and C<total_mins()>. (total song length)
158 Please note that those accessors are read-only: changing a value will B<not>
159 change the current settings of MPD server. Use the mpd modules to alter the
169 =item L<POE::Component::Client::MPD>
176 Jerome Quelin, C<< <jquelin at cpan.org> >>
179 =head1 COPYRIGHT & LICENSE
181 Copyright (c) 2007 Jerome Quelin, all rights reserved.
183 This program is free software; you can redistribute it and/or modify
184 it under the same terms as Perl itself.