MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Date check in cobol

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming
View previous topic :: View next topic  
Author Message
vivek1983
Intermediate


Joined: 20 Apr 2006
Posts: 222
Topics: 24

PostPosted: Thu Sep 13, 2007 1:17 am    Post subject: Date check in cobol Reply with quote

Hi,

I have a requirement in cobol wherein I need to check the correctness of a given date in a specific format.

Following are the details:

Format: YYYY-MM-DD

Examples of invalid formats:

YY-MM-DD, YYYY#MM-DD(Invalid character), YY---MM-DD, ETC.

I checked the link http://www.mvsforums.com/helpboards/viewtopic.php?t=5081&highlight=date+validation+cobol

I tried using CEEDAYS specified in the above link. But Given a valid date it shows as invalid. Also the program gets abended.

Following is an excerpt from the program:


Code:


MOVE REQUEST-DT TO WS-DATE                         
DISPLAY 'W-REQ-DATE =' WS-DATE                         
MOVE 'YYYY-MM-DD' TO WS-FORMAT                         
CALL 'CEEDAYS'  USING                                   
                PNUT-REQUEST-DT,                       
                WS-FORMAT,                             
                WS-FC-CODE                             
DISPLAY 'AFTER CALL'                                   
DISPLAY 'FC-SEVERITY==>' FC-SEVERITY                   
DISPLAY 'FC-MESSAGE===>' FC-MESSAGE                     
IF FC-SEVERITY = 00                                     
   DISPLAY 'THE DATE IS VALID'                         
ELSE                                                   
   DISPLAY '******************************************'
   DISPLAY '** DATE IS NOT GIVEN IN CORRECT FORMAT  **'
   DISPLAY '******************************************'
   MOVE 'N'  TO WS-INSERT-FLAG                         
END-IF                                                 



Following the abend message in sysout:

Code:

CEE2520S CEEDAYS detected non-numeric data in a numeric field, or the date string did not match the picture string.


I am planning to use some cobol features to check the correctness. Can you please suggest me if this can be achieved in a better way.

Thanks in advance.

Vivek G
Back to top
View user's profile Send private message
CICS Guy
Intermediate


Joined: 30 Apr 2007
Posts: 292
Topics: 3

PostPosted: Thu Sep 13, 2007 3:23 am    Post subject: Reply with quote

shankar.v wrote:
Please check with the following cobol code for your requirement.
Input should be supplied via SYSIN DD with date format YYYY-MM-DD and (- is used as an separator, u can use any other character for separator)
Code:
       IDENTIFICATION DIVISION.             
       PROGRAM-ID. DATECHK.                 
       DATA DIVISION.                       
       WORKING-STORAGE SECTION.             
       01 WS-DATE PIC X(10).                 
       01 WS-TEMP-DATE REDEFINES WS-DATE.   
        02 WS-YEAR PIC 9(4).                 
        02 FILLER PIC X.                     
        02 WF-VALID-MONTH PIC 9(2).         
         88 JAN VALUE 01.                   
         88 FEB VALUE 02.                   
         88 MAR VALUE 03.                   
         88 APR VALUE 04.                   
         88 MAY VALUE 05.                   
         88 JUN VALUE 06.                   
         88 JUL VALUE 07.                   
         88 AUG VALUE 08.                   
         88 SEP VALUE 09.                   
         88 SEP VALUE 09.                                           
         88 OCT VALUE 10.                                           
         88 NOV VALUE 11.                                           
         88 DEC VALUE 12.                                           
         88 VALID-MONTH VALUE 01 THRU 12.                           
         88 NOT-VALID-MONTH VALUE 13 THRU 99.                       
        02 FILLER PIC X.                                           
        02 WS-DATE-PART PIC 99.                                     
        77 WS-QUOTIENT PIC 9(4).                                   
        77 WF-ZERO PIC 9.                                           
         88 ZERO-YES VALUE 0.                                       
         88 ZERO-NO VALUE 1 THRU 3.                                 
       PROCEDURE DIVISION.                                         
           ACCEPT WS-DATE                                           
           DISPLAY WS-DATE                                         
           DIVIDE WS-YEAR BY 4 GIVING WS-QUOTIENT REMAINDER WF-ZERO
           END-DIVIDE                                               
           IF VALID-MONTH                                           
            IF JAN AND WS-DATE-PART <= 31 OR                     
               FEB AND ZERO-YES AND WS-DATE-PART <= 29 OR       
               FEB AND ZERO-NO AND WS-DATE-PART <= 28 OR         
               MAR AND WS-DATE-PART <= 31 OR                     
               APR AND WS-DATE-PART <= 30 OR                     
               MAY AND WS-DATE-PART <= 31 OR                     
               JUN AND WS-DATE-PART <= 30 OR                     
               JUL AND WS-DATE-PART <= 31 OR                     
               AUG AND WS-DATE-PART <= 31 OR                     
               SEP AND WS-DATE-PART <= 30 OR                     
               OCT AND WS-DATE-PART <= 31 OR                     
               NOV AND WS-DATE-PART <= 30 OR                     
               DEC AND WS-DATE-PART <= 31                       
             DISPLAY 'VALID DATE'                               
             GOBACK                                             
            ELSE                                                 
             DISPLAY 'NOT VALID DATE'                           
             GOBACK                                           
            END-IF                                           
           ELSE                                               
            DISPLAY 'NOT VALID DATE'                         
            GOBACK                                           
           END-IF.
Back to top
View user's profile Send private message
vivek1983
Intermediate


Joined: 20 Apr 2006
Posts: 222
Topics: 24

PostPosted: Thu Sep 13, 2007 4:30 am    Post subject: Reply with quote

CICS Guy,

Thank you. Its working fine.

Vivek G
_________________
Vivek G
--------------------------------------
A dream is just a dream. A goal is a dream with a plan and a deadline. (Harvey Mackay)
Back to top
View user's profile Send private message
Terry_Heinze
Supermod


Joined: 31 May 2004
Posts: 391
Topics: 4
Location: Richfield, MN, USA

PostPosted: Thu Sep 13, 2007 8:59 am    Post subject: Reply with quote

I think the DIVIDE statement will S0C7 if the 1st 4 characters of WS-DATE are non-numeric. The same would be true for month and day. You might want to check for numerics 1st.
_________________
....Terry
Back to top
View user's profile Send private message Send e-mail
shankar.v
Beginner


Joined: 04 Sep 2007
Posts: 4
Topics: 0

PostPosted: Thu Sep 13, 2007 10:51 am    Post subject: Reply with quote

vivek1983,

The above posted cobol code was wrong with the leap year logic. Please check with the following cobol code which is the updated one.

Following is the one i got from wikipedia to check for leap year.
Code:
if year modulo 400 is 0 then leap
 else if year modulo 100 is 0 then no_leap
 else if year modulo 4 is 0 then leap
 else no_leap

Code:
       IDENTIFICATION DIVISION.
       PROGRAM-ID. DATECHK.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-DATE PIC X(10).
       01 WS-TEMP-DATE REDEFINES WS-DATE.
        02 WS-YEAR PIC 9(4).
        02 FILLER PIC X.
        02 WF-VALID-MONTH PIC 9(2).
         88 JAN VALUE 01.
         88 FEB VALUE 02.
         88 MAR VALUE 03.
         88 APR VALUE 04.
         88 MAY VALUE 05.
         88 JUN VALUE 06.
         88 JUL VALUE 07.
         88 AUG VALUE 08.
         88 SEP VALUE 09.
         88 OCT VALUE 10.
         88 NOV VALUE 11.
         88 DEC VALUE 12.
         88 VALID-MONTH VALUE 01 THRU 12.
         88 NOT-VALID-MONTH VALUE 13 THRU 99.
        02 FILLER PIC X.
        02 WS-DATE-PART PIC 99.
        77 WS-QUOTIENT PIC 9(4).
        77 WF-ZERO PIC 999.
         88 ZERO-YES VALUE 0.
         88 ZERO-NO VALUE 1 THRU 399.
        77 WF-LEAP PIC 9.
         88 LEAP-YES VALUE 0.
         88 LEAP-NO VALUE 1.
       PROCEDURE DIVISION.
           ACCEPT WS-DATE
           DISPLAY WS-DATE
           IF WS-YEAR NOT = ZERO
            DIVIDE WS-YEAR BY 400 GIVING WS-QUOTIENT REMAINDER WF-ZERO
            END-DIVIDE
            IF ZERO-YES
             SET LEAP-YES TO TRUE
            ELSE
             DIVIDE WS-YEAR BY 100 GIVING WS-QUOTIENT REMAINDER WF-ZERO
             END-DIVIDE
             IF ZERO-YES
              SET LEAP-NO TO TRUE
             ELSE
              DIVIDE WS-YEAR BY 4 GIVING WS-QUOTIENT REMAINDER WF-ZERO
              END-DIVIDE
              IF ZERO-YES
               SET LEAP-YES TO TRUE
              ELSE
               SET LEAP-NO TO TRUE
              END-IF
             END-IF
            END-IF
            IF VALID-MONTH
             IF JAN AND WS-DATE-PART <= 31 OR
                FEB AND LEAP-YES AND WS-DATE-PART <= 29 OR
                FEB AND LEAP-NO AND WS-DATE-PART <= 28 OR
                MAR AND WS-DATE-PART <= 31 OR
                APR AND WS-DATE-PART <= 30 OR
                MAY AND WS-DATE-PART <= 31 OR
                JUN AND WS-DATE-PART <= 30 OR
                JUL AND WS-DATE-PART <= 31 OR
                AUG AND WS-DATE-PART <= 31 OR
                SEP AND WS-DATE-PART <= 30 OR
                OCT AND WS-DATE-PART <= 31 OR
                NOV AND WS-DATE-PART <= 30 OR
                DEC AND WS-DATE-PART <= 31
              DISPLAY 'VALID DATE'
              GOBACK
             ELSE
              DISPLAY 'NOT VALID DATE'
              GOBACK
             END-IF
            ELSE
             DISPLAY 'NOT VALID DATE'
             GOBACK
            END-IF
           ELSE
            DISPLAY 'NOT VALID DATE'
            GOBACK
           END-IF.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Thu Sep 13, 2007 10:58 am    Post subject: Reply with quote

shankar.v,

Run your code with the following date

Code:

    -01-01


and let me know what happens. I gave the year as spaces

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
shankar.v
Beginner


Joined: 04 Sep 2007
Posts: 4
Topics: 0

PostPosted: Thu Sep 13, 2007 11:15 am    Post subject: Reply with quote

kolusu,
That code will works fine for input numeric values of YYYY,MM,DD with
format YYYY-MM-DD. If you want to check for alphabets, special characters, spaces... for YYYY,MM,DD, i will modify the code and post it once I completed.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Thu Sep 13, 2007 11:19 am    Post subject: Reply with quote

shankar.v,

The Op's request is to error out invalid dates. It can be any format in 10 bytes. You need to validate to YYYY-MM-DD format.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Thu Sep 13, 2007 12:26 pm    Post subject: Reply with quote

This has fewer lines of code and handles the non-numerics:

Code:

01  INFLE-WORK-REC.                         
  03  INFLE-DATE     PIC X(10).             
                                             
01  WS-VARS.                                 
  03  WS-VALID-DATE-IND   PIC X.             
    88  VALID-DATE            VALUE 'Y'.     
    88  INVALID-DATE          VALUE 'N'.     

  03  WS-DATE.                               
    05  WS-DATE-CCYY  PIC 9(04).             
    05  WS-DATE-DASH1 PIC X(01).             
    05  WS-DATE-MM    PIC 9(02).             
    05  WS-DATE-DASH2 PIC X(01).             
    05  WS-DATE-DD    PIC 9(02).           
 
01  WS-DAYS-IN-MONTH-AREA.                     
  03  WS-DAYS-IN-MONTH-VALS.                   
    05  JAN-DAYS      PIC X(02) VALUE '31'.     
    05  FEB-DAYS      PIC X(02) VALUE '28'.     
    05  MAR-DAYS      PIC X(02) VALUE '31'.     
    05  APR-DAYS      PIC X(02) VALUE '30'.     
    05  MAY-DAYS      PIC X(02) VALUE '31'.     
    05  JUN-DAYS      PIC X(02) VALUE '30'.     
    05  JUL-DAYS      PIC X(02) VALUE '31'.     
    05  AUG-DAYS      PIC X(02) VALUE '31'.     
    05  SEP-DAYS      PIC X(02) VALUE '30'.     
    05  OCT-DAYS      PIC X(02) VALUE '31'.     
    05  NOV-DAYS      PIC X(02) VALUE '30'.     
    05  DEC-DAYS      PIC X(02) VALUE '31'.     
  03  WS-DAYS-IN-MONTH-TBL REDEFINES WS-DAYS-IN-MONTH-VALS.
    05  WS-DAYS-IN-MONTH   OCCURS 12 TIMES                 
                           INDEXED BY DIM-NDX             
                           PIC 9(02).                     


0100-VALIDATE-DATE.                                               
                                                                 
    MOVE INFLE-DATE TO WS-DATE                                   
    IF  WS-DATE-CCYY  NOT NUMERIC                                 
    OR  WS-DATE-DASH1 NOT = '-'                                   
    OR  WS-DATE-MM    NOT NUMERIC                                 
    OR  WS-DATE-DASH2 NOT = '-'                                   
    OR  WS-DATE-DD    NOT NUMERIC                                 
        SET INVALID-DATE TO TRUE                                 
    ELSE                                                         
        SET DIM-NDX TO WS-DATE-MM                                 
        IF  WS-DATE-DD >= 1                                       
        AND WS-DATE-DD <= WS-DAYS-IN-MONTH(DIM-NDX)               
            SET VALID-DATE TO TRUE                               
        ELSE                                                     
            IF WS-DATE-MM = 2 AND WS-DATE-DD = 29                 
                EVALUATE TRUE                                     
                    WHEN FUNCTION MOD (WS-DATE-CCYY , 400) = 0   
                         SET VALID-DATE TO TRUE                   
                    WHEN FUNCTION MOD (WS-DATE-CCYY , 100) = 0   
                         SET INVALID-DATE TO TRUE                 
                    WHEN FUNCTION MOD (WS-DATE-CCYY , 4)   = 0   
                         SET VALID-DATE TO TRUE                   
                    WHEN OTHER                                   
                         SET INVALID-DATE TO TRUE                 
                END-EVALUATE                                     
            ELSE                                                 
                SET INVALID-DATE TO TRUE               
            END-IF                                     
        END-IF                                         
    END-IF                                             
    .                                                   
0100-EXIT. EXIT.                                       
Back to top
View user's profile Send private message
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Thu Sep 13, 2007 12:33 pm    Post subject: Reply with quote

I know. I know. I left out the check for MM between 1 and 12. Embarassed Add the MM checks to the IF so it looks like this:

Code:

    IF  WS-DATE-CCYY  NOT NUMERIC     
    OR  WS-DATE-DASH1 NOT = '-'       
    OR  WS-DATE-MM    NOT NUMERIC     
    OR  WS-DATE-DASH2 NOT = '-'       
    OR  WS-DATE-DD    NOT NUMERIC     
    OR  WS-DATE-MM    < 1             
    OR  WS-DATE-MM    > 12           
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Thu Sep 13, 2007 1:30 pm    Post subject: Reply with quote

Quote:

EVALUATE TRUE
WHEN FUNCTION MOD (WS-DATE-CCYY , 400) = 0
SET VALID-DATE TO TRUE
WHEN FUNCTION MOD (WS-DATE-CCYY , 100) = 0
SET INVALID-DATE TO TRUE
WHEN FUNCTION MOD (WS-DATE-CCYY , 4) = 0
SET VALID-DATE TO TRUE
WHEN OTHER
SET INVALID-DATE TO TRUE
END-EVALUATE

jsharon1248,

run your code with the date as '2004-02-29'(valid leap year date) and you would still set it as invalid date Very Happy Very Happy Very Happy

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Thu Sep 13, 2007 1:56 pm    Post subject: Reply with quote

Kolusu

I used '2004-02-29' as a test case and it worked. Here's the PERFORM and DISPLAY where I'm reading dates from a file and just showing whether the indicator was set to VALID or INVALID:
Code:

    PERFORM  UNTIL  INFLE-EOF                         
        PERFORM 0100-VALIDATE-DATE                   
           THRU 0100-EXIT                             
        IF VALID-DATE                                 
            DISPLAY WS-DATE ': VALID'                 
        ELSE                                         
            DISPLAY WS-DATE ': INVALID'               
        END-IF                                       
        PERFORM F1000-READ-INFLE-FILE                 
           THRU F1000-EXIT                           
    END-PERFORM                                       


Here's the results:

Code:

2000-02-00: INVALID   
2000-02-01: VALID     
2000-02-15: VALID     
2000-02-28: VALID     
2000-02-29: VALID     
2000-02-30: INVALID   
1899-02-29: INVALID   
1900-02-29: INVALID   
1996-02-29: VALID     
1997-02-29: INVALID   
1998-02-29: INVALID   
1999-02-29: INVALID   
2000-02-29: VALID     
2001-02-29: INVALID   
2002-02-29: INVALID   
2003-02-29: INVALID   
2004-02-29: VALID     


As far as I can tell, it's producing the correct results.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Thu Sep 13, 2007 2:19 pm    Post subject: Reply with quote

jsharon1248,

sorry I copied your code I had WHEN statement in the wrong order when checking for leap year

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Thu Sep 13, 2007 2:45 pm    Post subject: Reply with quote

Twisted Evil Cut and paste demons at it again. Twisted Evil

I live in a glass house too, so you won't catch me giving you any grief... Wink
Back to top
View user's profile Send private message
vivek1983
Intermediate


Joined: 20 Apr 2006
Posts: 222
Topics: 24

PostPosted: Fri Sep 14, 2007 2:13 am    Post subject: Reply with quote

Hi guys,

Thank you all for your help and support. Am proud to be a member of this forum!
Have incorporated all the checks and its working fine.

Cheers,
Vivek G
_________________
Vivek G
--------------------------------------
A dream is just a dream. A goal is a dream with a plan and a deadline. (Harvey Mackay)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group