File Coverage

blib/lib/Date/Remind/Event.pm
Criterion Covered Total %
statement 44 45 97.7
branch 8 8 100.0
condition 2 6 33.3
subroutine 12 13 92.3
pod 6 6 100.0
total 72 78 92.3


line stmt bran cond sub pod time code
1             package Date::Remind::Event;
2 1     1   722 use strict;
  1         1  
  1         25  
3 1     1   3 use warnings;
  1         1  
  1         30  
4 1     1   15 use Carp qw/croak/;
  1         1  
  1         56  
5 1     1   843 use DateTime;
  1         308782  
  1         41  
6 1     1   9 use DateTime::Duration;
  1         2  
  1         22  
7 1     1   3 use POSIX qw/floor/;
  1         1  
  1         9  
8 1     1   67 use constant { MINUTES_PER_HOUR => 60, };
  1         1  
  1         335  
9              
10             our $VERSION = '0.06';
11             our $ERROR = '';
12             our $BFLAG = 0;
13              
14             sub new {
15 4     4 1 3263 my $proto = shift;
16 4   33     11 my $text = shift || croak 'usage: new($text)';
17              
18 4         17 my ( $date, $special, $tag, $duration, $time, $body ) =
19             split( / /, $text, 6 );
20              
21 4         11 my ( $y, $mon, $d ) = split( /\//, $date );
22              
23 4 100       42 my $dt = DateTime->new(
    100          
24             year => $y,
25             month => $mon,
26             day => $d,
27             hour => $time eq '*' ? 0 : floor( $time / MINUTES_PER_HOUR ),
28             minute => $time eq '*' ? 0 : $time % MINUTES_PER_HOUR,
29             );
30              
31 4         876 my $dtduration;
32              
33 4 100       8 if ( $duration eq '*' ) {
34 1         3 my $end = $dt->clone;
35 1         11 $end->add( days => 1 );
36 1         767 $end->truncate( to => 'day' );
37 1         168 $dtduration = $end - $dt;
38             }
39             else {
40 3         12 $dtduration = DateTime::Duration->new( minutes => $duration, );
41              
42             # Depending on what value of -b remind is called with, the body
43             # is prefixed with human-readable duration text. Lets remove
44             # (only) that text if it is there.
45 3 100       196 if ( $BFLAG != 2 ) {
46 2         8 $body =~ s/^.*? //;
47             }
48             }
49              
50 4         210 my $self = {
51             dt => $dt,
52             tag => $tag,
53             body => $body,
54             duration => $dtduration,
55             };
56              
57 4   33     19 my $class = ref($proto) || $proto;
58 4         5 bless( $self, $class );
59 4         10 return $self;
60             }
61              
62 1     1 1 212 sub date { shift->{dt} }
63 0     0 1 0 sub tag { shift->{tag} }
64 3     3 1 65 sub body { shift->{body} }
65 2     2 1 2543 sub duration { shift->{duration} }
66              
67             sub end {
68 2     2 1 1238 my $self = shift;
69 2         10 return $self->{dt}->clone->add_duration( $self->{duration} );
70             }
71              
72             1;
73              
74             __END__
75              
76             =head1 NAME
77              
78             Date::Remind::Event - Manipulate 'remind' output with Perl
79              
80             =head1 SYNOPSIS
81              
82             use Date::Remind::Event;
83             $Date::Remind::Event::BFLAG = 1;
84              
85             my $e = Date::Remind::Event->new(
86             '2010/07/06 * * 60 1080 18:00-19:00 My Event'
87             );
88              
89             print 'Start: '. $e->date->hms ."\n";
90             print 'Duration: '. $e->duration->hours ." hour\n";
91             print 'Description: '. $e->body ."\n";
92              
93             =head1 DESCRIPTION
94              
95             B<Date::Remind::Event> provides a Perl object interface to textual
96             events emitted by the L<remind>(1) calendar program. The expected
97             format of the input is the same as what is produced by "remind -s" (as
98             defined in the L<rem2ps>(1) manpage under "REM2PS INPUT FORMAT").
99              
100             L<remind>(1) produces slightly different output depending on the value
101             of the -b flag. To make sure that Date::Remind::Event handles this
102             correctly you should set $Date::Remind::Event::BFLAG to the same value
103             (default is 0).
104              
105             See the "example/remind-simple" file in the distribution for one
106             possible way of calling L<remind>(1) from within Perl and parsing its
107             output.
108              
109             =head1 CONSTRUCTOR
110              
111             =over 4
112              
113             =item new($text) => Date::Remind::Event
114              
115             Converts $text into a single Date::Remind::Event object.
116              
117             =back
118              
119             =head1 ATTRIBUTES
120              
121             =over 4
122              
123             =item date -> DateTime
124              
125             The start of the event.
126              
127             =item duration -> DateTime::Duration
128              
129             The length of the event.
130              
131             =item end -> DateTime
132              
133             The end of the remind event.
134              
135             =item tag -> string
136              
137             The TAG value of the event.
138              
139             =item body -> string
140              
141             The body of the remind event.
142              
143             =back
144              
145             =head1 SEE ALSO
146              
147             L<DateTime>, L<DateTime::Duration>, L<remind>(1), L<rem2ps>(1),
148             http://www.roaringpenguin.com/products/remind
149              
150             =head1 AUTHOR
151              
152             Mark Lawrence E<lt>nomad@null.netE<gt>
153              
154             =head1 COPYRIGHT AND LICENSE
155              
156             Copyright 2010-2011 Mark Lawrence
157              
158             This program is free software; you can redistribute it and/or modify it
159             under the terms of the GNU General Public License as published by the
160             Free Software Foundation; either version 3 of the License, or (at your
161             option) any later version.
162              
163             =cut
164