Perl FAQ 4.12: How can I find the Julian Day?

Perl FAQ 4.12

How can I find the Julian Day?

Here's an example of a Julian Date function provided by Thomas R. Kimpton*.


#!/usr/local/bin/perl

@theJulianDate = ( 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 );

#************************************************************************
#****   Return 1 if we are after the leap day in a leap year.       *****
#************************************************************************
			   
sub leapDay                  
{                            
    my($year,$month,$day) = @_;

    if (year % 4) {
	return(0);
    }

if (!(year % 100)) {             # years that are multiples of 100
				     # are not leap years
	if (year % 400) {            # unless they are multiples of 400
	    return(0);
	}
    }
    if (month < 2) {
	return(0);
    } elsif ((month == 2) && (day < 29)) {
	return(0);
    } else {
	return(1);
    }
}
    
#************************************************************************
#****   Pass in the date, in seconds, of the day you want the       *****
#****   julian date for.  If your localtime() returns the year day  *****
#****   return that, otherwise figure out the julian date.          *****
#************************************************************************
	    
sub julianDate      
{           
    my($dateInSeconds) = @_;
    my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday);

    ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) =
	localtime($dateInSeconds);
    if (defined($yday)) {
	return($yday+1);
    } else {
	return($theJulianDate[$mon] + $mday + &leapDay($year,$mon,$mday));
    }

}

print "Today's julian date is: ",&julianDate(time),"\n";


Other resources at this site: