Time Formatting Parsing in COBOL

Our first example demonstrates time formatting and parsing in COBOL. Here’s the full source code:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. TIME-FORMATTING-PARSING.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-CURRENT-DATE.
           05 WS-YEAR             PIC 9(4).
           05 WS-MONTH            PIC 9(2).
           05 WS-DAY              PIC 9(2).
           05 WS-HOUR             PIC 9(2).
           05 WS-MINUTE           PIC 9(2).
           05 WS-SECOND           PIC 9(2).
           05 WS-HUNDREDTH        PIC 9(2).
       01 WS-FORMATTED-DATE       PIC X(29).
       01 WS-PARSED-DATE.
           05 WS-PARSED-YEAR      PIC 9(4).
           05 WS-PARSED-MONTH     PIC 9(2).
           05 WS-PARSED-DAY       PIC 9(2).
           05 WS-PARSED-HOUR      PIC 9(2).
           05 WS-PARSED-MINUTE    PIC 9(2).
           05 WS-PARSED-SECOND    PIC 9(2).
       01 WS-INPUT-DATE           PIC X(19).
       
       PROCEDURE DIVISION.
           MOVE FUNCTION CURRENT-DATE TO WS-CURRENT-DATE.
           
           * Formatting the current date and time
           STRING WS-YEAR "-" WS-MONTH "-" WS-DAY "T"
                  WS-HOUR ":" WS-MINUTE ":" WS-SECOND
                  INTO WS-FORMATTED-DATE.
           DISPLAY "Formatted date: " WS-FORMATTED-DATE.
           
           * Parsing a date string
           MOVE "2012-11-01T22:08:41" TO WS-INPUT-DATE.
           UNSTRING WS-INPUT-DATE DELIMITED BY "-" OR "T" OR ":"
               INTO WS-PARSED-YEAR
                    WS-PARSED-MONTH
                    WS-PARSED-DAY
                    WS-PARSED-HOUR
                    WS-PARSED-MINUTE
                    WS-PARSED-SECOND.
           DISPLAY "Parsed date: " WS-PARSED-YEAR "-" WS-PARSED-MONTH "-"
                   WS-PARSED-DAY " " WS-PARSED-HOUR ":" WS-PARSED-MINUTE ":"
                   WS-PARSED-SECOND.
           
           STOP RUN.

In this COBOL program, we demonstrate basic time formatting and parsing operations:

  1. We use the FUNCTION CURRENT-DATE to get the current date and time.

  2. We then format this date into a string similar to the RFC3339 format using the STRING verb.

  3. For parsing, we use a predefined date string and parse it using the UNSTRING verb, which splits the string into its component parts.

  4. Both the formatted and parsed dates are then displayed.

COBOL doesn’t have built-in functions for complex date formatting and parsing like some modern languages. However, we can achieve similar results using string manipulation operations.

To run this program:

  1. Save the code in a file with a .cob extension (e.g., time-formatting-parsing.cob).
  2. Compile the program using a COBOL compiler.
  3. Run the compiled program.

The output will show the current date formatted in a similar style to RFC3339, and then show the parsed components of the predefined date string.

Note that COBOL’s date and time handling capabilities can vary depending on the specific COBOL compiler and version you’re using. Some modern COBOL environments may provide additional intrinsic functions for more advanced date and time operations.