1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9 #include "pycore_modsupport.h" // _PyArg_UnpackKeywords()
10
11 PyDoc_STRVAR(datetime_date_fromtimestamp__doc__,
12 "fromtimestamp($type, timestamp, /)\n"
13 "--\n"
14 "\n"
15 "Create a date from a POSIX timestamp.\n"
16 "\n"
17 "The timestamp is a number, e.g. created via time.time(), that is interpreted\n"
18 "as local time.");
19
20 #define DATETIME_DATE_FROMTIMESTAMP_METHODDEF \
21 {"fromtimestamp", (PyCFunction)datetime_date_fromtimestamp, METH_O|METH_CLASS, datetime_date_fromtimestamp__doc__},
22
23 static PyObject *
24 iso_calendar_date_new_impl(PyTypeObject *type, int year, int week,
25 int weekday);
26
27 static PyObject *
iso_calendar_date_new(PyTypeObject * type,PyObject * args,PyObject * kwargs)28 iso_calendar_date_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
29 {
30 PyObject *return_value = NULL;
31 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
32
33 #define NUM_KEYWORDS 3
34 static struct {
35 PyGC_Head _this_is_not_used;
36 PyObject_VAR_HEAD
37 PyObject *ob_item[NUM_KEYWORDS];
38 } _kwtuple = {
39 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
40 .ob_item = { &_Py_ID(year), &_Py_ID(week), &_Py_ID(weekday), },
41 };
42 #undef NUM_KEYWORDS
43 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
44
45 #else // !Py_BUILD_CORE
46 # define KWTUPLE NULL
47 #endif // !Py_BUILD_CORE
48
49 static const char * const _keywords[] = {"year", "week", "weekday", NULL};
50 static _PyArg_Parser _parser = {
51 .keywords = _keywords,
52 .fname = "IsoCalendarDate",
53 .kwtuple = KWTUPLE,
54 };
55 #undef KWTUPLE
56 PyObject *argsbuf[3];
57 PyObject * const *fastargs;
58 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
59 int year;
60 int week;
61 int weekday;
62
63 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 3, 3, 0, argsbuf);
64 if (!fastargs) {
65 goto exit;
66 }
67 year = PyLong_AsInt(fastargs[0]);
68 if (year == -1 && PyErr_Occurred()) {
69 goto exit;
70 }
71 week = PyLong_AsInt(fastargs[1]);
72 if (week == -1 && PyErr_Occurred()) {
73 goto exit;
74 }
75 weekday = PyLong_AsInt(fastargs[2]);
76 if (weekday == -1 && PyErr_Occurred()) {
77 goto exit;
78 }
79 return_value = iso_calendar_date_new_impl(type, year, week, weekday);
80
81 exit:
82 return return_value;
83 }
84
85 PyDoc_STRVAR(datetime_date_replace__doc__,
86 "replace($self, /, year=unchanged, month=unchanged, day=unchanged)\n"
87 "--\n"
88 "\n"
89 "Return date with new specified fields.");
90
91 #define DATETIME_DATE_REPLACE_METHODDEF \
92 {"replace", _PyCFunction_CAST(datetime_date_replace), METH_FASTCALL|METH_KEYWORDS, datetime_date_replace__doc__},
93
94 static PyObject *
95 datetime_date_replace_impl(PyDateTime_Date *self, int year, int month,
96 int day);
97
98 static PyObject *
datetime_date_replace(PyDateTime_Date * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)99 datetime_date_replace(PyDateTime_Date *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
100 {
101 PyObject *return_value = NULL;
102 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
103
104 #define NUM_KEYWORDS 3
105 static struct {
106 PyGC_Head _this_is_not_used;
107 PyObject_VAR_HEAD
108 PyObject *ob_item[NUM_KEYWORDS];
109 } _kwtuple = {
110 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
111 .ob_item = { &_Py_ID(year), &_Py_ID(month), &_Py_ID(day), },
112 };
113 #undef NUM_KEYWORDS
114 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
115
116 #else // !Py_BUILD_CORE
117 # define KWTUPLE NULL
118 #endif // !Py_BUILD_CORE
119
120 static const char * const _keywords[] = {"year", "month", "day", NULL};
121 static _PyArg_Parser _parser = {
122 .keywords = _keywords,
123 .fname = "replace",
124 .kwtuple = KWTUPLE,
125 };
126 #undef KWTUPLE
127 PyObject *argsbuf[3];
128 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
129 int year = GET_YEAR(self);
130 int month = GET_MONTH(self);
131 int day = GET_DAY(self);
132
133 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 3, 0, argsbuf);
134 if (!args) {
135 goto exit;
136 }
137 if (!noptargs) {
138 goto skip_optional_pos;
139 }
140 if (args[0]) {
141 year = PyLong_AsInt(args[0]);
142 if (year == -1 && PyErr_Occurred()) {
143 goto exit;
144 }
145 if (!--noptargs) {
146 goto skip_optional_pos;
147 }
148 }
149 if (args[1]) {
150 month = PyLong_AsInt(args[1]);
151 if (month == -1 && PyErr_Occurred()) {
152 goto exit;
153 }
154 if (!--noptargs) {
155 goto skip_optional_pos;
156 }
157 }
158 day = PyLong_AsInt(args[2]);
159 if (day == -1 && PyErr_Occurred()) {
160 goto exit;
161 }
162 skip_optional_pos:
163 return_value = datetime_date_replace_impl(self, year, month, day);
164
165 exit:
166 return return_value;
167 }
168
169 PyDoc_STRVAR(datetime_time_replace__doc__,
170 "replace($self, /, hour=unchanged, minute=unchanged, second=unchanged,\n"
171 " microsecond=unchanged, tzinfo=unchanged, *, fold=unchanged)\n"
172 "--\n"
173 "\n"
174 "Return time with new specified fields.");
175
176 #define DATETIME_TIME_REPLACE_METHODDEF \
177 {"replace", _PyCFunction_CAST(datetime_time_replace), METH_FASTCALL|METH_KEYWORDS, datetime_time_replace__doc__},
178
179 static PyObject *
180 datetime_time_replace_impl(PyDateTime_Time *self, int hour, int minute,
181 int second, int microsecond, PyObject *tzinfo,
182 int fold);
183
184 static PyObject *
datetime_time_replace(PyDateTime_Time * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)185 datetime_time_replace(PyDateTime_Time *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
186 {
187 PyObject *return_value = NULL;
188 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
189
190 #define NUM_KEYWORDS 6
191 static struct {
192 PyGC_Head _this_is_not_used;
193 PyObject_VAR_HEAD
194 PyObject *ob_item[NUM_KEYWORDS];
195 } _kwtuple = {
196 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
197 .ob_item = { &_Py_ID(hour), &_Py_ID(minute), &_Py_ID(second), &_Py_ID(microsecond), &_Py_ID(tzinfo), &_Py_ID(fold), },
198 };
199 #undef NUM_KEYWORDS
200 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
201
202 #else // !Py_BUILD_CORE
203 # define KWTUPLE NULL
204 #endif // !Py_BUILD_CORE
205
206 static const char * const _keywords[] = {"hour", "minute", "second", "microsecond", "tzinfo", "fold", NULL};
207 static _PyArg_Parser _parser = {
208 .keywords = _keywords,
209 .fname = "replace",
210 .kwtuple = KWTUPLE,
211 };
212 #undef KWTUPLE
213 PyObject *argsbuf[6];
214 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
215 int hour = TIME_GET_HOUR(self);
216 int minute = TIME_GET_MINUTE(self);
217 int second = TIME_GET_SECOND(self);
218 int microsecond = TIME_GET_MICROSECOND(self);
219 PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None;
220 int fold = TIME_GET_FOLD(self);
221
222 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 5, 0, argsbuf);
223 if (!args) {
224 goto exit;
225 }
226 if (!noptargs) {
227 goto skip_optional_pos;
228 }
229 if (args[0]) {
230 hour = PyLong_AsInt(args[0]);
231 if (hour == -1 && PyErr_Occurred()) {
232 goto exit;
233 }
234 if (!--noptargs) {
235 goto skip_optional_pos;
236 }
237 }
238 if (args[1]) {
239 minute = PyLong_AsInt(args[1]);
240 if (minute == -1 && PyErr_Occurred()) {
241 goto exit;
242 }
243 if (!--noptargs) {
244 goto skip_optional_pos;
245 }
246 }
247 if (args[2]) {
248 second = PyLong_AsInt(args[2]);
249 if (second == -1 && PyErr_Occurred()) {
250 goto exit;
251 }
252 if (!--noptargs) {
253 goto skip_optional_pos;
254 }
255 }
256 if (args[3]) {
257 microsecond = PyLong_AsInt(args[3]);
258 if (microsecond == -1 && PyErr_Occurred()) {
259 goto exit;
260 }
261 if (!--noptargs) {
262 goto skip_optional_pos;
263 }
264 }
265 if (args[4]) {
266 tzinfo = args[4];
267 if (!--noptargs) {
268 goto skip_optional_pos;
269 }
270 }
271 skip_optional_pos:
272 if (!noptargs) {
273 goto skip_optional_kwonly;
274 }
275 fold = PyLong_AsInt(args[5]);
276 if (fold == -1 && PyErr_Occurred()) {
277 goto exit;
278 }
279 skip_optional_kwonly:
280 return_value = datetime_time_replace_impl(self, hour, minute, second, microsecond, tzinfo, fold);
281
282 exit:
283 return return_value;
284 }
285
286 PyDoc_STRVAR(datetime_datetime_now__doc__,
287 "now($type, /, tz=None)\n"
288 "--\n"
289 "\n"
290 "Returns new datetime object representing current time local to tz.\n"
291 "\n"
292 " tz\n"
293 " Timezone object.\n"
294 "\n"
295 "If no tz is specified, uses local timezone.");
296
297 #define DATETIME_DATETIME_NOW_METHODDEF \
298 {"now", _PyCFunction_CAST(datetime_datetime_now), METH_FASTCALL|METH_KEYWORDS|METH_CLASS, datetime_datetime_now__doc__},
299
300 static PyObject *
301 datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz);
302
303 static PyObject *
datetime_datetime_now(PyTypeObject * type,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)304 datetime_datetime_now(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
305 {
306 PyObject *return_value = NULL;
307 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
308
309 #define NUM_KEYWORDS 1
310 static struct {
311 PyGC_Head _this_is_not_used;
312 PyObject_VAR_HEAD
313 PyObject *ob_item[NUM_KEYWORDS];
314 } _kwtuple = {
315 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
316 .ob_item = { &_Py_ID(tz), },
317 };
318 #undef NUM_KEYWORDS
319 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
320
321 #else // !Py_BUILD_CORE
322 # define KWTUPLE NULL
323 #endif // !Py_BUILD_CORE
324
325 static const char * const _keywords[] = {"tz", NULL};
326 static _PyArg_Parser _parser = {
327 .keywords = _keywords,
328 .fname = "now",
329 .kwtuple = KWTUPLE,
330 };
331 #undef KWTUPLE
332 PyObject *argsbuf[1];
333 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
334 PyObject *tz = Py_None;
335
336 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
337 if (!args) {
338 goto exit;
339 }
340 if (!noptargs) {
341 goto skip_optional_pos;
342 }
343 tz = args[0];
344 skip_optional_pos:
345 return_value = datetime_datetime_now_impl(type, tz);
346
347 exit:
348 return return_value;
349 }
350
351 PyDoc_STRVAR(datetime_datetime_replace__doc__,
352 "replace($self, /, year=unchanged, month=unchanged, day=unchanged,\n"
353 " hour=unchanged, minute=unchanged, second=unchanged,\n"
354 " microsecond=unchanged, tzinfo=unchanged, *, fold=unchanged)\n"
355 "--\n"
356 "\n"
357 "Return datetime with new specified fields.");
358
359 #define DATETIME_DATETIME_REPLACE_METHODDEF \
360 {"replace", _PyCFunction_CAST(datetime_datetime_replace), METH_FASTCALL|METH_KEYWORDS, datetime_datetime_replace__doc__},
361
362 static PyObject *
363 datetime_datetime_replace_impl(PyDateTime_DateTime *self, int year,
364 int month, int day, int hour, int minute,
365 int second, int microsecond, PyObject *tzinfo,
366 int fold);
367
368 static PyObject *
datetime_datetime_replace(PyDateTime_DateTime * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)369 datetime_datetime_replace(PyDateTime_DateTime *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
370 {
371 PyObject *return_value = NULL;
372 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
373
374 #define NUM_KEYWORDS 9
375 static struct {
376 PyGC_Head _this_is_not_used;
377 PyObject_VAR_HEAD
378 PyObject *ob_item[NUM_KEYWORDS];
379 } _kwtuple = {
380 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
381 .ob_item = { &_Py_ID(year), &_Py_ID(month), &_Py_ID(day), &_Py_ID(hour), &_Py_ID(minute), &_Py_ID(second), &_Py_ID(microsecond), &_Py_ID(tzinfo), &_Py_ID(fold), },
382 };
383 #undef NUM_KEYWORDS
384 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
385
386 #else // !Py_BUILD_CORE
387 # define KWTUPLE NULL
388 #endif // !Py_BUILD_CORE
389
390 static const char * const _keywords[] = {"year", "month", "day", "hour", "minute", "second", "microsecond", "tzinfo", "fold", NULL};
391 static _PyArg_Parser _parser = {
392 .keywords = _keywords,
393 .fname = "replace",
394 .kwtuple = KWTUPLE,
395 };
396 #undef KWTUPLE
397 PyObject *argsbuf[9];
398 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
399 int year = GET_YEAR(self);
400 int month = GET_MONTH(self);
401 int day = GET_DAY(self);
402 int hour = DATE_GET_HOUR(self);
403 int minute = DATE_GET_MINUTE(self);
404 int second = DATE_GET_SECOND(self);
405 int microsecond = DATE_GET_MICROSECOND(self);
406 PyObject *tzinfo = HASTZINFO(self) ? self->tzinfo : Py_None;
407 int fold = DATE_GET_FOLD(self);
408
409 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 8, 0, argsbuf);
410 if (!args) {
411 goto exit;
412 }
413 if (!noptargs) {
414 goto skip_optional_pos;
415 }
416 if (args[0]) {
417 year = PyLong_AsInt(args[0]);
418 if (year == -1 && PyErr_Occurred()) {
419 goto exit;
420 }
421 if (!--noptargs) {
422 goto skip_optional_pos;
423 }
424 }
425 if (args[1]) {
426 month = PyLong_AsInt(args[1]);
427 if (month == -1 && PyErr_Occurred()) {
428 goto exit;
429 }
430 if (!--noptargs) {
431 goto skip_optional_pos;
432 }
433 }
434 if (args[2]) {
435 day = PyLong_AsInt(args[2]);
436 if (day == -1 && PyErr_Occurred()) {
437 goto exit;
438 }
439 if (!--noptargs) {
440 goto skip_optional_pos;
441 }
442 }
443 if (args[3]) {
444 hour = PyLong_AsInt(args[3]);
445 if (hour == -1 && PyErr_Occurred()) {
446 goto exit;
447 }
448 if (!--noptargs) {
449 goto skip_optional_pos;
450 }
451 }
452 if (args[4]) {
453 minute = PyLong_AsInt(args[4]);
454 if (minute == -1 && PyErr_Occurred()) {
455 goto exit;
456 }
457 if (!--noptargs) {
458 goto skip_optional_pos;
459 }
460 }
461 if (args[5]) {
462 second = PyLong_AsInt(args[5]);
463 if (second == -1 && PyErr_Occurred()) {
464 goto exit;
465 }
466 if (!--noptargs) {
467 goto skip_optional_pos;
468 }
469 }
470 if (args[6]) {
471 microsecond = PyLong_AsInt(args[6]);
472 if (microsecond == -1 && PyErr_Occurred()) {
473 goto exit;
474 }
475 if (!--noptargs) {
476 goto skip_optional_pos;
477 }
478 }
479 if (args[7]) {
480 tzinfo = args[7];
481 if (!--noptargs) {
482 goto skip_optional_pos;
483 }
484 }
485 skip_optional_pos:
486 if (!noptargs) {
487 goto skip_optional_kwonly;
488 }
489 fold = PyLong_AsInt(args[8]);
490 if (fold == -1 && PyErr_Occurred()) {
491 goto exit;
492 }
493 skip_optional_kwonly:
494 return_value = datetime_datetime_replace_impl(self, year, month, day, hour, minute, second, microsecond, tzinfo, fold);
495
496 exit:
497 return return_value;
498 }
499 /*[clinic end generated code: output=c7a04b865b1e0890 input=a9049054013a1b77]*/
500