Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DATE VALIDATION IN COBOL

Status
Not open for further replies.

caslan

MIS
May 18, 2001
3
US
I'M CURRENTLY WORKING ON AN INTERACTIVE PROGRAM PROJECT AND I WAS WANTING TO USE A COBOL FUNCTION TO TEST IF A DATE WAS A VALID DATE. IS THERE ANY FUNCTION THAT ALLOWS THIS? MY SEARCH THUS FAR HAS BEEN FRUITLESS.
 
caslan,

first, please turn off your caps lock.
Second, there is no COBOL function that i know of that validates a date, other then maybe those added to the standard by a specific compiler manufacturer.
In my case, a separate utiliy program is used to perform date functions, which can be called from any COBOL program. If nothing like this is available already it will have to be constructed.

Regards,
Ronald.
 
Mean and dirty:

use DB2 to validate

M.

(It wasn't me who said that!) BigMag, The Netherlands.
someone@euronet.nl (no kidding!)
 
The COBOL standard doesnt really have an intrinsic function that specifically tests the validity of a date. There may be some compilers that have added functionality that can do that.

There are some Intrinsic functions that do things like convert a date to a julian. It might be possible to build a program that can be called to test dates or use DB2 to test dates if you can use that. This is really a sorely needed function. Probably the only reason COBOL doesnt have one built in, is that mission critical data is usually in a database that has that capability.

This doesnt help you much if you can't afford a big database package. If you do not like my post feel free to point out your opinion or my errors.
 
Let me add to the other answers here. Both shops that I have worked at, my present shop and my previous shop, have had utility programs to call for checking date validity. I believe that such utility programs are commercially available.

However, as RonaldB said, you could construct your own utility program.

In order to construct your own program, first you need to figure out what date formats you will be using.

Will it be MM/DD/YYYY or else MM/DD/YY (with all digits).

Or it could be MONTH (spelled out) and then your date, then your year (either 2 digits or 4 digits). Example: "December 25, 2001"

Or else there is the Julian date format, which is formatted as CCYYDDD i.e. "1999364" which would be the year 1999 and the 364th day in that year -- which would be Dec. 30, 1999. This format is frequently used for elapsed time applications, as the dates can be directly subtracted from each other.

The utility program at my previous shop was written in-house. And first, the programmer would specify the format, and then the date would be passed. This program not only validated dates but would convert dates from one format to another.

After you decide on the format of the date to be validated, then each program must make sure that each date to be validated is in the proper format. Then code the program call so that the date will be passed to the utility program which you have constructed.

As far as constructing a utility program, I have a few ideas. The parameter of MONTHS could be specified as a SPECIAL-NAMES set of data, then code in the 12 months in whatever format you are using. Call this special name 'VALID'. Later on, the programmer can test the month against this constructed data-type i.e. IF TEST-MONTH VALID CONTINUE ELSE [do error routine].

The dates and years wouldn't need a SPECIAL-NAMES set, you could compare the passed data to the valid range of numerical values for either the day or the year.

Hope this helps, Nina Too

 
We made a program in JAVA in which we had to make our own date object from scratch and validate it. We used rule-based design techniques, to cover every possible combination. It wasnt too hard, but takes careful thought and a logical approach. Java has date objects built in, but we were suppose to do it the hard way and document all of the rules, because it was a software design class.

We also looked at the three inmates and 5 hats game. That gets pretty interesting. Maybe I will see if I have that at home. If you do not like my post feel free to point out your opinion or my errors.
 
I do not know of a COBOL or intrinsic function that provides a date validation routine. I have written callable programs that will validate, format and convert various dates. The following URL is more than what you were requesting but might be helpful.


You can view the example on-line and then download the source code for free and it contains reasonable documentation.

Good Luck,

Saginaw
helpdesk@simotime.com
 
You can use intrinsic date function to test for a leap year which of course would only affect february. Take the Mar 1st date for the giving year, call to convert to integer date, subtract 1 and call to convert back. You will get either 29 or 28.
 
I meant to include this example:
IDENTIFICATION DIVISION.
PROGRAM-ID. LEAPYEAR.
AUTHOR. Clive Cummins.
INSTALLATION. DATE-WRITTEN. Nov 21, 2001.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WORK-AREAS.
05 YEAR-IN-QUESTION PIC X(4).
05 LEAP-YEAR-SW PIC X(1).
88 LEAP-YEAR VALUE "Y".
05 WORK-DATE.
10 WORK-DATE-CCYY PIC 9(4).
10 WORK-DATE-MM PIC 9(2).
10 WORK-DATE-DD PIC 9(2).
05 WORK-DATE-NUM PIC 9(8).
05 WORK-DATE-INTEGER PIC 9(8).
PROCEDURE DIVISION.
ACCEPT YEAR-IN-QUESTION FROM COMMAND-LINE.
IF YEAR-IN-QUESTION NUMERIC
PERFORM CHECK-LEAP-YEAR
ELSE
DISPLAY "NON NUMERIC YEAR WAS INPUT".
GOBACK.
CHECK-LEAP-YEAR.
MOVE YEAR-IN-QUESTION TO WORK-DATE-CCYY.
MOVE 3 TO WORK-DATE-MM.
MOVE 1 TO WORK-DATE-DD.
MOVE WORK-DATE TO WORK-DATE-NUM.
MOVE FUNCTION INTEGER-OF-DATE (WORK-DATE-NUM)
TO WORK-DATE-INTEGER.
SUBTRACT 1 FROM WORK-DATE-INTEGER
MOVE FUNCTION DATE-OF-INTEGER (WORK-DATE-INTEGER)
TO WORK-DATE-NUM.
MOVE WORK-DATE-NUM TO WORK-DATE.
IF WORK-DATE-DD EQUAL 29
MOVE "Y" TO LEAP-YEAR-SW
ELSE
MOVE "N" TO LEAP-YEAR-SW.
EXHIBIT NAMED LEAP-YEAR-SW.
 
Hi Clive,

Just curious; you use intrinsic functions, an LE COBOL feature, then end the code w/EXHIBIT which was dropped in COBOLII (I don't know the non-IBM M/F equiv versions).

Was this a misprint or is EXHIBIT still supported in your version of COBOL?

Thanx, Jack.
 
Hi Caslan,

The key to verifying a date is the "30 days hath September.." part. That and the leap year calc.

What follows is what I use to do that.

Once you validate the month, you can use it as an index (subscript) into the table shown below. Yuo then use the table value to verify the DD portion of your date.

Regards, Jack.

Code:
           05  WS-DST-START-LONG.
               10  WDS-CCYY.
               20  WDS-CC                     PIC 9(02) VALUE 19.
               20  WDS-YY                     PIC 9(02).
               10  WDS-CCYY-NUM REDEFINES
                   WDS-CCYY                             PIC S9(04).
               10  WDS-MM                     PIC 9(02) VALUE 04.
               10  WDS-DD                     PIC 9(02) VALUE 01.
           05  FILLER      REDEFINES
               WS-DST-START-LONG.
               10  FILLER                               PIC XX.
               10  WS-DST-START                         PIC X(08).

           05  WS-LEAP-YR-IND             PIC  X(01).
               88  ITS-A-LEAP-YR                     VALUE 'L'.
               88  ITS-NOT-A-LEAP-YR                 VALUE 'N'.

           05  WS-REMAINDERS.
               10  WR-REMAINDER-4         PIC S9(01) COMP.
               88  DIVISIBLE-BY-4                    VALUE +0.
               10  WR-REMAINDER-100       PIC S9(02) COMP.
               88  DIVISIBLE-BY-100                  VALUE +0.
               10  WR-REMAINDER-400       PIC S9(03) COMP.
               88  DIVISIBLE-BY-400                  VALUE +0.

           05  WS-WORK-DATE.
               10  WS-MONTH                   PIC XX      VALUE SPACES.
               10  FILLER                     PIC X       VALUE '/'.
               10  WS-DAY                     PIC XX      VALUE SPACES.
               10  FILLER                     PIC X       VALUE '/'.
               10  WS-YEAR                    PIC XX      VALUE SPACES.
           05  WS-IT-DATE.
               10  WS-YY                      PIC S99     VALUE 0.
               10  WS-MM                      PIC S99     VALUE 0.
               10  WS-DD                      PIC S99     VALUE 0.
1     /
      *===============================================================
      *==================    END OF MOMTH TABLE    ===================
      *===============================================================
      *    NOTE: THIS IS A GENERALIZED TABLE TO BE USED FOR DATE     *
      *          MANIPULATION. EACH ENTRY CONTAINS THE NAME OF THE   *
      *          MONTH AND THE NUMBER OF DAYS IN EACH MONTH AND ITS  *
      *          IMMEDIATE NEIGHBORS. THE JAN-PREV-EOM FIELD CON-    *
      *          TAINS THE NUMBER OF DAYS IN THE PREVIOUS DECEMBER.  *
      *          THE DEC-NEXT-EOM FIELD CONTAINS THE NUMBER OF DAYS  *
      *          IN THE NEXT JANUARY.                                *
      *
      *          IN LEAP YEARS THE USER MUST INCREMENT THE FOLLOWING *
      *          FIELDS BY ONE:                                      *
      *                         JAN-NEXT-EOM-VAL                     *
      *                         FEB-EOM-VAL                          *
      *                         MAR-PREV-EOM-VAL                     *
      *===============================================================

       01  END-OF-MONTH-VALUES.
      *========================
           05  JAN-VALUES.
               10  JAN-NAME                PIC X(009) VALUE
               'JANUARY'.
               10  JAN-EOM-VAL             PIC 9(002) VALUE 31.
               10  JAN-PREV-EOM-VAL        PIC 9(002) VALUE 31.
               10  JAN-NEXT-EOM-VAL        PIC 9(002) VALUE 28.
           05  FEB-VALUES.
               10  FEB-NAME                PIC X(009) VALUE
               'FEBRUARY'.
               10  FEB-EOM-VAL             PIC 9(002) VALUE 28.
               10  FEB-PREV-EOM-VAL        PIC 9(002) VALUE 31.
               10  FEB-NEXT-EOM-VAL        PIC 9(002) VALUE 31.
           05  MAR-VALUES.
               10  MAR-NAME                PIC X(009) VALUE
               'MARCH'.
               10  MAR-EOM-VAL             PIC 9(002) VALUE 31.
               10  MAR-PREV-EOM-VAL        PIC 9(002) VALUE 28.
               10  MAR-NEXT-EOM-VAL        PIC 9(002) VALUE 30.
           05  APR-VALUES.
               10  APR-NAME                PIC X(009) VALUE
               'APRIL'.
               10  APR-EOM-VAL             PIC 9(002) VALUE 30.
               10  APR-PREV-EOM-VAL        PIC 9(002) VALUE 31.
               10  APR-NEXT-EOM-VAL        PIC 9(002) VALUE 31.
           05  MAY-VALUES.
               10  MAY-NAME                PIC X(009) VALUE
               'MAY'.
               10  MAY-EOM-VAL             PIC 9(002) VALUE 31.
               10  MAY-PREV-EOM-VAL        PIC 9(002) VALUE 30.
               10  MAY-NEXT-EOM-VAL        PIC 9(002) VALUE 30.
           05  JUN-VALUES.
               10  JUN-NAME                PIC X(009) VALUE
               'JUNE'.
               10  JUN-EOM-VAL             PIC 9(002) VALUE 30.
               10  JUN-PREV-EOM-VAL        PIC 9(002) VALUE 31.
               10  JUN-NEXT-EOM-VAL        PIC 9(002) VALUE 31.
           05  JUL-VALUES.
               10  JUL-NAME                PIC X(009) VALUE
               'JULY'.
               10  JUL-EOM-VAL             PIC 9(002) VALUE 31.
               10  JUL-PREV-EOM-VAL        PIC 9(002) VALUE 30.
               10  JUL-NEXT-EOM-VAL        PIC 9(002) VALUE 31.
           05  AUG-VALUES.
               10  AUG-NAME                PIC X(009) VALUE
               'AUGUST'.
               10  AUG-EOM-VAL             PIC 9(002) VALUE 31.
               10  AUG-PREV-EOM-VAL        PIC 9(002) VALUE 31.
               10  AUG-NEXT-EOM-VAL        PIC 9(002) VALUE 30.
           05  SEP-VALUES.
               10  SEP-NAME                PIC X(009) VALUE
               'SEPTEMBER'.
               10  SEP-EOM-VAL             PIC 9(002) VALUE 30.
               10  SEP-PREV-EOM-VAL        PIC 9(002) VALUE 31.
               10  SEP-NEXT-EOM-VAL        PIC 9(002) VALUE 31.
           05  OCT-VALUES.
               10  OCT-NAME                PIC X(009) VALUE
               'OCTOBER'.
               10  OCT-EOM-VAL             PIC 9(002) VALUE 31.
               10  OCT-PREV-EOM-VAL        PIC 9(002) VALUE 30.
               10  OCT-NEXT-EOM-VAL        PIC 9(002) VALUE 30.
           05  NOV-VALUES.
               10  NOV-NAME                PIC X(009) VALUE
               'NOVEMBER'.
               10  NOV-EOM-VAL             PIC 9(002) VALUE 30.
               10  NOV-PREV-EOM-VAL        PIC 9(002) VALUE 31.
               10  NOV-NEXT-EOM-VAL        PIC 9(002) VALUE 31.
           05  DEC-VALUES.
               10  DEC-NAME                PIC X(009) VALUE
               'DECEMBER'.
               10  DEC-EOM-VAL             PIC 9(002) VALUE 31.
               10  DEC-PREV-EOM-VAL        PIC 9(002) VALUE 30.
               10  DEC-NEXT-EOM-VAL        PIC 9(002) VALUE 31.

       01  EOM-TABLE REDEFINES
           END-OF-MONTH-VALUES.
           05  ET-MONTHLY-ENTRY     OCCURS 012  TIMES.
               10  ET-MONTH-NAME           PIC X(009).
               10  ET-EOM                  PIC 9(002).
               10  ET-PREV-EOM             PIC 9(002).
               10  ET-NEXT-EOM             PIC 9(002).

      ******************************************************************
      *  DETERMINE IF ITS A LEAP YEAR
      ******************************************************************
           SET     ITS-NOT-A-LEAP-YR    TO  TRUE
           DIVIDE  WDS-CCYY-NUM         BY  +4
           GIVING  WS-BIT-BUCKET
           REMAINDER WS-REMAINDER-4

           DIVIDE  WDS-CCYY-NUM         BY  +100
           GIVING  WS-BIT-BUCKET
           REMAINDER WS-REMAINDER-100

           DIVIDE  WDS-CCYY-NUM         BY  +400
           GIVING  WS-BIT-BUCKET
           REMAINDER WS-REMAINDER-400

           IF DIVISIBLE-BY-400
              OR
              (DIVISIBLE-BY-4 AND NOT DIVISIBLE-BY-100)
              SET ITS-A-LEAP-YR    TO TRUE
           END-IF
      ******************************************************************
      *  IF LEAP YR ADJUST YTD AND CALENDAR BY ONE.
      ******************************************************************
           IF      ITS-A-LEAP-YR
           ADD          +1              TO  WS-DAYS-TO-APR1
                                            WS-DAYS-TO-OCT31
                                            ET-NEXT-EOM(1)
                                            ET-EOM(2)
                                            ET-PREV-EOM(3)
           END-IF
           .
 
In CA-REALIA COBOL old and new statements are supported if you use the REALIA dialect options.

examples:

DIALECT-REALIA3
DIALECT-REALIA4
DIALECT-REALIA
DIALECT-REALIA-74
DIALECT-ANSI85
DIALECT-ANS
DIALECT-OSVS
DIALECT-VS2
DIALECT-SAA
DIALECT-REALIA4-74
DIALECT-ANSI85M
DIALECT-ANSI85I
DIALECT-ANSI74HI HIGH INTERMEDIATE LEVEL COBOL AS THE BASIS
DIALECT-ANSI74LI
DIALECT-ANSI74L

So in DIALECT-REALIA which is default, EXHIBIT NAMED is still supported.

Regards,

Crox
 
I was using Micro Focus COBOL. If you dont' specify a directive file you could probably write a letter and get it to compile.

OT. I have just joined this forum and I find that some posts containing code are unreadable. Is there something I could change to make them readable
 
Hi I am astudent of MIS so I have to take COBOL. I have to do a program for validation of department and also the displaying of month from from the tables. I can't figure it out can anyone help me.
 
upset wrote:
-----------------------
Hi I am astudent of MIS so I have to take COBOL. I have to do a program for validation of department and also the displaying of month from from the tables. I can't figure it out can anyone help me
-----------------------

Can you be more specific? What kinds of tables, do they come from files, or DB2 or some other database? Is this a batch or online program? Are you supposed to write a report?

Hmmm.... So you "have" to take COBOL. You know, I said that myself many years ago in school.

And now I'm making some pretty good bucks in the COBOL field, and the language is incredibly flexible. You can do anything with it, including designing and running web sites.

When I lost my job last year, I didn't have to do a whole lot to get a new one. Just posted my resume on a couple of web sites and voila! Loads of calls. Didn't have to worry about age discrimination, sex discrimination or any of that. All they were worried about was do I know how to code in COBOL? So now I have a new, better job with a government agency.

Nina Too
 
Hi Clive,

Just noticed your OT. I have the Emoticons/Smileys and Process TGML buttons checked and haven't experienced that problem. Maybe that's you answer, else maybe someone else will help.

Regards, Jack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top