plan9port/src/lib9/date.c

126 lines
1.9 KiB
C
Raw Normal View History

2003-11-23 18:12:54 +00:00
#include <u.h>
2004-03-25 23:03:57 +00:00
#include <stdlib.h> /* setenv etc. */
2003-11-25 02:11:11 +00:00
#define NOPLAN9DEFINES
2003-11-23 18:12:54 +00:00
#include <libc.h>
2003-11-25 02:11:11 +00:00
#include <time.h>
2003-11-23 18:12:54 +00:00
2003-11-25 02:11:11 +00:00
#define _HAVETIMEGM 1
#define _HAVETMZONE 1
#define _HAVETMTZOFF 1
2003-11-23 18:12:54 +00:00
2003-11-25 02:11:11 +00:00
#if defined(__linux__)
# undef _HAVETMZONE
# undef _HAVETMTZOFF
#elif defined(__sun__)
# undef _HAVETIMEGM
# undef _HAVETMZONE
# undef _HAVETMTZOFF
#endif
2003-11-23 18:12:54 +00:00
static Tm bigtm;
static void
tm2Tm(struct tm *tm, Tm *bigtm)
{
2004-03-25 23:03:57 +00:00
char *s;
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;
2004-04-20 17:00:01 +00:00
strftime(bigtm->zone, sizeof bigtm->zone, "%Z", tm);
2003-11-23 19:49:17 +00:00
#ifdef _HAVETZOFF
2003-11-23 18:12:54 +00:00
bigtm->tzoff = tm->tm_gmtoff;
2003-11-23 19:49:17 +00:00
#endif
2004-04-20 17:00:01 +00:00
2004-03-25 23:03:57 +00:00
if(bigtm->zone[0] == 0){
s = getenv("TIMEZONE");
if(s){
2004-03-26 01:59:35 +00:00
strecpy(bigtm->zone, bigtm->zone+4, s);
2004-03-25 23:03:57 +00:00
free(s);
}
}
2003-11-23 18:12:54 +00:00
}
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;
2003-11-23 19:49:17 +00:00
#ifdef _HAVETMZONE
2003-11-23 18:12:54 +00:00
tm->tm_zone = bigtm->zone;
2003-11-23 19:49:17 +00:00
#endif
#ifdef _HAVETZOFF
2003-11-23 18:12:54 +00:00
tm->tm_gmtoff = bigtm->tzoff;
2003-11-23 19:49:17 +00:00
#endif
2003-11-23 18:12:54 +00:00
}
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;
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);
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;
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);
return &bigtm;
}
2003-11-24 00:43:41 +00:00
#if !defined(_HAVETIMEGM)
static time_t
2003-11-23 19:49:17 +00:00
timegm(struct tm *tm)
{
2003-11-24 00:43:41 +00:00
time_t ret;
char *tz;
2003-11-25 02:11:11 +00:00
char *s;
2003-11-24 00:43:41 +00:00
tz = getenv("TZ");
2003-11-25 02:11:11 +00:00
putenv("TZ=");
2003-11-24 00:43:41 +00:00
tzset();
ret = mktime(tm);
2003-11-25 02:11:11 +00:00
if(tz){
s = smprint("TZ=%s", tz);
if(s)
putenv(s);
}
2003-11-24 00:43:41 +00:00
return ret;
2003-11-23 19:49:17 +00:00
}
#endif
2003-11-23 18:12:54 +00:00
long
p9tm2sec(Tm *bigtm)
{
struct tm tm;
Tm2tm(bigtm, &tm);
if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0)
return timegm(&tm);
return mktime(&tm); /* local time zone */
}