fixed tzset(), hopefully hg timezone bug should be fixed now

This commit is contained in:
aiju 2011-06-04 12:02:42 +02:00
parent 13ae620a45
commit 67daf453a7
3 changed files with 45 additions and 128 deletions

View file

@ -66,6 +66,8 @@ extern int nanosleep(const struct timespec *req, struct timespec *rem);
#ifdef _POSIX_SOURCE #ifdef _POSIX_SOURCE
extern char *tzname[2]; extern char *tzname[2];
extern long timezone;
extern int daylight;
#endif #endif
#endif /* __TIME_H */ #endif /* __TIME_H */

View file

@ -6,136 +6,54 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#define TZFILE "/etc/TZ"
static char TZ[128];
static char std[32] = "GMT0"; static char std[32] = "GMT0";
static char dst[32]; static char dst[32];
char *tzname[2] = { char *tzname[2] = {
std, dst std, dst
}; };
time_t tzoffset, tzdstoffset; long timezone;
int tzdst = 0; int daylight;
static int
offset(char *env, time_t *off)
{
int n, sign;
size_t len, retlen;
retlen = 0;
sign = 1;
/*
* strictly, no sign is allowed in the 'time' part of the
* dst start/stop rules, but who cares?
*/
if (*env == '-' || *env == '+') {
if (*env++ == '-')
sign = -1;
retlen++;
}
if ((len = strspn(env, ":0123456789")) == 0)
return 0;
retlen += len;
for (*off = 0; len && isdigit(*env); len--) /* hours */
*off = *off*10 + (*env++ - '0')*60*60;
if (len) {
if (*env++ != ':')
return 0;
len--;
}
for (n = 0; len && isdigit(*env); len--) /* minutes */
n = n*10 + (*env++ - '0')*60;
*off += n;
if (len) {
if (*env++ != ':')
return 0;
len--;
}
for (n = 0; len && isdigit(*env); len--) /* seconds */
n = n*10 + (*env++ - '0');
*off = (*off + n)*sign;
return retlen;
}
/*
* TZ=stdoffset[dst[offset][,start[/time],end[/time]]]
*/
void void
tzset(void) tzset(void)
{ {
char *env, *p, envbuf[128]; char *env, *p, *q;
int fd, i;
size_t len, retlen; if((p = getenv("timezone")) == 0)
time_t off; goto error;
if((env = malloc(strlen(p) + 1)) == 0)
goto error;
strcpy(env, p);
if((p = strchr(env, ' ')) == 0)
goto error;
*p = 0;
strncpy(std, env, sizeof std);
q = p + 1;
if((p = strchr(q, ' ')) == 0)
goto error;
timezone = - atoi(q);
q = p + 1;
if((p = strchr(q, ' ')) == 0)
goto nodst;
*p = 0;
strncpy(dst, q, sizeof dst);
q = p + 1;
daylight = 1;
free(env);
return;
/* error:
* get the TZ environment variable and check for validity. strcpy(std, "GMT0");
* the implementation-defined manner for dealing with the dst[0] = '\0';
* leading ':' format is to reject it. timezone = 0;
* if it's ok, stash a copy away for quick comparison next time. daylight = 0;
*/ if(env != 0)
if ((env = getenv("TZ")) == 0) { free(env);
if ((fd = open(TZFILE, O_RDONLY)) == -1) return;
return;
if (read(fd, envbuf, sizeof(envbuf)-1) == -1) { nodst:
close(fd); dst[0] = '\0';
return; daylight = 0;
} free(env);
close(fd); return;
for (i = 0; i < sizeof(envbuf); i++) {
if (envbuf[i] != '\n')
continue;
envbuf[i] = '\0';
break;
}
env = envbuf;
}
if (strcmp(env, TZ) == 0)
return;
if (*env == 0 || *env == ':')
return;
strncpy(TZ, env, sizeof(TZ)-1);
TZ[sizeof(TZ)-1] = 0;
/*
* get the 'std' string.
* before committing, check there is a valid offset.
*/
if ((len = strcspn(env, ":0123456789,-+")) == 0)
return;
if ((retlen = offset(env+len, &off)) == 0)
return;
for (p = std, i = len+retlen; i; i--)
*p++ = *env++;
*p = 0;
tzoffset = -off;
/*
* get the 'dst' string (if any).
*/
if (*env == 0 || (len = strcspn(env, ":0123456789,-+")) == 0)
return;
for (p = dst; len; len--)
*p++ = *env++;
*p = 0;
/*
* optional dst offset.
* default is one hour.
*/
tzdst = 1;
if (retlen = offset(env+len, &off)) {
tzdstoffset = -off;
env += retlen;
}
else
tzdstoffset = tzoffset + 60*60;
/*
* optional rule(s) for start/end of dst.
*/
if (*env == 0 || *env != ',' || *(env+1) == 0)
return;
env++;
/*
* we could go on...
* but why bother?
*/
} }

View file

@ -678,8 +678,7 @@ void inittimezone(PyObject *m) {
PyModule_AddIntConstant(m, "timezone", _timezone); PyModule_AddIntConstant(m, "timezone", _timezone);
#else /* !PYOS_OS2 */ #else /* !PYOS_OS2 */
#ifdef PLAN9APE #ifdef PLAN9APE
//PyModule_AddIntConstant(m, "timezone", timezone); PyModule_AddIntConstant(m, "timezone", timezone);
PyModule_AddIntConstant(m, "timezone", 0);
#endif #endif
#endif /* PYOS_OS2 */ #endif /* PYOS_OS2 */
#ifdef HAVE_ALTZONE #ifdef HAVE_ALTZONE
@ -689,14 +688,12 @@ void inittimezone(PyObject *m) {
PyModule_AddIntConstant(m, "altzone", _timezone-3600); PyModule_AddIntConstant(m, "altzone", _timezone-3600);
#else /* !PYOS_OS2 */ #else /* !PYOS_OS2 */
#ifdef PLAN9APE #ifdef PLAN9APE
//PyModule_AddIntConstant(m, "altzone", timezone-3600); PyModule_AddIntConstant(m, "altzone", timezone-3600);
PyModule_AddIntConstant(m, "altzone", 3600);
#endif #endif
#endif /* PYOS_OS2 */ #endif /* PYOS_OS2 */
#endif #endif
#ifdef PLAN9APE #ifdef PLAN9APE
//PyModule_AddIntConstant(m, "daylight", daylight); PyModule_AddIntConstant(m, "daylight", daylight);
PyModule_AddIntConstant(m, "daylight", 0);
PyModule_AddObject(m, "tzname", PyModule_AddObject(m, "tzname",
Py_BuildValue("(zz)", tzname[0], tzname[1])); Py_BuildValue("(zz)", tzname[0], tzname[1]));
#endif #endif