Break down timefuncs.c to unix and windows

This commit is contained in:
Lassi Kortela 2019-08-09 23:56:18 +03:00
parent 8a7ce61575
commit daaa5aaed0
3 changed files with 79 additions and 90 deletions

View File

@ -1,43 +1,12 @@
#include <stdlib.h>
#include <stdio.h>
#include <sys/poll.h>
#include <sys/time.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"
#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
/*
double tvals2float(struct tm *t, struct timeb *tstruct)
{
return (double)t->tm_hour * 3600 + (double)t->tm_min * 60 +
(double)t->tm_sec + (double)tstruct->millitm/1.0e3;
}
*/
double floattime()
{
struct timeb tstruct;
ftime(&tstruct);
return (double)tstruct.time + (double)tstruct.millitm / 1.0e3;
}
#else
double tv2float(struct timeval *tv)
{
return (double)tv->tv_sec + (double)tv->tv_usec / 1.0e6;
@ -47,39 +16,47 @@ 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()
uint64_t i64time(void)
{
uint64_t a;
#ifdef WIN32
struct timeb tstruct;
ftime(&tstruct);
a = (((uint64_t)tstruct.time) << 32) + (uint64_t)tstruct.millitm;
#else
struct timeval now;
gettimeofday(&now, NULL);
a = (((uint64_t)now.tv_sec) << 32) + (uint64_t)now.tv_usec;
#endif
return a;
}
double clock_now()
double clock_now(void)
{
#ifdef WIN32
return floattime();
#else
struct timeval now;
gettimeofday(&now, NULL);
return tv2float(&now);
#endif
}
#if defined(LINUX) || defined(MACOSX) || defined(OPENBSD) || defined(FREEBSD)
extern char *strptime(const char *s, const char *format, struct tm *tm);
void sleep_ms(int ms)
{
struct timeval timeout;
if (ms == 0) {
return;
}
timeout.tv_sec = ms / 1000;
timeout.tv_usec = (ms % 1000) * 1000;
select(0, NULL, NULL, NULL, &timeout);
}
void timeparts(int32_t *buf, double t)
{
struct tm tm;
time_t tme = (time_t)t;
localtime_r(&tme, &tm);
tm.tm_year += 1900;
memcpy(buf, (char *)&tm, sizeof(struct tm));
}
double parsetime(const char *str)
{
char *fmt = "%c"; /* needed to suppress GCC warning */
@ -99,40 +76,3 @@ double parsetime(const char *str)
}
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;
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));
#else
struct tm *tm;
tm = localtime(&tme);
tm->tm_year += 1900;
memcpy(buf, (char *)tm, sizeof(struct tm));
#endif
}

49
c/time_windows.c Normal file
View File

@ -0,0 +1,49 @@
#include <windows.h>
#include "timefuncs.h"
#if 0
double tvals2float(struct tm *t, struct timeb *tstruct)
{
return (double)t->tm_hour * 3600 + (double)t->tm_min * 60 +
(double)t->tm_sec + (double)tstruct->millitm/1.0e3;
}
#endif
double floattime(void)
{
struct timeb tstruct;
ftime(&tstruct);
return (double)tstruct.time + (double)tstruct.millitm / 1.0e3;
}
uint64_t i64time(void)
{
uint64_t a;
struct timeb tstruct;
ftime(&tstruct);
a = (((uint64_t)tstruct.time) << 32) + (uint64_t)tstruct.millitm;
return a;
}
double clock_now(void) { return floattime(); }
void sleep_ms(int ms)
{
if (ms == 0) {
return;
}
Sleep(ms);
}
void timeparts(int32_t *buf, double t)
{
time_t tme = (time_t)t;
struct tm *tm;
tm = localtime(&tme);
tm->tm_year += 1900;
memcpy(buf, (char *)tm, sizeof(struct tm));
}

View File

@ -37,14 +37,14 @@ $CC $CFLAGS -c ../c/random.c
$CC $CFLAGS -c ../c/socket.c
$CC $CFLAGS -c ../c/string.c
$CC $CFLAGS -c ../c/table.c
$CC $CFLAGS -c ../c/timefuncs.c
$CC $CFLAGS -c ../c/time_unix.c
$CC $CFLAGS -c ../c/utf8.c
$CC $LFLAGS -o flisp -lm \
bitvector-ops.o bitvector.o builtins.o dump.o \
equalhash.o flisp.o flmain.o fs_"$os".o fs_unix.o \
hashing.o htable.o int2str.o \
ios.o iostream.o lltinit.o ptrhash.o random.o socket.o \
string.o table.o timefuncs.o utf8.o
string.o table.o time_unix.o utf8.o
{ set +x; } 2>/dev/null
cd ../scheme-core
echo "Entering directory '$PWD'"