split out mlrdatetime from mlrutil to ease iterative windows port

This commit is contained in:
John Kerl 2017-01-17 22:06:09 -05:00
parent 51a310274f
commit b0a0583a35
2 changed files with 47 additions and 0 deletions

33
c/lib/mlrdatetime.c Normal file
View file

@ -0,0 +1,33 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/stat.h>
#include "lib/mlrdatetime.h"
// ----------------------------------------------------------------
// seconds since the epoch
double get_systime() {
struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
(void)gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)tv.tv_usec * 1e-6;
}
// ----------------------------------------------------------------
// See the GNU timegm manpage -- this is what it does.
time_t mlr_timegm(struct tm* ptm) {
time_t ret;
char* tz;
tz = getenv("TZ");
setenv("TZ", "GMT0", 1);
tzset();
ret = mktime(ptm);
if (tz) {
setenv("TZ", tz, 1);
} else {
unsetenv("TZ");
}
tzset();
return ret;
}

14
c/lib/mlrdatetime.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef MLRDATETIME_H
#define MLRDATETIME_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
double get_systime();
// portable timegm replacement
time_t mlr_timegm (struct tm *ptm);
#endif // MLRDATETIME_H