2 # various julian date calculations
4 # Copyright (c) - 1998 Dirk Koopman G1TLH
16 my @days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
18 # take a unix date and transform it into a julian day (ie (1998, 13) = 13th day of 1998)
22 my ($day, $mon, $year) = (gmtime($t))[3..5];
25 # set the correct no of days for february
27 $year += ($year < 50) ? 2000 : 1900;
29 $days[1] = isleap($year) ? 29 : 28;
30 for (my $i = 0, $jday = 0; $i < $mon; $i++) {
34 return ($year, $jday);
37 # take a julian date and subtract a number of days from it, returning the julian date
40 my ($year, $day, $amount) = @_;
41 my $diny = isleap($year) ? 366 : 365;
46 $diny = isleap($year) ? 366 : 365;
53 my ($year, $day, $amount) = @_;
54 my $diny = isleap($year) ? 366 : 365;
56 while ($day > $diny) {
59 $diny = isleap($year) ? 366 : 365;
66 my ($y1, $d1, $y2, $d2) = @_;
67 return $d1 - $d2 if ($y1 == $y2);
75 return ($year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0)) ? 1 : 0;
78 # this section deals with files that are julian date based
80 # open a data file with prefix $fn/$year/$day.dat and return an object to it
83 my ($pkg, $fn, $year, $day, $mode) = @_;
85 # if we are writing, check that the directory exists
87 my $dir = "$fn/$year";
88 mkdir($dir, 0777) if ! -e $dir;
91 $self->{fn} = sprintf "$fn/$year/%03d.dat", $day;
92 $mode = 'r' if !$mode;
93 my $fh = new FileHandle $self->{fn}, $mode;
95 $fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
97 $self->{year} = $year;
99 dbg("julian", "opening $self->{fn}\n");
101 return bless $self, $pkg;
104 # close the data file
108 undef $self->{fh}; # close the filehandle
112 sub DESTROY # catch undefs and do what is required further do the tree
115 dbg("julian", "closing $self->{fn}\n");
116 undef $self->{fh} if defined $self->{fh};