[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Strange behavior of a script



I have been trying to work out the following script, but I've noticed some
odd behaviors.

First, this script is an adaption of the work of another.. not my own
creation. In many places, I am still trying to figure out how it works,
what it does, and why. In any event, I managed to get it to run... and
approximate the unix "cal" program which another of my scripts uses. (I'm
trying to get the script to run with MacPerl instead of unix perl.)

The script that is appended to this message generates this out put on a
unix host:
Start with 2 1997
btime = 854776800
btime = 32f2dbe0=01/01/0097 00:00:00
Time is 32f2dbe0, which is Sat Feb 01,1997 00:00:00  31/365 ST
Day = 6
End with 2/1997
days per February 1997 = 28
mdays 28 wday 6, weeks 4
   February 1997
 | Sun| Mon| Tue| Wed| Thu| Fri| Sat|
 |    |    |    |    |    |    |   1|
 |   2|   3|   4|   5|   6|   7|   8|
 |   9|  10|  11|  12|  13|  14|  15|
 |  16|  17|  18|  19|  20|  21|  22|
 |  23|  24|  25|  26|  27|  28|    |

The unix Cal program generates: 

   February 1997
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28


The cal.pl script generates the same output from PC Perl as unix perl.

However, in order to get this output from MacPerl, I have to change the
$mday variable at one point in the script. (See comment in script below un
the "gettime" subroutine).

My question is: "why?"


Thanks, 

    Robert Sweeney

__Script__
#!/usr/bin/perl
# calendar.pl
#
# make a four year calendar, given a year
# was: make a calendar, given month & year.
#
# fixed Feb. 2000 bug, i.e. fixed isleapyr -- wls 5/21/97.

use Time::Local;

$data = <DATA>; #just another way to get variables.
chop ($data);
   $do = 'this';
   local ($atime) = "$data";
   &print_month($do, $atime);
   

exit(0);

########
# subs #
########


# simple calendar month
sub print_month {
  local ($do,$atime) = @_; # 'this&June 1997'
  local ($dbg) = 1;
  print "Start with $atime\n" if $dbg;
  local ($btime) = &get_time($do,$atime); #convert text to hex time.
  print &print_time($btime) if $dbg;
  local ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($btime);
 printf "Day = %s\n", $wday if $dbg; # fix for 2000 - wls 
                                     # (works till i'm 100!)
  $year = $year + 1900;
  printf "End with %s/%s\n", $mon+1,$year if $dbg;


  local ($see) = 1; # shall we display the month?
  local ($w,$d);
  local ($ready) = 0;
  # figure no. of days for this month
  local ($mdays) = (31,28,31,30,31,30,31,31,30,31,30,31)[$mon];
  local ($monthName) =
(January,February,March,April,May,June,July,August,September,October,Novembe
r,December)[$mon];
  $mdays += &isleapyr($year) if $mon;
  print "days per $monthName $year = $mdays\n" if $dbg;
  local ($wmax) = (($mdays+$wday-0)/7);
  printf "mdays %d wday %d, weeks %d\n", $mdays, $wday, $wmax if $dbg;
  local ($dt);
  printf "   %s %s\n", $monthName, $year if $see;
  print " | Sun| Mon| Tue| Wed| Thu| Fri| Sat|\n" if $see;
  for ($w=0;$w<$wmax;$w++) {
   print " |" if $see;
   for ($d=0;$d<7;$d++) {
     $ready = 1 if ($d == $wday) && !$ready; # are we ready to start
printing days yet?
     if ($ready) {
  $dt = 1+$d+($w*7)-$wday;
       printf "  %2d|",$dt if $see;
     } else {
       $dt = 0;
       print "    |" if $see;
     }
     if (($dt == $mdays) && $ready) {
       $ready = 0; # stop with days already
     $wday = -1; # and don't start again!
     }
   }
   print "\n" if $see;
  }
} # print_month



sub get_time {
  local ($do,$when) = @_;
  local ($dbg) = 1;
  local ($my_btime);
  local ($sec,$min,$hour,$mday,$mon,$year);
  if ($when =~ m/(\w+)\s(\w+)\s(\d+),(\d+)\s(\d+):(\d+):(\d+)/i) {
    local ($sec,$min,$hour,$mday,$mon,$year); 
    $sec = $7;
    $min = $6;
    $hour = $5;
    $year = $4 - 1900;
   $mday = $3;
    $mon = $2;
  } elsif ($when =~ m/(\d+)\s(\d+)/i) {
    $sec = 0; #This pattern matches "mm yyyy" or 1 1997
    $min = 0;
    $hour = 0;
    $year = $2 - 1900; # will this be a problem before I'm dead? wls
    $mday = 1;
     
     #
     #
     #
     #CHANGE HERE ^ if I change $mday = 1 to $mday = 2, the script runs on 
     #MacPerl but not unix Perl or PC Perl.
     #
     #
     #

    $mon = $1 - 1; #so I can reference date as mm yyyy and count 
                   # starting at 1 for January instead of 0.

  } elsif ($when =~ m/(\w+)\s(\d+)/i) {
    $sec = 0; #This pattern matches "January 1997"
    $min = 0;
    $hour = 0;
    $year = $2 - 1900; # will this be a problem before I'm dead? wls
    $mday = 1;
    $mon = $1;
  } else {
    return(time()); # invalid format?? use 'today'
  }
  $mon =~ s/Jan[^ \/]*/0/i;  #handles both Jan 1997 and January 1997
  $mon =~ s/Feb[^ \/]*/1/i;  #instead of changing $mon to 1uary 1997
  $mon =~ s/Mar[^ \/]*/2/i;
  $mon =~ s/Apr[^ \/]*/3/i;
  $mon =~ s/May[^ \/]*/4/i;
  $mon =~ s/Jun[^ \/]*/5/i;
  $mon =~ s/Jul[^ \/]*/6/i;
  $mon =~ s/Aug[^ \/]*/7/i;
  $mon =~ s/Sep[^ \/]*/8/i;
  $mon =~ s/Oct[^ \/]*/9/i;
  $mon =~ s/Nov[^ \/]*/10/i;
  $mon =~ s/Dec[^ \/]*/11/i;
  
  if ($do eq 'next') {
    if ($mon == 11) {
      $mon = 0;
      $year++;
    } else {
      $mon++;
    }
  } elsif ($do eq 'prev') {
    if ($mon == 0) {
      $mon = 11; 
      $year--;
    } else {
      $mon--;
    }
  }
  

  
  # convert to hex
$my_btime = timelocal($sec,$min,$hour,$mday,$mon,$year);
  print "btime = $my_btime\n" if $dbg;
  printf "btime = %08x=%02d/%02d/%04d %02d:%02d:%02d\n",
$my_btime,$mon,$mday,$year,$hour,$min,$sec if $dbg;
  return ($my_btime);
}
# get_time

# display the 'as of time'
sub print_time {
  local ($my_btime) = @_;

  local ($dbg) = 0;
  unless ($my_btime) {
    return("BackUp Time = $NA\n");
  }
  printf ("Time =  %08x\n",$my_btime) if $dbg;
  # start with btime, convert to time array
  local ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($my_btime);
  # beautify the date...
  # fix for 2000 - wls (works till i'm 100!)
  $year += 1900;
# if ($year > 44) {$year += 1900;} else {$year += 2000;}
  local ($theDay) = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[$wday];
  local ($theMonth) = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon];
  local ($dst) = (ST,DST)[$isdst];
  local ($ydays) = &days_per_this_year($year);
  local ($nice) = sprintf "Time is %08x, which is %3s %3s %02d,%04d
%02d:%02d:%02d %3d/%d %s\n",
$my_btime,$theDay,$theMonth,$mday,$year,$hour,$min,$sec,$yday,$ydays,$dst;
 return($nice);
} # print_time




# days per a specified year
sub days_per_this_year {
  local ($y) = @_;
  return (365 + &isleapyr($y));
} # days_per_this_year




# really ugly version of isleapyr
sub isleapyr {
  local ($y) = @_;
  return (1) unless
($y % 400); # very 'ordinary' century
  return (0) unless ($y % 100); # very special century
  return (1) unless ($y % 4); # lord's 'a leaping.
return (0);
} # isleapyr



__END__
2 1997

__End of Script__



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch