Another solution without using any module:
#!/usr/bin/perl -w
use strict;
while (1) {
print "Enter year (yyyy) and month (mm), separated by a space:";
chomp(my $input = <STDIN>);
last if !$input;
my ($year, $month) = split / /, $input;
my $m= $month % 12 + 1;
my $y= $year+int($month/12);
if ( $m<3) {
$m+=12;
--$y;
}
++$m;
my $dayname = (qw(Sat Sun Mon Tue Wed Thu Fri))[(int($m * 30.6) +
+int($y * 365.25)-int($y / 100)+int($y / 400)) % 7];
print "The last day of $month/$year will be a $dayname\n";
}
The formular used here works like this:
- if the month is January or february (1 or 2) shift it to the end of the preceeding year (month 13 and 14)
- add 1 to the month
- sum up the integer result of:
- month * 30.6
30.6 gives it the correct toggle between 30 and 31. February is the last month, so no problem with 28/29
- year * 365.25
this takes into account the leap years
- year / -100
this subtracts the non-leap years
- year / 400
this re-adds the non-non-leap years
- the day should then be added
the reminder of 7 is the weekday starting with Sat==0
s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|