]>
pere.pagekite.me Git - homepage.git/blob - linux/plan2icalendar
3 # Author: Petter Reinholdtsen <pere@hungry.com>
6 # Convert plan calender data to/from icalender format.
8 # plan, <URL:http://www.bitrot.de/>
9 # icalendar, RFC 2445, <URL:http://www.faqs.org/rfcs/rfc2445.html">iCalendar>
22 unless (getopts
('if:o:', \
%opts)) {
28 my $output = $opts{o
};
30 if ($opts{i
}) { # Import
31 read_icalendar
($input || "test.ical");
32 write_planfile
($output || $ENV{HOME
}."/.plan.dir/fromical");
33 } else { # Default is to export
34 read_planfile
($input || $ENV{HOME
}."/.plan.dir/dayplan");
35 write_icalendar
($output || "-");
41 Usage: $0 [-i] [-f infile] [-o outfile]
43 Imports or exports between plan and iCalendar.
46 -f infile read input from 'infile'
47 -o outfile read output from 'outfile'
52 open(PLAN
, "<$planfile") || die "Unable to read $planfile";
57 if (m
%\d
+/\d+/\d
+\s
%) {
58 my ($date, $start, $duration) = split(/\s+/);
61 my ($dontknow, $msg) = $info =~ m/^(.)\t(.+)$/;
62 print STDERR
"E: $date $start $duration $msg\n" if $debug;
64 my $timestamp = str2time
("$date $start");
65 my $isostartstamp = time2str
("%Y%m%dT%H%M%S", $timestamp);
66 my $icalstartstamp = Date
::ICal-
>new( epoch
=> $timestamp );
68 my @f = split(/:/, $duration);
69 $timestamp += ($f[0]*60*60 + $f[1]*60 +$f[2]);
70 my $isoendstamp = time2str
("%Y%m%dT%H%M%S", $timestamp);
71 my $icalendstamp = Date
::ICal-
>new( epoch
=> $timestamp );
73 print STDERR
"IE: $isostartstamp $isoendstamp\n" if $debug;
76 dtstart
=> $icalstartstamp->ical,
77 dtend
=> $icalendstamp->ical,
87 # :-//test environment//NONSGML plan2icalendar//EN
94 # :KOrganizer-141229514.660
109 sub write_icalendar
{
110 my $filename = shift;
111 open(PLAN
, ">$filename") || die "Unable to write to $filename";
115 PRODID:-//test environment//NONSGML plan2icalender//EN
119 for my $event (@events) {
120 $event->{uid
} = "plan2icalendar-$count" unless $event->{uid
};
121 $event->{dtstamp
} = "20020110T153952" unless $event->{dtstamp
};
122 $event->{'last-modified'} = "20020110T153939" unless $event->{'last-modified'};
123 $event->{sequence
} = $count unless $event->{sequence
};
124 $event->{created
} = "20020110T153939" unless $event->{created
};
127 CREATED:$event->{created}
130 LAST-MODIFIED:$event->{'last-modified'}
131 DTSTAMP:$event->{dtstamp}
132 SUMMARY:$event->{summary}
133 DTSTART:$event->{dtstart}
134 DTEND:$event->{dtend}
140 print PLAN
"END:VCALENDAR\n";
145 # Should probably leave this task to Net::ICal. But it is not
146 # available in debian/sarge, so I make a quick-n-dirty solution
150 my $filename = shift;
151 open (ICALENDAR
, "<$filename") or die "Unable to read from $filename";
154 while (<ICALENDAR
>) {
156 if (m/^BEGIN:VEVENT/) {
158 while (<ICALENDAR
>) {
160 last if (m/END:VEVENT/);
161 $event{description
} = $1 if (m/^DESCRIPTION\s*:\s*(.+)$/);
162 $event{created
} = $1 if (m/^CREATED\s*:\s*(.+)$/);
163 $event{dtend
} = $1 if (m/^DTEND\s*:\s*(.+)$/);
164 $event{dtstamp
} = $1 if (m/^DTSTAMP\s*:\s*(.+)$/);
165 $event{dtstart
} = $1 if (m/^DTSTART\s*:\s*(.+)$/);
166 $event{last-modified
} = $1 if (m/^LAST-MODIFIED\s*:\s*(.+)$/);
167 $event{sequence
} = $1 if (m/^SEQUENCE\s*:\s*(.+)$/);
168 $event{summary
} = $1 if (m/^SUMMARY\s*:\s*(.+)$/);
169 $event{uid
} = $1 if (m/^UID\s*:\s*(.+)$/);
171 push @events, \
%event;
178 # Simple version covering the chars I need.
188 my $filename = shift;
189 open(PLAN
, ">$filename") or die "Unable to write to $filename";
190 for my $event (@events) {
191 # offset = 0 -- assume all timestamps in local time zone
192 my $ical = Date
::ICal-
>new( ical
=> $event->{dtstart
}, offset
=> 0 );
193 my $date = join("/", $ical->month, $ical->day, $ical->year);
194 my $start = join(":", $ical->hour, $ical->min, $ical->sec);
196 my $icalstop = Date
::ICal-
>new( ical
=> $event->{dtend
}, offset
=> 0 );
197 my $icalduration = $icalstop - $ical;
198 my $duration = join(":",
199 $icalduration->hours || "0",
200 $icalduration->minutes || "0",
201 $icalduration->seconds || "0");
204 my $summary = utf8tolocalmap
($event->{summary
});
205 print PLAN
"$date $start $duration 0:0:0 0:0:0 ---------- 0 0\n";
206 print PLAN
"$code\t$summary\n";