Chinaunix首页 | 论坛 | 博客
  • 博客访问: 453301
  • 博文数量: 142
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 1861
  • 用 户 组: 普通用户
  • 注册时间: 2005-06-03 21:00
文章分类

全部博文(142)

文章存档

2011年(3)

2010年(32)

2009年(107)

我的朋友

分类:

2009-11-10 12:21:21

在测试时需要大量的vcalendar,这时需要自动生成很多vcalendar,下面的程序是用perl写的生产程序
操作系统:Vista or Win7
 
适用于windows calendar
 
先看配置文件:vcal_config.txt
 
C:\Users\Administrator\AppData\Local\Microsoft\Windows Calendar\Calendars\Administrator's Calendar.ics
vcal
2006,2007,2008,2009
 
 
下面是perl程序源代码:genvcalendar.pl
 
 

use strict;
use warnings;

use Win32::Guidgen;

my $guid0;
my $path = GetSetting("path");
my $vcalName = GetSetting("VcalName");
my $temp_years = GetSetting("years");

print "path = $path\n";
print "vcalName = $vcalName\n";
print "temp_years = $temp_years\n";

my @arrYears = split(/,/,$temp_years);

my @startTime;
my @endTime;

my $i = 0;
my $greenTime;
GenVcalHead($path);
foreach my $year (@arrYears)
{
    for($i = 0; $i < 365; $i++)
    {
        $greenTime = GenGreenTime();
        $guid0 = GetGuid();
        @startTime = GenStartTime($year);
        @endTime = GenEndTime($year);
        GenVcalBody($path);
    }
}
GenVcalEnd($path);

sub GenVcalHead {
    my $filename = shift;
    open (FH,">$filename") || die "can not open $filename";
print FH <<EOF;
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Microsoft Corporation//Windows Calendar 1.0//EN
CALSCALE:GREGORIAN
METHOD:PUBLISH
BEGIN:VTIMEZONE
TZID:Beijing\, Chongqing\, Hong Kong\, Urumqi
BEGIN:STANDARD
DTSTART:20000101T000000
TZNAME:China Standard Time
TZOFFSETFROM:+0900
TZOFFSETTO:+0800
END:STANDARD
END:VTIMEZONE
EOF
close FH || die "can not close";
}


sub GenVcalBody {
    my $filename = shift;
    open (FH,">>$filename") || die "can not open $filename";
print FH <<EOF;
BEGIN:VEVENT
DESCRIPTION:aaaaaaaaaaaaa\\n\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\\n\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@
 \@\@\@\@\@\@\@\\n\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\\n\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

 \$\$\$\$\$\$\$\$\$\$\\n\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\\n\&\&\&\&\&
 \&\&\&\&\&\&\&\&\&\&\&\&\&\&\&\&\&\&\&
DTSTAMP:$greenTime
DTSTART;TZID="Beijing, Chongqing, Hong Kong, Urumqi":$startTime[$i]
DTEND;TZID="Beijing, Chongqing, Hong Kong, Urumqi":$endTime[$i]
LOCATION:aaaaaaaaaaaaa\\n\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\~\\n\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@
 \@\@\@\@\@\@\@\\n\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\\n\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$

 \$\$\$\$\$\$\$\$\$\$\\n\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\\n\&\&\&\&\&
 \&\&\&\&\&\&\&\&\&\&\&\&\&\&\&\&\&\&\&
SUMMARY:$vcalName$i
UID:$guid0
END:VEVENT
EOF
close FH || die "can not close";
}

sub GenVcalEnd {
    my $filename = shift;
    open (FH,">>$filename") || die "can not open $filename";

print FH <<EOF;
END:VCALENDAR

EOF

close FH || die "can not close";
}

sub GetGuid
{
    my $tmpGuid = Win32::Guidgen::create();
    if ($tmpGuid =~ /{(.*)}/)
    {
        $tmpGuid = $1;
    }
    return $tmpGuid;
}


sub GenCurrentTime
{
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime;
    $year = $year + 1900;
    $mon = $mon + 1;
    my $currentTime = sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ",$year,$mon,$mday,$hour,$min,$sec);
    return $currentTime;
}


sub GenGreenTime
{
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime;
    $year = $year + 1900;
    $mon = $mon + 1;
    my $greenTime = sprintf("%04d%02d%02dT%02d:%02d:%02d",$year,$mon,$mday,$hour,$min,$sec);
    return $greenTime;
}

sub GenStartTime
{
    my $year = shift;
    my @startTime;
    my @monDay = (31,28,31,30,31,30,31,31,30,31,30,31);
    my $day = 0;
    for (my $j = 0; $j < 12; $j++)
    {
        for (my $i = 0; $i < $monDay[$j]; $i++)
        {
            $startTime[$day++] = sprintf("%04d%02d%02dT010000",$year,$j+1,$i+1);
        }
    }
    return @startTime;
}

sub GenEndTime
{
    my $year = shift;
    my @endTime;
    my @monDay = (31,28,31,30,31,30,31,31,30,31,30,31);
    my $day = 0;
    for (my $j = 0; $j < 12; $j++)
    {
        for (my $i = 0; $i < $monDay[$j]; $i++)
        {
            $endTime[$day++] = sprintf("%04d%02d%02dT100000",$year,$j+1,$i+1);
        }
    }
    return @endTime;
}

sub GetSetting
{
    my $Item = shift;
    
    open IN, "vcal_config.txt";
    my @contents = <IN>;
    close IN;
    
    my $path;
    my $vcalname;
    my $myyear;
    foreach my $line (@contents)
    {
        #print "line $line \n";

        if ($line =~ /<VcalName>(.*)/i)
        {
            $vcalname = $1;
        }
        elsif ($line =~ /<Years>(.*)/i)
        {
            $myyear = $1;
        }
        elsif ($line =~ /<Path>(.*)/i)
        {
            $path = $1;
        }
    }
    
    if ($Item =~ /VcalName/i)
    {
        return $vcalname;
    }
    elsif ($Item =~ /years/i)
    {
        return $myyear;
    }
    elsif ($Item =~ /path/i)
    {
        return $path;
    }
    else
    {
        return 0;
    }
}



在command中执行 就可以再window vcaledar 中生长很多的日历

 

阅读(850) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~