There are 2 kinds of Julian dates. One that counts daily since time of Julius Caesar or the julian date based on current year. The latter seems to be more popular. Todays date is August 31st which is 2010243 or 10243 Julian date. This equals to 243 days since the first day of the year. There are some examples on the net but they get fancy with finding the leap year to taking actual julian date 2455440. The problem with many is they include the time of day as well and thus result in creating Perl code to clean it up. The method I use below comprises of just 3 subroutines, one to get todays date, another to calculate the days since the first of the year and finally puts together the last 2 digits of the year and the total days since the first.


#!/usr/bin/perl -w
use strict;
use Time::localtime; ### For get_date subroutine

## Declare variables for date handling
my $yyyy;  	
my $mo;  	
my $da;		


  &get_date(0); # Pass number of days in past to current or past date. 0 = today, 1 is yesterday etc.
   
   my $jyyy = $yyyy; #Since we need to make year unique for julian part.

   my $dayOfYear = &dayofyear($da,$mo,$jyyy); # Figure days of the year using subroutine.
   $jyyy -= 2000; # Removes the first 2 digits - likely a y2k gotcha if we used 1900 in 20th century.
   
   my $julianDate = $jyyy . $dayOfYear; # Put year and days together
   my $todaysDate = $yyyy . "/" . $mo . "/" . $da; # Put together todays date
   
   print "Todays date is $todaysDate \n";
   print "julianDate is $julianDate \n"; # Output 20100831 will = 10243


# -----------------------------------------------------------------------
# get number of days since first of the Year - Julian
# -----------------------------------------------------------------------

sub dayofyear {
    my ($day1,$month,$year)=@_;  # Passed todays date information
    my @cumul_d_in_m =
(0,31,59,90,120,151,181,212,243,273,304,334,365);
    my $doy=$cumul_d_in_m[--$month]+$day1;
    return $doy if $month < 2;
    return $doy unless $year % 4 == 0;
    return ++$doy unless $year % 100 == 0;
    return $doy unless $year % 400 == 0;
    return ++$doy;
}

# -----------------------------------------------------------------------
# get local time or past date.
# -----------------------------------------------------------------------

sub get_date {
   my $pastno = shift;
   my $pastdate = localtime(time -(86400 * $pastno));
   $yyyy = $pastdate->year() + 1900;  # localtime->year() return years since 1900
   $mo = $pastdate->mon() + 1;        # localtime->mon() return 0-11,  Jan. - Dec.
   $da = $pastdate->mday();

   $mo = "0"."$mo"  if ($mo <= 9);
   $da = "0"."$da"  if ($da <= 9);
   
}

« »