]>
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 || "-");
45 open(PLAN
, "<$planfile") || die "Unable to read $planfile";
50 if (m
%\d
+/\d+/\d
+\s
%) {
51 my ($date, $start, $duration) = split(/\s+/);
54 my ($dontknow, $msg) = $info =~ m/^(.)\t(.+)$/;
55 print STDERR
"E: $date $start $duration $msg\n" if $debug;
57 my $timestamp = str2time
("$date $start");
58 my $isostartstamp = time2str
("%Y%m%dT%H%M%S", $timestamp);
59 my $icalstartstamp = Date
::ICal-
>new( epoch
=> $timestamp );
61 my @f = split(/:/, $duration);
62 $timestamp += ($f[0]*60*60 + $f[1]*60 +$f[2]);
63 my $isoendstamp = time2str
("%Y%m%dT%H%M%S", $timestamp);
64 my $icalendstamp = Date
::ICal-
>new( epoch
=> $timestamp );
66 print STDERR
"IE: $isostartstamp $isoendstamp\n" if $debug;
69 dtstart
=> $icalstartstamp->ical,
70 dtend
=> $icalendstamp->ical,
80 # :-//test environment//NONSGML plan2icalendar//EN
87 # :KOrganizer-141229514.660
102 sub write_icalendar
{
103 my $filename = shift;
104 open(PLAN
, ">$filename") || die "Unable to write to $filename";
108 PRODID:-//test environment//NONSGML plan2icalender//EN
112 for my $event (@events) {
113 $event->{uid
} = "plan2icalendar-$count" unless $event->{uid
};
114 $event->{dtstamp
} = "20020110T153952" unless $event->{dtstamp
};
115 $event->{'last-modified'} = "20020110T153939" unless $event->{'last-modified'};
116 $event->{sequence
} = $count unless $event->{sequence
};
117 $event->{created
} = "20020110T153939" unless $event->{created
};
120 CREATED:$event->{created}
123 LAST-MODIFIED:$event->{'last-modified'}
124 DTSTAMP:$event->{dtstamp}
125 SUMMARY:$event->{summary}
126 DTSTART:$event->{dtstart}
127 DTEND:$event->{dtend}
133 print PLAN
"END:VCALENDAR\n";
138 # Should probably leave this task to Net::ICal. But it is not
139 # available in debian/sarge, so I make a quick-n-dirty solution
143 my $filename = shift;
144 open (ICALENDAR
, "<$filename") or die "Unable to read from $filename";
147 while (<ICALENDAR
>) {
149 if (m/^BEGIN:VEVENT/) {
151 while (<ICALENDAR
>) {
153 last if (m/END:VEVENT/);
154 $event{description
} = $1 if (m/^DESCRIPTION\s*:\s*(.+)$/);
155 $event{created
} = $1 if (m/^CREATED\s*:\s*(.+)$/);
156 $event{dtend
} = $1 if (m/^DTEND\s*:\s*(.+)$/);
157 $event{dtstamp
} = $1 if (m/^DTSTAMP\s*:\s*(.+)$/);
158 $event{dtstart
} = $1 if (m/^DTSTART\s*:\s*(.+)$/);
159 $event{last-modified
} = $1 if (m/^LAST-MODIFIED\s*:\s*(.+)$/);
160 $event{sequence
} = $1 if (m/^SEQUENCE\s*:\s*(.+)$/);
161 $event{summary
} = $1 if (m/^SUMMARY\s*:\s*(.+)$/);
162 $event{uid
} = $1 if (m/^UID\s*:\s*(.+)$/);
164 push @events, \
%event;
171 # Simple version covering the chars I need.
181 my $filename = shift;
182 open(PLAN
, ">$filename") or die "Unable to write to $filename";
183 for my $event (@events) {
184 # offset = 0 -- assume all timestamps in local time zone
185 my $ical = Date
::ICal-
>new( ical
=> $event->{dtstart
}, offset
=> 0 );
186 my $date = join("/", $ical->month, $ical->day, $ical->year);
187 my $start = join(":", $ical->hour, $ical->min, $ical->sec);
189 my $icalstop = Date
::ICal-
>new( ical
=> $event->{dtend
}, offset
=> 0 );
190 my $icalduration = $icalstop - $ical;
191 my $duration = join(":",
192 $icalduration->hours || "0",
193 $icalduration->minutes || "0",
194 $icalduration->seconds || "0");
197 my $summary = utf8tolocalmap
($event->{summary
});
198 print PLAN
"$date $start $duration 0:0:0 0:0:0 ---------- 0 0\n";
199 print PLAN
"$code\t$summary\n";