Reworked holiday system. Added support for spring break
[tpope-extra.git] / perl / sctweb
index 64c4127a8f4b40ed6cc86dc31770655b298674a2..018077c0793336d20a7304fce624c2927df8da1d 100755 (executable)
@@ -10,13 +10,56 @@ use strict;
 use LWP::UserAgent;
 use HTML::TableExtract;
 #use Time::Local;
-use Date::Calc::Object qw(Day_of_Week Decode_Day_of_Week Week_of_Year Monday_of_Week Day_of_Week_Abbreviation Delta_Days Add_Delta_Days Gmtime Mktime);
+use Date::Calc::Object qw(Day_of_Week Decode_Day_of_Week Week_of_Year Monday_of_Week Day_of_Week_Abbreviation Delta_Days Add_Delta_Days Nth_Weekday_of_Month_Year Gmtime Mktime);
 use Date::Calendar::Profiles qw($Profiles);
 use Date::Calendar::Year;
 use vars qw($ua %opts %faculty);
 
 my ($response);
 
+$opts{'holidays'} = { # %{$Profiles->{'US-TX'}},
+    "Martin Luther King Day"    => "3/Mon/Jan",
+    "Good Friday"               => "-2",
+    "Spring Break Monday"      => \&Spring_Break,
+    "Spring Break Tuesday"     => \&Spring_Break,
+    "Spring Break Wednesday"   => \&Spring_Break,
+    "Spring Break Thursday"    => \&Spring_Break,
+    "Spring Break Friday"      => \&Spring_Break,
+    "Spring Break Saturday"    => \&Spring_Break,
+    "Fiesta Holiday"            => "4/Fri/Apr",
+    "Memorial Day"              => "5/Mon/May",
+    "Independence Day"          => \&US_Independence,
+    "Labor Day"                 => \&US_Labor,
+    "Columbus Day"              => "2/Mon/Oct",
+    "Thanksgiving Day"          => "4/Thu/Nov",
+    "Thanksgiving Friday"       => \&Thanksgiving_Friday,
+};
+
+sub Spring_Break {
+    my($year,$label) = @_;
+    $label =~ s/^Spring Break //;
+    return( Add_Delta_Days(
+           Nth_Weekday_of_Month_Year($year,1,1,1),
+           7*(11-1) # This is for the 11th Monday of the year
+           +Decode_Day_of_Week($label)-1) );
+}
+
+sub Thanksgiving_Friday {
+    my($year,$label) = @_;
+    return( Add_Delta_Days(Nth_Weekday_of_Month_Year($year,11,4,4), 1) );
+}
+sub US_Independence # Fourth of July
+{
+    my($year,$label) = @_;
+    return( &Date::Calendar::Profiles::Nearest_Workday($year,7,4) );
+}
+sub US_Labor # First Monday after the first Sunday in September
+{
+    my($year,$label) = @_;
+    return( Add_Delta_Days(
+        Nth_Weekday_of_Month_Year($year,9,7,1), +1) );
+}
+
 if (-r $ENV{HOME} . "/.sctwebrc") {
     open CONFIG, $ENV{HOME} . "/.sctwebrc";
     while(<CONFIG>) {
@@ -82,7 +125,7 @@ sub off_for_holidays {
     my $days = Delta_Days($1,$2,$3,$4,$5,$6);
     my $firstday = Date::Calc->new($1,$2,$3);
     my $lastday = Date::Calc->new($4,$5,$6);
-    my $year = Date::Calendar::Year->new($1, $Profiles->{'US-TX'});
+    my $year = Date::Calendar::Year->new($1, $opts{'holidays'});
     my @holidays=();
     my @days=();
     foreach(split(" ",$class{days})) {
@@ -90,15 +133,12 @@ sub off_for_holidays {
     }
     foreach ($year->search("")) {
        my $good=1;
-       foreach my $x ($year->labels($_)) {
-           $good=0 if $x =~ /Veteran/;
-           $good=2 if $x =~ /Thanksgiving/;
-       }
+       #foreach my $x ($year->labels($_)) {
+       #    $good=0 if ($x =~ /Veteran/ or $x =~ /President/);
+       #}
        next unless ($year->is_full($_) && $_>=$firstday && $_<=$lastday);
        push @holidays, $_
-           if (defined($days[Day_of_Week($_->date)]) && $good);
-       push @holidays, $_+1
-           if (defined($days[5]) && $good==2);
+           if (defined($days[Day_of_Week($_->date)]) && $good > 0);
     }
     return wantarray ? @holidays : "@holidays";
 }
@@ -135,7 +175,7 @@ sub capitalize {
     s/\b(I)(i*)\b/$1\U$2/g;
     s/\bUs\b/US/g;
     s/ (And|For|Of|Or|The|To|With) / \l$1 /g;
-    s/\b(Ma?c)([a-z])/$1\u$2/g;
+    s/\b(Mc)([a-z])/$1\u$2/g;
     s/\b(Tcp\/Ip|Pc)\b/\U$&/g;
     s/\bThru\b/Through/g;
     s/\bAcct\b/Accounting/g;
@@ -145,6 +185,8 @@ sub capitalize {
     s/\bFed\b/Federal/g;
     s/\bGen\b/General/g;
     s/\bIntro\b/Introduction/g;
+    s/\bPrgm\b/Programming/g;
+    s/\bOp Sys\b/Operating System/g;
     #s/\bGovt\b/Government/g;
     s/\bLit\b/Literature/g;
     s/\bPrin\b/Principles/g;
@@ -399,8 +441,8 @@ sub do_vcalendar_schedule {
        print "DTEND:", $startdate."T".$stoptime, "00\r\n";
        print "RRULE:W1 ", $row->{'days'} . " $stopdate", "T000000\r\n";
        print("EXDATE:$day\r\n") if($day);
-       print "ATTENDEE;ROLE=OWNER;STATUS=CONFIRMED:", $opts{NAME}, "\r\n" if(defined($opts{NAME}));
-       print "ATTENDEE;ROLE=ORGANIZER;STATUS=CONFIRMED:", $row->{'instructor'}, " <" . (get_faculty_email($row->{'instructor'}) || "fake\@ddress"), ">\n";
+       print "ATTENDEE;ROLE=OWNER;STATUS=CONFIRMED:", $opts{'name'}, "\r\n" if(defined($opts{'name'}));
+       print "ATTENDEE;ROLE=ORGANIZER;STATUS=CONFIRMED:", $row->{'instructor'}, " <" . (get_faculty_email($row->{'instructor'}) || "fake\@ddress"), ">\r\n";
        print "END:VEVENT\r\n";
        if(defined($file) && (-d $file)) {
            print "END:VCALENDAR\r\n";
@@ -448,8 +490,11 @@ sub do_html_grades {
     my $te = new HTML::TableExtract( headers => [ @readheaders ] );
     my $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rgrdterm.cfg"); # Valid grading terms
     die $response->status_line unless $response->is_success;
-    my @terms = grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content));
-    foreach(@_ || reverse @terms) {
+    #my @terms = reverse grep {s/^<option value="([^"]*)">.*/$1/} (split( /\r\n/, $response->content));
+    my @terms = ();
+    my $year = Date::Calc->localtime->year;
+    for(my $y=$year-4;$y<=$year;$y++) { push @terms, ($y.2,$y.3,$y.4,$y.1); }
+    foreach(@_ ? @_ : @terms) {
        $response = $ua->get("$url&tserve_tip_write=||WID|SID|PIN|Term&tserve_trans_config=rgrades.cfg&Term=".($_) );
        die $response->status_line unless $response->is_success;
        $te->parse($response->content);