https://www.codefull.org/2015/12/systime-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:
#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 ;
#endif
#endif
#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 ;
#endif
#endif
sys/times.cpp:
#include "sys/times.h"
int gettimeofday(struct timeval* t,void* timezone)
{ struct _timeb timebuffer;
_ftime( &timebuffer );
t->tv_sec=timebuffer.time;
t->tv_usec=1000*timebuffer.millitm;
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;
}
{ struct _timeb timebuffer;
_ftime( &timebuffer );
t->tv_sec=timebuffer.time;
t->tv_usec=1000*timebuffer.millitm;
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;
}