       IDENTIFICATION DIVISION.
       PROGRAM-ID. Y2KSAGE.
       AUTHOR. JAY MOSELEY.
       DATE-WRITTEN. DECEMBER, 1997.
       DATE-COMPILED.
      *                                                               *
      *   YY   YY   222   KK   KK  SSSSS     A     GGGGG  EEEEEE      *
      *   YY   YY  2   2  KK  KK  SS   SS   AAA   GG   GG EE          *
      *   YY   YY      2  KK KK   SS       AA AA  GG   GG EE          *
      *    YY YY       2  KKKK     SSSSS  AA   AA GG      EEEE        *
      *     YYY     222   KKKK         SS AA   AA GG  GGG EE          *
      *     YY     2      KK KK   SS   SS AAAAAAA GG   GG EE          *
      *     YY     2      KK  KK  SS   SS AA   AA GG   GG EE          *
      *     YY     22222  KK   KK  SSSSS  AA   AA  GGGGG  EEEEEEE     *
      *                                                               *
      *************************************************************** *
      * THIS SUBROUTINE COMPUTES THE NUMBER OF DAYS ELAPSED BETWEEN 2 *
      * GIVEN GREGORIAN DATES (MMDDYYYY) FOR SHORT TERM AGING         *
      *                                                               *
      *  VALID INPUT YEARS FOR THIS ROUTINE ARE THE YEARS 1601        *
      *  THROUGH 3399 A. D. (INCLUSIVE)                               *
      *                                                               *
      *  VALID INPUT MONTH VALUES FOR THIS ROUTINE ARE 01 THROUGH 12. *
      *                                                               *
      *  VALID INPUT DAY VALUES FOR THIS ROUTINE ARE 01 THROUGH 31    *
      *  WITH EXCEPTIONS FOR THE MONTHS LISTED:                       *
      *       MONTH          MAXIMUM DAY VALUE                        *
      *        04                    30                               *
      *        06                    30                               *
      *        09                    30                               *
      *        11                    30                               *
      *        02                    28                               *
      *        02 DURING LEAP YEAR   29                               *
      *                                                               *
      *  THE DATE FORMAT MUST BE MMDDYYYY, WHERE MM = THE 2 DIGIT     *
      *  MONTH VALUE, DD = THE 2 DIGIT DAY VALUE, AND YYYY = THE      *
      *  4 DIGIT YEAR VALUE.                                          *
      *                                                               *
      *  FOUR FIELDS ARE PASSED AS PARAMETERS TO THE ROUTINE:         *
      *  1) & 2)  8 BYTE FIELDS CONTAINING THE GREGORIAN DATES (IN    *
      *      (ZONED-DECIMAL FORMAT) BETWEEN WHICH THE NUMBER OF DAYS  *
      *      IS TO BE CALCULATED (ORDER OF THE DATES IS IMMATERIAL),  *
      *  3)  A 1 BYTE ZONED-DECIMAL RETURN CODE, AND                  *
      *  4)  A 7 BYTE FIELD WHICH WILL RECEIVE THE NUMBER OF ELAPSED  *
      *      DAYS (IN ZONED-DECIMAL FORMAT).                          *
      *  THE ROUTINE WILL NOT ALTER THE DATES PASSED TO IT.           *
      *                                                               *
      *  SUGGESTED CALLING SYNTAX FOR COBOL CALLERS:                  *
      *                                                               *
      *       01  SAGE-PARAMETERS.                                    *
      *           02  DATE1 PIC 9(8) VALUE 01011997.                  *
      *           02  DATE2 PIC 9(8) VALUE 04011997.                  *
      *           02  RC    PIC 9(1) VALUE 0.                         *
      *           02  DAYS  PIC S9(7) VALUE +0.                       *
      *       CALL 'Y2KSAGE' USING SAGE-PARAMETERS.                   *
      *                                                               *
      *  THE POSSIBLE VALUES FOR THE RETURN CODE FIELD ARE:           *
      *                                                               *
      *  0 INDICATES SUCCESSFUL EXECUTION OF THE ROUTINE.             *
      *                                                               *
      *  2 INDICATES INVALID DATA WAS FOUND IN THE DATE1 FIELD        *
      *  (3 FOR DATE2).  INVALID DATA ARE DETERMINED IF THE FIELD'S   *
      *  LOW ORDER BYTE'S ZONE CONTAINS AN INVALID SIGN, (NOT ONE     *
      *  OF X'C', X'D', OR X'F'), OR IF THE PRECEEDING BYTES' ZONES   *
      *  ARE OTHER THAN X'F', OR IF ANY BYTE'S LOW ORDER NIBBLE       *
      *  CONTAINS A VALUE GREATER THAN X'9'.                          *
      *                                                               *
      *  4 INDICATES THE VALUE OF THE YEAR SPECIFIED IN THE DATE1     *
      *  FIELD (5 FOR DATE2) WAS NOT IN THE RANGE SPECIFIED IN 1      *
      *  (ABOVE).                                                     *
      *                                                               *
      *  6 INDICATES THE VALUE OF THE MONTH SPECIFIED IN THE DATE1    *
      *  FIELD (7 FOR DATE2) WAS NOT IN THE RANGE SPECIFIED IN 2      *
      *  (ABOVE).                                                     *
      *                                                               *
      *  8 INDICATES THE VALUE OF THE DAY SPECIFIED IN THE DATE1      *
      *  FIELD (9 FOR DATE2) WAS NOT IN THE RANGE SPECIFIED IN 3      *
      *  (ABOVE).                                                     *
      *                                                               *
      *  UPON SUCCESSFUL EXECUTION, THE ABSOLUTE VALUE OF THE NUMBER  *
      *  OF DAYS BETWEEN THE 2 SPECIFIED DATES WILL BE PLACED IN THE  *
      *  4TH PARAMETER (IN PACKED DECIMAL FORMAT).  IF THE RETURN     *
      *  CODE IS A NON-ZERO VALUE (UNSUCCESSFUL EXECUTION), THE 4TH   *
      *  PARAMETER WILL CONTAIN ZEROS.                                *
      *                                                               *
      *---------------------------------------------------------------*
      *                                                               *
      * 18 Sep 2017 - Change from INITIAL to LOCAL-STORAGE            *
      *               This change has no affect on program execution. *
      *               It was made to accomodate a bug in GnuCOBOL 2.2 *
      *               which will be fixed, but the change makes sense *
      *               from a logical viewpoint, so thus is justified. *
      *                                                               *
      *************************************************************** *
      /
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.

       LOCAL-STORAGE SECTION.

       01  FILLER                      PIC X(24)
           VALUE 'Y2KSAGE STORAGE BEGINS->'.

      * ************************************************************* *
      * THESE ARE PROGRAM CONTROL SWITCHES AND WORK FIELDS.           *
      * ************************************************************* *
       01  PROGRAM-CONTROL-FIELDS.
           02  WORK-A                  PIC S9(7) COMP-3.

           02  GTOA-PARAMETERS.
               03  GTOAP-DATEG         PIC 9(8).
               03  GTOAP-RC            PIC 9(1).
               03  GTOAP-ANUM          PIC S9(7).

       01  FILLER                      PIC X(24)
           VALUE '<-Y2KSAGE STORAGE ENDS'.

       LINKAGE SECTION.

      * ************************************************************* *
      * THESE ARE THE FIELDS USED TO RECEIVE INPUT DATA FROM THE      *
      * CALLER AND PASS RESULT FIELDS BACK TO THE CALLER.             *
      * ************************************************************* *
       01  SAGE-PARAMETERS.
           02  SAGEP-DATE1             PIC 9(8).
           02  SAGEP-DATE2             PIC 9(8).
           02  SAGEP-RC                PIC 9(1).
           02  SAGEP-DAYS              PIC S9(7).
      /
       PROCEDURE DIVISION USING SAGE-PARAMETERS.

       0000-MAIN SECTION.
       0025-INITIALIZE.
           MOVE ZERO TO SAGEP-RC, SAGEP-DAYS.

       0050-GET-1ST-ASTRO.
           MOVE SAGEP-DATE1 TO GTOAP-DATEG.
           CALL 'Y2KGTOA' USING GTOA-PARAMETERS.

           IF GTOAP-RC NOT EQUAL ZERO
               MOVE GTOAP-RC TO SAGEP-RC
               GOBACK.

           MOVE GTOAP-ANUM TO WORK-A.


       0075-GET-2ND-ASTRO.
           MOVE SAGEP-DATE2 TO GTOAP-DATEG.
           CALL 'Y2KGTOA' USING GTOA-PARAMETERS.

           IF GTOAP-RC NOT EQUAL ZERO
               ADD 1 TO GTOAP-RC GIVING SAGEP-RC
               GOBACK.

       0100-COMPUTE-DIFFERENCE.
           SUBTRACT GTOAP-ANUM FROM WORK-A.

           IF WORK-A IS NEGATIVE
               MULTIPLY WORK-A BY -1 GIVING SAGEP-DAYS
           ELSE
               MOVE WORK-A TO SAGEP-DAYS.

           GOBACK.

       END PROGRAM Y2KSAGE.
