Blob
1 #include <stdlib.h> /* setenv etc. */3 #include <u.h>4 #define NOPLAN9DEFINES5 #include <libc.h>6 #include <time.h>8 #define _HAVETIMEGM 19 #define _HAVETMZONE 110 #define _HAVETMTZOFF 112 #if defined(__linux__)13 # undef _HAVETMZONE14 # undef _HAVETMTZOFF16 #elif defined(__sun__)17 # undef _HAVETIMEGM18 # undef _HAVETMZONE19 # undef _HAVETMTZOFF21 #endif23 static Tm bigtm;25 static void26 tm2Tm(struct tm *tm, Tm *bigtm)27 {28 memset(bigtm, 0, sizeof *bigtm);29 bigtm->sec = tm->tm_sec;30 bigtm->min = tm->tm_min;31 bigtm->hour = tm->tm_hour;32 bigtm->mday = tm->tm_mday;33 bigtm->mon = tm->tm_mon;34 bigtm->year = tm->tm_year;35 bigtm->wday = tm->tm_wday;36 #ifdef _HAVETMZONE37 strecpy(bigtm->zone, bigtm->zone+4, tm->tm_zone);38 #endif39 #ifdef _HAVETZOFF40 bigtm->tzoff = tm->tm_gmtoff;41 #endif42 }44 static void45 Tm2tm(Tm *bigtm, struct tm *tm)46 {47 memset(tm, 0, sizeof *tm);48 tm->tm_sec = bigtm->sec;49 tm->tm_min = bigtm->min;50 tm->tm_hour = bigtm->hour;51 tm->tm_mday = bigtm->mday;52 tm->tm_mon = bigtm->mon;53 tm->tm_year = bigtm->year;54 tm->tm_wday = bigtm->wday;55 #ifdef _HAVETMZONE56 tm->tm_zone = bigtm->zone;57 #endif58 #ifdef _HAVETZOFF59 tm->tm_gmtoff = bigtm->tzoff;60 #endif61 }63 Tm*64 p9gmtime(long t)65 {66 struct tm tm;68 tm = *gmtime(&t);69 tm2Tm(&tm, &bigtm);70 return &bigtm;71 }73 Tm*74 p9localtime(long t)75 {76 struct tm tm;78 tm = *localtime(&t);79 tm2Tm(&tm, &bigtm);80 return &bigtm;81 }83 #if !defined(_HAVETIMEGM)84 static time_t85 timegm(struct tm *tm)86 {87 time_t ret;88 char *tz;89 char *s;91 tz = getenv("TZ");92 putenv("TZ=");93 tzset();94 ret = mktime(tm);95 if(tz){96 s = smprint("TZ=%s", tz);97 if(s)98 putenv(s);99 }100 return ret;101 }102 #endif104 long105 p9tm2sec(Tm *bigtm)106 {107 struct tm tm;109 Tm2tm(bigtm, &tm);110 if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0)111 return timegm(&tm);112 return mktime(&tm); /* local time zone */113 }