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.
|
\code{daylight} is nonzero.
|
||||||
\end{datadesc}
|
\end{datadesc}
|
||||||
|
|
||||||
\begin{funcdesc}{asctime}{tuple}
|
\begin{funcdesc}{asctime}{\optional{tuple}}
|
||||||
Convert a tuple representing a time as returned by \function{gmtime()}
|
Convert a tuple representing a time as returned by \function{gmtime()}
|
||||||
or \function{localtime()} to a 24-character string of the following form:
|
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
|
\code{'Sun Jun 20 23:21:05 1993'}. If \var{tuple} is not provided, the
|
||||||
the same name, there is no trailing newline.
|
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}
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{funcdesc}{clock}{}
|
\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.
|
benchmarking\index{benchmarking} Python or timing algorithms.
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{funcdesc}{ctime}{secs}
|
\begin{funcdesc}{ctime}{\optional{secs}}
|
||||||
Convert a time expressed in seconds since the epoch to a string
|
Convert a time expressed in seconds since the epoch to a string
|
||||||
representing local time. \code{ctime(\var{secs})} is equivalent to
|
representing local time. If \var{secs} is not provided, the current time
|
||||||
\code{asctime(localtime(\var{secs}))}.
|
as returned by \function{time()} is used. \code{ctime(\var{secs})}
|
||||||
|
is equivalent to \code{asctime(localtime(\var{secs}))}.
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{datadesc}{daylight}
|
\begin{datadesc}{daylight}
|
||||||
Nonzero if a DST timezone is defined.
|
Nonzero if a DST timezone is defined.
|
||||||
\end{datadesc}
|
\end{datadesc}
|
||||||
|
|
||||||
\begin{funcdesc}{gmtime}{secs}
|
\begin{funcdesc}{gmtime}{\optional{secs}}
|
||||||
Convert a time expressed in seconds since the epoch to a time tuple
|
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
|
in UTC in which the dst flag is always zero. If \var{secs} is not
|
||||||
ignored. See above for a description of the tuple lay-out.
|
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}
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{funcdesc}{localtime}{secs}
|
\begin{funcdesc}{localtime}{\optional{secs}}
|
||||||
Like \function{gmtime()} but converts to local time. The dst flag is
|
Like \function{gmtime()} but converts to local time. The dst flag is
|
||||||
set to \code{1} when DST applies to the given time.
|
set to \code{1} when DST applies to the given time.
|
||||||
\end{funcdesc}
|
\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.
|
the scheduling of other activity in the system.
|
||||||
\end{funcdesc}
|
\end{funcdesc}
|
||||||
|
|
||||||
\begin{funcdesc}{strftime}{format, tuple}
|
\begin{funcdesc}{strftime}{format\optional{, tuple}}
|
||||||
Convert a tuple representing a time as returned by \function{gmtime()}
|
Convert a tuple representing a time as returned by \function{gmtime()}
|
||||||
or \function{localtime()} to a string as specified by the \var{format}
|
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.
|
The following directives can be embedded in the \var{format} string.
|
||||||
They are shown without the optional field width and precision
|
They are shown without the optional field width and precision
|
||||||
|
|
|
@ -169,6 +169,10 @@ Core language, builtins, and interpreter
|
||||||
|
|
||||||
Standard library
|
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
|
- The ftplib module now defaults to passive mode, which is deemed a
|
||||||
more useful default given that clients are often inside firewalls
|
more useful default given that clients are often inside firewalls
|
||||||
these days. Note that this could break if ftplib is used to connect
|
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)
|
time_time(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
double secs;
|
double secs;
|
||||||
if (!PyArg_NoArgs(args))
|
if (!PyArg_ParseTuple(args, ":time"))
|
||||||
return NULL;
|
return NULL;
|
||||||
secs = floattime();
|
secs = floattime();
|
||||||
if (secs == 0.0) {
|
if (secs == 0.0) {
|
||||||
|
@ -138,7 +138,7 @@ Fractions of a second may be present if the system clock provides them.";
|
||||||
static PyObject *
|
static PyObject *
|
||||||
time_clock(PyObject *self, PyObject *args)
|
time_clock(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
if (!PyArg_NoArgs(args))
|
if (!PyArg_ParseTuple(args, ":clock"))
|
||||||
return NULL;
|
return NULL;
|
||||||
return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
|
return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
|
||||||
}
|
}
|
||||||
|
@ -153,7 +153,7 @@ time_clock(PyObject *self, PyObject *args)
|
||||||
static LARGE_INTEGER divisor = {0,0};
|
static LARGE_INTEGER divisor = {0,0};
|
||||||
LARGE_INTEGER now, diff, rem;
|
LARGE_INTEGER now, diff, rem;
|
||||||
|
|
||||||
if (!PyArg_NoArgs(args))
|
if (!PyArg_ParseTuple(args, ":clock"))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (LargeIntegerEqualToZero(divisor)) {
|
if (LargeIntegerEqualToZero(divisor)) {
|
||||||
|
@ -192,7 +192,7 @@ static PyObject *
|
||||||
time_sleep(PyObject *self, PyObject *args)
|
time_sleep(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
double secs;
|
double secs;
|
||||||
if (!PyArg_Parse(args, "d", &secs))
|
if (!PyArg_ParseTuple(args, "d:sleep", &secs))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (floatsleep(secs) != 0)
|
if (floatsleep(secs) != 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -244,28 +244,34 @@ static PyObject *
|
||||||
time_gmtime(PyObject *self, PyObject *args)
|
time_gmtime(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
double when;
|
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 NULL;
|
||||||
return time_convert((time_t)when, gmtime);
|
return time_convert((time_t)when, gmtime);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char gmtime_doc[] =
|
static char gmtime_doc[] =
|
||||||
"gmtime(seconds) -> tuple\n\
|
"gmtime([seconds]) -> tuple\n\
|
||||||
\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 *
|
static PyObject *
|
||||||
time_localtime(PyObject *self, PyObject *args)
|
time_localtime(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
double when;
|
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 NULL;
|
||||||
return time_convert((time_t)when, localtime);
|
return time_convert((time_t)when, localtime);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char localtime_doc[] =
|
static char localtime_doc[] =
|
||||||
"localtime(seconds) -> tuple\n\
|
"localtime([seconds]) -> tuple\n\
|
||||||
Convert seconds since the Epoch to a time tuple expressing local time.";
|
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
|
static int
|
||||||
gettmarg(PyObject *args, struct tm *p)
|
gettmarg(PyObject *args, struct tm *p)
|
||||||
|
@ -314,7 +320,7 @@ gettmarg(PyObject *args, struct tm *p)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
time_strftime(PyObject *self, PyObject *args)
|
time_strftime(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *tup;
|
PyObject *tup = NULL;
|
||||||
struct tm buf;
|
struct tm buf;
|
||||||
const char *fmt;
|
const char *fmt;
|
||||||
size_t fmtlen, buflen;
|
size_t fmtlen, buflen;
|
||||||
|
@ -323,9 +329,15 @@ time_strftime(PyObject *self, PyObject *args)
|
||||||
|
|
||||||
memset((void *) &buf, '\0', sizeof(buf));
|
memset((void *) &buf, '\0', sizeof(buf));
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "sO:strftime", &fmt, &tup)
|
if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
|
||||||
|| !gettmarg(tup, &buf))
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
if (tup == NULL) {
|
||||||
|
time_t tt = time(NULL);
|
||||||
|
buf = *localtime(&tt);
|
||||||
|
} else if (!gettmarg(tup, &buf))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
fmtlen = strlen(fmt);
|
fmtlen = strlen(fmt);
|
||||||
|
|
||||||
/* I hate these functions that presume you know how big the output
|
/* 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[] =
|
static char strftime_doc[] =
|
||||||
"strftime(format, tuple) -> string\n\
|
"strftime(format[, tuple]) -> string\n\
|
||||||
\n\
|
\n\
|
||||||
Convert a time tuple to a string according to a format specification.\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 */
|
#endif /* HAVE_STRFTIME */
|
||||||
|
|
||||||
#ifdef HAVE_STRPTIME
|
#ifdef HAVE_STRPTIME
|
||||||
|
@ -401,12 +414,15 @@ See the library reference manual for formatting codes (same as strftime()).";
|
||||||
static PyObject *
|
static PyObject *
|
||||||
time_asctime(PyObject *self, PyObject *args)
|
time_asctime(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
PyObject *tup;
|
PyObject *tup = NULL;
|
||||||
struct tm buf;
|
struct tm buf;
|
||||||
char *p;
|
char *p;
|
||||||
if (!PyArg_ParseTuple(args, "O:asctime", &tup))
|
if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (!gettmarg(tup, &buf))
|
if (tup == NULL) {
|
||||||
|
time_t tt = time(NULL);
|
||||||
|
buf = *localtime(&tt);
|
||||||
|
} else if (!gettmarg(tup, &buf))
|
||||||
return NULL;
|
return NULL;
|
||||||
p = asctime(&buf);
|
p = asctime(&buf);
|
||||||
if (p[24] == '\n')
|
if (p[24] == '\n')
|
||||||
|
@ -415,9 +431,11 @@ time_asctime(PyObject *self, PyObject *args)
|
||||||
}
|
}
|
||||||
|
|
||||||
static char asctime_doc[] =
|
static char asctime_doc[] =
|
||||||
"asctime(tuple) -> string\n\
|
"asctime([tuple]) -> string\n\
|
||||||
\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 *
|
static PyObject *
|
||||||
time_ctime(PyObject *self, PyObject *args)
|
time_ctime(PyObject *self, PyObject *args)
|
||||||
|
@ -425,9 +443,14 @@ time_ctime(PyObject *self, PyObject *args)
|
||||||
double dt;
|
double dt;
|
||||||
time_t tt;
|
time_t tt;
|
||||||
char *p;
|
char *p;
|
||||||
if (!PyArg_Parse(args, "d", &dt))
|
|
||||||
return NULL;
|
if (PyTuple_Size(args) == 0)
|
||||||
tt = (time_t)dt;
|
tt = time(NULL);
|
||||||
|
else {
|
||||||
|
if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
|
||||||
|
return NULL;
|
||||||
|
tt = (time_t)dt;
|
||||||
|
}
|
||||||
#if defined(macintosh) && defined(USE_GUSI204)
|
#if defined(macintosh) && defined(USE_GUSI204)
|
||||||
tt = tt + GUSI_TO_MSL_EPOCH;
|
tt = tt + GUSI_TO_MSL_EPOCH;
|
||||||
#endif
|
#endif
|
||||||
|
@ -445,7 +468,8 @@ static char ctime_doc[] =
|
||||||
"ctime(seconds) -> string\n\
|
"ctime(seconds) -> string\n\
|
||||||
\n\
|
\n\
|
||||||
Convert a time in seconds since the Epoch to a string in local time.\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
|
#ifdef HAVE_MKTIME
|
||||||
static PyObject *
|
static PyObject *
|
||||||
|
@ -479,15 +503,15 @@ Convert a time tuple in local time to seconds since the Epoch.";
|
||||||
#endif /* HAVE_MKTIME */
|
#endif /* HAVE_MKTIME */
|
||||||
|
|
||||||
static PyMethodDef time_methods[] = {
|
static PyMethodDef time_methods[] = {
|
||||||
{"time", time_time, METH_OLDARGS, time_doc},
|
{"time", time_time, METH_VARARGS, time_doc},
|
||||||
#ifdef HAVE_CLOCK
|
#ifdef HAVE_CLOCK
|
||||||
{"clock", time_clock, METH_OLDARGS, clock_doc},
|
{"clock", time_clock, METH_VARARGS, clock_doc},
|
||||||
#endif
|
#endif
|
||||||
{"sleep", time_sleep, METH_OLDARGS, sleep_doc},
|
{"sleep", time_sleep, METH_VARARGS, sleep_doc},
|
||||||
{"gmtime", time_gmtime, METH_OLDARGS, gmtime_doc},
|
{"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
|
||||||
{"localtime", time_localtime, METH_OLDARGS, localtime_doc},
|
{"localtime", time_localtime, METH_VARARGS, localtime_doc},
|
||||||
{"asctime", time_asctime, METH_VARARGS, asctime_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
|
#ifdef HAVE_MKTIME
|
||||||
{"mktime", time_mktime, METH_VARARGS, mktime_doc},
|
{"mktime", time_mktime, METH_VARARGS, mktime_doc},
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue