« Automatic MySQL backup script | Home | Download manager in Java »

Calculating the date of Easter

The easiest way is to use specialized package (like Date::Calc).
If You need standalone function, the following method could be used.

#!/usr/bin/perl

$year=$ARGV[ 0 ];
$isJulian=(defined($ARGV[ 1 ]) && $ARGV[ 1 ] eq 'julian');
$isOrthodox=(defined($ARGV[ 1 ]) && $ARGV[ 1 ] eq 'orthodox');

my ( $G , $C , $H, $I, $J , $L );

$G = $year % 19 ;
if ( $isJulian || $isOrthodox ) {
   print ( $isJulian ? "Julian:" : "Orthodox:" );
  $I = ( 19*$G + 15) % 30 ;
  $J = ( $year + int($year/4) + $I) % 7 ;
}else{
  print "Gregorian:" ;
  $C = int( $year/100 );
  $H = ($C - int($C/4) - int((8*$C+13)/25) + 19*$G + 15) % 30 ;
  $I = $H - int($H/28)*(1 - int($H/28)*int(29/($H + 1))*int((21 - $G)/11)) ;
  $J = ($year + int($year/4) + $I + 2 - $C + int($C/4) ) % 7 ;
}
$L = $I - $J ;
$EasterMonth = 3 + int(($L + 40)/44) ;
$EasterDay = $L + 28 - 31*int($EasterMonth/4) ;

if ( $isOrthodox ) {
 ( $EasterDay, $EasterMonth, $year ) = EasterOrthodox( $EasterDay, $EasterMonth, $year )
}

printf("Day=%d Mon=%d Year=%d \n", $EasterDay, $EasterMonth, $year );

sub EasterOrthodox
{
   my ($pDay, $pMonth, $pYear) = @_ ;
   my ( $extra , $tmp ) = ( 0, 0 );
   my ( $rezDay, $rezMonth) = ( 0, 0 );

   if (($pYear > 1582) && ($pYear <= 4099)) {
      $extra = 10;
   if ($pYear > 1600) {
         $tmp = int($pYear/100) - 16;
         $extra = $extra + $tmp - int($tmp/4);
      }
     
      $rezDay = $pDay + $extra;
      $rezMonth = $pMonth;

      if (($rezMonth == 3) && ($rezDay > 31)) {
         $rezMonth = 4;
         $rezDay = $rezDay - 31;
      }

      if (($rezMonth == 4) && ($rezDay > 30)) {
         $rezMonth = 5;
         $rezDay = $rezDay - 30;
      }
   }
 return ( $rezDay, $rezMonth, $pYear )
}

Examples:
perl -w 2006
perl -w 2006 orthodox

Topics: date, perl | Submitter: checkthis

Comments

You must be logged in to post a comment.

Keep on coding