Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: •Day of week for an arbitary date using core modules

by YuckFoo (Abbot)
on Feb 22, 2002 at 17:29 UTC ( [id://146948]=note: print w/replies, xml ) Need Help??


in reply to Day of week for an arbitary date using core modules

I found this jewel in Greg Hewgill's implementation of the cal command on the Perl Power Tools page:

http://www.perl.com/language/ppt/src/cal/index.html

YuckFoo

# DOW = ([23m/9] + d + 4 + z + [z/4] - [z/100] + [z/400] - 2 (if m>=3 +)) mod 7 # # y is the year. # m is the month. # d is the day. # z = y - 1 (if m < 3) # = y (if m >= 3) # A mod B means take the reminder of A / B. # # The source: Journal on Recreational Mathematics, Vol. 22(4), pages +280-282, 1990. # The authors: Michael Keith and Tom Craver. # # The formula can be implemented by the following C function: # # int dayofweek(int y,m,d) # { # return((23*m/9+d+4+(m<3?y--:y-2)+y/4-y/100+y/400)%7); # } sub dow { my ($y, $m, $d) = @_; $y-- if $m < 3; $d += 11 if $y < 1752 || $y == 1752 && $m < 9; if ($y >= 1752) { return (int(23*$m/9)+$d+4+($m<3?$y+1:$y-2)+int($y/4)-int($y/100)+i +nt($y/400))%7; } else { return (int(23*$m/9)+$d+5+($m<3?$y+1:$y-2)+int($y/4))%7; } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-19 17:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found