“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.


#pragma once

#ifndef _TIMES_H

#include "sys/times.h"



#ifndef _TIMES_H
#define _TIMES_H

#ifdef _WIN32
#include <sys/timeb.h>
#include <sys/types.h>
#include <winsock2.h>

int gettimeofday(struct timeval* t,void* timezone);

// from linux's sys/times.h

//#include <features.h>

#define __need_clock_t
#include <time.h>

/* Structure describing CPU time used by a process and its children.  */
struct tms
    clock_t tms_utime;          /* User CPU time.  */
    clock_t tms_stime;          /* System CPU time.  */

    clock_t tms_cutime;         /* User CPU time of dead children.  */
    clock_t tms_cstime;         /* System CPU time of dead children.  */

/* Store the CPU time used by this process and all its
   dead children (and their dead children) in BUFFER.
   Return the elapsed real time, or (clock_t) -1 for errors.
   All times are in CLK_TCKths of a second.  */
clock_t times (struct tms *__buffer);

typedef long long suseconds_t ;



#include "sys/times.h"

int gettimeofday(struct timeval* t,void* timezone)
{       struct _timeb timebuffer;
        _ftime( &timebuffer );
		return 0;

clock_t times (struct tms *__buffer) {

	__buffer->tms_utime = clock();
	__buffer->tms_stime = 0;
	__buffer->tms_cstime = 0;
	__buffer->tms_cutime = 0;
	return __buffer->tms_utime;





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


    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.


    • 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.

    • Holger on March 26, 2021 at 5:24 AM
    • Reply

    It seems to be good, but sorry, I’m absolutely new with C++!

    I learned, that “gettimeofday” and “sys/time.h” is LINUX only. So we certainly have to add some workaround into Windows what’s given here…

    So, what I have to do?!
    1. Copy and paste code as snipped into my project files?
    2. Add those fieles to my project map?
    3. Same, but use a common system directory instead, wich would be read with the “#include sys/…” command? – And if so, where is this directory located with Windows 10?

    Many thanks!

      • Stray Fingers on April 17, 2021 at 4:55 AM
      • Reply


      1. You just make new files in your project named similarly (you will need to create the /sys/ folder manually, Microsoft didn’t think to make their tools do it), and just copy and paste!

      3. You would want to place them in the same directory as the project files, and make sure to replace every instance of #include with #include “sys/time.h” in your code, otherwise your editor will look in the wrong place!

      Best of luck!

    • Germán on June 2, 2021 at 2:24 PM
    • Reply

    Hi, I just run into this issue, too, while attempting to port various programs from Linux to Windows.

    I have installed MSYS2 in my computer, with msys and mingw64.

    I am using msys, because I prefer a Linux-like prompt; but, I am not using msys to compile the program. Instead, I am using mingw64 gnu tool chain so that I end up with a Windows native executables.

    Now, correct me if I am wrong but, from what I can tell, the above files do not quite solve the issue for a Windows native executable compiled with mingw64…the #include statements continue to need stuff from [/usr/include].

    Please advise.

Leave a Reply

Your email address will not be published.