Skip to content

Commit b21bded

Browse files
committed
date.fromordinal()
1 parent 8ba2af7 commit b21bded

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

Modules/_datetimemodule.c

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3141,35 +3141,60 @@ datetime_date_fromtimestamp_capi(PyObject *cls, PyObject *args)
31413141
return result;
31423142
}
31433143

3144-
/* Return new date from proleptic Gregorian ordinal. Raises ValueError if
3145-
* the ordinal is out of range.
3146-
*/
3144+
/*[clinic input]
3145+
@classmethod
3146+
datetime.date.fromordinal
3147+
3148+
cls: self(type="PyObject *")
3149+
defcls: defining_class
3150+
ordinal: int
3151+
/
3152+
3153+
int -> date corresponding to a proleptic Gregorian ordinal.
3154+
3155+
Return new date from proleptic Gregorian ordinal. Raises ValueError
3156+
if the ordinal is out of range.
3157+
[clinic start generated code]*/
3158+
31473159
static PyObject *
3148-
date_fromordinal(PyObject *cls, PyObject *args)
3160+
datetime_date_fromordinal_impl(PyObject *cls, PyTypeObject *defcls,
3161+
int ordinal)
3162+
/*[clinic end generated code: output=fa7ca4621f1ee8be input=76c85e045fdfbc97]*/
31493163
{
31503164
PyObject *result = NULL;
3151-
int ordinal;
3152-
3153-
if (PyArg_ParseTuple(args, "i:fromordinal", &ordinal)) {
3154-
int year;
3155-
int month;
3156-
int day;
3165+
int year;
3166+
int month;
3167+
int day;
31573168

3158-
if (ordinal < 1)
3159-
PyErr_SetString(PyExc_ValueError, "ordinal must be "
3160-
">= 1");
3161-
else {
3162-
ord_to_ymd(ordinal, &year, &month, &day);
3163-
datetime_state *st = find_module_state_by_def(cls);
3164-
result = new_date_subclass_ex(st, year, month, day, cls);
3165-
}
3169+
if (ordinal < 1) {
3170+
PyErr_SetString(PyExc_ValueError, "ordinal must be >= 1");
3171+
}
3172+
else {
3173+
ord_to_ymd(ordinal, &year, &month, &day);
3174+
datetime_state *st = get_module_state_by_cls(defcls);
3175+
result = new_date_subclass_ex(st, year, month, day, cls);
31663176
}
31673177
return result;
31683178
}
31693179

3170-
/* Return the new date from a string as generated by date.isoformat() */
3180+
/*[clinic input]
3181+
@classmethod
3182+
datetime.date.fromisoformat
3183+
3184+
cls: self(type="PyObject *")
3185+
defcls: defining_class
3186+
dtstr: object
3187+
/
3188+
3189+
str -> Construct a date from a string in ISO 8601 format.
3190+
3191+
Return the new date from a string as generated by date.isoformat().
3192+
[clinic start generated code]*/
3193+
31713194
static PyObject *
3172-
date_fromisoformat(PyObject *cls, PyObject *dtstr)
3195+
datetime_date_fromisoformat_impl(PyObject *cls, PyTypeObject *defcls,
3196+
PyObject *dtstr)
3197+
/*[clinic end generated code: output=c382fc14a39f8b89 input=67564908cc8f0013]*/
31733198
{
31743199
assert(dtstr != NULL);
31753200

@@ -3199,7 +3224,7 @@ date_fromisoformat(PyObject *cls, PyObject *dtstr)
31993224
if (rv < 0) {
32003225
goto invalid_string_error;
32013226
}
3202-
datetime_state *st = find_module_state_by_def(cls);
3227+
datetime_state *st = get_module_state_by_cls(defcls);
32033228
return new_date_subclass_ex(st, year, month, day, cls);
32043229

32053230
invalid_string_error:
@@ -3717,15 +3742,8 @@ static PyMethodDef date_methods[] = {
37173742

37183743
/* Class methods: */
37193744
DATETIME_DATE_FROMTIMESTAMP_METHODDEF
3720-
3721-
{"fromordinal", (PyCFunction)date_fromordinal, METH_VARARGS |
3722-
METH_CLASS,
3723-
PyDoc_STR("int -> date corresponding to a proleptic Gregorian "
3724-
"ordinal.")},
3725-
3726-
{"fromisoformat", (PyCFunction)date_fromisoformat, METH_O |
3727-
METH_CLASS,
3728-
PyDoc_STR("str -> Construct a date from a string in ISO 8601 format.")},
3745+
DATETIME_DATE_FROMORDINAL_METHODDEF
3746+
DATETIME_DATE_FROMISOFORMAT_METHODDEF
37293747

37303748
{"fromisocalendar", _PyCFunction_CAST(date_fromisocalendar),
37313749
METH_VARARGS | METH_KEYWORDS | METH_CLASS,

0 commit comments

Comments
 (0)