Information Technology Reference
In-Depth Information
Some programs that check for valid dates stop at the range check and don't pursue
validity checks. This means that a date of June 31 is acceptable as well as February 30 but
our logic won't accept those as valid dates. Nor will it accept February 29, 2100 since that
year will not be a leap year, as far as I know. That is why we have so many statements in
the copy member. However, once we write it and test the logic, we can use the same copy
member in any program that does date validity checking.
The first check will allow for the date to be all zeroes. Then we have
if date-yyyy > 0
to check that the zero is entered for the year. Valid years can be 1 and -1, with the latter
representing 1 BC. We'll be concerned with starting at the year 1 and going up to
December 31, 9999. Someone else can take care of other dates. The next two statements
relating to
date-mm
and
date-dd
check for valid months and days of the month, with
if date-mm > 0 and <= 12
looking to see that the month is between 1 and 12, inclusive. The logical operator
<=
represents less than or equal. You've seen it before. In the range-check procedure the
variable
date-switch
could be 2, 3 or 4 depending on whether the day was out of range, the month was out of
range or the year was entered as zero, respectively.
If the date passes these initial checks, we perform the procedure
validity-check .
This makes sure we don't have dates like April 31 or February 29 if the corresponding
year is not a leap year. If the day is 28 or less, we know that this is a valid date and we are
done with checking. That is why the first line is
validity-check: if work-dd > 28
and we start with a check for February. If the month is 2, we do the February check,
which I will get to later. Otherwise we check for the 30-day months and if we have one
and the day is 31, we have an invalid date. Any other date that is not in February will pass
the test and we are done. The procedure
february-check
simply verifies that a day of 29 corresponds to a leap year. If we don't have a leap year or
if the day is greater than 29,
date-switch
will be set to 5, since the date is invalid. To check for the leap year, we have two
possibilities. The first is that the year could end in two zeroes, like 1900 or 2000. That
year will be a leap year only if the year is perfectly divisible by 400. Thus 2000 was a leap
year but 1900 and 1800 were not. I wasn't around in the latter two cases but take my word
for it. If the year doesn't end in two zeros, it will be a leap year if the year is divisible by
4. This brings us to our next keyword,
Search WWH ::




Custom Search