Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

How to Validate DATE field in cobol program
Goto page 1, 2  Next
 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
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    Post subject: How to Validate DATE field in cobol program
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    Post subject:
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: 2150
Location: At my coffee table

PostPosted: Fri Aug 24, 2007 11:47 pm    Post subject:
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    Post subject:
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: 2150
Location: At my coffee table

PostPosted: Sat Aug 25, 2007 12:53 am    Post subject:
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

Site Director


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

PostPosted: Sat Aug 25, 2007 1:31 am    Post subject:
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    Post subject:
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: 632
Location: Wisconsin

PostPosted: Sat Aug 25, 2007 2:35 am    Post subject:
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: 2150
Location: At my coffee table

PostPosted: Sat Aug 25, 2007 3:09 am    Post subject:
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: 147
Location: Columbia, MO

PostPosted: Sat Aug 25, 2007 3:35 am    Post subject: Reply to: How to Validate DATE field in cobol program
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: 632
Location: Wisconsin

PostPosted: Sat Aug 25, 2007 4:10 am    Post subject:
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    Post subject:
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: 236
Location: Cincinnati OH USA

PostPosted: Sun Aug 26, 2007 12:46 am    Post subject:
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: 236
Location: Cincinnati OH USA

PostPosted: Sun Aug 26, 2007 12:51 am    Post subject:
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    Post subject: Reply to: How to Validate DATE field in cobol program
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: 632
Location: Wisconsin

PostPosted: Sun Aug 26, 2007 5:43 am    Post subject:
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    Post subject: Reply to: How to Validate DATE field in cobol program
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: 632
Location: Wisconsin

PostPosted: Sun Aug 26, 2007 7:19 pm    Post subject:
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    Post subject: Reply to: How to Validate DATE field in cobol program
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: 2502
Location: Atlanta, Georgia, USA

PostPosted: Wed Jun 10, 2009 6:09 pm    Post subject: Reply to: How to Validate DATE field in cobol program
Reply with quote

Why not let Language Environment do the work?

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

Regards,
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts I can not compile my program PL1 V3.R... Miguel Fernandez PL/I & Assembler 2 Tue Dec 06, 2016 8:30 pm
No new posts Performing arithmetic on input field zh_lad DFSORT/ICETOOL 1 Tue Dec 06, 2016 8:04 pm
No new posts Add PD field from 2nd file to PD in 1st Sushant Garje DFSORT/ICETOOL 6 Thu Dec 01, 2016 4:32 pm
No new posts IMS BMP program causes 878 system abend Artemk IMS DB/DC 7 Tue Nov 22, 2016 8:26 pm
This topic is locked: you cannot edit posts or make replies. RANDOM Function in COBOL swapnil781 COBOL Programming 2 Tue Nov 15, 2016 6:17 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us