1. protect against PC41s with field[3] == field[2]
[spider.git] / perl / Julian.pm
index 861f84d441aef14e79d9727b0956bb659e79b99b..2307e08a9d651a6b6a238b90d8af0f8945aabc1e 100644 (file)
 # $Id$
 #
 
+use strict;
+
 package Julian;
 
-use strict;
+sub alloc($$$)
+{
+       my ($pkg, $year, $thing) = @_;
+       return bless [$year, $thing], ref($pkg)||$pkg;
+}
+
+sub copy
+{
+       my $old = shift;
+       return $old->alloc(@$old);
+}
+
+sub cmp($$)
+{
+       my ($a, $b) = @_;
+       return $a->[1] - $b->[1] if ($a->[0] == $b->[0]);
+       return $a->[0] - $b->[0];
+}
+
+sub year
+{
+       return $_[0]->[0];
+}
+
+sub thing
+{
+       return $_[0]->[1];
+}
+
+package Julian::Day;
+
+use vars qw(@ISA);
+@ISA = qw(Julian);
 
 my @days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
 
-# take a unix date and transform it into a julian day (ie (1998, 13) = 13th day of 1998)
-sub unixtoj
+# is it a leap year?
+sub _isleap
 {
-       my $t = shift;
-       my ($year, $day) = (gmtime($t))[5,7];
-       
-       $year += 1900;
-       return ($year, $day+1);
+       my $year = shift;
+       return ($year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0)) ? 1 : 0; 
 }
 
-# take a unix and return a julian month from it
-sub unixtojm
+sub new($$)
 {
+       my $pkg = shift;
        my $t = shift;
-       my ($mon, $year) = (gmtime($t))[4..5];
-
+       my ($year, $day) = (gmtime($t))[5,7];
        $year += 1900;
-       return ($year, $mon + 1);
+       return $pkg->SUPER::alloc($year, $day+1);
 }
 
 # take a julian date and subtract a number of days from it, returning the julian date
-sub sub
+sub sub($$)
 {
-       my ($year, $day, $amount) = @_;
-       my $diny = isleap($year) ? 366 : 365;
-       $day -= $amount;
-       while ($day <= 0) {
-               $day += $diny;
-               $year -= 1;
-               $diny = isleap($year) ? 366 : 365;
+       my ($old, $amount) = @_;
+       my $self = $old->copy;
+       my $diny = _isleap($self->[0]) ? 366 : 365;
+       $self->[1] -= $amount;
+       while ($self->[1] <= 0) {
+               $self->[1] += $diny;
+               $self->[0] -= 1;
+               $diny = _isleap($self->[0]) ? 366 : 365;
        }
-       return ($year, $day);
+       return $self;
 }
 
-sub add
+sub add($$)
 {
-       my ($year, $day, $amount) = @_;
-       my $diny = isleap($year) ? 366 : 365;
-       $day += $amount;
-       while ($day > $diny) {
-               $day -= $diny;
-               $year += 1;
-               $diny = isleap($year) ? 366 : 365;
+       my ($old, $amount) = @_;
+       my $self = $old->copy;
+       my $diny = _isleap($self->[0]) ? 366 : 365;
+       $self->[1] += $amount;
+       while ($self->[1] > $diny) {
+               $self->[1] -= $diny;
+               $self->[0] += 1;
+               $diny = _isleap($self->[0]) ? 366 : 365;
        }
-       return ($year, $day);
+       return $self;
 } 
 
-# take a julian month and subtract a number of months from it, returning the julian month
-sub subm
+package Julian::Month;
+
+use vars qw(@ISA);
+@ISA = qw(Julian);
+
+sub new($$)
 {
-       my ($year, $mon, $amount) = @_;
-       $mon -= $amount;
-       while ($mon <= 0) {
-               $mon += 12;
-               $year -= 1;
-       }
-       return ($year, $mon);
+       my $pkg = shift;
+       my $t = shift;
+       my ($mon, $year) = (gmtime($t))[4,5];
+       $year += 1900;
+       return $pkg->SUPER::alloc($year, $mon+1);
 }
 
-sub addm
+# take a julian month and subtract a number of months from it, returning the julian month
+sub sub($$)
 {
-       my ($year, $mon, $amount) = @_;
-       $mon += $amount;
-       while ($mon > 12) {
-               $mon -= 12;
-               $year += 1;
+       my ($old, $amount) = @_;
+       my $self = $old->copy;
+       
+       $self->[1] -= $amount;
+       while ($self->[1] <= 0) {
+               $self->[1] += 12;
+               $self->[0] -= 1;
        }
-       return ($year, $mon);
-} 
-
-sub cmp
-{
-       my ($y1, $d1, $y2, $d2) = @_;
-       return $d1 - $d2 if ($y1 == $y2);
-       return $y1 - $y2;
+       return $self;
 }
 
-# is it a leap year?
-sub isleap
+sub add($$)
 {
-       my $year = shift;
-       return ($year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0)) ? 1 : 0; 
-}
+       my ($old, $amount) = @_;
+       my $self = $old->copy;
+
+       $self->[1] += $amount;
+       while ($self->[1] > 12) {
+               $self->[1] -= 12;
+               $self->[0] += 1;
+       }
+       return $self;
+} 
 
 
 1;