Get the Day of the Week in CL

Article ID: 57350

Q: Is there any CL command or function to retrieve the day of the week for a given date? I know I can retrieve the QDAYOFWEEK system value for the current date, but I want to be able to find the day of the week for any arbitrary date. I need a CL solution please, not RPG!

A: There's a really easy way to do this in ILE CL, using the ILE date and time APIs. These are some of the easiest APIs to use, and they're extremely powerful. There's no easy way to do it in OPM CL, though. So I suggest writing a program to retrieve the day of the week in ILE CL and calling that program from any OPM CL programs that need this functionality.

The ILE date and time APIs are documented in the Information Center. For this program, I'm going to use two APIs, Convert Date to Lilian Format (CEEDAYS) and Convert Lilian Date to Character Format (CEEDATE).

CEEDAYS takes a date in any format and converts it to Lilian format. Lilian format is a count of the number of days that have elapsed since October 14, 1582, which is the start of the Gregorian Calendar. However, the details of how Lilian dates work are unimportant to this program because I use the Lilian date only as a parameter to pass to the next API.

CEEDAYS determines its input format by reading a "picture string" as a parameter. The picture string simply describes which part of the date contains the month, day, and year values that it needs to understand the date. For example:

CALLPRC PRC(CEEDAYS) PARM('06/07/08' 'MM/DD/YY' &LILIAN *OMIT)

In this example, I've passed a date of 06/07/08 in the first parameter. How can the API know whether this means June 7, 2008, as is standard in the U.S., or July 6, 2008, as most Europeans write the date? Or July 8, 2006 if the year was written first? How can it tell? Because I've specified a "picture string" of MM/DD/YY in the second parameter. That picture string tells the API that the date does, indeed, refer to June 7, 2008. More examples of picture strings are in the Info Center.

With that in mind, I decided to make my program that retrieves the day of the week as versatile as possible. The caller will provide both the date and the picture string. That way, you can use it with any conceivable date format.

Here's the code for my Get Day of Week (GETDOW) program:

PGM PARM(&DATE &FORMAT &DOW)

    DCL VAR(&DATE)   TYPE(*CHAR) LEN(10)
    DCL VAR(&FORMAT) TYPE(*CHAR) LEN(10)
    DCL VAR(&DOW)    TYPE(*CHAR) LEN(3)
    DCL VAR(&LILIAN) TYPE(*CHAR) LEN(4)

    CALLPRC PRC(CEEDAYS) PARM(&DATE &FORMAT &LILIAN *OMIT)
    CALLPRC PRC(CEEDATE) PARM(&LILIAN 'WWW' &DOW *OMIT)

ENDPGM

CEEDAYS takes the date provided by the caller and converts it to Lilian format. CEEDATE reverses the process and takes the Lilian and converts it back to a character date according to the picture string. For CEEDATE, I provided a picture string of WWW which means that I want the API to return a three-character day of the week. I didn't ask the API to output anything else besides the day of the week, so when the program finishes running, the &DOW variable will contain one of the following values: SUN, MON, TUE, WED, THU, FRI, or SAT.

You could, of course, change the picture string on the CEEDATE API if you wanted to make the program output something else. For example, you could specify Wwwwwwwwww to get full words for the day of the week, such as Monday or Tuesday. Or you could specify something like YYYYMMDD if you wanted to write a tool that simply reformats the date in year/month/day sequence.

The preceding program was written in ILE CL, and that means that the source member type should be set to CLLE (not CLP, which is OPM CL). To try the GETDOW program, type the preceding code into your favorite source code editor, and then type the following command to compile it:

CRTBNDCL PGM(GETDOW) SRCFILE(QCLSRC)

Now that you have the GETDOW program, you can call it to retrieve the day of the week. Here's an example of calling it from a simple OPM CL program:

PGM
     DCL VAR(&DOW) TYPE(*CHAR) LEN(3)

     CALL GETDOW PARM('20081023' 'YYYYMMDD' &DOW)

     SNDPGMMSG MSGID(CPF9898) MSGF(QCPFMSG) MSGTYPE(*COMP) +
               MSGDTA('Day of week is ' *CAT &DOW)
ENDPGM

This should output "Day of week is THU" because October 23, 2008, is a Thursday.

ProVIP Sponsors

ProVIP Sponsors