Find if a Year is Leap Year

It is quite easy to identify if a given Year is a Leap Year or not using COBOL intrinsic functions. To be a Leap Year, the Year should be divisible by 4 and not divisible by 100. If divisible by 100, then it should also be divisible by 400.












The following code is present in the Screenshot above.

01 WS-YEAR                     PIC 9(04). 

EVALUATE TRUE                                  
    WHEN FUNCTION MOD (WS-YEAR 4)   NOT ZERO    
    WHEN FUNCTION MOD (WS-YEAR 100)     ZERO    
     AND FUNCTION MOD (WS-YEAR 400) NOT ZERO    
    DISPLAY 'IT IS NOT A LEAP YEAR ' WS-YEAR 
WHEN OTHER                                      
    DISPLAY 'IT IS A LEAP YEAR     ' WS-YEAR 
END-EVALUATE.


Another way of identifying is by converting it to Gregorian Date as given below.





















The following code is present in the Screenshot above.

01 WS-JULIAN-DATE.
    05 WS-JULIAN-YR            PIC 9(04).
    05 WS-JULIAN-DDD           PIC 9(03)  VALUE 060.
01 WS-JULIAN-DATE-R REDEFINES WS-JULIAN-DATE PIC 9(07).

01 WS-GREGORIAN-DATE           PIC 9(08).
    05 WS-GREGORIAN-DATE-R REDEFINES WS-GREGORIAN-DATE.
        10 WS-GREGORIAN-YR     PIC 9(04).
        10 WS-GREGORIAN-MM     PIC 9(02).
        10 WS-GREGORIAN-DD     PIC 9(02).

01 WS-YEAR                     PIC 9(04).

MOVE WS-YEAR       TO WS-JULIAN-YR.

COMPUTE WS-GREGORIAN-DATE = FUNCTION DATE-OF-INTEGER 
                            (FUNCTION INTEGER-OF-DAY 
                            (WS-JULIAN-DATE-R)).

IF WS-GREGORIAN-MM = 02
    DISPLAY 'IT IS A LEAP YEAR     ' WS-YEAR
ELSE
    DISPLAY 'IT IS NOT A LEAP YEAR ' WS-YEAR 
END-IF.

No comments: