Thread: Time-of-Day Functionality WITHOUT Standard Libs

  1. #1
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133

    Time-of-Day Functionality WITHOUT Standard Libs

    I'm working on a project which requires me to generate an API which wraps around some automatically generated code being spit out of one of Matlab's tools. The goal and requirement is to have the API built into a single object/library file for inclusion in some legacy FORTRAN code and accompanying model.

    My question is this:

    Is there some way to access some Time-of-Day functionality similar to that in the C Standard Libraries (time.h), without using the C Standard Libraries? In order to integrate my code with FORTRAN, we need to eliminate the use of any C libraries as this might raise problems in the future.

    Any help would be appreciated.

    - Jeremy

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What platform is this for? Windows, Linux or something else?

    Assuming you can use the native API, you should be able to use the gettimeofday() for Linux, and GetSystemTime()

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    I believe it is for Windows, yes. I will look into that second option there. Thanks a lot.

  4. #4
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Turns out that we cannot assume we'll be running under Windows. We cannot assume that we'll be linking in the kernel32.lib library. I need to find a solution which is completely stand-alone with no reference to the development environment. Do I have any other options, or am I up the creek?

    I have several rand seeds which I need to update with each test run. I need to pass in a time stamp as the rand seed to ensure that the randoms are all independent and sufficiently different so as to get truly random numbers out of my rand generators.

    I have ways to get around this issue, but I would rather not if it can be helped.

    Thanks again for the help.

    - Jeremy

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you can't link to the standard C library, and not link to the API library of the OS that you are running on, then you are quite far up that famous stinky creek, and paddles are not an option.

    If you want a semi-portable solution, you would make something like this:
    Code:
    int getRandomSeed()
    {
    #ifdef WINVER
        GetSystemTime(...);
        return calculation_of_system_time_to_seed;
    #elif LINUX
        gettimeofday();
        return calculation_of_seed;
    #else
    #error "Unknown architecture"
    #endif
    }
    But that assumes you can use API libraries for the relevant OS.

    Another option that should work OK is to take the CPU timestamp count [assuming processor support, e.g. x86]. That would NOT use any external library, but on the other hand depends on the processor supporting the timestamp count functionality [which has been around since about 1997 or so].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    That's exactly what I was thinking.

    I figured I could use a function which tested for the development environment and ran the appropriate time function. Unfortunately, I believe we have to support embedded applications of this API as well. This means, to my knowledge, that we won't be guaranteed access to ANY development environment libraries.

    I will look into doing a processor/architecture specific time function. I just have to check and see if we are actually running on some sort of recognizable board or if this is functioning completely inside whatever FORTRAN model they want to plug my library into, however that would work.

    My other option is to simply generate the time stamps on the fly in the calling function (the tester that I have written) and pass them into my API. This is what I was trying to avoid because it adds another set of parameters to an already clunky function. It seems that might be the only viable option at this point, however. Basically I'm just throwing it up a level. Out of sight, out of mind kind of thing.

    Thanks again.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One alternative is of course to have a separate function that has a reference implementation [a'la the above code sample], which is called by your code, and it's up to the target system user to implement a correct function for their platform. And that may include something along this if they want to:
    Code:
    int getRandomSeed()
    {
        const static int seeds[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, ... };
        static int index = 5;
        static int index2 = 1;
        int r;
    
        r = seeds[index] * seeds[index1];
        index = (index + 1) % sizeof(seeds)/sizeof(seeds[0]);
        if (!index) index1 = (index1 + 1) sizeof(seeds)/sizeof(seeds[0]);
        return r;
    }
    Obviously "seeds" above needs to be fairly large to make it viable for MANY calls, but at least it's square the size, so 1000 gives you 1 million combinations. [And they don't have to be prime numbers of course - just any combination of not so sequential numbers].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. time of the day
    By warney_out in forum C Programming
    Replies: 1
    Last Post: 04-06-2006, 03:26 AM
  2. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  3. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM