Perl4 script for Day-of-week from date? 
Author Message
 Perl4 script for Day-of-week from date?

Being new to using PERL (and just joining this rather busy newsgroup), can
someone kindly let me know where I can get a subroutine to derive the day of
the week (Sat Sun etc) from any given date ?

While I am at it - is there a useful source of such Perl library
routines for common tasks available on Internet?

Many Thanks

-------------------------------------------------------------------------------
Andy Gillanders, W R Grace Ltd, Slough, UK

  +44 (0)734 733595 (home)    Compuserve: 100144,3005
-------------------------------------------------------------------------------



Fri, 14 Mar 1997 19:16:00 GMT  
 Perl4 script for Day-of-week from date?
: Being new to using PERL (and just joining this rather busy newsgroup), can
: someone kindly let me know where I can get a subroutine to derive the day of
: the week (Sat Sun etc) from any given date ?
:

There are several algorithms for doing this... probably one of the most
famous is Zeller's congruence and here's a little snippet of code that I
typed in one day (just because :-)

----snip------snip------
#!/usr/bin/perl

   'Sunday',
   'Monday',
   'Tuesday',
   'Wednesday',
   'Thursday',
   'Friday',
   'Saturday'
);

# The following is an implementation of the original Zeller's congruence
# Apparently the term goes negative in the first few months of the first
# few years of the century
sub zeller1 {

   local($century, $offset);
   local($leap_part, $day_of_week);

   if ($month < 3) { $month += 12; $year--; }

   $century = int($year / 100);
   $offset = int($year % 100);
   $leap_part = $offset + int($offset/4) + int($century/4) - 2 * $century;
   int(($day + int(($month+1)*26 / 10) + $leap_part + 6 ) % 7);

Quote:
}

# This is an implementation of "Zeller's Congruence" that I found somewhere
# that doesn't go negative in the first few months of the first few years of
# the century
sub zeller2 {

   local($leap_part, $day_of_week);

   if ($month < 3) { $month += 12; $year--; }

   $leap_part = $year + int($year/4) + int($year/400) - int($year/100) + 1;
   int(($leap_part + int(($month+1) * 6 / 10) + $month * 2 + $day) % 7);

Quote:
}

sub zeller3 {

   local($leap_part, $day_of_week);

   if ($month < 3) { $month += 12; $year--; }

   $leap_part = $year + int($year/4) + int($year/400) - int($year/100);
   int(($day + int(($month+1)*26 / 10) + $leap_part + 6) % 7);

Quote:
}


print "Enter a date of the form mm/dd/yyyy :  ";
$date = <STDIN>;
die "Incorrect date format!\n" unless ($date =~ m%(\d\d)/(\d\d)/(\d\d\d\d)%);

chop($date);
printf"$date is a %s\n", $weekday[&zeller1($1,$2,$3)];
printf"$date is a %s\n", $weekday[&zeller2($1,$2,$3)];
printf"$date is a %s\n", $weekday[&zeller3($1,$2,$3)];
----snip------snip------

I'm not sure that all of the the above are correct because I don't remember
what the tradeoffs are, but feel free to play with them all...

but probably the "best" way to find out the day of the week is to use
timelocal.pl like so....

!! Warning, untested code follows !!

#!/usr/bin/perl

   'Sunday',
   'Monday',
   'Tuesday',
   'Wednesday',
   'Thursday',
   'Friday',
   'Saturday'
);

require 'timelocal.pl';

print "Enter a date of the form mm/dd/yyyy :  ";
$date = <STDIN>;
die "Incorrect date format!\n" unless ($date =~ m%(\d\d)/(\d\d)/(\d\d\d\d)%);
($mn, $da, $yr) = ($1,$2,$3);
chop($date);

$gmt = &timelocal(0,0,0,$da,$mn,$yr);
$daynumber = (localtime($gmt))[6];

printf "$date is a %s\n", $weekday[$daynumber];

----snip----snip----

But then, these are just my ramblings, take them how you like...

regards,

-Scott
--

Systems Programmer                    URL:   http://www.cbi.tamucc.edu/~duff/
Conrad Blucher Institute for Surveying and Science
6300 Ocean Drive, Corpus Christi  TX 78412



Sun, 16 Mar 1997 07:36:38 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Days between dates, day of week ...

2. Program to give day of the week from given date

3. Program to give day of the week from given date

4. day of the week from date?

5. Algorithm for Day Of Week from date YY/MM/DD

6. Getting the week day from a previous date.

7. Trying to find day of week from date

8. Program to give day of the week from given date

9. Day of Week Display for User-defined date

10. Day of Week Display for User-Input Date

11. Wanna get the Week Day for a specified date

12. SOLUTION - How to find day of week from a given date (PERL)

 

 
Powered by phpBB® Forum Software