Skip to content

Commit c2b315e

Browse files
committed
datetime.isoformat()
1 parent de23ca5 commit c2b315e

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

Modules/_datetimemodule.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6236,12 +6236,28 @@ datetime_str(PyDateTime_DateTime *self)
62366236
return res;
62376237
}
62386238

6239+
/*[clinic input]
6240+
datetime.datetime.isoformat
6241+
6242+
defcls: defining_class
6243+
sep: int(accept={str}, c_default="'T'", py_default="'T'") = 0
6244+
timespec: str = NULL
6245+
6246+
[sep] -> string in ISO 8601 format,
6247+
6248+
YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].
6249+
sep is used to separate the year from the time, and defaults to 'T'.
6250+
The optional argument timespec specifies the number of additional terms
6251+
of the time to include. Valid options are 'auto', 'hours', 'minutes',
6252+
'seconds', 'milliseconds' and 'microseconds'.
6253+
[clinic start generated code]*/
6254+
62396255
static PyObject *
6240-
datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
6256+
datetime_datetime_isoformat_impl(PyDateTime_DateTime *self,
6257+
PyTypeObject *defcls, int sep,
6258+
const char *timespec)
6259+
/*[clinic end generated code: output=d7968a085a5f38c7 input=dbd22819421fe7be]*/
62416260
{
6242-
int sep = 'T';
6243-
char *timespec = NULL;
6244-
static char *keywords[] = {"sep", "timespec", NULL};
62456261
char buffer[100];
62466262
PyObject *result = NULL;
62476263
int us = DATE_GET_MICROSECOND(self);
@@ -6254,9 +6270,6 @@ datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
62546270
};
62556271
size_t given_spec;
62566272

6257-
if (!PyArg_ParseTupleAndKeywords(args, kw, "|Cs:isoformat", keywords, &sep, &timespec))
6258-
return NULL;
6259-
62606273
if (timespec == NULL || strcmp(timespec, "auto") == 0) {
62616274
if (us == 0) {
62626275
/* seconds */
@@ -6294,7 +6307,7 @@ datetime_isoformat(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
62946307
return result;
62956308

62966309
/* We need to append the UTC offset. */
6297-
datetime_state *st = find_module_state_by_def(Py_TYPE(self));
6310+
datetime_state *st = get_module_state_by_cls(defcls);
62986311
if (format_utcoffset(st, buffer, sizeof(buffer), ":", self->tzinfo,
62996312
(PyObject *)self) < 0) {
63006313
Py_DECREF(result);
@@ -7056,16 +7069,7 @@ static PyMethodDef datetime_methods[] = {
70567069
{"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS,
70577070
PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")},
70587071

7059-
{"isoformat", _PyCFunction_CAST(datetime_isoformat), METH_VARARGS | METH_KEYWORDS,
7060-
PyDoc_STR("[sep] -> string in ISO 8601 format, "
7061-
"YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].\n"
7062-
"sep is used to separate the year from the time, and "
7063-
"defaults to 'T'.\n"
7064-
"The optional argument timespec specifies the number "
7065-
"of additional terms\nof the time to include. Valid "
7066-
"options are 'auto', 'hours', 'minutes',\n'seconds', "
7067-
"'milliseconds' and 'microseconds'.\n")},
7068-
7072+
DATETIME_DATETIME_ISOFORMAT_METHODDEF
70697073
DATETIME_DATETIME_UTCOFFSET_METHODDEF
70707074
DATETIME_DATETIME_TZNAME_METHODDEF
70717075
DATETIME_DATETIME_DST_METHODDEF

0 commit comments

Comments
 (0)