"Fossies" - the Fresh Open Source Software Archive

Member "absence-v2.1/cgi-bin/old/AbsenceHolidays.pm" (21 Aug 2008, 2364 Bytes) of package /linux/www/web-absence-2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbsenceHolidays.pm" see the Fossies "Dox" file reference documentation.

    1 package AbsenceHolidays;
    2 
    3 use FileHandle;
    4 
    5 use AbsenceConfig;
    6 
    7 my $HOLIDAY_DB_FILE = AbsenceConfig::fetch('holiday_db_file');
    8 my $INIT        = 0;
    9 my $DEBUG       = 0;
   10 my $VERSION = '1.2';
   11 
   12 sub readHolidays
   13 {
   14     my ($day, $month, $year, $desc);
   15 
   16     my $fh = FileHandle->new($HOLIDAY_DB_FILE);
   17     defined($fh) || die "open [$HOLIDAY_DB_FILE] for reading";
   18     while(<$fh>) {
   19         chomp;
   20         ($DEBUG > 1) && print "HOLIDAY> $_\n";
   21         if (/^\s*$/) { next; }
   22         if (/^\s*#$/) { next; }
   23         if (/^(\d+)\.(\d+).(\d+|\*)\t(\S+(\s+\S+)*)\s*$/) {
   24             ($day, $month, $year, $desc) = ($1, $2, $3, $4);
   25             $DEBUG && print "match: d=[$day], m=[$month], y=[$year], d=[$desc]\n";
   26             push(@{$DB{$month}->{$year}}, [$day, $month, $year, $desc]);
   27         }
   28     }
   29     $fh->close;
   30 
   31     $INIT = 1;
   32 }
   33 
   34 #-------------------------------------------------------------------
   35 # getList()
   36 #
   37 # given a month and year, returns a *SORTED* list of holidays that
   38 # fall within that month.  List is sorted ascending according to day
   39 #-------------------------------------------------------------------
   40 sub getList
   41 {
   42     my ($month, $year) = @_;
   43 
   44     if (!$INIT) { readHolidays(); }
   45 
   46     my @out;
   47 
   48     if (!exists($DB{$month})) { return (); }
   49 
   50     if (exists($DB{$month}->{$year})) {
   51         foreach my $ref (@{$DB{$month}->{$year}}) {
   52             push(@out, $ref);
   53         }
   54     }
   55     if (exists($DB{$month}->{'*'})) {
   56         foreach my $ref (@{$DB{$month}->{'*'}}) {
   57             push(@out, $ref);
   58         }
   59     }
   60 
   61     return sort {$a->[0] <=> $b->[0]} @out;
   62 }
   63 
   64 1;
   65 
   66 __END__
   67 
   68 =head1 NAME
   69 
   70 AbsenceHolidays.pm - primitive interface to a holiday database
   71 
   72 =head1 SYNOPSIS
   73 
   74   use AbsenceHolidays;
   75 
   76   @holiday_list = AbsenceHolidays::getList($month, $year);
   77 
   78 =head1 DESCRIPTION
   79 
   80 B<AbsenceHolidays> provides a simple interface to a text file
   81 containing dates and descriptions of holidays.  The format of the
   82 file is:
   83 
   84   day.month.year description
   85 
   86 for example:
   87 
   88   31.3.2002   Easter
   89 
   90 if year equals '*' (asterisk), the holiday occurs on the same day each
   91 year, otherwise a line for each year desired must appear in the file.
   92 Blank lines and lines beginning with hash ('#') are ignored.
   93 
   94 =head1 FUNCTIONS
   95 
   96 =over 4
   97 
   98 =item B<getList>
   99 
  100 returns a list of all holidays present in a particular
  101 month for a particular year.  The list returned consists of references
  102 to a data-stucture:
  103 
  104   [ $day, $month, $year, $description ]
  105 
  106 simple, eh?
  107 
  108 =head1 AUTHOR
  109 
  110 Robert Urban <urban@tru64.org>
  111 
  112 Copyright (C) Robert Urban 2003
  113 
  114 =cut