#!/usr/bin/perl # iCalendar(.ics) 形式の古いイベントと新しいイベントを振り分ける # # clearance.pl file.ics # 標準出力に未来のイベント、標準エラー出力に古いイベントを出力 # # -n new.ics 未来のイベントの出力指定 # -o old.ics 古いイベントの出力指定 # -t YYYYMMDD 振り分け時間 # -m DD 振り分け時間を指定日だけさかのぼる。 use Jcode ; use Time::Local ; require "getopts.pl" ; Getopts( 't:n:o:m:h' ) ; my $time = time() ; if ( $opt_h ) { print << "EOF" ; $0 [option] file.ics 標準出力に未来のイベント、標準エラー出力に古いイベントを出力 option: -n new.ics 未来のイベントの出力指定 -o old.ics 古いイベントの出力指定 -t YYYYMMDD 振り分け時間 -m DD 振り分け時間を指定日だけさかのぼる。 EOF exit( 0 ) ; } if ( $opt_t ne "" ) { if ( $opt_t =~ /^(\d\d\d\d)(\d\d)(\d\d)$/ ) { $time = timelocal( 0 , 0 , 0 , $3 , $2 - 1 , $1 - 1900 ) ; } else { die( "Usage $0 -t YYYYMMDD" ) ; } } # 振り分け時間を指定日だけさかのぼる if ( $opt_m ne "" ) { $time -= $opt_m * 24*3600 ; } my $c_event = 0 ; my $c_todo = 0 ; my $event = "" ; my $fh_new = STDOUT ; my $fh_old = STDERR ; if ( $opt_n ne "" ) { open( NEW , ">$opt_n" ) ; $fh_new = NEW ; $fh_old = STDOUT if ( $opt_o eq "" ) ; } if ( $opt_o ne "" ) { open( OLD , ">$opt_o" ) ; $fh_old = OLD ; $fh_new = STDOUT if ( $opt_n eq "" ) ; } while( <> ) { $line = $_ ; # VEVENT,VTODO 先頭をみつける if ( $line =~ /^BEGIN:(VEVENT|VTODO)\r*$/ ) { $c_event++ if ( $1 eq "VEVENT" ) ; $c_todo++ if ( $1 eq "VTODO" ) ; $event = "" ; } # VEVENT,VTODO 内外の処理 if ( $c_event > 0 || $c_todo > 0 ) { # VEVENT,VTODO 内部なら event 保存 $event .= $line ; } else { # VEVENT,VTODO 以外は両方に出力 print $fh_new $line ; print $fh_old $line ; } # VEVENT,VTODO 末尾の処理 if ( $line =~ /^END:(VEVENT|VTODO)\r*$/ ) { # event の終了時間を抜き出す my $last_str = "" ; if ( $1 eq "VEVENT" ) { $c_event-- ; if ( $event =~ /\nDTEND(;TZID=.*|):([\dTZ]+)\r*\n/ ) { $last_str = $2 ; } } elsif ( $1 eq "VTODO" ) { $c_todo-- ; if ( $event =~ /\nCOMPLETED(;TZID=.*|):([\dTZ]+)\r*\n/ ) { $last_str = $2 ; } } # 終了時間を GMT 経過秒に直す my $last_time ; if ( $last_str eq "" ) { $last_time = $time ; } elsif ( $last_str =~ /^(\d\d\d\d)(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)(Z|)$/ ) { if ( $7 eq "Z" ) { $last_time = timegm( $6 , $5 , $4 , $3 , $2 - 1 , $1 - 1900 ) ; } else { $last_time = timelocal( $6 , $5 , $4 , $3 , $2 - 1 , $1 - 1900 ) ; } } elsif ( $last_str =~ /^(\d\d\d\d)(\d\d)(\d\d)(Z|)$/ ) { if ( $4 eq "Z" ) { $last_time = timegm( 0 , 0 , 0 , $3 , $2 - 1 , $1 - 1900 ) ; } else { $last_time = timelocal( 0 , 0 , 0 , $3 , $2 - 1 , $1 - 1900 ) ; } } # 終了時間の前後で出力先を切り分ける。 if ( $time <= $last_time ) { #print $fh_new "### $last_str\r\n" ; print $fh_new $event ; } else { #print $fh_old "### $last_str\r\n" ; print $fh_old $event ; } } } ### Local Variables: ### ### mode: perl ### ### tab-width: 4 ### ### End: ###