From rbowen@aiclex.com Sat Mar  9 00:12:35 1996
SUB: [comp.lang.perl.misc] unix "cal" in perl
SUM: eichin->eichin@Kitten.gen.ma.us

------- Start of forwarded message -------
From: rbowen@aiclex.com (Richard Bowen)
Subject: unix "cal" in perl
Newsgroups: comp.lang.perl.misc
Date: Mon, 4 Mar 1996 21:36:54 GMT
Organization: Analysts International Corporation
Reply-To: rbowen@aiclex.com
Path: cambridge-news.cygnus.com!news3.near.net!news.ner.bbnplanet.net!news.mathworks.com!newsfeed.internetmci.com!howland.reston.ans.net!newsjunkie.ans.net!newsfeeds.ans.net!lexmark!usenet
Sender: usenet@lexmark.com (News Dude)
Message-ID: <DnrIwr.Ex1@lexmark.com>
Nntp-Posting-Host: npa.pfv.prtdev.lexmark.com
X-Newsreader: Forte Free Agent 1.0.82
X-Disclaimer: These views are the poster's and not necessarily those of Lexmark
Lines: 105

I asked several weeks ago if anyone had a perl version of the unix
command "cal"
Well, here it is.

The reason that I am sticking things into an array then printing the
array is that I really did not want it to print, I just wanted the
data for a different reason - see
http://www.aiclex.com/webmaster/calendar/hypercal.cgi
Anyways, enjoy.  I expect that there are better and/or easier ways to
do some of these things, but since no one was able to tell me that
they had already done it, I don't care.

Rich
rbowen@aiclex.com
webmaster@aiclex.com
http://www.aiclex.com/
http://www.aiclex.com/webmaster/calendar/
************************
***********************
cal.pl
*******************

#!/usr/bin/perl
#
#       Replacement for `cal`

@months=("December","January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December");
@last_days=(31,31,28,31,30,31,30,31,31,30,31,30,31);
@days_of_week=(" Sun","Mon","Tue","Wed","Thu","Fri","Sat");
@month_offset=(3,3,0,3,2,3,2,3,3,2,3,2,3);

sub GetDate     {
$date=localtime(time);
($day_of_week, $month, $day, $time, $year) = split(/\s+/,$date);
if ($month =~ /jan/i) {$month=1}
elsif ($month =~ /feb/i) {$month=2}
elsif ($month =~ /mar/i) {$month=3}
elsif ($month =~ /apr/i) {$month=4}
elsif ($month =~ /may/i) {$month=5}
elsif ($month =~ /jun/i) {$month=6}
elsif ($month =~ /jul/i) {$month=7}
elsif ($month =~ /aug/i) {$month=8}
elsif ($month =~ /sep/i) {$month=9}
elsif ($month =~ /oct/i) {$month=10}
elsif ($month =~ /nov/i) {$month=11}
elsif ($month =~ /dec/i) {$month=12}
                }       # End sub GetDate

################
# Main Program
################

$month=@ARGV[0];
$year=@ARGV[1];
if (@ARGV[1] eq "")     {&GetDate};

for ($i=1906; $i<$year; $i++)   {
        $days_offset++;
        if (($i-1)%4==0)
                {$days_offset++};  #  Leap years
        } #  End for
if (($year%4)==0 && ($month>2))
        {$days_offset++};  #  Current year is leap year

for ($j=1; $j<($month); $j++)   {
        $days_offset+=@month_offset[$j]};

$first_day_of_month=($days_offset%7);

push (@output,"          @months[$month] $year \n");
$week_days=join(" ",@days_of_week);
push (@output,"$week_days\n");

$last_day_in_month=@last_days[$month];
if (($month==2)&&($year%4==0)){$last_day_in_month=29};

$date_place=0;

while ($date_place<$last_day_in_month)  {
$this_week="";
for ($j=0; $j<=6; $j++) {
        if
(($first_day_of_month>=0)||($date_place>=$last_day_in_month))
        {$this_week.="    ";
        $first_day_of_month--}
        else    {
        $date_place++;
        $this_week.=sprintf"%04.00d",$date_place};
}  # end for
        $this_week.="\n";
        push (@output, $this_week);
}       # end while

for $line (@output)     {print $line};



###############################################
Richard Bowen
rbowen@aiclex.com          webmaster@aiclex.com
http://www.aiclex.com/
###############################################

------- End of forwarded message -------


