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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date formatting 2

Status
Not open for further replies.

SiouxCityElvis

Programmer
Jun 6, 2003
228
0
0
US
Hello.

I'm on RMCOBOL-85 on Linux. I'm parsing a record and trying to figure out the best way to reformat a date field on it. The date field appears in the very beginning of the record.

There are many ways I should anticipate this date field coming in, but my end goal is to put it into a YYYYMMDD format.

Possible dates examples.

09/01/2004
9/01/2004
9/1/2004

10/15/2004(Pretty easy on this)
10/01/2004(easy also)
10/1/2004(not easy)

It is simple to put the xx/xx/xxxx dates into a working storage

Code:
01 ws-date-rfmt1.
   03 ws-mm     pic x(2)
   03 ws-slash1 pic x(1)
   03 ws-dd pic x(2)
   03 ws-slash2 pic x(1)
   03 ws-yyyy  pic x(4)

then move each individual ws above to 

01 ws-date-rfmt2.
   03 ws-rfmt-yyyy pic x(4)
   03 ws-rfmt-mm   pic x(2)
   03 ws-rfmt-dd   pic x(2)

I can think of approaching my problems when receiving fields in x/xx/xxxx, xx/x/xxxx, and x/x/xxxx formats, but I start thinking of all these diffents INSPECT statements and wonder if I'm making things difficult.

Any easy solutions to this type of challenge?
I know when I had to find the number of days between 2 given dates someone once pointed me to a site that had various date difference functions in it, but it didn't have a date reformatting function in it.

Thanks.
-David
 
[tt]How about using unstring, like;[/tt]
Code:
       .  .  . 
       .  .  .
       01 WS-DATE-IN         PIC X(10).
       01 WS-DATE-OUT.     
           02 WS-MM          PIC X(2).
           02 WS-DD          PIC X(2).
           02 WS-YYYY        PIC X(4).
       .  .  . 
       .  .  .
            MOVE SPACES TO WS-DATE-OUT
            UNSTRING WS-DATE DELIMITED BY "/" INTO
            WS-MM
            WS-DD
            WS-YYYY
            END-UNSTRING
       .  .  .
       .  .  .
 
[tt]Oops, a correction;[/tt]
Code:
       .  .  .                                       
       .  .  .                                       
       01 WS-DATE-IN         PIC X(10).              
       01 WS-DATE-OUT.                               
           02 WS-MM          PIC X(2).               
           02 WS-DD          PIC X(2).               
           02 WS-YYYY        PIC X(4).               
       .  .  .                                       
       .  .  .                                       
            MOVE SPACES TO WS-DATE-OUT               
            UNSTRING WS-DATE-IN DELIMITED BY "/" INTO
            WS-MM                                    
            WS-DD                                    
            WS-YYYY                                  
            END-UNSTRING                             
       .  .  .                                       
       .  .  .
 
CobolKid,

You seem to have failed to account for the fact that the implicit move caused by the UNSTRING is alphanumeric and therefore left justified. So, for the example date of [tt]9/1/2004[/tt], you get both the month and the day incorrectly stored (here I am presuming that David would like 9/1/2004 stored as 20040901).

David, how confident are you that the dates you are parsing are valid? If you have a high degree of confidence, then a slightly trickier UNSTRING will suffice. Or are you also validating these dates as well?

Tom Morrison
 
Tom,

I'm confident that the dates are valid simply to the point that they are using quickbooks when sending this data.
I guess, since really I'm storing this field as a field in a check stub record(simply informational) it doesn't matter if they put 14/21/2021 in there - that's there problem if they put in garbage. I am going to store it as alphanumeric in the even that they use ab/01/abcd or something goofy like that.

What were you thinking as far as date being valid?

Thanks.
-David
 
David,

Okay, then with that relaxed standard, try this variation of the CobolKid example against your test data:
Code:
       01 WS-DATE-IN         PIC X(10).              
       01 WS-DATE-OUT.                               
           02 WS-MM          PIC X(2) JUSTIFIED.
           02 WS-DD          PIC X(2) JUSTIFIED.
           02 WS-YYYY        PIC X(4) JUSTIFIED.
       .  .  .                                       
       .  .  .                                       
            MOVE SPACES TO WS-DATE-OUT
            UNSTRING WS-DATE-IN
                 DELIMITED BY "/" OR SPACE 
                 INTO WS-MM
                      WS-DD
                      WS-YYYY
            END-UNSTRING
            INSPECT WS-DATE-OUT REPLACING ALL SPACE BY ZERO
NOTE: The UNSTRING presumes no embedded spaces in the date (none were shown in your example).

Tom Morrison
 
Works like a charm. Thanks COBOLKid and K5tm(Tom).
I tried to hand out stars to you but it said page was undergoing maintenance....

-David
 
Doesn't that JUSTIFIED justify it to the Left???
So, wouldn't 8/09/2004 yield

8space in the WS-MM field
09 in the WS-DD field
and 2004 in the WS-YYYY field

?

Not trying to be difficult, just want to understand. My approach without help would have been to JUST RIGHT the WS-MM, DD, and YYYY fields.
 
Doesn't that JUSTIFIED justify it to the Left???
There is only one behavior for the [tt]JUSTIFIED[/tt] clause, and that is to cause right justification of an alphanumeric data item. In the COBOL language syntax, the word RIGHT is an optional word. After all, left justification is the normal storage mechanism.

Tom Morrison
 
You seem to have failed to account for the fact that the implicit move caused by the UNSTRING is alphanumeric and therefore left justified.
[tt]Tom,

Failed to account for? Actually not. I contemplated formatting the output as you've depicted, however I decided not to based upon the content of AmarilloElvis' post.

Regards,
Steve[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top