Changeset 300

Show
Ignore:
Timestamp:
04/16/06 19:32:22 (2 years ago)
Author:
ogawa
Message:

Add more robust timezone support.
Add a document.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • cybozu2ical/trunk/cybozu2ical.pl

    • Property svn:keywords set to Id
    r297 r300  
    11#!/usr/bin/perl 
    2 # cybozu2ical.pl: converting CybozuOffice6 calendar into iCal format 
     2# cybozu2ical: Convert Cybozu Office 6 calendar into iCal format 
    33# 
    4 # Hirotaka Ogawa (hirotaka.ogawa at gmail.com) 
     4# $Id$ 
    55 
    66use strict; 
     
    1313use Data::ICal::Entry::TimeZone; 
    1414use Data::ICal::Entry::TimeZone::Standard; 
    15 use Date::ICal; 
    16  
    17 our $VERSION = '0.01'; 
     15use DateTime; 
     16use DateTime::TimeZone; 
     17 
     18our $VERSION = '0.02'; 
    1819 
    1920my $cfg = YAML::LoadFile($ARGV[0] || 'config.yaml'); 
     21my $tz = DateTime::TimeZone->new(name => $cfg->{time_zone} || 'Asia/Tokyo'); 
    2022 
    2123my $ua = LWP::UserAgent->new(); 
     
    4850 
    4951# current timestamp 
    50 my $dtstamp = Date::ICal->new(epoch => time)->ical
     52my $dtstamp = dt2ical(DateTime->from_epoch(epoch => time))
    5153 
    5254while ($#lines != -1) { 
     
    5961    my $epoch = $fields[1]; 
    6062    $epoch =~ s/ts\.//; 
    61     my $created = Date::ICal->new(epoch => $epoch)->ical; 
    62  
    63     my @d = split("/", $fields[3]); 
    64     my @t = split(":", $fields[5]); 
    65     my $date = Date::ICal->new( 
    66         year => $d[0], month => $d[1], day => $d[2], 
    67         hour => $t[0] || 0, min => $t[1] || 0, sec => $t[2] || 0 
    68     ); 
    69     my $dtstart = $date->ical(offset => '+0900'); 
    70     $dtstart =~ s/T000000// if $fields[5] =~ /^:$/; 
    71  
    72     my @d = split("/", $fields[4]); 
    73     my @t = split(":", $fields[6]); 
    74     my $date = Date::ICal->new( 
    75         year => $d[0], month => $d[1], day => $d[2], 
    76         hour => $t[0] || 0, min => $t[1] || 0, sec => $t[2] || 0 
    77     ); 
    78     $date += 'P1D' if $fields[6] =~ /^:$/; # full-day or multiple-days events 
    79     my $dtend = $date->ical(offset => '+0900'); 
    80     $dtend =~ s/T000000// if $fields[6] =~ /^:$/; 
     63    my $created = dt2ical(DateTime->from_epoch(epoch => $epoch)); 
     64 
     65    my $dt1 = cydate2dt($fields[3], $fields[5], $tz); 
     66    my $dtstart = ($fields[5] !~ /^:$/) ? 
     67        dt2ical($dt1) : $dt1->ymd(''); 
     68 
     69    my $dt2 = cydate2dt($fields[4], $fields[6], $tz); 
     70    my $dtend = ($fields[6] !~ /^:$/) ? 
     71        dt2ical($dt2) : $dt2->add(days => 1)->ymd(''); 
    8172 
    8273    my $vevent = Data::ICal::Entry::Event->new(); 
     
    9485 
    9586my $vtimezone = Data::ICal::Entry::TimeZone->new(); 
    96 $vtimezone->add_properties(tzid => 'Asia/Tokyo'); 
    97  
     87$vtimezone->add_properties(tzid => $tz->name); 
     88 
     89# probably we need to support the Daylight Saving Time 
    9890my $standard = Data::ICal::Entry::TimeZone::Standard->new(); 
     91 
     92my $dt = cydate2dt("1970/01/01", "00:00:00", $tz); 
     93my $offset = DateTime::TimeZone::offset_as_string($dt->offset) || '+0900'; 
     94my $tzname = $cfg->{tzname} || $tz->short_name_for_datetime($dt) || 'JST'; 
     95 
    9996$standard->add_properties( 
    100     tzoffsetfrom => '+0900'
    101     tzoffsetto => '+0900'
    102     tzname => 'JST'
    103     dtstart => '19700101T000000' 
     97    tzoffsetfrom => $offset
     98    tzoffsetto => $offset
     99    tzname => $tzname
     100    dtstart => dt2ical($dt) 
    104101); 
    105  
    106102$vtimezone->add_entry($standard); 
     103 
    107104$vcalendar->add_entry($vtimezone); 
    108105 
    109106print $vcalendar->as_string; 
    110107 
     108sub cydate2dt { 
     109    my($date, $time, $tz) = @_; 
     110    my @d = split("/", $date); 
     111    my @t = split(":", $time); 
     112 
     113    my $dt = DateTime->new( 
     114        year => $d[0], month => $d[1], day => $d[2], 
     115        hour => $t[0] || 0, minute => $t[1] || 0, second => $t[2] || 0, 
     116        time_zone => $tz || 'Asia/Tokyo' 
     117    ); 
     118    return $dt; 
     119} 
     120 
     121sub dt2ical { 
     122    my($dt) = @_; 
     123    $dt->ymd('') . 'T' . $dt->hms('') . ($dt->time_zone->is_utc ? 'Z' : ''); 
     124} 
     125 
    1111261; 
     127=head1 NAME 
     128 
     129cybozu2ical - Convert CybozuOffice6 calendar into iCal format 
     130 
     131=head1 SYNOPSIS 
     132 
     133  % cybozu2ical 
     134  % cybozu2ical /path/to/config.yaml 
     135 
     136=head1 DESCRIPTION 
     137 
     138=head1 DESCRIPTION 
     139 
     140C<cybozu2ical> is a command line application that fetches Cybozu 
     141Office 6 calendar items and converts them into a iCal file.  It allows 
     142you to easily integrate the Cybozu Calendar into iCalendar-enabled 
     143Calendar applications, such as Microsoft Outlook, Apple iCal, and of 
     144course, Google Calendar. 
     145 
     146You can run this via crontab, for example, every 1 hour. 
     147 
     148=head1 REQUIREMENT 
     149 
     150This application requires perl 5.8.0 with following Perl modules 
     151installed on your box. 
     152 
     153=over 4 
     154 
     155=item Data::ICal 
     156 
     157=item DateTime 
     158 
     159=item YAML 
     160 
     161=back 
     162 
     163=head1 OPTIONS 
     164 
     165This application has a command-line option as follows: 
     166 
     167=over 4 
     168 
     169=item path/to/config.yaml 
     170 
     171Specified the path to a configuration file. By default, C<config.yaml> 
     172in the current directory. 
     173 
     174=back 
     175 
     176=head1 CONFIGURATION 
     177 
     178The distributions includes a sample configuration file 
     179C<config.yaml.sample>. You can rename it to C<config.yaml> and 
     180configure C<cybozu2ical>. 
     181 
     182=over 4 
     183 
     184=item cybozu_url 
     185 
     186Set the URL of your Cybozu Office 6. 
     187 
     188=item username, password 
     189 
     190Set your username and password for Cybozu Office 6. 
     191 
     192=item time_zone 
     193 
     194Set the timezone of your Cybozu Office 6 (e.g., Asia/Tokyo). 
     195 
     196=item tzname 
     197 
     198Set the short timezone name of your Cybozu Office 6 (e.g., JST). 
     199 
     200=back 
     201 
     202=head1 DEVELOPMENT 
     203 
     204The development version is always available from the following 
     205subversion repository: 
     206 
     207  svn://svn.as-is.net/public/cybozu2ical/trunk 
     208 
     209You can browse the files via SVN::Web from the following: 
     210 
     211  http://svn.as-is.net/svnweb/public/browse/cybozu2ical/trunk/ 
     212 
     213Any comments, suggestions, or patches are welcome. 
     214 
     215=head1 AUTHOR 
     216 
     217Hirotaka Ogawa E<lt>hirotaka.ogawa at gmail.comE<gt> 
     218 
     219This script is free software and licensed under the same terms as Perl 
     220(Artistic/GPL). 
     221 
     222=cut