#define NOPLAN9DEFINES | |
#include <u.h> | |
#include <libc.h> | |
#include <stdlib.h> /* setenv etc. */ | |
#include <time.h> | |
static int didtz; | |
static int tzdelta; | |
static char tzone[4]; | |
static void | |
dotz(void) | |
{ | |
time_t t; | |
if(didtz) | |
return; | |
t = time(0); | |
tzdelta = t - mktime(gmtime(&t)); | |
strftime(tzone, sizeof tzone, "%Z", localtime(&t)); | |
} | |
static void | |
tm2Tm(struct tm *tm, Tm *bigtm, int gmt) | |
{ | |
memset(bigtm, 0, sizeof *bigtm); | |
bigtm->sec = tm->tm_sec; | |
bigtm->min = tm->tm_min; | |
bigtm->hour = tm->tm_hour; | |
bigtm->mday = tm->tm_mday; | |
bigtm->mon = tm->tm_mon; | |
bigtm->year = tm->tm_year; | |
bigtm->wday = tm->tm_wday; | |
if(gmt){ | |
strcpy(bigtm->zone, "GMT"); | |
bigtm->tzoff = 0; | |
}else{ | |
dotz(); | |
strcpy(bigtm->zone, tzone); | |
bigtm->tzoff = tzdelta; | |
} | |
} | |
static void | |
Tm2tm(Tm *bigtm, struct tm *tm) | |
{ | |
memset(tm, 0, sizeof *tm); | |
tm->tm_sec = bigtm->sec; | |
tm->tm_min = bigtm->min; | |
tm->tm_hour = bigtm->hour; | |
tm->tm_mday = bigtm->mday; | |
tm->tm_mon = bigtm->mon; | |
tm->tm_year = bigtm->year; | |
tm->tm_wday = bigtm->wday; | |
} | |
Tm* | |
p9gmtime(long x) | |
{ | |
time_t t; | |
struct tm tm; | |
static Tm bigtm; | |
t = (time_t)x; | |
tm = *gmtime(&t); | |
tm2Tm(&tm, &bigtm, 1); | |
return &bigtm; | |
} | |
Tm* | |
p9localtime(long x) | |
{ | |
time_t t; | |
struct tm tm; | |
static Tm bigtm; | |
t = (time_t)x; | |
tm = *localtime(&t); | |
tm2Tm(&tm, &bigtm, 0); | |
return &bigtm; | |
} | |
long | |
p9tm2sec(Tm *bigtm) | |
{ | |
time_t t; | |
struct tm tm; | |
Tm2tm(bigtm, &tm); | |
t = mktime(&tm); | |
if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0){ | |
dotz(); | |
t += tzdelta; | |
} | |
return t; | |
} | |