“sys/time.h” Replacement for Windows

Some C/C++ code targeted for GNU family compilers fail to compile under Windows due to the dependency on sys/time.h header file. The repository here has provided a neat implementation for it. Basically you need these three files: time.h, times.h and times.cpp. I have included them here (in case the repository ever went dead). Note that this is not my code and the original license of the code was LGPL.

sys/time.h:

sys/times.h:

sys/time.cpp:

 

 

 

8 comments

Skip to comment form

    • Hossein on January 17, 2017 at 12:47 AM
    • Reply

    Thanks Mehran jan!
    It’s very helpful.

    • Jay on May 7, 2017 at 8:02 PM
    • Reply

    With Cuda 8, the following code needs to be added into times.h:


    struct timezone {
    int tz_minuteswest; /* minutes west of Greenwich */
    int tz_dsttime; /* type of DST correction */
    };

    • Pit on May 30, 2017 at 6:28 PM
    • Reply

    you can add as well stucture

    struct timespec {
    time_t tv_sec; /* seconds */
    long tv_nsec; /* and nanoseconds */
    };

    • KYO on July 25, 2018 at 9:35 AM
    • Reply

    Where need I to add the files (“sys/time.h”,…)? Inside the project?

    1. Yes, and note that it should be “sys/time.h” (not ) so that it can be found in the local project files (not system include dir).

    • James on December 18, 2018 at 2:50 AM
    • Reply

    Hi,

    For some reason I’m unable to get this to work as clock_t hasn’t been declared anywhere. It should be in time.h, but hasn’t been in this above case. Any help would be great.

    Cheers
    James

    • Brian on October 4, 2020 at 9:32 AM
    • Reply

    To follow up on Kyo’s comment, where exactly should these files be placed? For instance, if a package is being installed, and inside this package no such folder “sys” exist, should a folder “sys” be made to place these files?

    • Shreya Das on November 25, 2020 at 1:47 AM
    • Reply

    Thank you very much Mehran Maghoumi!
    I use Cygwin to run near Linux system in my Windows laptop. And sys/time.h header file is not included in the version that I am currently using. But using the codes provided in this page, I could get the same result as when the sys/time.h header file were present.

Leave a Reply

Your email address will not be published.