Blob
1 #include <u.h>2 #include <stdlib.h> /* setenv etc. */3 #define NOPLAN9DEFINES4 #include <libc.h>5 #include <time.h>7 #define _HAVETIMEGM 18 #define _HAVETMZONE 19 #define _HAVETMTZOFF 111 #if defined(__linux__)12 # undef _HAVETMZONE13 # undef _HAVETMTZOFF15 #elif defined(__sun__)16 # undef _HAVETIMEGM17 # undef _HAVETMZONE18 # undef _HAVETMTZOFF20 #endif22 static Tm bigtm;24 static void25 tm2Tm(struct tm *tm, Tm *bigtm)26 {27 char *s;29 memset(bigtm, 0, sizeof *bigtm);30 bigtm->sec = tm->tm_sec;31 bigtm->min = tm->tm_min;32 bigtm->hour = tm->tm_hour;33 bigtm->mday = tm->tm_mday;34 bigtm->mon = tm->tm_mon;35 bigtm->year = tm->tm_year;36 bigtm->wday = tm->tm_wday;37 strftime(bigtm->zone, sizeof bigtm->zone, "%Z", tm);38 #ifdef _HAVETZOFF39 bigtm->tzoff = tm->tm_gmtoff;40 #endif42 if(bigtm->zone[0] == 0){43 s = getenv("TIMEZONE");44 if(s){45 strecpy(bigtm->zone, bigtm->zone+4, s);46 free(s);47 }48 }49 }51 static void52 Tm2tm(Tm *bigtm, struct tm *tm)53 {54 memset(tm, 0, sizeof *tm);55 tm->tm_sec = bigtm->sec;56 tm->tm_min = bigtm->min;57 tm->tm_hour = bigtm->hour;58 tm->tm_mday = bigtm->mday;59 tm->tm_mon = bigtm->mon;60 tm->tm_year = bigtm->year;61 tm->tm_wday = bigtm->wday;62 #ifdef _HAVETMZONE63 tm->tm_zone = bigtm->zone;64 #endif65 #ifdef _HAVETZOFF66 tm->tm_gmtoff = bigtm->tzoff;67 #endif68 }70 Tm*71 p9gmtime(long x)72 {73 time_t t;74 struct tm tm;76 t = (time_t)x;77 tm = *gmtime(&t);78 tm2Tm(&tm, &bigtm);79 return &bigtm;80 }82 Tm*83 p9localtime(long x)84 {85 time_t t;86 struct tm tm;88 t = (time_t)x;89 tm = *localtime(&t);90 tm2Tm(&tm, &bigtm);91 return &bigtm;92 }94 #if !defined(_HAVETIMEGM)95 static time_t96 timegm(struct tm *tm)97 {98 time_t ret;99 char *tz;100 char *s;102 tz = getenv("TZ");103 putenv("TZ=");104 tzset();105 ret = mktime(tm);106 if(tz){107 s = smprint("TZ=%s", tz);108 if(s)109 putenv(s);110 }111 return ret;112 }113 #endif115 long116 p9tm2sec(Tm *bigtm)117 {118 struct tm tm;120 Tm2tm(bigtm, &tm);121 if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0)122 return timegm(&tm);123 return mktime(&tm); /* local time zone */124 }