2 # various julian date calculations
4 # Copyright (c) - 1998 Dirk Koopman G1TLH
17 my @days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
19 # take a unix date and transform it into a julian day (ie (1998, 13) = 13th day of 1998)
23 my ($day, $mon, $year) = (gmtime($t))[3..5];
26 # set the correct no of days for february
28 $year += ($year < 50) ? 2000 : 1900;
30 $days[1] = isleap($year) ? 29 : 28;
31 for (my $i = 0, $jday = 0; $i < $mon; $i++) {
35 return ($year, $jday);
38 # take a julian date and subtract a number of days from it, returning the julian date
41 my ($year, $day, $amount) = @_;
42 my $diny = isleap($year) ? 366 : 365;
47 $diny = isleap($year) ? 366 : 365;
54 my ($year, $day, $amount) = @_;
55 my $diny = isleap($year) ? 366 : 365;
57 while ($day > $diny) {
60 $diny = isleap($year) ? 366 : 365;
67 my ($y1, $d1, $y2, $d2) = @_;
68 return $d1 - $d2 if ($y1 == $y2);
76 return ($year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0)) ? 1 : 0;
79 # this section deals with files that are julian date based
81 # open a data file with prefix $fn/$year/$day.dat and return an object to it
84 my ($pkg, $fn, $year, $day, $mode) = @_;
86 # if we are writing, check that the directory exists
88 my $dir = "$fn/$year";
89 mkdir($dir, 0777) if ! -e $dir;
92 $self->{fn} = sprintf "$fn/$year/%03d.dat", $day;
93 $mode = 'r' if !$mode;
94 my $fh = new FileHandle $self->{fn}, $mode;
96 $fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
98 $self->{year} = $year;
100 dbg("julian", "opening $self->{fn}\n");
102 return bless $self, $pkg;
105 # close the data file
109 undef $self->{fh}; # close the filehandle
113 sub DESTROY # catch undefs and do what is required further do the tree
116 dbg("julian", "closing $self->{fn}\n");
117 undef $self->{fh} if defined $self->{fh};