Skip to main content

Problem:

In a Windows environment it is possible to use the WIN API GetSystemTime which returns time to milliseconds.  

However, is there a similar functionality for UNIX platforms?

Resolution:

The standard functions ftime() and gettimeofday(), both of which are part of the Single UNIX Specification. ftime() is an outdated function; gettimeofday() is preferred for new code.

To use either of those functions in COBOL you will have to convert the appropriate structure from the system's version of /usr/include/sys/time.h to a COBOL group item, then call the function

passing the item by reference.

For example, on Solaris 9, in a 32-bit environment:

-----

$set sourceformat(free)

identification division.

program-id. time-in-ms.

data division.

working-storage section.

    01 timeval-32bit.

       02 seconds      pic x(4) comp-5.

       02 microseconds pic x(4) comp-5.

    77 tzp usage pointer value null.

    77 ms-since-epoch  pic 9(18) display.

procedure division.

    call "gettimeofday" using

       by reference timeval-32bit

       by value     tzp

       end-call

    compute ms-since-epoch = seconds * 1000 microseconds / 1000

    display ms-since-epoch

    stop run

    .

-----

If the you want the local time including milliseconds, or some other variation, you willl need to look into the other UNIX time functions, such as localtime(). The usual method for this is to call gettimeofday, convert the seconds portion  to a tm structure with localtime, convert the microseconds portion to milliseconds by dividing by 1000, and then display the milliseconds

along with the other relevant information from the tm structure.

Old KB# 2254