Return to the CL Tips
How do I check for a leap year in a CL program?
The CVTDAT (Convert Date) command already knows which years are leap years. When you want to check for a leap year, just concatenate (*CAT) a two-character year field to the end
of a four-character string that contains the value 0229 and send the string to the CVTDAT command (see the code below). If CVTDAT returns error message "CPF0555 Date not in
specified format or date not valid," the date is invalid and the year is not a leap year. If no error message is returned, the year is a leap year.
Pgm ( &Year ) Dcl &Year *Char ( 2 ) Dcl &MDY *Char ( 6 ) ChgVar ( &MDY ) ( '0229' *Cat &Year ) CvtDat Date( &MDY ) + ToVar( &MDY ) + FromFmt( *MDY ) + ToFmt( *MDY ) + ToSep( *None ) MonMsg ( CPF0555 ) Exec( + Do ) SndPgmMsg MsgID( CPF9898 ) + MsgF( QCPFMsg ) + MsgDta( 'Year' *BCat &Year + *BCat 'is not a leap year' ) + MsgType( *Comp ) Return EndDo SndPgmMsg MsgID( CPF9898 ) + MsgF( QCPFMsg ) + MsgDta( 'Year' *BCat &Year + *BCat 'is a leap year' ) + MsgType( *Comp ) EndPgm
[report a broken link by clicking here]