Inside Cobol #49 (New Date Intrinsics)
by
Shawn Gordon
Since the year 2000 is rapidly approaching, I am going to be covering anything that comes up in that arena as it concerns COBOL before addressing anything else I might have already promised. So in that spirit, I am going to go over the new date intrinsics that HP gave us in MPE/iX 5.5 PP 4.
I have been doing a lot of Y2K consulting lately, and it seems that everyone has written their own date routines, and most of them will break shortly. My goal was to implement the HP supplied solution, and be able to support YYMMDD as well as YYYYMMDD date functions during the conversion process. The documentation for the new intrinsics can be found at http://jazz.external.hp.com/src/year2000/dateintr.txt. The only thing bad I can say about them is that I wish they had been created 10 years ago, or with the introduction of the Spectrum series computers.
There are 6 new intrinsics that are available, and the documentation from jazz only shows one short example. What you will notice is that all the parameters to all the intrinsics are now 32 bit, this means they will work for as long as anyone will ever care.
1. HPDATECONVERT – converts dates from one supported format to another
2. HPDATEFORMAT – converts a date into a display type (I use this instead of HPDATECONVERT usually)
3. HPDATEDIFF – returns the number of days between to given dates
4. HPDATEOFFSET – returns a date that is +/- the number of days from the source date
5. HPDATEVALIDATE – verifies that the date conforms to a supported date format
6. New 32-bit HPCALENDAR format (HPCALENDAR, HPFMTCALENDAR).
There are a couple of things to make sure you do correctly. There are some parameters on some intrinsics that need to be passed by value, so we use the \\ backward slashes around the variable name. The other thing that can be confusing is the DATE-CUTOFF. Basically this defines a “split” year. If anything is below this value, it will be translated to the next century. In other words, if the value of DATE-CUTOFF is 50, and you are using a 2 digit year of 00..49, then it will be resolved as 2000..2049, and those in the range of 50..99 will be 1950..1999.
If you use a value of -1 then the intrinsic will pick up the value of the predefined system variable HPSPLITYEAR. This way you can control it outside of your program, so I use a DATE-CUTOFF of -1 to stay modular.
The other thing to note is the DATE-CODE which indicates the style of the date that you are working with. I am using 15 because it works with both YYMMDD and YYYYMMDD format.
I’ll show you some code examples of the variable declarations and the results of running a program with the functions. I feel it’s important to standardize on the new HP supplied intrinsics, because you are going to be a lot safer than trying to maintain some piece of code that was probably written 20 years ago that no one knows how it works anymore.
Until next month………
01 DATE-CODE PIC S9(9) COMP VALUE 15. 01 DATE-RESULT PIC S9(9) COMP VALUE 0. 01 DATE-STATUS. 03 S-INFO PIC S9(4) COMP VALUE 0. 03 S-SUBSYS PIC S9(4) COMP VALUE 0. 01 DATE-CUTOFF PIC S9(9) COMP VALUE -1. 01 FORMAT-LEN PIC S9(9) COMP VALUE 20. 01 FROM-DATE PIC 9(8) COMP. 01 THRU-DATE PIC 9(8) COMP. 01 DAYS-DIFF PIC S9(9) COMP. 01 FORMAT-DATE PIC X(20). 01 FORMAT-TYPE PIC X(20). CALL INTRINSIC "HPDATEFORMAT" USING \DATE-CODE\, FROM-DATE, HOLD-FORMAT, FORMAT-DATE, FORMAT-LEN, DATE-STATUS, \DATE-CUTOFF\. IF S-INFO <> 0 DISPLAY "Error in HPDATEFORMAT". CALL INTRINSIC "HPDATEDIFF" USING \DATE-CODE\, FROM-DATE, THRU-DATE, DUMMY-VAL, DATE-STATUS, \DATE-CUTOFF\. IF S-INFO <> 0 DISPLAY "Error in HPDATEDIFF". CALL INTRINSIC "HPDATEOFFSET" USING \DATE-CODE\, DISPLAY "Error in HPDATEOFFSET". CALL INTRINSIC "HPDATEVALIDATE" USING \DATE-CODE\, FROM-DATE, \DATE-CUTOFF\ GIVING DATE-RESULT. IF S-INFO <> 0 DISPLAY "Error in HPDATEVALIDATE". :RUN MYDATE Enter date in YYMMDD or YYYYMMDD format: 19980317 Enter date format string: MM/DD/YY Formatted date is 03/17/98 Julian date is 01998076 Enter From date: 19980101 Enter Thru date: 19980501 Number of days = +000000120 Enter start date: 19980801 Enter day offset: -31 New date is 19980701