formatting DATE-TIME IN COBOL

删除回忆录丶 提交于 2019-12-13 09:18:10

问题


i want to format the FUNCTION DATE-TIME. What i have is

MOVE FUNCTION CURRENT-DATE TO WS-CURRENT-DATE-DATA
DISPLAY WS-CURRENT-DATE-DATA

and I tried using a filler

01 WS-CURRENT-DATE-DATA.                               
05 WS-CURRENT-DATE.                                 
   10  WS-CURRENT-YEAR         PIC 9(04).           
   10  FILLER                  PIC X(01)  VALUE "-".
   10  WS-CURRENT-MONTH        PIC 9(02).           
   10  WS-CURRENT-DAY          PIC 9(02).           
05 WS-CURRENT-TIME.                                 
   10  WS-CURRENT-HOURS        PIC 9(02).           
   10  WS-CURRENT-MINUTE       PIC 9(02).           
   10  WS-CURRENT-SECOND       PIC 9(02).           
   10  WS-CURRENT-MILLISECONDS PIC 9(02).

what I get is a result with the hyphen at the back like

2017122818242863- 

but what I want is possibly this

2017-12-28-18:24:28:63  

Pls help. Thanks in advance


回答1:


The reason that you are seeing this hyphen at then end is because this is actual data definition FUNCTION CURRENT-DATE is expecting:

05 WS-CURRENT-DATE-TIME.            
   10 WS-CURRENT-DATE.              
      15 WS-CURRENT-YEAR  PIC  9(4).
      15 WS-CURRENT-MONTH PIC  9(2).
      15 WS-CURRENT-DAY   PIC  9(2).
   10 WS-CURRENT-TIME.              
      15 WS-CURRENT-HOUR  PIC  9(2).
      15 WS-CURRENT-MIN   PIC  9(2).
      15 WS-CURRENT-SEC   PIC  9(2).
      15 WS-CURRENT-MS    PIC  9(2).
   10 WS-DIFF-GMT         PIC S9(4).

You will notice that I have an extra field at the end for Greenwich Mean Time Difference. Also, you cannot just add filler to your definition and expect that function to know what to do with it. Basically what is happening is the hyphen that you are putting in (in the picture clause) is being overwritten by the data passed from the function what you need is 2 places in working storage. One to hold the return from the function and one to hold the formatted value, so something like this:

01 WS-TEMP-DT.   
   05 WS-TEMP-DATE-TIME.            
      10 WS-TEMP-DATE.              
         15 WS-TEMP-YEAR  PIC  9(4). 
         15 WS-TEMP-MONTH PIC  9(2).
         15 WS-TEMP-DAY   PIC  9(2).
      10 WS-TEMP-TIME.              
         15 WS-TEMP-HOUR  PIC  9(2).
         15 WS-TEMP-MIN   PIC  9(2).
         15 WS-TEMP-SEC   PIC  9(2).
         15 WS-TEMP-MS    PIC  9(2).
      10 WS-DIFF-GMT         PIC S9(4).

01 WS-FORMATTED-DT.   
   05 WS-FORMATTED-DATE-TIME.                       
      15 WS-FORMATTED-YEAR  PIC  9(4). 
      15 FILLER             PIC X VALUE '-'.
      15 WS-FORMATTED-MONTH PIC  9(2).
      15 FILLER             PIC X VALUE '-'.
      15 WS-FORMATTED-DAY   PIC  9(2).  
      15 FILLER             PIC X VALUE '-'.           
      15 WS-FORMATTED-HOUR  PIC  9(2).
      15 FILLER             PIC X VALUE ':'.
      15 WS-FORMATTED-MIN   PIC  9(2).
      15 FILLER             PIC X VALUE ':'.
      15 WS-FORMATTED-SEC   PIC  9(2).
      15 FILLER             PIC X VALUE ':'.
      15 WS-FORMATTED-MS    PIC  9(2).

MOVE FUNCTION CURRENT-DATE TO WS-TEMP-DATE-TIME
MOVE WS-TEMP-YEAR  TO WS-FORMATTED-YEAR
MOVE WS-TEMP-MONTH TO WS-FORMATTED-MONTH
MOVE WS-TEMP-DAY   TO WS-FORMATTED-DAY
MOVE WS-TEMP-HOUR  TO WS-FORMATTED-HOUR
MOVE WS-TEMP-MIN   TO WS-FORMATTED-MIN
MOVE WS-TEMP-SEC   TO WS-FORMATTED-SEC
MOVE WS-TEMP-MS    TO WS-FORMATTED-MS

DISPLAY WS-FORMATTED-DATE-TIME



回答2:


You will need to defined subfields under WS-CURRENT-DATE-DATA to break out yy, mm, dd, hh, mn, ss, ms and then move those individually into the fields under WS-CURRENT-DATE and WS-CURRENT-TIME to properly get your '-' and ':' chars to show up.




回答3:


As Joe already said - if you move something to a group item you get the data in without conversion, so either move sub-fields or (if your compiler supports it - you've missed to specify which one you've used) use FUNCTION FORMATTED-DATETIME.




回答4:


CURRENT-DATE is a 21 character field with date, time, and offset from GMT. The "hyphen at the back" is actually the first position of the offset from GMT.

The UNSTRING statement may be used to move the individual parts of CURRENT-DATE.

       UNSTRING FUNCTION CURRENT-DATE
       INTO WS-CURRENT-YEAR WS-CURRENT-MONTH WS-CURRENT-DAY
           WS-CURRENT-HOURS WS-CURRENT-MINUTE
           WS-CURRENT-SECOND WS-CURRENT-MILLISECONDS
       END-UNSTRING
       DISPLAY WS-CURRENT-DATE-DATA



回答5:


SaggingRufus's answer was informative. I've tried to produce the expected output using Reference Modification.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-DATETIME              PIC X(21).
01 WS-FORMATTED-DT.
   05 WS-FORMATTED-DTE-TME.
      15 WS-FORMATTED-YEAR  PIC  9(4). 
      15 FILLER             PIC X VALUE '-'.
      15 WS-FORMATTED-MONTH PIC  9(2).
      15 FILLER             PIC X VALUE '-'.
      15 WS-FORMATTED-DY    PIC  9(2).
      15 FILLER             PIC X VALUE '-'.
      15 WS-FORMATTED-HOUR  PIC  9(2).
      15 FILLER             PIC X VALUE ':'.
      15 WS-FORMATTED-MINS  PIC  9(2).
      15 FILLER             PIC X VALUE ':'.
      15 WS-FORMATTED-SEC   PIC  9(2).
      15 FILLER             PIC X VALUE ':'.
      15 WS-FORMATTED-MS    PIC  9(2).
PROCEDURE DIVISION.
MOVE FUNCTION CURRENT-DATE TO WS-DATETIME. 
MOVE WS-DATETIME(1:4)  TO WS-FORMATTED-YEAR.
MOVE WS-DATETIME(5:2)  TO WS-FORMATTED-MONTH.
MOVE WS-DATETIME(7:2)  TO WS-FORMATTED-DY.
MOVE WS-DATETIME(9:2)  TO WS-FORMATTED-HOUR.
MOVE WS-DATETIME(11:2) TO WS-FORMATTED-MINS.
MOVE WS-DATETIME(13:2) TO WS-FORMATTED-SEC.
MOVE WS-DATETIME(15:2) TO WS-FORMATTED-MS.
DISPLAY WS-DATETIME.
DISPLAY WS-FORMATTED-DT.
STOP RUN.

Result:

2018011005202041+0000
2018-01-10-05:20:20:41


来源:https://stackoverflow.com/questions/48016044/formatting-date-time-in-cobol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!