CGI Day Month Calendar

Day Month Calendar

#!perl

###########################################################################
# configuration
###########################################################################

# display date or month calendar?
# 0 - Date
# 1 - Month
$Cal = 1;

# display in what language?
# 0 - Chinese
# 1 - English
# 2 - German (Friedemann Lindenthal)
# 3 - French (Jean-Francois Rouault)
$Lang = 1;

###########################################################################
# main programme
###########################################################################

use CGI qw(param);
use POSIX qw(ceil);
use Time::Local qw(timelocal);

# get argument, if any
if (defined(param('Cal')))  {$Cal = param('Cal');}
if (defined(param('Lang'))) {$Lang = param('Lang');}

@TimeNow = localtime;
if (defined(param('Year'))) {
  $Year = param('Year')-1900;
} else {
  $Year = $TimeNow[5];
}
if (defined(param('Month'))) {
  $Month = param('Month')-1;
} else {
  $Month = $TimeNow[4];
}
if (defined(param('Date'))) {
  $Date = param('Date');
} else {
  $Date = $TimeNow[3];
}

# define constants
@MonthDays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
if (&isLeap($Year+1900)) {$MonthDays[1]=29;}

@Month0 = ('¤@¤ë', '¤G¤ë', '¤T¤ë', '¥|¤ë', '¤­¤ë', '¤»¤ë', '¤C¤ë',
           '¤K¤ë', '¤E¤ë', '¤Q¤ë', '¤Q¤@¤ë', '¤Q¤G¤ë');
@Month1 = ('January', 'February', 'March', 'April', 'May', 'June', 'July',
           'August', 'September', 'October', 'November', 'December');
@Month2 = ('Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli',
           'August', 'September', 'Oktober', 'November', 'Dezember');
@Month3 = ('Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet',
           'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre');
@Day0   = ('¬P´Á¤é', '¬P´Á¤@', '¬P´Á¤G', '¬P´Á¤T', '¬P´Á¥|', '¬P´Á¤­', '¬P´Á¤»');
@Day1   = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
@Day2   = ('Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag');
@Day3   = ('Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi');
$Week00 = '²Ä';
$Week01 = '¶g';
$Week1  = 'Week';
$Week2  = 'Woche';
$Week3  = 'Semaine';

$Space = ' ';

# table cell space packing for Netscape browser only
if ( ($ENV{'HTTP_USER_AGENT'} =~ /Mozilla/i) &&
     ($ENV{'HTTP_USER_AGENT'} !~ /MSIE/i) ) {
  $Pack = '<FONT STYLE="font-size:1pt">&nbsp;</FONT>';
} else {
  $Pack = '';
}

# get time
@aTime = localtime(timelocal(0,0,0,$Date,$Month,$Year));

# construct calendar
if ($Cal==0) {
  $out = &calDate;
} elsif ($Cal==1) {
  $out = &calMonth;
}

# show output
print "Content-type: text/html\n\n";
print $out;

exit(0);

###########################################################################
# sub-routines
###########################################################################

sub calDate {
  my($Head1Col, $Head2Col, $ForeCol, $BackCol, $ShadowCol, $FontSize);
  my($Year, $Month, $Date, $Day, $Week);
  my($DateCal);

  # define colours & constants
  $Head1Col = 'orange';                # Background colour for header
  $Head2Col = 'peru';                  # Shadow colour for header
  if ($aTime[6]==0) {                  # Text colour
    $ForeCol = 'red';
  } else {
    $ForeCol = 'green';
  }
  $BackCol   = 'lightyellow';          # Background colour
  $ShadowCol = 'silver';               # Shadow colour

  # construct time components
  if ($Lang==0) {                      # Chinese
    $Year  = $aTime[5]+1900;
    $Month = $Month0[$aTime[4]];
    $Date  = $aTime[3];
    $Day   = $Day0[$aTime[6]];
    if ($aTime[7]<=$aTime[6]) {
      $Week = $Week00.' 0 '.$Week01;
    } else {
      $Week = $Week00.' '.ceil(($aTime[7]-$aTime[6])/7).' '.$Week01;
    }

    $FontSize = '-2';
  } elsif ($Lang==1) {                 # English
    $Year  = $aTime[5]+1900;
    $Month = substr($Month1[$aTime[4]],0,3);
    $Date  = $aTime[3];
    $Day   = $Day1[$aTime[6]];
    if ($aTime[7]<=$aTime[6]) {
      $Week = $Week1.' 0';
    } else {
      $Week = $Week1.' '.ceil(($aTime[7]-$aTime[6])/7);
    }

    $FontSize = '+0';
  } elsif ($Lang==2) {                 # German
    $Year  = $aTime[5]+1900;
    $Month = substr($Month2[$aTime[4]],0,3);
    $Date  = $aTime[3];
    $Day   = $Day2[$aTime[6]];
    if ($aTime[7]<=$aTime[6]) {
      $Week = $Week2.' 0';
    } else {
      $Week = $Week2.' '.ceil(($aTime[7]-$aTime[6])/7);
    }

    $FontSize = '+0';
  } elsif ($Lang==3) {                 # French
    $Year  = $aTime[5]+1900;
    $Month = substr($Month3[$aTime[4]],0,3);
    $Date  = $aTime[3];
    $Day   = $Day3[$aTime[6]];
    if ($aTime[7]<=$aTime[6]) {
      $Week = $Week3.' 0';
    } else {
      $Week = $Week3.' '.ceil(($aTime[7]-$aTime[6])/7);
    }

    $FontSize = '+0';
  }

  # construct output
  $DateCal = qq~
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD WIDTH="2"  HEIGHT="6" BGCOLOR="$Head1Col">$Pack</TD>
<TD WIDTH="36" BGCOLOR="$Head1Col">$Pack</TD>
<TD WIDTH="36" BGCOLOR="$Head1Col">$Pack</TD>
<TD WIDTH="2"  BGCOLOR="$Head1Col">$Pack</TD>
<TD WIDTH="3"  BGCOLOR="$Head2Col">$Pack</TD>
</TR>
<TR>
<TD HEIGHT="18" BGCOLOR="$BackCol">$Pack</TD>
<TD BGCOLOR="$BackCol" ALIGN="LEFT"><FONT FACE="Arial" COLOR="$ForeCol">$Year</FONT></TD>
<TD BGCOLOR="$BackCol" ALIGN="RIGHT"><FONT FACE="Arial" SIZE="$FontSize" COLOR="$ForeCol">$Month</FONT></TD>
<TD BGCOLOR="$BackCol">$Pack</TD>
<TD BGCOLOR="$ShadowCol">$Pack</TD>
</TR>
<TR>
<TD HEIGHT="42" BGCOLOR="$BackCol">$Pack</TD>
<TH BGCOLOR="$BackCol" ALIGN="CENTER" COLSPAN="2"><FONT FACE="Arial" SIZE="+3" COLOR="$ForeCol">$Date</FONT></TH>
<TD BGCOLOR="$BackCol">$Pack</TD>
<TD BGCOLOR="$ShadowCol">$Pack</TD>
</TR>
<TR>
<TD HEIGHT="12" BGCOLOR="$BackCol">$Pack</TD>
<TD BGCOLOR="$ForeCol" ALIGN="CENTER" COLSPAN="2"><FONT FACE="Arial" SIZE="-2" COLOR="$BackCol">$Day</FONT></TD>
<TD BGCOLOR="$BackCol">$Pack</TD>
<TD BGCOLOR="$ShadowCol">$Pack</TD>
</TR>
<TR>
<TD HEIGHT="18" BGCOLOR="$BackCol">$Pack</TD>
<TD BGCOLOR="$BackCol" ALIGN="CENTER" COLSPAN="2"><FONT FACE="Arial" SIZE="-2" COLOR="$ForeCol">$Week</FONT></TD>
<TD BGCOLOR="$BackCol">$Pack</TD>
<TD BGCOLOR="$ShadowCol">$Pack</TD>
</TR>
<TR>
<TD HEIGHT="3" BGCOLOR="$ShadowCol" COLSPAN="5">$Pack</TD>
</TR>
</TABLE>
~;

  return($DateCal);
}

sub calMonth {
  my($SunCol, $DayCol, $WeekBGCol, $NumBGCol, $TodayCol, $BorderCol, $Width);
  my($Year, $Month, $D0, $D1, $D2, $D3, $D4, $D5, $D6);
  my($wday, $i, $j);
  my($MonthCal);

  # define colours & constants
  $SunCol    = 'red';                  # Text colour for sundays
  $DayCol    = 'black';                # Text colour for others
  $WeekBGCol = 'lightblue';            # Background colour for week header
  $NumBGCol  = 'lightcyan';            # Background colour for others
  $TodayCol  = 'lightyellow';          # Background highlight colour for today
  $BorderCol = 'skyblue';              # Border colour

  $Width = 30;

  # construct time components
  if ($Lang==0) {                      # Chinese
    $Year  = $aTime[5]+1900;
    $Month = $Month0[$aTime[4]];

    $D0 = substr($Day0[0],4,2); $D1 = substr($Day0[1],4,2);
    $D2 = substr($Day0[2],4,2); $D3 = substr($Day0[3],4,2);
    $D4 = substr($Day0[4],4,2); $D5 = substr($Day0[5],4,2);
    $D6 = substr($Day0[6],4,2);
  } elsif ($Lang==1) {                 # English
    $Year  = $aTime[5]+1900;
    $Month = $Month1[$aTime[4]];

    $D0 = substr($Day1[0],0,3); $D1 = substr($Day1[1],0,3);
    $D2 = substr($Day1[2],0,3); $D3 = substr($Day1[3],0,3);
    $D4 = substr($Day1[4],0,3); $D5 = substr($Day1[5],0,3);
    $D6 = substr($Day1[6],0,3);
  } elsif ($Lang==2) {                 # German
    $Year  = $aTime[5]+1900;
    $Month = $Month2[$aTime[4]];

    $D0 = substr($Day2[0],0,2); $D1 = substr($Day2[1],0,2);
    $D2 = substr($Day2[2],0,2); $D3 = substr($Day2[3],0,2);
    $D4 = substr($Day2[4],0,2); $D5 = substr($Day2[5],0,2);
    $D6 = substr($Day2[6],0,2);
  } elsif ($Lang==3) {                 # French
    $Year  = $aTime[5]+1900;
    $Month = $Month3[$aTime[4]];

    $D0 = substr($Day3[0],0,2); $D1 = substr($Day3[1],0,2);
    $D2 = substr($Day3[2],0,2); $D3 = substr($Day3[3],0,2);
    $D4 = substr($Day3[4],0,2); $D5 = substr($Day3[5],0,2);
    $D6 = substr($Day3[6],0,2);
  }

  # get weekday of day1
  $wday = $aTime[6]-($aTime[3]-1)%7;
  if ($wday<0) {$wday+=7;}

  # construct table header
  $MonthCal = qq~
<TABLE BGCOLOR="$NumBGCol" BORDER="0" CELLPADDING="1" CELLSPACING="0">
<TR BGCOLOR="$BorderCol">
<TH COLSPAN="9">$Pack</TH>
</TR>
<TR>
<TH BGCOLOR="$BorderCol">$Pack</TH>
<TH COLSPAN="7"><FONT COLOR="$DayCol">$Year$Space$Space$Space$Month</FONT></TH>
<TH BGCOLOR="$BorderCol">$Pack</TH>
</TR>
<TR BGCOLOR="$WeekBGCol">
<TH BGCOLOR="$BorderCol">$Pack</TH>
<TH WIDTH="$Width"><FONT COLOR="$SunCol">$D0</FONT></TH>
<TH WIDTH="$Width"><FONT COLOR="$DayCol">$D1</FONT></TH>
<TH WIDTH="$Width"><FONT COLOR="$DayCol">$D2</FONT></TH>
<TH WIDTH="$Width"><FONT COLOR="$DayCol">$D3</FONT></TH>
<TH WIDTH="$Width"><FONT COLOR="$DayCol">$D4</FONT></TH>
<TH WIDTH="$Width"><FONT COLOR="$DayCol">$D5</FONT></TH>
<TH WIDTH="$Width"><FONT COLOR="$DayCol">$D6</FONT></TH>
<TH BGCOLOR="$BorderCol">$Pack</TH>
</TR>
~;

  # construct pre-day1 table cells
  for ($i=0; $i<=($wday-1); $i++) {
    if ($i==0) {
      $MonthCal .= "<TR>\n";
      $MonthCal .= "<TH BGCOLOR=\"$BorderCol\">$Pack</TH>\n";
    }
    $MonthCal .= "<TH>$Space</TH>\n";
  }

  # construct day1->dayLast table cells
  for ($i=1; $i<=$MonthDays[$aTime[4]]; $i++) {
    if ($i==$aTime[3]) {
      $j=" BGCOLOR=\"$TodayCol\"";
    } else {
      $j='';
    }

    if ($wday==0) {
      $MonthCal .= "<TR>\n";
      $MonthCal .= "<TH BGCOLOR=\"$BorderCol\">$Pack</TH>\n";
      $MonthCal .= "<TH$j><FONT COLOR=\"$SunCol\">$i</FONT></TH>\n";
    } elsif ($wday==6) {
      $MonthCal .= "<TH$j><FONT COLOR=\"$DayCol\">$i</FONT></TH>\n";
      $MonthCal .= "<TH BGCOLOR=\"$BorderCol\">$Pack</TH>\n";
      $MonthCal .= "</TR>\n";
    } else {
      $MonthCal .= "<TH$j><FONT COLOR=\"$DayCol\">$i</FONT></TH>\n";
    }

    $wday++;
    if ($wday==7) {$wday=0;}
  }

  # construct post-dayLast table cells
  if ($wday!=0) {
    for ($i=$wday; $i<=6; $i++) {
      $MonthCal .= "<TH>$Space</TH>\n";
      if ($i==6) {
        $MonthCal .= "<TH BGCOLOR=\"$BorderCol\">$Pack</TH>\n";
        $MonthCal .= "</TR>\n";
      }
    }
  }

  # construct table footer
  $MonthCal .= qq~
<TR BGCOLOR="$BorderCol">
<TH COLSPAN="9">$Pack</TH>
</TR>
</TABLE>
~;

  return($MonthCal);
}

sub isLeap {
  my($y)=shift;
  if (($y%4==0)&&($y%100!=0)||($y%400==0)) {
    return(1);
  } else {
    return(0);
  }
}