upscheme/c/timefuncs.c

139 lines
2.7 KiB
C
Raw Normal View History

2008-06-30 21:53:51 -04:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "dtypes.h"
2008-06-30 21:53:51 -04:00
#ifdef WIN32
#include <malloc.h>
#include <sys/timeb.h>
#include <windows.h>
#else
#include <sys/time.h>
#include <sys/poll.h>
#include <unistd.h>
#endif
#include "timefuncs.h"
#ifdef WIN32
/*
2008-06-30 21:53:51 -04:00
double tvals2float(struct tm *t, struct timeb *tstruct)
{
return (double)t->tm_hour * 3600 + (double)t->tm_min * 60 +
2008-06-30 21:53:51 -04:00
(double)t->tm_sec + (double)tstruct->millitm/1.0e3;
}
*/
2008-06-30 21:53:51 -04:00
double floattime()
{
struct timeb tstruct;
ftime(&tstruct);
return (double)tstruct.time + (double)tstruct.millitm / 1.0e3;
2008-06-30 21:53:51 -04:00
}
#else
double tv2float(struct timeval *tv)
{
return (double)tv->tv_sec + (double)tv->tv_usec / 1.0e6;
2008-06-30 21:53:51 -04:00
}
double diff_time(struct timeval *tv1, struct timeval *tv2)
{
return tv2float(tv1) - tv2float(tv2);
}
#endif
// return as many bits of system randomness as we can get our hands on
uint64_t i64time()
2008-06-30 21:53:51 -04:00
{
uint64_t a;
2008-06-30 21:53:51 -04:00
#ifdef WIN32
struct timeb tstruct;
ftime(&tstruct);
a = (((uint64_t)tstruct.time) << 32) + (uint64_t)tstruct.millitm;
2008-06-30 21:53:51 -04:00
#else
struct timeval now;
gettimeofday(&now, NULL);
a = (((uint64_t)now.tv_sec) << 32) + (uint64_t)now.tv_usec;
2008-06-30 21:53:51 -04:00
#endif
return a;
}
double clock_now()
{
#ifdef WIN32
return floattime();
#else
struct timeval now;
gettimeofday(&now, NULL);
return tv2float(&now);
#endif
}
#if defined(LINUX) || defined(MACOSX) || defined(OPENBSD) || defined(FREEBSD)
2008-06-30 21:53:51 -04:00
extern char *strptime(const char *s, const char *format, struct tm *tm);
2010-05-02 14:28:53 -04:00
double parsetime(const char *str)
2008-06-30 21:53:51 -04:00
{
char *fmt = "%c"; /* needed to suppress GCC warning */
char *res;
time_t t;
struct tm tm;
res = strptime(str, fmt, &tm);
if (res != NULL) {
tm.tm_isdst =
-1; /* Not set by strptime(); tells mktime() to determine
whether daylight saving time is in effect */
2008-06-30 21:53:51 -04:00
t = mktime(&tm);
if (t == ((time_t)-1))
return -1;
return (double)t;
}
return -1;
}
#else
// TODO
#endif
void sleep_ms(int ms)
{
if (ms == 0)
return;
#ifdef WIN32
Sleep(ms);
#else
struct timeval timeout;
timeout.tv_sec = ms / 1000;
2008-06-30 21:53:51 -04:00
timeout.tv_usec = (ms % 1000) * 1000;
select(0, NULL, NULL, NULL, &timeout);
#endif
}
void timeparts(int32_t *buf, double t)
{
time_t tme = (time_t)t;
#ifndef WIN32
struct tm tm;
localtime_r(&tme, &tm);
tm.tm_year += 1900;
memcpy(buf, (char *)&tm, sizeof(struct tm));
2008-06-30 21:53:51 -04:00
#else
struct tm *tm;
tm = localtime(&tme);
tm->tm_year += 1900;
memcpy(buf, (char *)tm, sizeof(struct tm));
2008-06-30 21:53:51 -04:00
#endif
}