如何从条目数据库中为iCal日历创建时间范围?

时间:2014-06-04 16:26:34

标签: macos perl calendar metaprogramming icalendar

我有一些Perl脚本,我用来刮取日历事件。我有一个事件数据库,现在我想将它们处理成一个iCal文件。我打开了大学的学术iCal日历并获得了以下内容:

BEGIN:VCALENDAR
CALSCALE:GREGORIAN
X-WR-TIMEZONE;VALUE=TEXT:America/Los_Angeles
PRODID:-//MMISoftware\, //MMISoftware//EN
X-WR-CALNAME;VALUE=TEXT:spring14cal
X-WR-RELCALID;VALUE=TEXT:87FB4C57-1D7B-4B80-B200-AEED845CB3AF
VERSION:2.0
BEGIN:VEVENT
SEQUENCE:1
UID:41D26C6E-965B-4BFB-AEC0-F1614074A4AF
DTSTAMP:20140127T111052Z
DTSTART;VALUE=DATE:20140101
SUMMARY:FAFSA
DESCRIPTION:First day to obtain FAFSA financial aid applications for 201
 4-15 academic year online at http://www.fafsa.ed.gov
DURATION:P1D
END:VEVENT
BEGIN:VEVENT
SEQUENCE:1
UID:DFE257FB-F475-43C2-9AEB-569EC4BA321D
DTSTAMP:20140127T111052Z
DTSTART;VALUE=DATE:20140106
SUMMARY:CA Dream
DESCRIPTION:First day to obtain California Dream Act financial aid appli
 cations for 2014-15 academic year online at https://dream.csac.ca.gov
.
.
.
.

我看到了开始日期,结束日期和时间戳,但我看不到范围......不确定如何使用我现有的数据库制作iCal文件---你知道,元编程 - - 时间范围如下:

http://musformation.com/pics/thg_sync_apple_ical.jpg

1 个答案:

答案 0 :(得分:2)

您可以使用Date::ICal格式化持续时间字符串。

持续时间本身(在您的情况下,我们只能看到第一个事件的持续时间,因为第二个事件的持续时间是您跳过的第一行)是DURATION:P1D,其中P1D是持续时间本身和DURATION是属性名称。

有关IC Duration的更多信息,请访问here

如果要解析ICal个文件,可以使用ICal::Parser

使用Data::ICal可以轻松创建ICal文件。一个例子:

use Data::ICal;
use Data::ICal::Entry::Event;
use strict;
use warnings;

my $calendar = Data::ICal->new;

my $vtodo = Data::ICal::Entry::Event->new;
$vtodo->add_properties(
  summary => "read SO",
  description => "reading cool SO posts",
  dtstart => Date::ICal->new( 
    day => 4, 
    month => 6, 
    year => 2014,
    hour => 8,
    min => 30,
    sec => 00
  )->ical,
  dtend => Date::ICal->new(
    day => 5, 
    month => 6, 
    year => 2014,
    hour => 9,
    min => 50,
    sec => 00
  )->ical,
);
$calendar->add_entry($vtodo);
print $calendar->as_string;