Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Useful Date Algorithms

by Sol-Invictus (Scribe)
on Jan 27, 2004 at 21:37 UTC ( #324540=snippet: print w/ replies, xml ) Need Help??

Description: Date calculations : yes there are modules for this on CPAN, but wouldn't you like to know how to do this kind of stuff :)

Some algorithms useful for calculating:

  • leap years
  • the day of the week a given dates fell on
    (but bear in mind the exact dates when countries started changing from Julian to Gregorian vary, more info here).
#!/usr/bin/perl -w

#=========== declare includes =============

use strict;
use diagnostics-verbose;



#========== declare variables =============

my($Adjuster,$GregorianDay,$JulianDay,$LastOfMonth,$day,$month,$year);
my(@DaysOfWeek);




# Need to print out  calendar for a given year?
# 30 days hath September, April, June and November
# all the rest have 31
# except February 28 days clear, 29 each leap year
# the rule for working out leap years =
# If the year is divisible by 100 it's NOT a leap year
# If the year is divisible by 4 or 400 it's a leap year

$LastOfMonth = '30'if ($month == (4||6||9||11));
$LastOfMonth = '31'if ($month == (1||4||5||7||8||10||12) );

if ($month == 2){
                
 if ( $year % 100 != 0 && $year % 4 == 0 || $year % 400 == 0 ){
    $LastOfMonth = '29';
    } else {  $LastOfMonth = '28'; }
}




# The programmatic way to convert the date into the name for the day o
+f the date given
# this script expects to be fed the date, which it will then output a 
+number 
# which represents Julian day and Gregorian day with Sunday=0
# Julian days are useful for calculating dates before the change to th
+e Gregorian Calendar


$day= 1;
$month=3;
$year=2003;


$Adjuster = int    (     (    14-$month    )/12     );
$month = int ( $month +(    (    $Adjuster*12    )-2    )     );
$year =  int ( ( $year -$Adjuster )    );

$JulianDay = (5+$day+$year+int($year/4)+int(31*$month/12) ) %7;
$GregorianDay = ($day + $year + int($year/4) - int($year/100) + int($y
+ear/400) +int (31*$month/12) ) %7;



print "The value of \$JulianDay is: ",$JulianDay,"\n";
print "The value of \$GregorianDay is: ",$GregorianDay,"\n";


# use the value output on an array like this to get the day of the wee
+k

@DaysOfWeek=qw(    Sunday
                Monday
                Tuesday
                Wednesday
                Thursday
                Friday
                Saturday
                );


Comment on Useful Date Algorithms
Download Code
Re: Useful Date Algorithms
by Anonymous Monk on Jan 27, 2004 at 23:53 UTC
    February has 30 days every 400 years. It begins with the standard compliment of 28, plus one for the usual leap year, plus another because the Earth's orbit is 365.24 days long, not 365.25.
      1. No. You're wrong. It's 28 every year, except the mod 4 ones where it's 29, except the mod 100 ones, where it's 28, except the mod 400 ones, where it's 29. (I'm sure there's a mod 2000 requirement there somewhere, but that issue hasn't come up much ...)
      2. Try learning to multiply and add. (365 * 400) + (24 * 4) = 146096 = 365.24 * 400 There's no extra day every 400th year.
      3. Try learning to spell. It's a "standard complement", and the fact you can't spell is no compliment to your teachers.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        4. Learn to relax.

        Just some friendly advice. :)

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://324540]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2014-12-29 10:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (186 votes), past polls