plan9port/src/lib9/date.c

114 lines
1.9 KiB
C
Raw Normal View History

2003-11-25 02:11:11 +00:00
#define NOPLAN9DEFINES
#include <u.h>
2003-11-23 18:12:54 +00:00
#include <libc.h>
#include <stdlib.h> /* setenv etc. */
2003-11-25 02:11:11 +00:00
#include <time.h>
2003-11-23 18:12:54 +00:00
static int didtz;
static int tzdelta;
static char tzone[4];
2003-11-25 02:11:11 +00:00
static void
dotz(void)
{
time_t t;
2006-05-20 04:24:24 +00:00
struct tm *gtm;
struct tm tm;
2003-11-23 18:12:54 +00:00
if(didtz)
return;
2006-05-30 16:47:24 +00:00
didtz = 1;
t = time(0);
strftime(tzone, sizeof tzone, "%Z", localtime(&t));
2006-05-20 04:24:24 +00:00
tm = *localtime(&t); /* set local time zone field */
gtm = gmtime(&t);
tm.tm_sec = gtm->tm_sec;
tm.tm_min = gtm->tm_min;
tm.tm_hour = gtm->tm_hour;
tm.tm_mday = gtm->tm_mday;
tm.tm_mon = gtm->tm_mon;
tm.tm_year = gtm->tm_year;
tm.tm_wday = gtm->tm_wday;
tzdelta = t - mktime(&tm);
}
2003-11-23 18:12:54 +00:00
static void
tm2Tm(struct tm *tm, Tm *bigtm, int gmt)
2003-11-23 18:12:54 +00:00
{
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;
2004-03-25 23:03:57 +00:00
}
2003-11-23 18:12:54 +00:00
}
static void
Tm2tm(Tm *bigtm, struct tm *tm)
{
2006-05-18 23:08:02 +00:00
/* initialize with current time to get local time zone! (tm_isdst) */
time_t t;
time(&t);
*tm = *localtime(&t);
2003-11-23 18:12:54 +00:00
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*
2004-12-29 01:25:41 +00:00
p9gmtime(long x)
2003-11-23 18:12:54 +00:00
{
2004-12-29 01:25:41 +00:00
time_t t;
2003-11-23 18:12:54 +00:00
struct tm tm;
static Tm bigtm;
2004-12-29 01:25:41 +00:00
t = (time_t)x;
2003-11-23 18:12:54 +00:00
tm = *gmtime(&t);
tm2Tm(&tm, &bigtm, 1);
2003-11-23 18:12:54 +00:00
return &bigtm;
}
Tm*
2004-12-29 01:25:41 +00:00
p9localtime(long x)
2003-11-23 18:12:54 +00:00
{
2004-12-29 01:25:41 +00:00
time_t t;
2003-11-23 18:12:54 +00:00
struct tm tm;
static Tm bigtm;
2003-11-23 18:12:54 +00:00
2004-12-29 01:25:41 +00:00
t = (time_t)x;
2003-11-23 18:12:54 +00:00
tm = *localtime(&t);
tm2Tm(&tm, &bigtm, 0);
2003-11-23 18:12:54 +00:00
return &bigtm;
}
long
p9tm2sec(Tm *bigtm)
{
time_t t;
2003-11-23 18:12:54 +00:00
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;
2003-11-23 18:12:54 +00:00
}