#!/usr/bin/perl # $Id: holidays 121 2004-08-17 02:25:26Z steve $ # $Log$ # Revision 1.10 2004/08/17 02:25:26 steve # abbv of last name, for pivacy # # Revision 1.9 2004/05/16 05:24:00 steve # updated class final parsing # # Revision 1.8 2004/03/09 06:18:27 steve # added a public mode which hides data # # Revision 1.7 2003/07/17 13:28:01 steve # added a -d flag to override the default datebook # # Revision 1.6 2003/07/11 15:08:56 steve # added "use strict" # # Revision 1.5 2003/04/02 15:59:40 steve # understand more types of holidays # # Revision 1.4 2003/02/14 22:41:28 steve # added a css class to the image # # Revision 1.3 2003/02/14 21:33:13 steve # added an icon using the img: flag in a palm note # # Revision 1.2 2003/01/15 23:55:55 steve # merged changes from mu # # Revision 1.1 2002/12/13 21:07:55 steve # Initial revision ######################################### use strict; use Palm::PDB; use Palm::Datebook; use Palm::StdAppInfo; use Time::Repeat; use Time::ParseDate; use Getopt::Std; our($opt_h, $opt_s, $opt_H, $opt_d, $opt_p); getopts("pHhsd:"); usage() if $opt_h || @ARGV >1; my $prschool = $opt_s if defined $opt_s; my $time = time; my $tense = 1; my $html = defined $opt_H; my $prc_override = $opt_d; my $public = $opt_p; my $aday = 24 * 60 * 60; my $dayname = "Today"; $dayname = $ARGV[0] if defined $ARGV[0]; my @tobe = ("was", "is", "will be"); my @tobepl = ("were", "are", "will be"); $time = parsedate($dayname, PREFER_FUTURE=> 1); if($ARGV[0] =~/^(\d{3,})$/) { use POSIX qw(strftime); $time = $1; $dayname = strftime("%A %B %d, %Y", localtime($time)); } $tense++ if $time/$aday > time/$aday; $tense-- if $time/$aday < time/$aday; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time); $mon++; #make it real $year += 1900; my $db = "$ENV{'HOME'}/.palm/DatebookDB.pdb"; $db = $ENV{'PALM_DATEBOOK_DATABASE'} if exists $ENV{'PALM_DATEBOOK_DATABASE'}; $db = $prc_override if $prc_override; my $pdb = new Palm::Datebook; $pdb->Load( $db ); my $start = parsedate( "$dayname 0:00"); #$time; my $end = parsedate( "$dayname 23:59"); $start = start_of_day( $time ); $end = end_of_day( $time ); my (@bdays, @hdays ); foreach my $record ( @{$pdb->{records}} ){ pdbToRepeat( $record ); foreach my $repeat (getRepeat( $record, $start, $end )){ my $handled; # handle finals. Format: # Crypto final @ 70-3455 if($record->{description} =~ /(.+)\s+final\s*(?:[\@\-]\s*)?(\b\d\d\-\w{4}\b)?/i){ print "A $1 final $tobe[$tense] held $dayname"; print " in room $2" if defined $2; print ".\n"; $handled = 1; } if(!$handled && $record->{start_hour} == 0xff){ if($record->{description} =~ /^birthday\s*-\s*(.+)\s+(\d+)/i){ push @bdays, $record; # if it's either a single word, ala "halloween" or it ends # in "day", "week" or "flux" }elsif($record->{description} =~ /(?:^\w+$)|(?:(.+day|week|flux)$)/i){ push @hdays, (format_record($record, $html, $public)) unless $handled; #print "$dayname $tobe[$tense] $1.\n"; }else{ print format_record($record, $html, $public)."\n"; } }else{ } } } print bday( $public, @bdays ); print mult_events( "pre", @hdays ); sub usage { die< USAGE } sub bday{ my ($public, @bdays) = @_; my @out; foreach my $bday (@bdays){ if( my( $who, $when ) = $bday->{description} =~ /^birthday\s*-\s*(.+)\s+(\d+)/i){ my $age = $year - $when; my $ord=('th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th','th')[$age%10]; if( $public ){ my( $first, $lastltr, $last ) = $who =~ /(\w+)(?:\s*(\w)(\w+))?/i; $lastltr = " $lastltr."; push @out, "$first$lastltr\'s birthday"; }else{ push @out, "$who\'s $age$ord birthday"; } } } return mult_events("post", @out); } sub mult_events{ my ($format, @events) = @_; my $out = ""; if( @events ){ if( $format eq "pre"){ $out .= $dayname; # if( @events == 1 ){ $out .= " $tobe[$tense] "; # }else{ # $out .= " $tobepl[$tense] "; # } } for( my $i = 0; $i < @events; $i++){ $out .= " and " if $i == (@events - 1) && @events > 1; $out .= ", " if $i < (@events - 1) && $i > 0; $out .= $events[$i]; } if( $format eq "post" ){ if( @events == 1 ){ $out .= " $tobe[$tense] "; }else{ $out .= " $tobepl[$tense] "; } $out .= "\l$dayname"; } $out .= ".\n"; } return $out; } sub start_of_day { my ($time) = @_; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time); return $time - $hour * 60 * 60 - $min * 60 - $sec; } sub end_of_day { my ($time) = @_; return start_of_day($time) + 60 * 60 * 24 - 1; } sub format_record{ my ($record, $html, $public) = @_; my $text = ""; my $url = ""; my $icon = ""; if( $record->{'note'} =~ /^(?:url|href)\:\s*(.+)$/i ){ $url = $1; } if( $record->{'note'} =~ /^(?:img)\:\s*(.+)$/i ){ $icon = $1; } if( $html ){ $text .= imglink(src => $icon, alt=>"") if $icon; if ( $url ){ $text .= imglink(link => $url, text => $record->{description}); }else{ $text .= $record->{description}; } }else{ $text = $record->{description}; } return $text; } sub imglink{ my (%opts) = @_; my $code = ""; my $params=""; $params .= " src=\"$opts{'src'}\"" if exists $opts{'src'}; $params .= " alt=\"$opts{'alt'}\"" if exists $opts{'alt'}; $params .= " $opts{'atts'}" if exists $opts{'atts'} && defined $opts{'atts'}; $code = "" if exists $opts{'src'}; $code .= $opts{'text'} if exists $opts{'text'}; $code = "$code" if exists $opts{'link'}; return $code; }