[Demonstration]
This utility takes the input year, month and day (all in digits)
and then returns the name of the day. How is this possible?
Easy! The names of days always follow the same pattern:
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
Because of this, the appearance of a certain day name follows
a constant mathematical formula. This subroutine/utility has
had that mathematical formula written into it along with the
dynamic generation of certain constants which will affect the
outcome. (i.e. leap years).
The subroutine calculates Leap Years on it's own and makes all
necessary adjustments for them.
To use this subroutine you must pass three arguments and there
are a few rules about these arguments. The three arguments, in
order are the Year, the Month and the Day of the Month for which
you want to return a day's name.
Here are the rules for the arguments being passed:
- The arguments must all be DIGITS. You can not pass, for example,
the word 'August'.
- The year argument MUST be four digits long.
i.e.: 1998 is valid, 98 is not valid.
- The year argument must be equal to or greater than 1800 and less than
or equal to 2099.
- The subroutine will check to ensure that the day of month submitted
does not exceed the permitted number of days in that month. For example,
submitting 31 as the day of the month is valid for July but it is NOT
valid for June.
If you violate any of these above rules the subroutine will return a null value (zero).
If you successfully pass the above arguments, then the subroutine will return the
name of the day corresponding to the day of month, month and year submitted.
Here are some examples of calls to the subroutine:
- EX.1
-
$year = 1965;
$month = 6;
$day = 19;
$name_of_day = dayname($year, $month, $day);
- EX.2
-
$name_of_day = dayname(1965, 6, 19);
- EX.3
-
$year = 1965;
push(@date_arguments, $year);
$month = 6;
push(@date_arguments, $month);
$day = 19;
push(@date_arguments, $day);
$name_of_day = dayname(@date_arguments);
- EX.4
-
$month = 6;
$day = 19;
$name_of_day = dayname(1965, $month, $day);
NOTE: If your on the ball then you realized you can also use
this subroutine simply to ensure that the user has submitted
a valid date that exists between the years 1800 and 2099. To do
this you will make the subroutine part of a test for a logical
loop. Here is an example:
if(dayname($year, $month, $day)) {
YOUR LOGICAL BLOCK HERE. THIS LOGICAL
BLOCK WILL EXECUTE IF THE DATE SUBMITTED
IS A VALID DATE.
} else {
YOUR ERROR TRAP HERE. IT WILL EXECUTE
IF THE DAY SUBMITTED IS TOO BIG FOR THE
MONTH SUBMITTED or a violation of any of
the other argument rules.
}
[Demonstration]
To use this subroutine either cut and paste it into your own script
or simply require this script anywhere before you first call the
subroutine by simply placing the following line in your script
(without the hash mark of course):
require 'dayname.pl';
|