IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How to Validate DATE field in cobol program


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
futuredba

New User


Joined: 08 Jan 2006
Posts: 22
Location: Delhi

PostPosted: Fri Aug 24, 2007 11:15 pm
Reply with quote

Hi All,

I am expecting a field in my cobol program to have a DATE data. The field is defined with PIC X(10). The format of the DATE will be DB2 date format i.e. YYYY-MM-DD.

The logic of my program is such that sometime it will have valid date and some other time it can have low-value/spaces/zeroes or something else which is not a DATE data.

Now how can I check whether it has a valid DATE or not?
Back to top
View user's profile Send private message
ashwinreddy

Active User


Joined: 16 Sep 2004
Posts: 106
Location: Hyderabad

PostPosted: Fri Aug 24, 2007 11:36 pm
Reply with quote

Hi,

Best way to validate a date field is you would having date routine in your application, where you can pass the date and check whether the date is valid. And also to convert from one format to another.


If you want to manually validate the date, use the reference modification to validate the given date.

Use this logic

If WW-DATE (1:4) NOT = SPACES
IF WW-DATE(1:4) > '0000'
AND WW-DATE(1:4) < = '9999'
MOVE WW-DATE TO WW-VALID-DATE

ELSE
MOVE 'INVALID DATE' TO WW-ERROR

END-IF
END-IF

I think you can continue this logic

hope this helps for you

cheers
Ashwin
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Fri Aug 24, 2007 11:47 pm
Reply with quote

Yea, I have to agree with Ashwin, the old fashioned "COBOL 101" was, parse and validate the field and its components.....
Back to top
View user's profile Send private message
futuredba

New User


Joined: 08 Jan 2006
Posts: 22
Location: Delhi

PostPosted: Sat Aug 25, 2007 12:28 am
Reply with quote

Hi Ashwin,

If I have a date routine, then certainly I can pass this value to it and check whether the return-code of the routine is 0 or not. If its 0 then it is a valid date otherwise not.

But I dont have any such routine. I have to manully check it or by using any cobol verbs/functions.

The problem I see with your logic above is that it only checks whether it is a valid YEAR or not. But not anything more. It will fail if the MM and DD sub-fields of the date field are non-numeric.

Even checking the YYYY, MM and DD fields to be numeric or not will not help me. Because 2007-50-50 (for example) is not a valid date. If I check whether MM <= 12 and as per diff values of MM again if I check whether DD is 28, 30 or 31.. then also there is a loop hole... Sometimes for MM=02, the max value of DD can be 28 and some other time the max value can be 29. icon_sad.gif

I feel only some well-defined cobol verb or function can help me out! Do any one of you know about such type of function.

Ofcourse I dont want to code 100 lines to do this.

Thanks,
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Sat Aug 25, 2007 12:53 am
Reply with quote

futuredba wrote:
But I dont have any such routine. I have to manully check it or by using any cobol verbs/functions.
You have to design and code it....
Quote:
The problem I see with your logic above is that it only checks whether it is a valid YEAR or not. But not anything more. It will fail if the MM and DD sub-fields of the date field are non-numeric.
ashwinreddy wrote:
I think you can continue this logic
Did you miss this?
Quote:
Even checking the YYYY, MM and DD fields to be numeric or not will not help me. Because 2007-50-50 (for example) is not a valid date. If I check whether MM <= 12 and as per diff values of MM again if I check whether DD is 28, 30 or 31.. then also there is a loop hole... Sometimes for MM=02, the max value of DD can be 28 and some other time the max value can be 29. icon_sad.gif
That is what we (in the olden days), called validity checking.....
Quote:
I feel only some well-defined cobol verb or function can help me out! Do any one of you know about such type of function.
If you can get it close enough to a valid format, there might be a ECOBAL intrinsic or LE function that could take care of this for you, but it will be validation and interface code you will have to write...
Quote:
Ofcourse I dont want to code 100 lines to do this.
Sometime life is a beach...
How about a DB2 date function?
Quote:
Thanks
You are welcome....
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19243
Location: Inside the Matrix

PostPosted: Sat Aug 25, 2007 1:31 am
Reply with quote

Hello,

If there is no date validation routine at your location and you don't want to use DB2, you could write a callable routine that did proper date validation and in your program call your new routine.

It would take a small bit of code, but once you have it, it can be used over and over.

You might also ask people who have been there a while if there is a list of callable reotines already in use and one of them just might be a date routine. Keep in mind that if there is a date conversion routine, it should validate the date before trying to convert it. . .
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Sat Aug 25, 2007 1:49 am
Reply with quote

Copy this and modify for your own use; using 88-Levels, you can let the compiler write your code
Code:

        01  DFH014-WORK-AREA.
           05 DFH014-DATE.
              10 DFH014-YEAR             PIC 9(04).
                 88 DFH014-VALID-YEAR    VALUES 2001 THRU 9999.
              10 DFH014-YEAR-X-TYP
                 REDEFINES
                 DFH014-YEAR.
                 15  FILLER              PIC X(02).
                 15  DFH014-YEAR-JJ      PIC 9(02).
                     88  DFH014-YEAR-NOT-A-LEAP VALUES 01 03 05 07 09.
                     88  DFH014-YEAR-MUST-BE-LEAP VALUES 00 04 08.
              10 DFH014-MONTH            PIC 9(02).
                 88 MONTH-OK             VALUE 1  THRU 12.
                 88 MONTH-31             VALUE 1 3 5 7 8 10 12.
                 88 MONTH-30             VALUE 4 6 9 11.
                 88 MONTH-28-29          VALUE 2.
              10 DFH014-DAY              PIC 9(02).
                 88 DAY-31               VALUE 1  THRU 31.
                 88 DAY-30               VALUE 1  THRU 30.
                 88 DAY-29               VALUE 1  THRU 29.
                 88 DAY-28               VALUE 1  THRU 28.
      *
      *
           05 DFH014-DIVIDE-WORK.
              10 QUOTIENT                PIC S9(04).
              10 REST                    PIC S9(02).
                 88  IT-IS-A-LEAP-YEAR   VALUE ZERO.
                 88  NOT-A-LEAP-YEAR     VALUE 1 2 3.




           MOVE <8 CHAR DATE YYYYMMDD>
             TO DFH014-DATE
                                       IN DFH014-WORK-AREA
      *
           IF  DFH014-VALID-YEAR
                                       IN DFH014-YEAR
                                       IN DFH014-DATE
                                       IN DFH014-WORK-AREA
           THEN
               CONTINUE
           ELSE
               write your own error
           END-IF
      *
           IF  MONTH-OK
                                       IN DFH014-MONTH
                                       IN DFH014-DATE
                                       IN DFH014-WORK-AREA
           THEN
               CONTINUE
           ELSE
               write your own error
           END-IF
      *
      *
           IF  MONTH-28-29
                                       IN DFH014-MONTH
                                       IN DFH014-DATE
                                       IN DFH014-WORK-AREA
           THEN
      *
               EVALUATE  TRUE
      *
                   WHEN  DFH014-YEAR-NOT-A-LEAP
                                       IN DFH014-YEAR-JJ
                                       IN DFH014-YEAR-X-TYP
                                       IN DFH014-DATE
                                       IN DFH014-WORK-AREA
                         SET  NOT-A-LEAP-YEAR
                                       IN REST
                                       IN DFH014-DIVIDE-WORK
                           TO TRUE
      *
                   WHEN  DFH014-YEAR-MUST-BE-LEAP
                                       IN DFH014-YEAR-JJ
                                       IN DFH014-YEAR-X-TYP
                                       IN DFH014-DATE
                                       IN DFH014-WORK-AREA
                         SET  IT-IS-A-LEAP-YEAR
                                       IN REST
                                       IN DFH014-DIVIDE-WORK
                           TO TRUE
      *
                   WHEN  OTHER
                         DIVIDE      DFH014-YEAR
                                       IN DFH014-DATE
                                       IN DFH014-WORK-AREA
                          BY         4
                          GIVING     QUOTIENT
                          REMAINDER  REST
                         END-DIVIDE
      *
               END-EVALUATE
      *
           END-IF
      *
           EVALUATE  TRUE              ALSO     TRUE
      *
               WHEN  MONTH-31          ALSO     DAY-31
               WHEN  MONTH-30          ALSO     DAY-30
               WHEN  MONTH-28-29       ALSO     DAY-28
               WHEN  MONTH-28-29       ALSO     DAY-29
                 AND IT-IS-A-LEAP-YEAR
                     CONTINUE
      *
               WHEN  OTHER
                     write your own error
           END-EVALUATE
      *
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 631
Location: Wisconsin

PostPosted: Sat Aug 25, 2007 2:35 am
Reply with quote

88 DFH014-YEAR-MUST-BE-LEAP VALUES 00 04 08.

Just a nitpick, but 00 Isn't always a leap year, we just got lucky with 2000 because it was divisible by 400. So 2100 is not a leap year. But, I'm sure your code won't be running by that time right, just like the Y2k thing...
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Sat Aug 25, 2007 3:09 am
Reply with quote

stodolas wrote:
But, I'm sure your code won't be running by that time right, just like the Y2k thing...
icon_lol.gif
Back to top
View user's profile Send private message
socker_dad

Active User


Joined: 05 Dec 2006
Posts: 177
Location: Seattle, WA

PostPosted: Sat Aug 25, 2007 3:35 am
Reply with quote

I don't know what version of COBOL you are using or what system you are on, but I looked through the Enterprise COBOL Programmer's Guide and found an entire chapter on Processing two-digit-year dates. Granted, it's not the DB2 format, but they mention all sorts of little toys for manipulating and converting date fields:

- DATE FORMAT clause
- DATEVAL and UNDATE functions
- DATE-OF-INTEGER, DATE-TO-YYYYMMDD, DAY-OF-INTEGER,
DAY-TO-YYYYMMDD and YEAR-TO-YYYY.

I havne't had time to play with any of these, so let us know how they work.
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 631
Location: Wisconsin

PostPosted: Sat Aug 25, 2007 4:10 am
Reply with quote

He's not even trying to process yet, he is just trying to verify that he has a date and if its valid...
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Sat Aug 25, 2007 4:55 am
Reply with quote

stodolas wrote:
He's not even trying to process yet, he is just trying to verify that he has a date and if its valid...
which nothing in the Language Environment or COBOL Intrinsic Function arsenal accomplish. icon_rolleyes.gif

Few years ago, I did this, because I did not feel like validating dates with the code I posted earlier in this thread:
Code:

SELECT 1
FROM SYSIBM.SYSDUMMY1
WHERE :HOST-DATE < CURRENT_DATE;

If I got a -181 I knew the date was invalid. But, it was a DB2 application. icon_lol.gif
Back to top
View user's profile Send private message
dneufarth

Active User


Joined: 27 Apr 2005
Posts: 420
Location: Inside the SPEW (Southwest Ohio, USA)

PostPosted: Sun Aug 26, 2007 12:46 am
Reply with quote

leap year

The Gregorian Calendar Rule

- Every fourth year is a leap year.
2004, 2008, and 2012 are leap years.

- However, every hundredth year is not a leap year.
1900 and 2100 are not leap years.

- Every four hundred years, there's a leap year after all.
Back to top
View user's profile Send private message
dneufarth

Active User


Joined: 27 Apr 2005
Posts: 420
Location: Inside the SPEW (Southwest Ohio, USA)

PostPosted: Sun Aug 26, 2007 12:51 am
Reply with quote

leap year continued

years divisible by 4000 are not leap years
Back to top
View user's profile Send private message
futuredba

New User


Joined: 08 Jan 2006
Posts: 22
Location: Delhi

PostPosted: Sun Aug 26, 2007 4:44 am
Reply with quote

I am not having any Date validation routine but I can use DB2 to validate the data. Thank you all for your valuable comments and thank you very much Dick Brenholtz. I ll try to use the sample sql code given by you for validating the Date data. This is the easiest way which I can see. If -181 , it means invalid date. Hopefully it will work.

However, I feel cobol should also have something to validate Date using some func.

Thanks
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 631
Location: Wisconsin

PostPosted: Sun Aug 26, 2007 5:43 am
Reply with quote

I feel that COBOL should support variable length strings and support regular expressions. Wait in line for you date validation verb!
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Sun Aug 26, 2007 11:29 am
Reply with quote

steve,

E-COBOL does support variable length strings; the length function recognizes Null-delimited strings. UNSTRING, available in all COBOLs, will return the variable length, also.

But, I imagine you are addressing dynamic field length, which COBOL will probably never do. COBOL was not designed to work on the web.

regular expressions - DB2 does not even do that, yet.

as far as date validation is concerned, pass a date to any of the COBOL (intrinsic or LE routines) date conversion functions and if it is a bad date, you will be informed.
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 631
Location: Wisconsin

PostPosted: Sun Aug 26, 2007 7:19 pm
Reply with quote

Yeah, you got my drift. But I did find that it sort of handles dynamic length strings. If you are using XML GENERATE it puts the data elements as trimmed.
Back to top
View user's profile Send private message
arunklnce

New User


Joined: 15 Sep 2008
Posts: 2
Location: Bangalore

PostPosted: Wed Jun 10, 2009 5:57 pm
Reply with quote

Hi All,

The below code will do the leap year calcuation.

IF EDIT-YYYY(3:2) NOT= ZEROS
MOVE 4 TO WS-DIV-BY
ELSE
MOVE 400 TO WS-DIV-BY
END-IF

DIVIDE EDIT-YYYY-N
BY WS-DIV-BY
GIVING WS-DIVIDEND
REMAINDER WS-REM
END-DIVIDE

IF WS-REM = ZEROS
SET WF-LEAP-YEAR-Y TO TRUE
ELSE
SET WF-LEAP-YEAR-N TO TRUE
END-IF
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Wed Jun 10, 2009 6:09 pm
Reply with quote

Why not let Language Environment do the work?

ibmmainframes.com/viewtopic.php?p=192595&highlight=#192595

Regards,
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8700
Location: Dubuque, Iowa, USA

PostPosted: Wed Jun 10, 2009 6:13 pm
Reply with quote

Arun, you DID see that the post you're replying to was made in August 2007 -- almost 2 years ago?
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Help required to reset decimal points... DFSORT/ICETOOL 9
No new posts COBOL sorting, with input GDG base COBOL Programming 7
No new posts Need help with ADABAS query (COBOL-AD... All Other Mainframe Topics 0
No new posts Date format correction using dfsort DFSORT/ICETOOL 3
No new posts Replacing FILLER with FILLER<SeqNu... DFSORT/ICETOOL 2
Search our Forums:

Back to Top