localtime: use correct time zone more often (Michael Teichgräber)
This commit is contained in:
parent
865e8b0fb3
commit
57ce0d667e
1 changed files with 13 additions and 27 deletions
|
|
@ -4,22 +4,13 @@
|
||||||
#include <stdlib.h> /* setenv etc. */
|
#include <stdlib.h> /* setenv etc. */
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
static int didtz;
|
static int
|
||||||
static int tzdelta;
|
dotz(time_t t, char *tzone)
|
||||||
static char tzone[32];
|
|
||||||
|
|
||||||
static void
|
|
||||||
dotz(void)
|
|
||||||
{
|
{
|
||||||
time_t t;
|
|
||||||
struct tm *gtm;
|
struct tm *gtm;
|
||||||
struct tm tm;
|
struct tm tm;
|
||||||
|
|
||||||
if(didtz)
|
strftime(tzone, 32, "%Z", localtime(&t));
|
||||||
return;
|
|
||||||
didtz = 1;
|
|
||||||
t = time(0);
|
|
||||||
strftime(tzone, sizeof tzone, "%Z", localtime(&t));
|
|
||||||
tm = *localtime(&t); /* set local time zone field */
|
tm = *localtime(&t); /* set local time zone field */
|
||||||
gtm = gmtime(&t);
|
gtm = gmtime(&t);
|
||||||
tm.tm_sec = gtm->tm_sec;
|
tm.tm_sec = gtm->tm_sec;
|
||||||
|
|
@ -29,11 +20,11 @@ dotz(void)
|
||||||
tm.tm_mon = gtm->tm_mon;
|
tm.tm_mon = gtm->tm_mon;
|
||||||
tm.tm_year = gtm->tm_year;
|
tm.tm_year = gtm->tm_year;
|
||||||
tm.tm_wday = gtm->tm_wday;
|
tm.tm_wday = gtm->tm_wday;
|
||||||
tzdelta = t - mktime(&tm);
|
return t - mktime(&tm);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
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);
|
memset(bigtm, 0, sizeof *bigtm);
|
||||||
bigtm->sec = tm->tm_sec;
|
bigtm->sec = tm->tm_sec;
|
||||||
|
|
@ -43,15 +34,9 @@ tm2Tm(struct tm *tm, Tm *bigtm, int gmt)
|
||||||
bigtm->mon = tm->tm_mon;
|
bigtm->mon = tm->tm_mon;
|
||||||
bigtm->year = tm->tm_year;
|
bigtm->year = tm->tm_year;
|
||||||
bigtm->wday = tm->tm_wday;
|
bigtm->wday = tm->tm_wday;
|
||||||
if(gmt){
|
bigtm->tzoff = tzoff;
|
||||||
strcpy(bigtm->zone, "GMT");
|
strncpy(bigtm->zone, zone, 3);
|
||||||
bigtm->tzoff = 0;
|
|
||||||
}else{
|
|
||||||
dotz();
|
|
||||||
strncpy(bigtm->zone, tzone, 3);
|
|
||||||
bigtm->zone[3] = 0;
|
bigtm->zone[3] = 0;
|
||||||
bigtm->tzoff = tzdelta;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
@ -80,7 +65,7 @@ p9gmtime(long x)
|
||||||
|
|
||||||
t = (time_t)x;
|
t = (time_t)x;
|
||||||
tm = *gmtime(&t);
|
tm = *gmtime(&t);
|
||||||
tm2Tm(&tm, &bigtm, 1);
|
tm2Tm(&tm, &bigtm, 0, "GMT");
|
||||||
return &bigtm;
|
return &bigtm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,10 +75,11 @@ p9localtime(long x)
|
||||||
time_t t;
|
time_t t;
|
||||||
struct tm tm;
|
struct tm tm;
|
||||||
static Tm bigtm;
|
static Tm bigtm;
|
||||||
|
char tzone[32];
|
||||||
|
|
||||||
t = (time_t)x;
|
t = (time_t)x;
|
||||||
tm = *localtime(&t);
|
tm = *localtime(&t);
|
||||||
tm2Tm(&tm, &bigtm, 0);
|
tm2Tm(&tm, &bigtm, dotz(t, tzone), tzone);
|
||||||
return &bigtm;
|
return &bigtm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -102,12 +88,12 @@ p9tm2sec(Tm *bigtm)
|
||||||
{
|
{
|
||||||
time_t t;
|
time_t t;
|
||||||
struct tm tm;
|
struct tm tm;
|
||||||
|
char tzone[32];
|
||||||
|
|
||||||
Tm2tm(bigtm, &tm);
|
Tm2tm(bigtm, &tm);
|
||||||
t = mktime(&tm);
|
t = mktime(&tm);
|
||||||
if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0){
|
if(strcmp(bigtm->zone, "GMT") == 0 || strcmp(bigtm->zone, "UCT") == 0){
|
||||||
dotz();
|
t += dotz(t, tzone);
|
||||||
t += tzdelta;
|
|
||||||
}
|
}
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue