cinap_lenrek 2011-06-04 21:36:15 +00:00
commit ff0bd4e552
7 changed files with 82 additions and 131 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

@ -243,6 +243,13 @@ converts its numerical argument, a character number, to a
.SM UTF .SM UTF
string string
.TP .TP
.BI substr( s , " m")
the maximum length substring of
.I s
that begins at position
.IR m
counted from 1.
.TP
.BI substr( s , " m" , " n\fL) .BI substr( s , " m" , " n\fL)
the the
.IR n -character .IR n -character
@ -303,6 +310,10 @@ If
is not given, is not given,
.B $0 .B $0
is used. is used.
.B &
in
.I t
is replaced by the match.
.TP .TP
.B gsub .B gsub
same as same as

View File

@ -255,6 +255,30 @@ Append the pattern space to
if a replacement if a replacement
was made. was made.
.RE .RE
.IP
An ampersand
.L &
appearing in the replacement
is replaced by the string matching the regular expression.
The characters
.BI \e n\f1,
where
.I n
is a digit,
are replaced by the text matched by the
.IR n -th
regular subexpression
enclosed between
.L (
and
.LR ) .
When
nested parenthesized subexpressions
are present,
.I n
is determined by counting occurrences of
.L (
starting from the left.
.TP .TP
.BI t " label" .BI t " label"
Test. Test.

View File

@ -110,8 +110,8 @@ mktime(struct tm *t)
d = ptm->tm_hour; d = ptm->tm_hour;
ptm = localtime(&a); ptm = localtime(&a);
d -= ptm->tm_hour; d -= ptm->tm_hour;
if(d < 0) // if(d < 0)
d += 24; // d += 24;
if(t->tm_isdst == 0 && ptm->tm_isdst) if(t->tm_isdst == 0 && ptm->tm_isdst)
d--; d--;
if(t->tm_isdst > 0 && !ptm->tm_isdst) if(t->tm_isdst > 0 && !ptm->tm_isdst)

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

View File

@ -45,7 +45,6 @@ typedef unsigned short u_short;
typedef unsigned long u_long; typedef unsigned long u_long;
#define EISCONN 63 #define EISCONN 63
#define EWOULDBLOCK 64
#define FD_SETSIZE 96 /* see /sys/include/ape/sys/select.h */ #define FD_SETSIZE 96 /* see /sys/include/ape/sys/select.h */