Tickers in COBOL

Timers are for when you want to do something once in the future - tickers are for when you want to do something repeatedly at regular intervals. Here’s an example of a ticker that ticks periodically until we stop it.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. TICKERS.
       ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-CURRENT-TIME    PIC 9(8).
       01 WS-START-TIME      PIC 9(8).
       01 WS-ELAPSED-TIME    PIC 9(8).
       01 WS-INTERVAL        PIC 9(8) VALUE 500.
       01 WS-STOP-TIME       PIC 9(8) VALUE 1600.
       01 WS-DONE            PIC X VALUE 'N'.

       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           PERFORM GET-START-TIME
           PERFORM TICKER-LOOP UNTIL WS-DONE = 'Y'
           DISPLAY "Ticker stopped"
           STOP RUN.

       GET-START-TIME.
           ACCEPT WS-START-TIME FROM TIME.

       TICKER-LOOP.
           PERFORM GET-CURRENT-TIME
           COMPUTE WS-ELAPSED-TIME = WS-CURRENT-TIME - WS-START-TIME
           IF WS-ELAPSED-TIME >= WS-STOP-TIME
               MOVE 'Y' TO WS-DONE
           ELSE
               IF FUNCTION MOD(WS-ELAPSED-TIME, WS-INTERVAL) = 0
                   DISPLAY "Tick at " WS-CURRENT-TIME
               END-IF
           END-IF.

       GET-CURRENT-TIME.
           ACCEPT WS-CURRENT-TIME FROM TIME.

In COBOL, we don’t have built-in tickers or timers like in more modern languages. Instead, we simulate a ticker by continuously checking the current time and performing an action at regular intervals.

We use the ACCEPT ... FROM TIME statement to get the current time in hundredths of a second since midnight. We calculate the elapsed time since the start of the program and use the MOD function to determine if it’s time for a “tick”.

The program will run until the elapsed time reaches 1600 hundredths of a second (16 seconds), simulating the behavior of stopping the ticker after a certain duration.

When we run this program, it should display several “ticks” before stopping:

Tick at 10305010
Tick at 10305015
Tick at 10305020
Ticker stopped

Note that the exact times will vary depending on when you run the program.

This COBOL implementation doesn’t use channels or goroutines as in the original example, as COBOL doesn’t have these concepts. Instead, it uses a simple loop with time-based checks to simulate periodic ticking.