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 #ifdef _HAVETMZONE38 strecpy(bigtm->zone, bigtm->zone+4, tm->tm_zone);39 #endif40 #ifdef _HAVETZOFF41 bigtm->tzoff = tm->tm_gmtoff;42 #endif43 if(bigtm->zone[0] == 0){44 s = getenv("TIMEZONE");45 if(s){46 strecpy(bigtm->zone, bigtm->zone+4, s);47 free(s);48 }49 }50 }52 static void53 Tm2tm(Tm *bigtm, struct tm *tm)54 {55 memset(tm, 0, sizeof *tm);56 tm->tm_sec = bigtm->sec;57 tm->tm_min = bigtm->min;58 tm->tm_hour = bigtm->hour;59 tm->tm_mday = bigtm->mday;60 tm->tm_mon = bigtm->mon;61 tm->tm_year = bigtm->year;62 tm->tm_wday = bigtm->wday;63 #ifdef _HAVETMZONE64 tm->tm_zone = bigtm->zone;65 #endif66 #ifdef _HAVETZOFF67 tm->tm_gmtoff = bigtm->tzoff;68 #endif69 }71 Tm*72 p9gmtime(long t)73 {74 struct tm tm;76 tm = *gmtime(&t);77 tm2Tm(&tm, &bigtm);78 return &bigtm;79 }81 Tm*82 p9localtime(long t)83 {84 struct tm tm;86 tm = *localtime(&t);87 tm2Tm(&tm, &bigtm);88 return &bigtm;89 }91 #if !defined(_HAVETIMEGM)92 static time_t93 timegm(struct tm *tm)94 {95 time_t ret;96 char *tz;97 char *s;99 tz = getenv("TZ");100 putenv("TZ=");101 tzset();102 ret = mktime(tm);103 if(tz){104 s = smprint("TZ=%s", tz);105 if(s)106 putenv(s);107 }108 return ret;109 }110 #endif112 long113 p9tm2sec(Tm *bigtm)114 {115 struct tm tm;117 Tm2tm(bigtm, &tm);118 if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0)119 return timegm(&tm);120 return mktime(&tm); /* local time zone */121 }