Sunteți pe pagina 1din 11

MC Press Online

The CL Corner: Handling Those Pesky Holidays


Contributed by Bruce Vining Friday, 09 May 2008 Last Updated Thursday, 08 May 2008

Holidays should be fun! With CL-based variables, at least they're not a headache.

By Bruce Vining

In the last column in this series, we looked at how to calculate a date such that it would be a minimum of 60 days from an arbitrary date and ensure that the calculated date did not fall on a Saturday or Sunday. We accomplished this using the i5/OS APIs CEEDAYS, CEEDYWK, and CEEDATE. We noticed, however, that holidays were not being handled. When avoiding a Saturday or Sunday calculation, we would use the following Monday, which might be a holiday. In today's column, we'll look at one possible solution for bypassing holidays. Note that there are many, many possible ways to handle holidays. This is simply one: one that may be better or worse than other methods, depending on your situation.

Holidays should be fun! With CL-based variables, at least they're not a headache.

In the last column in this series, we looked at how to calculate a date such that it would be a minimum of 60 days from an arbitrary date and ensure that the calculated date did not fall on a Saturday or Sunday. We accomplished this using the i5/OS APIs CEEDAYS, CEEDYWK, and CEEDATE. We noticed, however, that holidays were not being handled. When avoiding a Saturday or Sunday calculation, we would use the following Monday, which might be a holiday. In today's column, we'll look at one possible solution for bypassing holidays. Note that there are many, many possible ways to handle holidays. This is simply one: one that may be better or worse than other methods, depending on your situation.

As we learned in "So You're Looking for a Date?", Lilian dates are represented by a sequential number where October 15, 1582, is 1; October 16, 1582, is 2; and March 21, 2008, is 155387. This attribute seems ready-made for an array of work vs. non-work days with the Lilian date being an offset into the array, and this is just what we will do. As it is unlikely that we need to support dates far in the past (back to 1582) or far into the future (the year 9999), we will arbitrarily create an array of 1-byte character fields representing the dates January 1, 1940, through December 31, 2199, where a value of 0 indicates a work day and a value of 1 a non-work day. As January 1, 1940, corresponds to Lilian date 130470 and December 31, 2199, corresponds to Lilian date 225433, this means we will create an array of 94964 1-byte elements (225433 - 130470 + 1). And we will not allow the small implementation detail that CL doesn't support arrays, or a variable length greater than 32767 bytes, to deter us!

The following program, CRTCAL, creates a user space (*USRSPC) named CALENDAR in library QUSRSYS. The *USRSPC, when created, is initialized so that every day is a work day. Following that, CRTCAL marks each Saturday and Sunday in the *USRSPC as a non-work day.

Pgm

http://www.mcpressonline.com

Powered by Joomla!

Generated: 29 August, 2008, 00:19

MC Press Online

Dcl

Var(&UsrSpc_Ptr) Type(*Ptr)

Dcl

Var(&WeekEnd)

Type(*Char) Stg(*Based) +

Len(2) BasPtr(&UsrSpc_Ptr)

Dcl

Var(&Size)

Type(*Int) Value(94964)

Call

Pgm(QUSCRTUS) Parm('CALENDAR QUSRSYS' ' ' +

&Size '0' '*USE' 'Calendar array')

Call

Pgm(QUSPTRUS) Parm('CALENDAR QUSRSYS' +

&UsrSpc_Ptr)

ChgVar

Var(%Ofs(&UsrSpc_Ptr)) +

Value(%Ofs(&UsrSpc_Ptr) + 5)

http://www.mcpressonline.com

Powered by Joomla!

Generated: 29 August, 2008, 00:19

MC Press Online

DoUntil

Cond(%Ofs(&UsrSpc_Ptr) > 94964)

ChgVar Var(&WeekEnd) Value('11')

ChgVar Var(%Ofs(&UsrSpc_Ptr)) +

Value(%Ofs(&UsrSpc_Ptr) + 7)

EndDo

EndPgm

Without going into the details of the Create User Space (QUSCRTUS) API (they can be found here), CRTCAL is creating the *USRSPC QUSRSYS/CALENDAR that is a minimum of 94964 bytes in size. Each byte is initialized to 0 and the user space has PUBLIC(*USE) authority. There are several additional parameters not shown. A key one is a parameter to replace the *USRSPC if it currently exists. The default is to not replace an existing *USRSPC, and I'm taking that default just in case you already have a *USRSPC by that name on your system. If you need to run CRTCAL several times when testing, you can either add the Replace parameter to the call to QUSCRTUS or use the DLTUSRSPC command prior to running CRTCAL.

After creating CALENDAR CRTCAL, we set the weekends as non-work days. To do this, we will use pointers and based variables. This is the real reason I chose an array based solution: I wanted to familiarize you with this capability/technique of CL, one that I believe is underutilized. First, CRTCAL gets a pointer to CALENDAR using the Retrieve Pointer to User Space (QUSPTRUS) API. This pointer, &UsrSpc_Ptr, will point to, or address, the first byte of the *USRSPC. As the first byte represents January 1, 1940, and we know from the CEEDYWK API (not shown) that this date is a Monday, CRTCAL adds 5 to the value of &UsrSpc_Ptr so that it now addresses January 6, 1940: a Saturday. CRTCAL then moves the 2-byte value 11 to variable &WeekEnd. This sets both the Saturday and Sunday bytes as being non-working (note that the variable &WeekEnd is defined as being *Based on &UsrSpc_Ptr so &WeekEnd is associated with whatever two bytes &UsrSpc_Ptr is currently addressing, in this case, Saturday, January 6, 1940, and Sunday, January 7, 1940), adds 7 to &UsrSpc_Ptr to address the next Saturday in CALENDAR, and then loops until all weekends in our supported range of dates are updated.

http://www.mcpressonline.com

Powered by Joomla!

Generated: 29 August, 2008, 00:19

MC Press Online

I will point out that there is one "bug" or exposure in CRTCAL, but I left it in to simplify the code. Namely, if December 31, 2199, was a Saturday, then updating Sunday, January 1, 2200 (which CRTCAL would do with the ChgVar &Weekend command as it's currently coded) could cause a MCH0601 error: "Space offset is outside of current limit." In the case of CRTCAL, this won't happen for two reasons. First, December 31, 2199, is not a Saturday. Second, *USRSPCs are created with sizes that are multiples of 4096. So even though CRTCAL asked for only 94964 bytes, additional bytes were allocated, so a byte is actually there for January 1, 2200. Building in a check for this situation, while easily done, would add code without any educational benefit, so I left it out.

Having created CALENDAR, now let's look at updating holidays. The following program, SETDAY, actually allows you to mark any given day as either a work day or a non-work day.

Pgm

Parm(&YYMDDay &Status)

Dcl

Var(&YYMDDay)

Type(*Char) Len(8)

Dcl

Var(&Status)

Type(*Char) Len(5)

Dcl

Var(&LilDay)

Type(*Int)

Dcl

Var(&UsrSpc_Ptr) Type(*Ptr)

Dcl

Var(&Day)

Type(*Char) Stg(*Based) +

http://www.mcpressonline.com

Powered by Joomla!

Generated: 29 August, 2008, 00:19

MC Press Online

Len(1) BasPtr(&UsrSpc_Ptr)

Dcl

Var(&Base)

Type(*Int) Value(130470)

Dcl

Var(&Top)

Type(*Int) Value(225433)

CallPrc

Prc(CEEDAYS) Parm((&YYMDDay) ('YYYYMMDD') +

(&LilDay) (*Omit))

If

Cond((&LilDay *GE &Base) *And +

(&LilDay *LE &Top)) Then(Do)

Call Pgm(QUSPTRUS) Parm('CALENDAR QUSRSYS' +

&UsrSpc_Ptr)

ChgVar Var(%Ofs(&UsrSpc_Ptr)) +

http://www.mcpressonline.com

Powered by Joomla!

Generated: 29 August, 2008, 00:19

MC Press Online

Value(%Ofs(&UsrSpc_Ptr) + &LilDay - &Base)

Select

When Cond(&Status = '*WORK') +

Then(ChgVar Var(&Day) Value('0'))

When Cond(&Status = '*OFF') +

Then(ChgVar Var(&Day) Value('1'))

OtherWise Cmd(SndPgmMsg +

Msg('Invalid status for day. +

Valid values are *WORK and *OFF') +

ToPgmQ(*Ext))

EndSelect

EndDo

Else

Cmd(SndPgmMsg +

http://www.mcpressonline.com

Powered by Joomla!

Generated: 29 August, 2008, 00:19

MC Press Online

Msg('Invalid date specified. +

The valid range is Jan 1 1940 to Dec 31 +

2199.') ToPgmQ(*Ext))

EndPgm

SETDAY requires two parameters. The first, &YYMDDay, is the day we want to set and is formatted as YYMD. The second, &Status, is the status we want to assign to the day. The two supported values for &Status are *WORK and *OFF. Even with parameter validation (range checking and a valid value for &Status), the program is quite straightforward. SETDAY gets the Lilian date for the day passed in, retrieves &UsrSpc_Ptr (which addresses the first byte of the CALENDAR *USRSPC), calculates the byte offset into CALENDAR for the specified date by adding the Lilian date to &UsrSpc_Ptr and then subtracting the &Base (or starting) Lilian date value for January 1, 1940, and then sets the corresponding byte to the appropriate value. That's it!

Our last program, GREGTOLIL3, is shown below.

Pgm

Parm(&YYMDGreg)

Dcl

Var(&YYMDGreg) Type(*Char) Len(8)

http://www.mcpressonline.com

Powered by Joomla!

Generated: 29 August, 2008, 00:19

MC Press Online

Dcl

Var(&CurLilDate) Type(*Int)

Dcl

Var(&NewLilDate) Type(*Int)

Dcl

Var(&NewGregDt) Type(*Char) Len(32)

Dcl

Var(&UsrSpc_Ptr) Type(*Ptr)

Dcl

Var(&Day)

Type(*Char) Stg(*Based) +

Len(1) BasPtr(&UsrSpc_Ptr)

Dcl

Var(&Base)

Type(*Int) Value(130470)

Dcl

Var(&Top)

Type(*Int) Value(225433)

Dcl

Var(&DayOff)

Type(*Char) Len(1) Value('1')

CallPrc

Prc(CEEDAYS) Parm((&YYMDGreg) ('YYYYMMDD') +

http://www.mcpressonline.com

Powered by Joomla!

Generated: 29 August, 2008, 00:19

MC Press Online

(&CurLilDate) (*Omit))

If

Cond((&CurLilDate *GE (&Base - 60)) *And +

(&CurLilDAte *LE (&Top - 60))) Then(Do)

ChgVar Var(&NewLilDate) Value(&CurLildate + 60)

Call Pgm(QUSPTRUS) Parm('CALENDAR QUSRSYS' +

&UsrSpc_Ptr)

ChgVar Var(%Ofs(&UsrSpc_Ptr)) +

Value(%Ofs(&UsrSpc_Ptr) + &NewLilDate - +

&Base)

DoWhile Cond(&Day = &DayOff)

ChgVar Var(%Ofs(&UsrSpc_Ptr)) +

http://www.mcpressonline.com

Powered by Joomla!

Generated: 29 August, 2008, 00:19

MC Press Online

Value(%Ofs(&UsrSpc_Ptr) + 1)

EndDo

ChgVar Var(&NewLilDate) +

Value(%ofs(&UsrSpc_Ptr) + &Base)

CallPrc Prc(CEEDATE) Parm((&NewLilDate) +

('MM/DD/YYYY') (&NewGregDt) (*Omit))

SndPgmMsg Msg(&NewGregDt) ToPgmQ(*Ext)

EndDo

Else

Cmd(SndPgmMsg +

Msg('Date is outside the valid range.'))

EndPgm

http://www.mcpressonline.com

Powered by Joomla!

Generated: 29 August, 2008, 00:19

MC Press Online

GREGTOLIL3 has one required parameter, &YYMDGreg, which is the starting date in YYMD format. After appropriate range checking, GREGTOLIL3 adds 60 days to the input value, checks to see if the calculated date is a non-work day (&Day = '1'), and if so, adds one day to the calculated date and tests again. When the calculated day is a work day (&Day = '0'), GREGTOLIL formats the date, displays it, and ends.

If we now CALL GREGTOLIL3 with the parameter '20080325', we will see the message 05/26/2008. As we know from our earlier discussion, this date is actually a holiday in the United States. Let's now CALL SETDAY with parameters ('20080526' *OFF). After this, CALL GREGTOLIL3 '20080325' results in the message 05/27/2008. If it was later decided that we would be working on Saturday, May 24, 2008, we could CALL SETDAY ('20080524' *WORK), and then CALL GREGTOLIL3 '20080325' would result in the message 05/24/2008. If, rather than finding the next work day, we wanted to calculate the previous work day, we simply subtract 1 instead of adding 1 when in the DoWhile loop. Pretty straightforward.

There are a few other benefits as well: As the CALENDAR entries are stored in a *USRSPC and accessed by a basing pointer, there is no duplication of the CALENDAR in each job accessing the CALENDAR entries. This also means that the instant SETDAY changes the &Status of a day, that change is immediately seen by all users of CALENDAR. There is no "loading" of the data as there would be with a database file containing the date information. And, as there is no database involved, we also don't have to worry about GREGTOLIL3 running into an end-of-file condition and being unable to read the entries anymore (though this CL consideration is lifted in V6R1).

But even more importantly, you've now seen how easy it can be to simulate an array and easily access entries within that array. Arrays, as we have seen here, can be much larger than what can be declared within CL for a *Char variable. Using *Based variables, as we did with &Day, we only need to define and use the (in this case) one byte that's currently important to us of the 94964-byte array. A very powerful tool has been added to your CL toolbox with the support for pointers and based variables in V5R4. I hope you can take advantage of this tool in your future application development.

More CL Questions? Wondering how to accomplish a function in CL? Send your CL-related questions to me at bvining@brucevining.com. I'll see what I can do about answering your burning questions in future columns.

http://www.mcpressonline.com

Powered by Joomla!

Generated: 29 August, 2008, 00:19

S-ar putea să vă placă și