Make the 'time' argument to the timemodule functions strftime, asctime,
ctime, gmtime and localtime optional, defaulting to 'the current time' in all cases. Adjust docs, add news item. Also convert all argument-handling to METH_VARARGS. Closes SF patch #103265.
This commit is contained in:
parent
5566c1ce36
commit
fe385251f4
|
@ -116,11 +116,12 @@ is defined. This is negative if the local DST timezone is east of UTC
|
|||
\code{daylight} is nonzero.
|
||||
\end{datadesc}
|
||||
|
||||
\begin{funcdesc}{asctime}{tuple}
|
||||
\begin{funcdesc}{asctime}{\optional{tuple}}
|
||||
Convert a tuple representing a time as returned by \function{gmtime()}
|
||||
or \function{localtime()} to a 24-character string of the following form:
|
||||
\code{'Sun Jun 20 23:21:05 1993'}. Note: unlike the C function of
|
||||
the same name, there is no trailing newline.
|
||||
\code{'Sun Jun 20 23:21:05 1993'}. If \var{tuple} is not provided, the
|
||||
current time as returned by \function{localtime()} is used. Note: unlike
|
||||
the C function of the same name, there is no trailing newline.
|
||||
\end{funcdesc}
|
||||
|
||||
\begin{funcdesc}{clock}{}
|
||||
|
@ -131,23 +132,26 @@ of the same name, but in any case, this is the function to use for
|
|||
benchmarking\index{benchmarking} Python or timing algorithms.
|
||||
\end{funcdesc}
|
||||
|
||||
\begin{funcdesc}{ctime}{secs}
|
||||
\begin{funcdesc}{ctime}{\optional{secs}}
|
||||
Convert a time expressed in seconds since the epoch to a string
|
||||
representing local time. \code{ctime(\var{secs})} is equivalent to
|
||||
\code{asctime(localtime(\var{secs}))}.
|
||||
representing local time. If \var{secs} is not provided, the current time
|
||||
as returned by \function{time()} is used. \code{ctime(\var{secs})}
|
||||
is equivalent to \code{asctime(localtime(\var{secs}))}.
|
||||
\end{funcdesc}
|
||||
|
||||
\begin{datadesc}{daylight}
|
||||
Nonzero if a DST timezone is defined.
|
||||
\end{datadesc}
|
||||
|
||||
\begin{funcdesc}{gmtime}{secs}
|
||||
\begin{funcdesc}{gmtime}{\optional{secs}}
|
||||
Convert a time expressed in seconds since the epoch to a time tuple
|
||||
in UTC in which the dst flag is always zero. Fractions of a second are
|
||||
ignored. See above for a description of the tuple lay-out.
|
||||
in UTC in which the dst flag is always zero. If \var{secs} is not
|
||||
provided, the current time as returned by \function{time()} is used.
|
||||
Fractions of a second are ignored. See above for a description of the
|
||||
tuple lay-out.
|
||||
\end{funcdesc}
|
||||
|
||||
\begin{funcdesc}{localtime}{secs}
|
||||
\begin{funcdesc}{localtime}{\optional{secs}}
|
||||
Like \function{gmtime()} but converts to local time. The dst flag is
|
||||
set to \code{1} when DST applies to the given time.
|
||||
\end{funcdesc}
|
||||
|
@ -171,10 +175,11 @@ time may be longer than requested by an arbitrary amount because of
|
|||
the scheduling of other activity in the system.
|
||||
\end{funcdesc}
|
||||
|
||||
\begin{funcdesc}{strftime}{format, tuple}
|
||||
\begin{funcdesc}{strftime}{format\optional{, tuple}}
|
||||
Convert a tuple representing a time as returned by \function{gmtime()}
|
||||
or \function{localtime()} to a string as specified by the \var{format}
|
||||
argument. \var{format} must be a string.
|
||||
argument. If \var{tuple} is not provided, the current time as returned by
|
||||
\function{localtime()} is used. \var{format} must be a string.
|
||||
|
||||
The following directives can be embedded in the \var{format} string.
|
||||
They are shown without the optional field width and precision
|
||||
|
|
|
@ -169,6 +169,10 @@ Core language, builtins, and interpreter
|
|||
|
||||
Standard library
|
||||
|
||||
- In the time module, the time argument to the functions strftime,
|
||||
localtime, gmtime, asctime and ctime is now optional, defaulting to
|
||||
the current time (in the local timezone).
|
||||
|
||||
- The ftplib module now defaults to passive mode, which is deemed a
|
||||
more useful default given that clients are often inside firewalls
|
||||
these days. Note that this could break if ftplib is used to connect
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue