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 t)72 {73 struct tm tm;75 tm = *gmtime(&t);76 tm2Tm(&tm, &bigtm);77 return &bigtm;78 }80 Tm*81 p9localtime(long t)82 {83 struct tm tm;85 tm = *localtime(&t);86 tm2Tm(&tm, &bigtm);87 return &bigtm;88 }90 #if !defined(_HAVETIMEGM)91 static time_t92 timegm(struct tm *tm)93 {94 time_t ret;95 char *tz;96 char *s;98 tz = getenv("TZ");99 putenv("TZ=");100 tzset();101 ret = mktime(tm);102 if(tz){103 s = smprint("TZ=%s", tz);104 if(s)105 putenv(s);106 }107 return ret;108 }109 #endif111 long112 p9tm2sec(Tm *bigtm)113 {114 struct tm tm;116 Tm2tm(bigtm, &tm);117 if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0)118 return timegm(&tm);119 return mktime(&tm); /* local time zone */120 }