diff options
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/timemodule.c | 84 |
1 files changed, 54 insertions, 30 deletions
diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 31c1d21..d4b2f37 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -109,7 +109,7 @@ static PyObject * time_time(PyObject *self, PyObject *args) { double secs; - if (!PyArg_NoArgs(args)) + if (!PyArg_ParseTuple(args, ":time")) return NULL; secs = floattime(); if (secs == 0.0) { @@ -138,7 +138,7 @@ Fractions of a second may be present if the system clock provides them."; static PyObject * time_clock(PyObject *self, PyObject *args) { - if (!PyArg_NoArgs(args)) + if (!PyArg_ParseTuple(args, ":clock")) return NULL; return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC); } @@ -153,7 +153,7 @@ time_clock(PyObject *self, PyObject *args) static LARGE_INTEGER divisor = {0,0}; LARGE_INTEGER now, diff, rem; - if (!PyArg_NoArgs(args)) + if (!PyArg_ParseTuple(args, ":clock")) return NULL; if (LargeIntegerEqualToZero(divisor)) { @@ -192,7 +192,7 @@ static PyObject * time_sleep(PyObject *self, PyObject *args) { double secs; - if (!PyArg_Parse(args, "d", &secs)) + if (!PyArg_ParseTuple(args, "d:sleep", &secs)) return NULL; if (floatsleep(secs) != 0) return NULL; @@ -244,28 +244,34 @@ static PyObject * time_gmtime(PyObject *self, PyObject *args) { double when; - if (!PyArg_Parse(args, "d", &when)) + if (PyTuple_Size(args) == 0) + when = floattime(); + if (!PyArg_ParseTuple(args, "|d:gmtime", &when)) return NULL; return time_convert((time_t)when, gmtime); } static char gmtime_doc[] = -"gmtime(seconds) -> tuple\n\ +"gmtime([seconds]) -> tuple\n\ \n\ -Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT)."; +Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\ +GMT). When 'seconds' is not passed in, convert the current time instead."; static PyObject * time_localtime(PyObject *self, PyObject *args) { double when; - if (!PyArg_Parse(args, "d", &when)) + if (PyTuple_Size(args) == 0) + when = floattime(); + if (!PyArg_ParseTuple(args, "|d:localtime", &when)) return NULL; return time_convert((time_t)when, localtime); } static char localtime_doc[] = -"localtime(seconds) -> tuple\n\ -Convert seconds since the Epoch to a time tuple expressing local time."; +"localtime([seconds]) -> tuple\n\ +Convert seconds since the Epoch to a time tuple expressing local time.\n\ +When 'seconds' is not passed in, convert the current time instead."; static int gettmarg(PyObject *args, struct tm *p) @@ -314,7 +320,7 @@ gettmarg(PyObject *args, struct tm *p) static PyObject * time_strftime(PyObject *self, PyObject *args) { - PyObject *tup; + PyObject *tup = NULL; struct tm buf; const char *fmt; size_t fmtlen, buflen; @@ -323,9 +329,15 @@ time_strftime(PyObject *self, PyObject *args) memset((void *) &buf, '\0', sizeof(buf)); - if (!PyArg_ParseTuple(args, "sO:strftime", &fmt, &tup) - || !gettmarg(tup, &buf)) + if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup)) + return NULL; + + if (tup == NULL) { + time_t tt = time(NULL); + buf = *localtime(&tt); + } else if (!gettmarg(tup, &buf)) return NULL; + fmtlen = strlen(fmt); /* I hate these functions that presume you know how big the output @@ -353,10 +365,11 @@ time_strftime(PyObject *self, PyObject *args) } static char strftime_doc[] = -"strftime(format, tuple) -> string\n\ +"strftime(format[, tuple]) -> string\n\ \n\ Convert a time tuple to a string according to a format specification.\n\ -See the library reference manual for formatting codes."; +See the library reference manual for formatting codes. When the time tuple\n\ +is not present, current time as returned by localtime() is used."; #endif /* HAVE_STRFTIME */ #ifdef HAVE_STRPTIME @@ -401,12 +414,15 @@ See the library reference manual for formatting codes (same as strftime())."; static PyObject * time_asctime(PyObject *self, PyObject *args) { - PyObject *tup; + PyObject *tup = NULL; struct tm buf; char *p; - if (!PyArg_ParseTuple(args, "O:asctime", &tup)) + if (!PyArg_ParseTuple(args, "|O:asctime", &tup)) return NULL; - if (!gettmarg(tup, &buf)) + if (tup == NULL) { + time_t tt = time(NULL); + buf = *localtime(&tt); + } else if (!gettmarg(tup, &buf)) return NULL; p = asctime(&buf); if (p[24] == '\n') @@ -415,9 +431,11 @@ time_asctime(PyObject *self, PyObject *args) } static char asctime_doc[] = -"asctime(tuple) -> string\n\ +"asctime([tuple]) -> string\n\ \n\ -Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'."; +Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\ +When the time tuple is not present, current time as returned by localtime()\n\ +is used."; static PyObject * time_ctime(PyObject *self, PyObject *args) @@ -425,9 +443,14 @@ time_ctime(PyObject *self, PyObject *args) double dt; time_t tt; char *p; - if (!PyArg_Parse(args, "d", &dt)) - return NULL; - tt = (time_t)dt; + + if (PyTuple_Size(args) == 0) + tt = time(NULL); + else { + if (!PyArg_ParseTuple(args, "|d:ctime", &dt)) + return NULL; + tt = (time_t)dt; + } #if defined(macintosh) && defined(USE_GUSI204) tt = tt + GUSI_TO_MSL_EPOCH; #endif @@ -445,7 +468,8 @@ static char ctime_doc[] = "ctime(seconds) -> string\n\ \n\ Convert a time in seconds since the Epoch to a string in local time.\n\ -This is equivalent to asctime(localtime(seconds))."; +This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\ +not present, current time as returned by localtime() is used."; #ifdef HAVE_MKTIME static PyObject * @@ -479,15 +503,15 @@ Convert a time tuple in local time to seconds since the Epoch."; #endif /* HAVE_MKTIME */ static PyMethodDef time_methods[] = { - {"time", time_time, METH_OLDARGS, time_doc}, + {"time", time_time, METH_VARARGS, time_doc}, #ifdef HAVE_CLOCK - {"clock", time_clock, METH_OLDARGS, clock_doc}, + {"clock", time_clock, METH_VARARGS, clock_doc}, #endif - {"sleep", time_sleep, METH_OLDARGS, sleep_doc}, - {"gmtime", time_gmtime, METH_OLDARGS, gmtime_doc}, - {"localtime", time_localtime, METH_OLDARGS, localtime_doc}, + {"sleep", time_sleep, METH_VARARGS, sleep_doc}, + {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc}, + {"localtime", time_localtime, METH_VARARGS, localtime_doc}, {"asctime", time_asctime, METH_VARARGS, asctime_doc}, - {"ctime", time_ctime, METH_OLDARGS, ctime_doc}, + {"ctime", time_ctime, METH_VARARGS, ctime_doc}, #ifdef HAVE_MKTIME {"mktime", time_mktime, METH_VARARGS, mktime_doc}, #endif |