Epoch in COBOL

A common requirement in programs is getting the number of seconds, milliseconds, or nanoseconds since the Unix epoch. Here’s how to do it in COBOL.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. EPOCH-TIME.
       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-SECOND    PIC 9(2).
       01 WS-EPOCH-SECONDS           PIC 9(10).
       01 WS-EPOCH-MILLISECONDS      PIC 9(13).
       01 WS-EPOCH-NANOSECONDS       PIC 9(18).

       PROCEDURE DIVISION.
           MOVE FUNCTION CURRENT-DATE TO WS-CURRENT-DATE.
           DISPLAY WS-CURRENT-DATE.

           COMPUTE WS-EPOCH-SECONDS = FUNCTION INTEGER-OF-DATE(WS-YEAR * 10000 + WS-MONTH * 100 + WS-DAY)
               * 86400 + WS-HOUR * 3600 + WS-MINUTE * 60 + WS-SECOND
               - 2208988800.
           DISPLAY WS-EPOCH-SECONDS.

           COMPUTE WS-EPOCH-MILLISECONDS = WS-EPOCH-SECONDS * 1000
               + WS-HUNDREDTH-SECOND * 10.
           DISPLAY WS-EPOCH-MILLISECONDS.

           COMPUTE WS-EPOCH-NANOSECONDS = WS-EPOCH-MILLISECONDS * 1000000.
           DISPLAY WS-EPOCH-NANOSECONDS.

           STOP RUN.

In COBOL, we use the FUNCTION CURRENT-DATE to get the current date and time. This function returns a string containing the current date and time components, which we move into our WS-CURRENT-DATE structure.

To calculate the number of seconds since the Unix epoch (January 1, 1970), we first convert the date to days using FUNCTION INTEGER-OF-DATE, multiply by the number of seconds in a day (86400), and add the time components. We then subtract the number of seconds from January 1, 1970, to January 1, 1601 (which is 2208988800) because COBOL’s date functions typically use 1601 as the base year.

For milliseconds, we multiply the seconds by 1000 and add the hundredths of a second multiplied by 10.

For nanoseconds, we multiply the milliseconds by 1,000,000.

Note that COBOL doesn’t have built-in functions for Unix time calculations, so we have to perform these calculations manually. Also, the precision of these calculations may be limited by the capabilities of the COBOL compiler and the underlying system.

To run the program, you would typically compile it and then execute the resulting program:

$ cobc -x epoch-time.cob
$ ./epoch-time
20230615120000
1686830400
1686830400000
1686830400000000000

The output will vary based on the current date and time when you run the program.

COBOL doesn’t have a direct equivalent to Go’s time.Unix() function for converting Unix timestamps back into dates. To achieve this, you would need to write a custom function to perform the reverse calculations.

Next, we’ll look at another time-related task: date formatting and parsing in COBOL.