From 57ce0d667e0a4f749e6c3528c5a15d8233172ae7 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 6 May 2008 14:45:39 -0400 Subject: =?UTF-8?q?localtime:=20use=20correct=20time=20zone=20more=20often?= =?UTF-8?q?=20(Michael=20Teichgr=C3=A4ber)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib9/date.c | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/lib9/date.c b/src/lib9/date.c index a0f39060..8509bb48 100644 --- a/src/lib9/date.c +++ b/src/lib9/date.c @@ -4,22 +4,13 @@ #include /* setenv etc. */ #include -static int didtz; -static int tzdelta; -static char tzone[32]; - -static void -dotz(void) +static int +dotz(time_t t, char *tzone) { - time_t t; struct tm *gtm; struct tm tm; - if(didtz) - return; - didtz = 1; - t = time(0); - strftime(tzone, sizeof tzone, "%Z", localtime(&t)); + strftime(tzone, 32, "%Z", localtime(&t)); tm = *localtime(&t); /* set local time zone field */ gtm = gmtime(&t); tm.tm_sec = gtm->tm_sec; @@ -29,11 +20,11 @@ dotz(void) tm.tm_mon = gtm->tm_mon; tm.tm_year = gtm->tm_year; tm.tm_wday = gtm->tm_wday; - tzdelta = t - mktime(&tm); + return t - mktime(&tm); } static void -tm2Tm(struct tm *tm, Tm *bigtm, int gmt) +tm2Tm(struct tm *tm, Tm *bigtm, int tzoff, char *zone) { memset(bigtm, 0, sizeof *bigtm); bigtm->sec = tm->tm_sec; @@ -43,15 +34,9 @@ tm2Tm(struct tm *tm, Tm *bigtm, int gmt) bigtm->mon = tm->tm_mon; bigtm->year = tm->tm_year; bigtm->wday = tm->tm_wday; - if(gmt){ - strcpy(bigtm->zone, "GMT"); - bigtm->tzoff = 0; - }else{ - dotz(); - strncpy(bigtm->zone, tzone, 3); - bigtm->zone[3] = 0; - bigtm->tzoff = tzdelta; - } + bigtm->tzoff = tzoff; + strncpy(bigtm->zone, zone, 3); + bigtm->zone[3] = 0; } static void @@ -80,7 +65,7 @@ p9gmtime(long x) t = (time_t)x; tm = *gmtime(&t); - tm2Tm(&tm, &bigtm, 1); + tm2Tm(&tm, &bigtm, 0, "GMT"); return &bigtm; } @@ -90,10 +75,11 @@ p9localtime(long x) time_t t; struct tm tm; static Tm bigtm; + char tzone[32]; t = (time_t)x; tm = *localtime(&t); - tm2Tm(&tm, &bigtm, 0); + tm2Tm(&tm, &bigtm, dotz(t, tzone), tzone); return &bigtm; } @@ -102,12 +88,12 @@ p9tm2sec(Tm *bigtm) { time_t t; struct tm tm; + char tzone[32]; Tm2tm(bigtm, &tm); t = mktime(&tm); if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0){ - dotz(); - t += tzdelta; + t += dotz(t, tzone); } return t; } -- cgit v1.2.3