Convert 10 bytes DATE to integer



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Convert 10 bytes DATE to integer

Postby Manjucan » Thu Oct 31, 2019 5:49 pm

Hi

Suppose I have a date field declared as x(10).
I need to perform a manipulation on this date and for that purpose I need to convert it to integer.
I tried the INTEGER-OF-DATE function for this date.
But I am getting error saying the parameter for the function is invalid .
I doubt the issue is because the date I am passing as input is of the format YYYY-MM-DD but the function expects input as YYYYMMDD.
So how do I handle this ?
Manjucan
 
Posts: 8
Joined: Thu Feb 07, 2019 10:09 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Convert 10 bytes DATE to integer

 

Re: Convert 10 bytes DATE to integer

Postby prino » Thu Oct 31, 2019 6:21 pm

One option is to use REDEFINE and MOVE CORRESPONDING, or whatever the "BY NAME" option is called in COBOL.
Robert AH Prins
robert.ah.prins @ the.17+Gb.Google thingy
User avatar
prino
 
Posts: 591
Joined: Wed Mar 11, 2009 12:22 am
Location: Vilnius, Lithuania
Has thanked: 3 times
Been thanked: 26 times

Re: Convert 10 bytes DATE to integer

Postby Robert Sample » Thu Oct 31, 2019 8:46 pm

There are, of course, multiple ways to handle this. Two of them are:
      WORKING-STORAGE SECTION.                                                
       01  WS-VARS.                                                            
           05  WS-SOURCE               PIC X(10) VALUE '2019-10-31'.            
           05  WS-SOURCE-R             REDEFINES WS-SOURCE                      
                                       PIC 9999B99B99.                          
           05                          REDEFINES WS-SOURCE.                    
               10  WS-SRC-1            PIC 9(04).                              
               10                      PIC X(01).                              
               10  WS-SRC-2            PIC 9(02).                              
               10                      PIC X(01).                              
               10  WS-SRC-3            PIC 9(02).                              
           05  WS-TARGET               PIC 9(08).                              
           05  WS-INT-DATE             PIC 9(07).                              
       PROCEDURE DIVISION.                                                      
           COMPUTE WS-TARGET = WS-SRC-1 * 10000 +                              
                               WS-SRC-2 * 100   +                              
                               WS-SRC-3.                                        
           COMPUTE WS-INT-DATE = FUNCTION INTEGER-OF-DATE (WS-TARGET).          
           DISPLAY WS-SOURCE ' ' WS-TARGET ' ' WS-INT-DATE.                    
           MOVE WS-SOURCE-R            TO  WS-TARGET.                          
           COMPUTE WS-INT-DATE = FUNCTION INTEGER-OF-DATE (WS-TARGET).          
           DISPLAY WS-SOURCE ' ' WS-TARGET ' ' WS-INT-DATE.              
producing results of
2019-10-31 20191031 0152975
2019-10-31 20191031 0152975
As otherwise stated, MOVE CORRESPONDING would be a third method.
Robert Sample
Global moderator
 
Posts: 3620
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 267 times

Re: Convert 10 bytes DATE to integer

Postby Manjucan » Thu Oct 31, 2019 10:34 pm

Thanks for your inputs.
So you mean to say the below statement would work ??

WS-DATE PIC X(10)
WS-INT-DATE 9(8)

MOVE CORRESPONDING WS-DATE TO WS-INT-DATE

This way ws-int-date will only have numeric without the hyphen in date ?
Manjucan
 
Posts: 8
Joined: Thu Feb 07, 2019 10:09 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Convert 10 bytes DATE to integer

Postby Robert Sample » Fri Nov 01, 2019 12:08 am

No -- you have no understanding of MOVE CORRESPONDING. You would need to code something like this (untested):
05  WS-DATE.
    10  WS-1 PIC 9(04).
    10       PIC X(01).
    10  WS-2 PIC 9(02).
    10       PIC X(01).
    10  WS-3 PIC 9(02).
...
05  WS-INT-DATE.
    10  WS-1 PIC 9(04).
    10  WS-2 PIC 9(02).
    10  WS-3 PIC 9(02).
...
MOVE CORRESPONDING WS-DATE TO WS-INT-DATE.
MOVE CORRESPONDING works on a group-level variable (your post referenced elementary variables which cannot be used in a MOVE CORRESPONDING) and moves all same-named variables to the destination group-level variable. They do not have to be in the same order, nor do the PICTURE clauses have to match, but the names must be exactly the same. Filler won't be moved, nor will variables whose names are in the source or destination group variable only.
Robert Sample
Global moderator
 
Posts: 3620
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 267 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post