CURRENT-DATE and slicing in COBOL
One function call returns the whole timestamp; reference modification slices it.
IDENTIFICATION DIVISION.
PROGRAM-ID. CURRENT-DATE-DEMO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NOW-STAMP PIC X(21).
PROCEDURE DIVISION.
*> CURRENT-DATE returns 21 characters; (start:length) slices them.
MOVE FUNCTION CURRENT-DATE TO NOW-STAMP
DISPLAY "Year: " NOW-STAMP(1:4)
DISPLAY "Month: " NOW-STAMP(5:2)
DISPLAY "Clock: " NOW-STAMP(9:2) ":" NOW-STAMP(11:2)
STOP RUN.
How it works
FUNCTION CURRENT-DATEyields 21 characters: date, time, and zone offset.NOW-STAMP(1:4)is reference modification — start position, then length.- Three slices pull the year, the month, and the clock out of one field.
Keywords and builtins used here
DATADISPLAYDIVISIONIDENTIFICATIONMOVEPROCEDURESECTIONSTOP
The run, in numbers
- Lines
- 12
- Characters to type
- 376
- Tokens
- 60
- Three-star pace
- 40 tpm
At the three-star pace of 40 tokens a minute, this run takes about 90 seconds.
Step 2 of 3 in Functions & calls, step 23 of 27 in Language basics.