Project

General

Profile

How to get system time and date ?

Added by Antonio Delgado about 1 month ago

I'm new to the Eigen Oberon-2 compiler. I'm trying to use the random module and I assume I should use the current system time and date to initialize the ramdom number generator seed. How to get system time and date ?


Replies (1)

RE: How to get system time and date ? - Added by Florian Negele about 1 month ago

Thanks for your message. Unfortunately, the system time and date are currently not exposed by any library. Depending on your target system however, you may be able to initialise the random number generator using the clock function from the standard C library. In order to call it from Oberon code, you have to use an external forward declaration as follows:

MODULE Test;

IMPORT SYSTEM, Random IN OBL;

VAR seed: Random.Seed;

VAR ^ clock ["_initial_clock"]: LONGINT;

PROCEDURE ^ Clock ["clock"] (): LONGINT;

BEGIN
    Random.Initialize (seed, Clock () + clock);
    LOOP
        TRACE (Random.Integer (seed));
    END;
END Test.

Please note that the clock function in most cases returns the number of elapsed milliseconds since the program has started and may therefore not be suitable for the seed initialisation on its own. This is why the value of the external clock variable is added which stores the number of elapsed milliseconds between system and program startup. I hope this helps and good luck!

    (1-1/1)