[project @ 6332]
[audio-mpd-common.git] / lib / POE / Component / Client / MPD / Time.pm
blobc07e68ac009182bbae0ca0c2ec9f149941e2c492
2 # This file is part of POE::Component::Client::MPD.
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 POE::Component::Client::MPD::Time;
12 use warnings;
13 use strict;
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
21 ] );
23 #our ($VERSION) = '$Rev$' =~ /(\d+)/;
26 #--
27 # Constructor
30 # my $status = POE::Component::Client::MPD::Time->new( $time )
32 # The constructor for the class POE::Component::Client::MPD::Time. $time is
33 # the time value (on the "time" line) of what the output MPD server returns
34 # to the status command.
36 sub new {
37 my ($class, $time) = @_;
38 $time ||= '0:0';
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;
53 # Parse the time left
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;
59 # create object
60 my $self = {
61 # time elapsed in seconds
62 seconds_sofar => $seconds_sofar,
63 seconds_left => $seconds_left,
64 seconds_total => $seconds_total,
66 # cooked values
67 sofar => $sofar,
68 left => $left,
69 total => $total,
70 percent => sprintf("%.1f", $percent), # 1 decimal
72 # details
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,
80 bless $self, $class;
81 return $self;
87 __END__
89 =pod
91 =head1 NAME
93 POE::Component::Client::MPD::Time - class representing time of current song
96 =head1 SYNOPSIS
98 my $time = $status->time;
99 print $time->sofar;
102 =head1 DESCRIPTION
104 C<POCOCM::Status> returns some time information with the C<time()>
105 accessor. This information relates to the elapsed time of the current song,
106 as well as the remaining and total time. This information is encapsulated
107 in a C<POCOCM::Time> object.
109 Note that an C<POCOCM::Time> object does B<not> update itself regularly,
110 and thus should be used immediately.
113 =head1 METHODS
115 =head2 Constructor
117 =over 4
119 =item new( $time )
121 The C<new()> method is the constructor for the C<POCOCM::Time> class.
122 It is called internally during the C<POCOCM::Status> object creation,
123 with the C<time> line of the C<status> command sent to MPD server.
125 Note: one should B<never> ever instantiate an C<POCOCM::Time> object
126 directly - use the C<time()> method of C<POCOCM::Status>.
128 =back
131 =head2 Accessors
133 Once created, one can access to the following members of the object:
135 =over 4
137 =item cooked values:
139 The C<sofar()>, C<left()> and C<total()> methods return the according values
140 under the form C<minutes:seconds>. Note the existence of a C<percent()>
141 method returning a percentage complete. (one decimal)
144 =item values in seconds:
146 The C<seconds_sofar()>, C<seconds_left()> and C<seconds_total()> return the
147 according values in seconds.
150 =item detailled values:
152 If you want to cook your own value, then the following methods can help.
153 C<sofar_secs()> and C<sofar_mins()> return the seconds and minutes elapsed.
154 Same for C<left_secs()> and C<left_mins()> (time remaining), C<total_secs()>
155 and C<total_mins()>. (total song length)
158 =back
161 Please note that those accessors are read-only: changing a value will B<not>
162 change the current state of MPD server. Use C<Audio::MPD> methods to alter
163 the song playing.
166 =head1 SEE ALSO
168 For all related information (bug reporting, mailing-list, pointers to
169 MPD and POE, etc.), refer to C<POE::Component::Client::MPD>'s pod,
170 section C<SEE ALSO>
173 =head1 AUTHOR
175 Jerome Quelin, C<< <jquelin at cpan.org> >>
178 =head1 COPYRIGHT & LICENSE
180 Copyright (c) 2007 Jerome Quelin, all rights reserved.
182 This program is free software; you can redistribute it and/or modify
183 it under the same terms as Perl itself.
185 =cut