blob: 8a4d149befb52a451fa06ad8d19c3934ef46e78c [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Time module */
2
Barry Warsaw9a2a8a81996-12-06 23:32:14 +00003#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00004
Guido van Rossum87ce7bb1998-06-09 16:30:31 +00005#include <ctype.h>
6
Victor Stinnerec895392012-04-29 02:41:27 +02007#ifdef HAVE_SYS_TIMES_H
8#include <sys/times.h>
9#endif
10
Thomas Wouters0e3f5912006-08-11 14:57:12 +000011#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000012#include <sys/types.h>
Victor Stinnerec895392012-04-29 02:41:27 +020013#endif
14
15#if defined(HAVE_SYS_RESOURCE_H)
16#include <sys/resource.h>
17#endif
Guido van Rossum6d946f91992-08-14 13:49:30 +000018
Guido van Rossumb6775db1994-08-01 11:34:53 +000019#ifdef QUICKWIN
20#include <io.h>
21#endif
22
pdoxe14679c2017-10-05 00:01:56 -070023#if defined(HAVE_PTHREAD_H)
24# include <pthread.h>
25#endif
26
Batuhan Taskaya45410862020-05-16 12:39:09 +030027#if defined(_AIX)
28# include <sys/thread.h>
29#endif
30
Guido van Rossum7bf22de1997-12-02 20:34:19 +000031#if defined(__WATCOMC__) && !defined(__QNX__)
Victor Stinner62183b82020-04-15 02:04:42 +020032# include <i86.h>
Guido van Rossumbceeac81996-05-23 22:53:47 +000033#else
Victor Stinner62183b82020-04-15 02:04:42 +020034# ifdef MS_WINDOWS
35# define WIN32_LEAN_AND_MEAN
36# include <windows.h>
37# endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000038#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000039
Gregory P. Smithb474e672018-12-30 17:05:36 -080040#ifdef _Py_MEMORY_SANITIZER
41# include <sanitizer/msan_interface.h>
42#endif
43
Zackery Spytz6673dec2019-02-25 16:56:44 -070044#ifdef _MSC_VER
45#define _Py_timezone _timezone
46#define _Py_daylight _daylight
47#define _Py_tzname _tzname
48#else
49#define _Py_timezone timezone
50#define _Py_daylight daylight
51#define _Py_tzname tzname
52#endif
53
Victor Stinnerc29b5852017-11-02 07:28:27 -070054#define SEC_TO_NS (1000 * 1000 * 1000)
55
Guido van Rossum234f9421993-06-17 12:35:49 +000056/* Forward declarations */
Victor Stinnercb29f012015-03-27 13:31:18 +010057static int pysleep(_PyTime_t);
Victor Stinnerec895392012-04-29 02:41:27 +020058
Victor Stinner85fdfa82012-01-27 00:38:48 +010059
Victor Stinnera997c7b2017-10-10 02:51:50 -070060static PyObject*
61_PyFloat_FromPyTime(_PyTime_t t)
62{
63 double d = _PyTime_AsSecondsDouble(t);
64 return PyFloat_FromDouble(d);
65}
66
Victor Stinnerc29b5852017-11-02 07:28:27 -070067
Victor Stinner4195b5c2012-02-08 23:03:19 +010068static PyObject *
Victor Stinnerc29b5852017-11-02 07:28:27 -070069time_time(PyObject *self, PyObject *unused)
Victor Stinner85fdfa82012-01-27 00:38:48 +010070{
Victor Stinnerc29b5852017-11-02 07:28:27 -070071 _PyTime_t t = _PyTime_GetSystemClock();
72 return _PyFloat_FromPyTime(t);
73}
74
75
76PyDoc_STRVAR(time_doc,
77"time() -> floating point number\n\
78\n\
79Return the current time in seconds since the Epoch.\n\
80Fractions of a second may be present if the system clock provides them.");
81
82static PyObject *
83time_time_ns(PyObject *self, PyObject *unused)
84{
85 _PyTime_t t = _PyTime_GetSystemClock();
86 return _PyTime_AsNanosecondsObject(t);
87}
88
89PyDoc_STRVAR(time_ns_doc,
90"time_ns() -> int\n\
91\n\
92Return the current time in nanoseconds since the Epoch.");
93
94#if defined(HAVE_CLOCK)
95
96#ifndef CLOCKS_PER_SEC
97# ifdef CLK_TCK
98# define CLOCKS_PER_SEC CLK_TCK
99# else
100# define CLOCKS_PER_SEC 1000000
101# endif
102#endif
103
104static int
105_PyTime_GetClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
106{
107 static int initialized = 0;
108 clock_t ticks;
109
110 if (!initialized) {
111 initialized = 1;
112
113 /* must sure that _PyTime_MulDiv(ticks, SEC_TO_NS, CLOCKS_PER_SEC)
114 above cannot overflow */
115 if ((_PyTime_t)CLOCKS_PER_SEC > _PyTime_MAX / SEC_TO_NS) {
116 PyErr_SetString(PyExc_OverflowError,
117 "CLOCKS_PER_SEC is too large");
118 return -1;
119 }
Victor Stinner85fdfa82012-01-27 00:38:48 +0100120 }
Victor Stinnerc29b5852017-11-02 07:28:27 -0700121
Victor Stinnerec895392012-04-29 02:41:27 +0200122 if (info) {
123 info->implementation = "clock()";
124 info->resolution = 1.0 / (double)CLOCKS_PER_SEC;
Benjamin Peterson49a69e42012-05-01 09:38:34 -0400125 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +0200126 info->adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +0200127 }
Victor Stinnerc29b5852017-11-02 07:28:27 -0700128
129 ticks = clock();
130 if (ticks == (clock_t)-1) {
131 PyErr_SetString(PyExc_RuntimeError,
132 "the processor time used is not available "
133 "or its value cannot be represented");
134 return -1;
135 }
136 *tp = _PyTime_MulDiv(ticks, SEC_TO_NS, (_PyTime_t)CLOCKS_PER_SEC);
137 return 0;
Victor Stinner85fdfa82012-01-27 00:38:48 +0100138}
139#endif /* HAVE_CLOCK */
140
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700141static PyObject*
142perf_counter(_Py_clock_info_t *info)
143{
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -0700144 _PyTime_t t;
145 if (_PyTime_GetPerfCounterWithInfo(&t, info) < 0) {
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700146 return NULL;
147 }
Victor Stinnerc29b5852017-11-02 07:28:27 -0700148 return _PyFloat_FromPyTime(t);
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700149}
150
Victor Stinnere0be4232011-10-25 13:06:09 +0200151#ifdef HAVE_CLOCK_GETTIME
152static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +0100153time_clock_gettime(PyObject *self, PyObject *args)
Victor Stinnere0be4232011-10-25 13:06:09 +0200154{
155 int ret;
Victor Stinnere0be4232011-10-25 13:06:09 +0200156 struct timespec tp;
157
Michael Felte2926b72018-12-28 14:57:37 +0100158#if defined(_AIX) && (SIZEOF_LONG == 8)
159 long clk_id;
160 if (!PyArg_ParseTuple(args, "l:clock_gettime", &clk_id)) {
161#else
162 int clk_id;
Victor Stinnerc29b5852017-11-02 07:28:27 -0700163 if (!PyArg_ParseTuple(args, "i:clock_gettime", &clk_id)) {
Michael Felte2926b72018-12-28 14:57:37 +0100164#endif
Victor Stinnere0be4232011-10-25 13:06:09 +0200165 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -0700166 }
Victor Stinnere0be4232011-10-25 13:06:09 +0200167
168 ret = clock_gettime((clockid_t)clk_id, &tp);
Antoine Pitrou2c085602012-01-18 01:41:44 +0100169 if (ret != 0) {
Victor Stinnera734af32014-07-31 13:07:17 +0200170 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrou2c085602012-01-18 01:41:44 +0100171 return NULL;
172 }
Victor Stinner4195b5c2012-02-08 23:03:19 +0100173 return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
Victor Stinnere0be4232011-10-25 13:06:09 +0200174}
175
176PyDoc_STRVAR(clock_gettime_doc,
Victor Stinnerc29b5852017-11-02 07:28:27 -0700177"clock_gettime(clk_id) -> float\n\
Victor Stinnere0be4232011-10-25 13:06:09 +0200178\n\
179Return the time of the specified clock clk_id.");
Victor Stinnerc29b5852017-11-02 07:28:27 -0700180
181static PyObject *
182time_clock_gettime_ns(PyObject *self, PyObject *args)
183{
184 int ret;
185 int clk_id;
186 struct timespec ts;
187 _PyTime_t t;
188
189 if (!PyArg_ParseTuple(args, "i:clock_gettime", &clk_id)) {
190 return NULL;
191 }
192
193 ret = clock_gettime((clockid_t)clk_id, &ts);
194 if (ret != 0) {
195 PyErr_SetFromErrno(PyExc_OSError);
196 return NULL;
197 }
198 if (_PyTime_FromTimespec(&t, &ts) < 0) {
199 return NULL;
200 }
201 return _PyTime_AsNanosecondsObject(t);
202}
203
204PyDoc_STRVAR(clock_gettime_ns_doc,
205"clock_gettime_ns(clk_id) -> int\n\
206\n\
207Return the time of the specified clock clk_id as nanoseconds.");
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700208#endif /* HAVE_CLOCK_GETTIME */
Victor Stinner30d79472012-04-03 00:45:07 +0200209
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700210#ifdef HAVE_CLOCK_SETTIME
Victor Stinner30d79472012-04-03 00:45:07 +0200211static PyObject *
212time_clock_settime(PyObject *self, PyObject *args)
213{
Victor Stinnerb8d01692012-04-13 23:44:05 +0200214 int clk_id;
Victor Stinner30d79472012-04-03 00:45:07 +0200215 PyObject *obj;
Victor Stinnerb3b45442015-03-28 04:09:41 +0100216 _PyTime_t t;
Victor Stinner30d79472012-04-03 00:45:07 +0200217 struct timespec tp;
218 int ret;
219
220 if (!PyArg_ParseTuple(args, "iO:clock_settime", &clk_id, &obj))
221 return NULL;
222
Victor Stinner02937aa2015-03-28 05:02:39 +0100223 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_FLOOR) < 0)
Victor Stinner30d79472012-04-03 00:45:07 +0200224 return NULL;
Victor Stinnerb3b45442015-03-28 04:09:41 +0100225
226 if (_PyTime_AsTimespec(t, &tp) == -1)
227 return NULL;
Victor Stinner30d79472012-04-03 00:45:07 +0200228
229 ret = clock_settime((clockid_t)clk_id, &tp);
230 if (ret != 0) {
Victor Stinnera734af32014-07-31 13:07:17 +0200231 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinner30d79472012-04-03 00:45:07 +0200232 return NULL;
233 }
234 Py_RETURN_NONE;
235}
236
237PyDoc_STRVAR(clock_settime_doc,
238"clock_settime(clk_id, time)\n\
239\n\
240Set the time of the specified clock clk_id.");
Victor Stinnerc29b5852017-11-02 07:28:27 -0700241
242static PyObject *
243time_clock_settime_ns(PyObject *self, PyObject *args)
244{
245 int clk_id;
246 PyObject *obj;
247 _PyTime_t t;
248 struct timespec ts;
249 int ret;
250
251 if (!PyArg_ParseTuple(args, "iO:clock_settime", &clk_id, &obj)) {
252 return NULL;
253 }
254
255 if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
256 return NULL;
257 }
258 if (_PyTime_AsTimespec(t, &ts) == -1) {
259 return NULL;
260 }
261
262 ret = clock_settime((clockid_t)clk_id, &ts);
263 if (ret != 0) {
264 PyErr_SetFromErrno(PyExc_OSError);
265 return NULL;
266 }
267 Py_RETURN_NONE;
268}
269
270PyDoc_STRVAR(clock_settime_ns_doc,
271"clock_settime_ns(clk_id, time)\n\
272\n\
273Set the time of the specified clock clk_id with nanoseconds.");
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700274#endif /* HAVE_CLOCK_SETTIME */
Victor Stinnere0be4232011-10-25 13:06:09 +0200275
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700276#ifdef HAVE_CLOCK_GETRES
Victor Stinnere0be4232011-10-25 13:06:09 +0200277static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +0100278time_clock_getres(PyObject *self, PyObject *args)
Victor Stinnere0be4232011-10-25 13:06:09 +0200279{
280 int ret;
Victor Stinnerb8d01692012-04-13 23:44:05 +0200281 int clk_id;
Victor Stinnere0be4232011-10-25 13:06:09 +0200282 struct timespec tp;
283
Victor Stinner4195b5c2012-02-08 23:03:19 +0100284 if (!PyArg_ParseTuple(args, "i:clock_getres", &clk_id))
Victor Stinnere0be4232011-10-25 13:06:09 +0200285 return NULL;
286
287 ret = clock_getres((clockid_t)clk_id, &tp);
Antoine Pitrou2c085602012-01-18 01:41:44 +0100288 if (ret != 0) {
Victor Stinnera734af32014-07-31 13:07:17 +0200289 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrou2c085602012-01-18 01:41:44 +0100290 return NULL;
291 }
Victor Stinner4195b5c2012-02-08 23:03:19 +0100292
293 return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
Victor Stinnere0be4232011-10-25 13:06:09 +0200294}
295
296PyDoc_STRVAR(clock_getres_doc,
Victor Stinner4195b5c2012-02-08 23:03:19 +0100297"clock_getres(clk_id) -> floating point number\n\
Victor Stinnere0be4232011-10-25 13:06:09 +0200298\n\
299Return the resolution (precision) of the specified clock clk_id.");
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700300#endif /* HAVE_CLOCK_GETRES */
Victor Stinnere0be4232011-10-25 13:06:09 +0200301
pdoxe14679c2017-10-05 00:01:56 -0700302#ifdef HAVE_PTHREAD_GETCPUCLOCKID
303static PyObject *
304time_pthread_getcpuclockid(PyObject *self, PyObject *args)
305{
306 unsigned long thread_id;
307 int err;
308 clockid_t clk_id;
309 if (!PyArg_ParseTuple(args, "k:pthread_getcpuclockid", &thread_id)) {
310 return NULL;
311 }
312 err = pthread_getcpuclockid((pthread_t)thread_id, &clk_id);
313 if (err) {
314 errno = err;
315 PyErr_SetFromErrno(PyExc_OSError);
316 return NULL;
317 }
Gregory P. Smithb474e672018-12-30 17:05:36 -0800318#ifdef _Py_MEMORY_SANITIZER
319 __msan_unpoison(&clk_id, sizeof(clk_id));
320#endif
pdoxe14679c2017-10-05 00:01:56 -0700321 return PyLong_FromLong(clk_id);
322}
323
324PyDoc_STRVAR(pthread_getcpuclockid_doc,
325"pthread_getcpuclockid(thread_id) -> int\n\
326\n\
327Return the clk_id of a thread's CPU time clock.");
328#endif /* HAVE_PTHREAD_GETCPUCLOCKID */
329
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000330static PyObject *
Victor Stinnercb29f012015-03-27 13:31:18 +0100331time_sleep(PyObject *self, PyObject *obj)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332{
Victor Stinnercb29f012015-03-27 13:31:18 +0100333 _PyTime_t secs;
Pablo Galindo59af94f2017-10-18 08:13:09 +0100334 if (_PyTime_FromSecondsObject(&secs, obj, _PyTime_ROUND_TIMEOUT))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 return NULL;
Victor Stinner7f53a502011-07-05 22:00:25 +0200336 if (secs < 0) {
337 PyErr_SetString(PyExc_ValueError,
338 "sleep length must be non-negative");
339 return NULL;
340 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100341 if (pysleep(secs) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200343 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000344}
345
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000346PyDoc_STRVAR(sleep_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000347"sleep(seconds)\n\
348\n\
349Delay execution for a given number of seconds. The argument may be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000350a floating point number for subsecond precision.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000351
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000352static PyStructSequence_Field struct_time_type_fields[] = {
Alexander Belopolsky69f3fd02010-06-05 15:04:51 +0000353 {"tm_year", "year, for example, 1993"},
354 {"tm_mon", "month of year, range [1, 12]"},
355 {"tm_mday", "day of month, range [1, 31]"},
356 {"tm_hour", "hours, range [0, 23]"},
357 {"tm_min", "minutes, range [0, 59]"},
358 {"tm_sec", "seconds, range [0, 61])"},
359 {"tm_wday", "day of week, range [0, 6], Monday is 0"},
360 {"tm_yday", "day of year, range [1, 366]"},
361 {"tm_isdst", "1 if summer time is in effect, 0 if not, and -1 if unknown"},
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400362 {"tm_zone", "abbreviation of timezone name"},
363 {"tm_gmtoff", "offset from UTC in seconds"},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000365};
366
367static PyStructSequence_Desc struct_time_type_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 "time.struct_time",
Alexander Belopolsky69f3fd02010-06-05 15:04:51 +0000369 "The time value as returned by gmtime(), localtime(), and strptime(), and\n"
370 " accepted by asctime(), mktime() and strftime(). May be considered as a\n"
371 " sequence of 9 integers.\n\n"
372 " Note that several fields' values are not the same as those defined by\n"
373 " the C language standard for struct tm. For example, the value of the\n"
374 " field tm_year is the actual year, not year - 1900. See individual\n"
375 " fields' descriptions for details.",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 struct_time_type_fields,
377 9,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000378};
Tim Peters9ad4b682002-02-13 05:14:18 +0000379
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000380static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000381static PyTypeObject StructTimeType;
382
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400383
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000384static PyObject *
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400385tmtotuple(struct tm *p
386#ifndef HAVE_STRUCT_TM_TM_ZONE
Victor Stinner0d659e52017-04-25 01:22:42 +0200387 , const char *zone, time_t gmtoff
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400388#endif
389)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 PyObject *v = PyStructSequence_New(&StructTimeType);
392 if (v == NULL)
393 return NULL;
Tim Peters9ad4b682002-02-13 05:14:18 +0000394
Christian Heimes217cfd12007-12-02 14:31:20 +0000395#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 SET(0, p->tm_year + 1900);
398 SET(1, p->tm_mon + 1); /* Want January == 1 */
399 SET(2, p->tm_mday);
400 SET(3, p->tm_hour);
401 SET(4, p->tm_min);
402 SET(5, p->tm_sec);
403 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
404 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
405 SET(8, p->tm_isdst);
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400406#ifdef HAVE_STRUCT_TM_TM_ZONE
407 PyStructSequence_SET_ITEM(v, 9,
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100408 PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape"));
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400409 SET(10, p->tm_gmtoff);
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400410#else
411 PyStructSequence_SET_ITEM(v, 9,
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100412 PyUnicode_DecodeLocale(zone, "surrogateescape"));
Victor Stinner0d659e52017-04-25 01:22:42 +0200413 PyStructSequence_SET_ITEM(v, 10, _PyLong_FromTime_t(gmtoff));
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400414#endif /* HAVE_STRUCT_TM_TM_ZONE */
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000415#undef SET
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 if (PyErr_Occurred()) {
417 Py_XDECREF(v);
418 return NULL;
419 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000422}
423
Fred Drakef901abd2004-08-03 17:58:55 +0000424/* Parse arg tuple that can contain an optional float-or-None value;
425 format needs to be "|O:name".
426 Returns non-zero on success (parallels PyArg_ParseTuple).
427*/
428static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200429parse_time_t_args(PyObject *args, const char *format, time_t *pwhen)
Fred Drakef901abd2004-08-03 17:58:55 +0000430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 PyObject *ot = NULL;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100432 time_t whent;
Fred Drakef901abd2004-08-03 17:58:55 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (!PyArg_ParseTuple(args, format, &ot))
435 return 0;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100436 if (ot == NULL || ot == Py_None) {
437 whent = time(NULL);
438 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 else {
Victor Stinner02937aa2015-03-28 05:02:39 +0100440 if (_PyTime_ObjectToTime_t(ot, &whent, _PyTime_ROUND_FLOOR) == -1)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100441 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 }
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100443 *pwhen = whent;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 return 1;
Fred Drakef901abd2004-08-03 17:58:55 +0000445}
446
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000447static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000448time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000449{
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100450 time_t when;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400451 struct tm buf;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100452
453 if (!parse_time_t_args(args, "|O:gmtime", &when))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 return NULL;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100455
456 errno = 0;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400457 if (_PyTime_gmtime(when, &buf) != 0)
458 return NULL;
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400459#ifdef HAVE_STRUCT_TM_TM_ZONE
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100460 return tmtotuple(&buf);
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400461#else
462 return tmtotuple(&buf, "UTC", 0);
463#endif
Guido van Rossum234f9421993-06-17 12:35:49 +0000464}
465
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400466#ifndef HAVE_TIMEGM
467static time_t
468timegm(struct tm *p)
469{
470 /* XXX: the following implementation will not work for tm_year < 1970.
471 but it is likely that platforms that don't have timegm do not support
472 negative timestamps anyways. */
473 return p->tm_sec + p->tm_min*60 + p->tm_hour*3600 + p->tm_yday*86400 +
474 (p->tm_year-70)*31536000 + ((p->tm_year-69)/4)*86400 -
475 ((p->tm_year-1)/100)*86400 + ((p->tm_year+299)/400)*86400;
476}
477#endif
478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000479PyDoc_STRVAR(gmtime_doc,
Christian Heimes9a371592007-12-28 14:08:13 +0000480"gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,\n\
Fred Drake193a3f62002-03-12 21:38:49 +0000481 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000482\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000483Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400484GMT). When 'seconds' is not passed in, convert the current time instead.\n\
485\n\
486If the platform supports the tm_gmtoff and tm_zone, they are available as\n\
487attributes only.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000488
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000489static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000490time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000491{
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100492 time_t when;
493 struct tm buf;
494
495 if (!parse_time_t_args(args, "|O:localtime", &when))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 return NULL;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400497 if (_PyTime_localtime(when, &buf) != 0)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100498 return NULL;
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400499#ifdef HAVE_STRUCT_TM_TM_ZONE
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100500 return tmtotuple(&buf);
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400501#else
502 {
503 struct tm local = buf;
504 char zone[100];
Victor Stinner0d659e52017-04-25 01:22:42 +0200505 time_t gmtoff;
Steve Dowerc3c6f712016-12-14 11:22:05 -0800506 strftime(zone, sizeof(zone), "%Z", &buf);
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400507 gmtoff = timegm(&buf) - when;
508 return tmtotuple(&local, zone, gmtoff);
509 }
510#endif
Guido van Rossum234f9421993-06-17 12:35:49 +0000511}
512
Benjamin Peterson5633c4f2018-09-14 09:09:04 -0700513#if defined(__linux__) && !defined(__GLIBC__)
514static const char *utc_string = NULL;
515#endif
516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000517PyDoc_STRVAR(localtime_doc,
Christian Heimes9a371592007-12-28 14:08:13 +0000518"localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 tm_sec,tm_wday,tm_yday,tm_isdst)\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000520\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000521Convert seconds since the Epoch to a time tuple expressing local time.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000522When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000523
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000524/* Convert 9-item tuple to tm structure. Return 1 on success, set
525 * an exception and return 0 on error.
526 */
Guido van Rossum9e90a671993-06-24 11:10:19 +0000527static int
Oren Milman1d1d3e92017-08-20 18:35:36 +0300528gettmarg(PyObject *args, struct tm *p, const char *format)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 int y;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumb9081262007-08-25 03:14:09 +0000533
Alexander Belopolsky610e5442011-01-06 21:57:06 +0000534 if (!PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 PyErr_SetString(PyExc_TypeError,
536 "Tuple or struct_time argument required");
537 return 0;
538 }
Skip Montanaro41cfce92007-08-24 21:11:00 +0000539
Oren Milman1d1d3e92017-08-20 18:35:36 +0300540 if (!PyArg_ParseTuple(args, format,
Alexander Belopolsky610e5442011-01-06 21:57:06 +0000541 &y, &p->tm_mon, &p->tm_mday,
542 &p->tm_hour, &p->tm_min, &p->tm_sec,
543 &p->tm_wday, &p->tm_yday, &p->tm_isdst))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 return 0;
Gregory P. Smith76be0ff2018-08-24 18:08:50 -0700545
546 if (y < INT_MIN + 1900) {
547 PyErr_SetString(PyExc_OverflowError, "year out of range");
548 return 0;
549 }
550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 p->tm_year = y - 1900;
552 p->tm_mon--;
553 p->tm_wday = (p->tm_wday + 1) % 7;
554 p->tm_yday--;
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400555#ifdef HAVE_STRUCT_TM_TM_ZONE
Dong-hee Na1b55b652020-02-17 19:09:15 +0900556 if (Py_IS_TYPE(args, &StructTimeType)) {
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400557 PyObject *item;
Victor Stinner1cdfcfc2018-11-28 15:19:51 +0100558 item = PyStructSequence_GET_ITEM(args, 9);
Benjamin Peterson5633c4f2018-09-14 09:09:04 -0700559 if (item != Py_None) {
Xiang Zhang163eca32018-10-28 23:58:42 +0800560 p->tm_zone = (char *)PyUnicode_AsUTF8(item);
Benjamin Peterson5633c4f2018-09-14 09:09:04 -0700561 if (p->tm_zone == NULL) {
562 return 0;
563 }
564#if defined(__linux__) && !defined(__GLIBC__)
565 // Make an attempt to return the C library's own timezone strings to
566 // it. musl refuses to process a tm_zone field unless it produced
567 // it. See issue #34672.
568 if (utc_string && strcmp(p->tm_zone, utc_string) == 0) {
569 p->tm_zone = utc_string;
570 }
571 else if (tzname[0] && strcmp(p->tm_zone, tzname[0]) == 0) {
572 p->tm_zone = tzname[0];
573 }
574 else if (tzname[1] && strcmp(p->tm_zone, tzname[1]) == 0) {
575 p->tm_zone = tzname[1];
576 }
577#endif
578 }
Victor Stinner1cdfcfc2018-11-28 15:19:51 +0100579 item = PyStructSequence_GET_ITEM(args, 10);
Benjamin Peterson5633c4f2018-09-14 09:09:04 -0700580 if (item != Py_None) {
581 p->tm_gmtoff = PyLong_AsLong(item);
582 if (PyErr_Occurred())
583 return 0;
584 }
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400585 }
586#endif /* HAVE_STRUCT_TM_TM_ZONE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 return 1;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000588}
589
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000590/* Check values of the struct tm fields before it is passed to strftime() and
591 * asctime(). Return 1 if all values are valid, otherwise set an exception
592 * and returns 0.
593 */
Victor Stinneref128102010-10-07 01:00:52 +0000594static int
595checktm(struct tm* buf)
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000596{
Victor Stinneref128102010-10-07 01:00:52 +0000597 /* Checks added to make sure strftime() and asctime() does not crash Python by
598 indexing blindly into some array for a textual representation
599 by some bad index (fixes bug #897625 and #6608).
600
601 Also support values of zero from Python code for arguments in which
602 that is out of range by forcing that value to the lowest value that
603 is valid (fixed bug #1520914).
604
605 Valid ranges based on what is allowed in struct tm:
606
607 - tm_year: [0, max(int)] (1)
608 - tm_mon: [0, 11] (2)
609 - tm_mday: [1, 31]
610 - tm_hour: [0, 23]
611 - tm_min: [0, 59]
612 - tm_sec: [0, 60]
613 - tm_wday: [0, 6] (1)
614 - tm_yday: [0, 365] (2)
615 - tm_isdst: [-max(int), max(int)]
616
617 (1) gettmarg() handles bounds-checking.
618 (2) Python's acceptable range is one greater than the range in C,
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000619 thus need to check against automatic decrement by gettmarg().
620 */
621 if (buf->tm_mon == -1)
622 buf->tm_mon = 0;
623 else if (buf->tm_mon < 0 || buf->tm_mon > 11) {
624 PyErr_SetString(PyExc_ValueError, "month out of range");
625 return 0;
626 }
627 if (buf->tm_mday == 0)
628 buf->tm_mday = 1;
629 else if (buf->tm_mday < 0 || buf->tm_mday > 31) {
630 PyErr_SetString(PyExc_ValueError, "day of month out of range");
631 return 0;
632 }
633 if (buf->tm_hour < 0 || buf->tm_hour > 23) {
634 PyErr_SetString(PyExc_ValueError, "hour out of range");
635 return 0;
636 }
637 if (buf->tm_min < 0 || buf->tm_min > 59) {
638 PyErr_SetString(PyExc_ValueError, "minute out of range");
639 return 0;
640 }
641 if (buf->tm_sec < 0 || buf->tm_sec > 61) {
642 PyErr_SetString(PyExc_ValueError, "seconds out of range");
643 return 0;
644 }
645 /* tm_wday does not need checking of its upper-bound since taking
646 ``% 7`` in gettmarg() automatically restricts the range. */
647 if (buf->tm_wday < 0) {
648 PyErr_SetString(PyExc_ValueError, "day of week out of range");
649 return 0;
650 }
651 if (buf->tm_yday == -1)
652 buf->tm_yday = 0;
653 else if (buf->tm_yday < 0 || buf->tm_yday > 365) {
654 PyErr_SetString(PyExc_ValueError, "day of year out of range");
655 return 0;
656 }
657 return 1;
658}
659
Victor Stinnerc1f32ca2011-10-14 02:36:13 +0200660#ifdef MS_WINDOWS
661 /* wcsftime() doesn't format correctly time zones, see issue #10653 */
662# undef HAVE_WCSFTIME
663#endif
Alexander Belopolskycf774542012-10-02 18:39:16 -0400664#define STRFTIME_FORMAT_CODES \
665"Commonly used format codes:\n\
666\n\
667%Y Year with century as a decimal number.\n\
668%m Month as a decimal number [01,12].\n\
669%d Day of the month as a decimal number [01,31].\n\
670%H Hour (24-hour clock) as a decimal number [00,23].\n\
671%M Minute as a decimal number [00,59].\n\
672%S Second as a decimal number [00,61].\n\
673%z Time zone offset from UTC.\n\
674%a Locale's abbreviated weekday name.\n\
675%A Locale's full weekday name.\n\
676%b Locale's abbreviated month name.\n\
677%B Locale's full month name.\n\
678%c Locale's appropriate date and time representation.\n\
679%I Hour (12-hour clock) as a decimal number [01,12].\n\
680%p Locale's equivalent of either AM or PM.\n\
681\n\
682Other codes may be available on your platform. See documentation for\n\
683the C library strftime function.\n"
Victor Stinnerc1f32ca2011-10-14 02:36:13 +0200684
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000685#ifdef HAVE_STRFTIME
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000686#ifdef HAVE_WCSFTIME
687#define time_char wchar_t
688#define format_time wcsftime
689#define time_strlen wcslen
690#else
691#define time_char char
692#define format_time strftime
693#define time_strlen strlen
694#endif
695
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000696static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000697time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 PyObject *tup = NULL;
700 struct tm buf;
701 const time_char *fmt;
Victor Stinnerb2904782010-09-29 10:34:19 +0000702#ifdef HAVE_WCSFTIME
703 wchar_t *format;
704#else
705 PyObject *format;
706#endif
Victor Stinneref128102010-10-07 01:00:52 +0000707 PyObject *format_arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 size_t fmtlen, buflen;
Victor Stinnerb2904782010-09-29 10:34:19 +0000709 time_char *outbuf = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 size_t i;
Victor Stinnerb2904782010-09-29 10:34:19 +0000711 PyObject *ret = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 /* Will always expect a unicode string to be passed as format.
716 Given that there's no str type anymore in py3k this seems safe.
717 */
Victor Stinneref128102010-10-07 01:00:52 +0000718 if (!PyArg_ParseTuple(args, "U|O:strftime", &format_arg, &tup))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 if (tup == NULL) {
722 time_t tt = time(NULL);
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400723 if (_PyTime_localtime(tt, &buf) != 0)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100724 return NULL;
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000725 }
Oren Milman1d1d3e92017-08-20 18:35:36 +0300726 else if (!gettmarg(tup, &buf,
727 "iiiiiiiii;strftime(): illegal time tuple argument") ||
728 !checktm(&buf))
729 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 return NULL;
Oren Milman1d1d3e92017-08-20 18:35:36 +0300731 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000732
pxinwrf1464f42019-04-15 17:06:21 +0800733#if defined(_MSC_VER) || (defined(__sun) && defined(__SVR4)) || defined(_AIX) || defined(__VXWORKS__)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000734 if (buf.tm_year + 1900 < 1 || 9999 < buf.tm_year + 1900) {
Victor Stinner6f0e4f92011-03-21 02:14:53 +0100735 PyErr_SetString(PyExc_ValueError,
736 "strftime() requires year in [1; 9999]");
Alexander Belopolsky0dd06f42011-01-08 01:23:02 +0000737 return NULL;
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000738 }
Victor Stinner73ea29c2011-01-08 01:56:31 +0000739#endif
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 /* Normalize tm_isdst just in case someone foolishly implements %Z
742 based on the assumption that tm_isdst falls within the range of
743 [-1, 1] */
744 if (buf.tm_isdst < -1)
745 buf.tm_isdst = -1;
746 else if (buf.tm_isdst > 1)
747 buf.tm_isdst = 1;
Brett Cannond1080a32004-03-02 04:38:10 +0000748
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000749#ifdef HAVE_WCSFTIME
Victor Stinnerbeb4135b2010-10-07 01:02:42 +0000750 format = PyUnicode_AsWideCharString(format_arg, NULL);
Victor Stinnerb2904782010-09-29 10:34:19 +0000751 if (format == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 return NULL;
Victor Stinnerb2904782010-09-29 10:34:19 +0000753 fmt = format;
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000754#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 /* Convert the unicode string to an ascii one */
Victor Stinner1b579672011-12-17 05:47:23 +0100756 format = PyUnicode_EncodeLocale(format_arg, "surrogateescape");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 if (format == NULL)
758 return NULL;
759 fmt = PyBytes_AS_STRING(format);
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000760#endif
Amaury Forgeot d'Arcb5be6d42009-03-02 23:52:57 +0000761
Stefan Krah4aea7d32012-02-27 16:30:26 +0100762#if defined(MS_WINDOWS) && !defined(HAVE_WCSFTIME)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 /* check that the format string contains only valid directives */
Steve Dowere5b58952015-09-06 19:20:51 -0700764 for (outbuf = strchr(fmt, '%');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 outbuf != NULL;
Victor Stinner5a3ff792011-10-16 19:08:23 +0200766 outbuf = strchr(outbuf+2, '%'))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 {
Steve Dowere5b58952015-09-06 19:20:51 -0700768 if (outbuf[1] == '#')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 ++outbuf; /* not documented by python, */
Steve Dowere5b58952015-09-06 19:20:51 -0700770 if (outbuf[1] == '\0')
771 break;
772 if ((outbuf[1] == 'y') && buf.tm_year < 0) {
Tim Golden6e51b8f2013-11-12 12:36:54 +0000773 PyErr_SetString(PyExc_ValueError,
774 "format %y requires year >= 1900 on Windows");
775 Py_DECREF(format);
776 return NULL;
777 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 }
Jakub Kulík6f9bc722018-12-31 03:16:40 +0100779#elif (defined(_AIX) || (defined(__sun) && defined(__SVR4))) && defined(HAVE_WCSFTIME)
Steve Dowere5b58952015-09-06 19:20:51 -0700780 for (outbuf = wcschr(fmt, '%');
Victor Stinner55329f82013-11-17 23:39:21 +0100781 outbuf != NULL;
782 outbuf = wcschr(outbuf+2, '%'))
783 {
Steve Dowere5b58952015-09-06 19:20:51 -0700784 if (outbuf[1] == L'\0')
785 break;
Victor Stinner55329f82013-11-17 23:39:21 +0100786 /* Issue #19634: On AIX, wcsftime("y", (1899, 1, 1, 0, 0, 0, 0, 0, 0))
787 returns "0/" instead of "99" */
788 if (outbuf[1] == L'y' && buf.tm_year < 0) {
789 PyErr_SetString(PyExc_ValueError,
790 "format %y requires year >= 1900 on AIX");
Zackery Spytz91e6c872018-09-21 00:09:48 -0600791 PyMem_Free(format);
Victor Stinner55329f82013-11-17 23:39:21 +0100792 return NULL;
793 }
794 }
Amaury Forgeot d'Arcb5be6d42009-03-02 23:52:57 +0000795#endif
796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 fmtlen = time_strlen(fmt);
Guido van Rossumc222ec21999-02-23 00:00:10 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 /* I hate these functions that presume you know how big the output
800 * will be ahead of time...
801 */
802 for (i = 1024; ; i += i) {
803 outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char));
804 if (outbuf == NULL) {
Victor Stinnerb2904782010-09-29 10:34:19 +0000805 PyErr_NoMemory();
806 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 }
Steve Dower57ab1cd2015-09-22 14:51:42 -0700808#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
809 errno = 0;
810#endif
Steve Dowerd9ef74e2015-05-22 15:08:34 -0700811 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 buflen = format_time(outbuf, i, fmt, &buf);
Steve Dowerd9ef74e2015-05-22 15:08:34 -0700813 _Py_END_SUPPRESS_IPH
Victor Stinner136ea492011-12-17 22:37:18 +0100814#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Steve Dower97cded92015-09-08 19:12:51 -0700815 /* VisualStudio .NET 2005 does this properly */
816 if (buflen == 0 && errno == EINVAL) {
817 PyErr_SetString(PyExc_ValueError, "Invalid format string");
818 PyMem_Free(outbuf);
819 break;
820 }
Victor Stinner136ea492011-12-17 22:37:18 +0100821#endif
Steve Dower97cded92015-09-08 19:12:51 -0700822 if (buflen > 0 || i >= 256 * fmtlen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 /* If the buffer is 256 times as long as the format,
824 it's probably not failing for lack of room!
825 More likely, the format yields an empty result,
826 e.g. an empty format, or %Z when the timezone
827 is unknown. */
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000828#ifdef HAVE_WCSFTIME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 ret = PyUnicode_FromWideChar(outbuf, buflen);
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000830#else
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100831 ret = PyUnicode_DecodeLocaleAndSize(outbuf, buflen, "surrogateescape");
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000832#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 PyMem_Free(outbuf);
Victor Stinnerb2904782010-09-29 10:34:19 +0000834 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 }
836 PyMem_Free(outbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 }
Victor Stinnerb2904782010-09-29 10:34:19 +0000838#ifdef HAVE_WCSFTIME
839 PyMem_Free(format);
840#else
841 Py_DECREF(format);
842#endif
843 return ret;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000844}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000845
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000846#undef time_char
847#undef format_time
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000848PyDoc_STRVAR(strftime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000849"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000850\n\
851Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000852See the library reference manual for formatting codes. When the time tuple\n\
Alexander Belopolskycf774542012-10-02 18:39:16 -0400853is not present, current time as returned by localtime() is used.\n\
854\n" STRFTIME_FORMAT_CODES);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000855#endif /* HAVE_STRFTIME */
856
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000857static PyObject *
858time_strptime(PyObject *self, PyObject *args)
859{
Victor Stinnerdbe28d22016-12-09 00:38:53 +0100860 PyObject *module, *func, *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200861 _Py_IDENTIFIER(_strptime_time);
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000862
Victor Stinnerdbe28d22016-12-09 00:38:53 +0100863 module = PyImport_ImportModuleNoBlock("_strptime");
864 if (!module)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 return NULL;
Victor Stinnerdbe28d22016-12-09 00:38:53 +0100866
867 func = _PyObject_GetAttrId(module, &PyId__strptime_time);
868 Py_DECREF(module);
869 if (!func) {
870 return NULL;
871 }
872
873 result = PyObject_Call(func, args, NULL);
874 Py_DECREF(func);
875 return result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000876}
877
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000878
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000879PyDoc_STRVAR(strptime_doc,
Brett Cannon20def8b2003-07-01 05:16:08 +0000880"strptime(string, format) -> struct_time\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000881\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000882Parse a string to a time tuple according to a format specification.\n\
Alexander Belopolskycf774542012-10-02 18:39:16 -0400883See the library reference manual for formatting codes (same as\n\
884strftime()).\n\
885\n" STRFTIME_FORMAT_CODES);
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000886
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000887static PyObject *
888_asctime(struct tm *timeptr)
889{
890 /* Inspired by Open Group reference implementation available at
891 * http://pubs.opengroup.org/onlinepubs/009695399/functions/asctime.html */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200892 static const char wday_name[7][4] = {
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000893 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
894 };
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200895 static const char mon_name[12][4] = {
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000896 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
897 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
898 };
Victor Stinner499dfcf2011-03-21 13:26:24 +0100899 return PyUnicode_FromFormat(
900 "%s %s%3d %.2d:%.2d:%.2d %d",
901 wday_name[timeptr->tm_wday],
902 mon_name[timeptr->tm_mon],
903 timeptr->tm_mday, timeptr->tm_hour,
904 timeptr->tm_min, timeptr->tm_sec,
905 1900 + timeptr->tm_year);
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000906}
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000907
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000908static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000909time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 PyObject *tup = NULL;
912 struct tm buf;
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup))
915 return NULL;
916 if (tup == NULL) {
917 time_t tt = time(NULL);
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400918 if (_PyTime_localtime(tt, &buf) != 0)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100919 return NULL;
Oren Milman1d1d3e92017-08-20 18:35:36 +0300920 }
921 else if (!gettmarg(tup, &buf,
922 "iiiiiiiii;asctime(): illegal time tuple argument") ||
923 !checktm(&buf))
924 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 return NULL;
Oren Milman1d1d3e92017-08-20 18:35:36 +0300926 }
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000927 return _asctime(&buf);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000928}
929
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000930PyDoc_STRVAR(asctime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000931"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000932\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000933Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
934When the time tuple is not present, current time as returned by localtime()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000935is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000936
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000937static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000938time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 time_t tt;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100941 struct tm buf;
942 if (!parse_time_t_args(args, "|O:ctime", &tt))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 return NULL;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400944 if (_PyTime_localtime(tt, &buf) != 0)
Alexander Belopolsky5da468f2011-01-04 17:15:52 +0000945 return NULL;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100946 return _asctime(&buf);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000947}
948
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000949PyDoc_STRVAR(ctime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000950"ctime(seconds) -> string\n\
951\n\
952Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000953This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000954not present, current time as returned by localtime() is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000955
Guido van Rossum60cd8131998-03-06 17:16:21 +0000956#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000957static PyObject *
Victor Stinner87094902019-04-09 19:12:26 +0200958time_mktime(PyObject *self, PyObject *tm_tuple)
Guido van Rossum234f9421993-06-17 12:35:49 +0000959{
Victor Stinner87094902019-04-09 19:12:26 +0200960 struct tm tm;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 time_t tt;
Michael Felte2926b72018-12-28 14:57:37 +0100962
Victor Stinner87094902019-04-09 19:12:26 +0200963 if (!gettmarg(tm_tuple, &tm,
Oren Milman1d1d3e92017-08-20 18:35:36 +0300964 "iiiiiiiii;mktime(): illegal time tuple argument"))
965 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 return NULL;
Oren Milman1d1d3e92017-08-20 18:35:36 +0300967 }
Victor Stinner87094902019-04-09 19:12:26 +0200968
pxinwrf1464f42019-04-15 17:06:21 +0800969#if defined(_AIX) || (defined(__VXWORKS__) && !defined(_WRS_CONFIG_LP64))
Victor Stinner87094902019-04-09 19:12:26 +0200970 /* bpo-19748: AIX mktime() valid range is 00:00:00 UTC, January 1, 1970
971 to 03:14:07 UTC, January 19, 2038. Thanks to the workaround below,
972 it is possible to support years in range [1902; 2037] */
973 if (tm.tm_year < 2 || tm.tm_year > 137) {
974 /* bpo-19748: On AIX, mktime() does not report overflow error
pxinwrf1464f42019-04-15 17:06:21 +0800975 for timestamp < -2^31 or timestamp > 2**31-1. VxWorks has the
976 same issue when working in 32 bit mode. */
Victor Stinner1ac42612014-02-21 09:27:17 +0100977 PyErr_SetString(PyExc_OverflowError,
978 "mktime argument out of range");
979 return NULL;
980 }
pxinwrf1464f42019-04-15 17:06:21 +0800981#endif
Victor Stinner87094902019-04-09 19:12:26 +0200982
pxinwrf1464f42019-04-15 17:06:21 +0800983#ifdef _AIX
Victor Stinner87094902019-04-09 19:12:26 +0200984 /* bpo-34373: AIX mktime() has an integer overflow for years in range
985 [1902; 1969]. Workaround the issue by using a year greater or equal than
986 1970 (tm_year >= 70): mktime() behaves correctly in that case
987 (ex: properly report errors). tm_year and tm_wday are adjusted after
988 mktime() call. */
989 int orig_tm_year = tm.tm_year;
990 int delta_days = 0;
991 while (tm.tm_year < 70) {
992 /* Use 4 years to account properly leap years */
993 tm.tm_year += 4;
Michael Felte2926b72018-12-28 14:57:37 +0100994 delta_days -= (366 + (365 * 3));
995 }
Victor Stinner1ac42612014-02-21 09:27:17 +0100996#endif
Victor Stinner87094902019-04-09 19:12:26 +0200997
998 tm.tm_wday = -1; /* sentinel; original value ignored */
999 tt = mktime(&tm);
1000
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +00001001 /* Return value of -1 does not necessarily mean an error, but tm_wday
Ezio Melotti13925002011-03-16 11:05:33 +02001002 * cannot remain set to -1 if mktime succeeded. */
Victor Stinner93037492013-06-25 22:54:35 +02001003 if (tt == (time_t)(-1)
Victor Stinner93037492013-06-25 22:54:35 +02001004 /* Return value of -1 does not necessarily mean an error, but
1005 * tm_wday cannot remain set to -1 if mktime succeeded. */
Victor Stinner87094902019-04-09 19:12:26 +02001006 && tm.tm_wday == -1)
Victor Stinner93037492013-06-25 22:54:35 +02001007 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 PyErr_SetString(PyExc_OverflowError,
1009 "mktime argument out of range");
1010 return NULL;
1011 }
Victor Stinner87094902019-04-09 19:12:26 +02001012
1013#ifdef _AIX
1014 if (delta_days != 0) {
1015 tm.tm_year = orig_tm_year;
1016 if (tm.tm_wday != -1) {
1017 tm.tm_wday = (tm.tm_wday + delta_days) % 7;
1018 }
1019 tt += delta_days * (24 * 3600);
1020 }
1021#endif
1022
Victor Stinner4195b5c2012-02-08 23:03:19 +01001023 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +00001024}
Guido van Rossum0ef577b1998-06-27 20:38:36 +00001025
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001026PyDoc_STRVAR(mktime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +00001027"mktime(tuple) -> floating point number\n\
1028\n\
Alexander Belopolskyc142bba2012-06-13 22:15:26 -04001029Convert a time tuple in local time to seconds since the Epoch.\n\
1030Note that mktime(gmtime(0)) will not generally return zero for most\n\
1031time zones; instead the returned value will either be equal to that\n\
1032of the timezone or altzone attributes on the time module.");
Guido van Rossum60cd8131998-03-06 17:16:21 +00001033#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +00001034
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001035#ifdef HAVE_WORKING_TZSET
Victor Stinner3bb150d2018-12-03 13:45:38 +01001036static int init_timezone(PyObject *module);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001037
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001038static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001039time_tzset(PyObject *self, PyObject *unused)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 PyObject* m;
Fred Drake9bb74322002-04-01 14:49:59 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 m = PyImport_ImportModuleNoBlock("time");
1044 if (m == NULL) {
1045 return NULL;
1046 }
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 tzset();
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 /* Reset timezone, altzone, daylight and tzname */
Victor Stinner3bb150d2018-12-03 13:45:38 +01001051 if (init_timezone(m) < 0) {
Victor Stinner503ce5c2018-12-01 00:39:36 +01001052 return NULL;
1053 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 Py_DECREF(m);
Victor Stinner2ff51b82013-07-17 21:42:45 +02001055 if (PyErr_Occurred())
1056 return NULL;
Tim Peters1b6f7a92004-06-20 02:50:16 +00001057
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001058 Py_RETURN_NONE;
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001059}
1060
1061PyDoc_STRVAR(tzset_doc,
R. David Murray4d55bf92010-12-14 00:55:46 +00001062"tzset()\n\
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001063\n\
1064Initialize, or reinitialize, the local timezone to the value stored in\n\
1065os.environ['TZ']. The TZ environment variable should be specified in\n\
Neal Norwitzdc8e1942004-07-20 22:34:37 +00001066standard Unix timezone format as documented in the tzset man page\n\
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001067(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
1068fall back to UTC. If the TZ environment variable is not set, the local\n\
1069timezone is set to the systems best guess of wallclock time.\n\
1070Changing the TZ environment variable without calling tzset *may* change\n\
1071the local timezone used by methods such as localtime, but this behaviour\n\
1072should not be relied on.");
1073#endif /* HAVE_WORKING_TZSET */
1074
Victor Stinnerae586492014-09-02 23:18:25 +02001075static PyObject *
Victor Stinnerec895392012-04-29 02:41:27 +02001076time_monotonic(PyObject *self, PyObject *unused)
Victor Stinner071eca32012-03-15 01:17:09 +01001077{
Victor Stinnerc29b5852017-11-02 07:28:27 -07001078 _PyTime_t t = _PyTime_GetMonotonicClock();
1079 return _PyFloat_FromPyTime(t);
Victor Stinner071eca32012-03-15 01:17:09 +01001080}
1081
Victor Stinnerec895392012-04-29 02:41:27 +02001082PyDoc_STRVAR(monotonic_doc,
1083"monotonic() -> float\n\
Victor Stinner8b302012012-02-07 23:29:46 +01001084\n\
Victor Stinnerec895392012-04-29 02:41:27 +02001085Monotonic clock, cannot go backward.");
Victor Stinnerec919cc2012-03-15 00:58:32 +01001086
Victor Stinnerec895392012-04-29 02:41:27 +02001087static PyObject *
Victor Stinnerc29b5852017-11-02 07:28:27 -07001088time_monotonic_ns(PyObject *self, PyObject *unused)
1089{
1090 _PyTime_t t = _PyTime_GetMonotonicClock();
1091 return _PyTime_AsNanosecondsObject(t);
1092}
1093
1094PyDoc_STRVAR(monotonic_ns_doc,
1095"monotonic_ns() -> int\n\
1096\n\
1097Monotonic clock, cannot go backward, as nanoseconds.");
1098
1099static PyObject *
Victor Stinnerec895392012-04-29 02:41:27 +02001100time_perf_counter(PyObject *self, PyObject *unused)
1101{
1102 return perf_counter(NULL);
1103}
1104
1105PyDoc_STRVAR(perf_counter_doc,
1106"perf_counter() -> float\n\
1107\n\
1108Performance counter for benchmarking.");
1109
Victor Stinnerc29b5852017-11-02 07:28:27 -07001110static PyObject *
1111time_perf_counter_ns(PyObject *self, PyObject *unused)
1112{
1113 _PyTime_t t = _PyTime_GetPerfCounter();
1114 return _PyTime_AsNanosecondsObject(t);
1115}
1116
1117PyDoc_STRVAR(perf_counter_ns_doc,
1118"perf_counter_ns() -> int\n\
1119\n\
1120Performance counter for benchmarking as nanoseconds.");
1121
1122static int
1123_PyTime_GetProcessTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
Victor Stinnerec895392012-04-29 02:41:27 +02001124{
1125#if defined(MS_WINDOWS)
1126 HANDLE process;
1127 FILETIME creation_time, exit_time, kernel_time, user_time;
1128 ULARGE_INTEGER large;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001129 _PyTime_t ktime, utime, t;
Victor Stinnerec895392012-04-29 02:41:27 +02001130 BOOL ok;
1131
1132 process = GetCurrentProcess();
Victor Stinnerc29b5852017-11-02 07:28:27 -07001133 ok = GetProcessTimes(process, &creation_time, &exit_time,
1134 &kernel_time, &user_time);
1135 if (!ok) {
1136 PyErr_SetFromWindowsErr(0);
1137 return -1;
1138 }
Victor Stinnerec895392012-04-29 02:41:27 +02001139
Victor Stinnerec895392012-04-29 02:41:27 +02001140 if (info) {
1141 info->implementation = "GetProcessTimes()";
1142 info->resolution = 1e-7;
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001143 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001144 info->adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001145 }
Victor Stinnerc29b5852017-11-02 07:28:27 -07001146
1147 large.u.LowPart = kernel_time.dwLowDateTime;
1148 large.u.HighPart = kernel_time.dwHighDateTime;
1149 ktime = large.QuadPart;
1150
1151 large.u.LowPart = user_time.dwLowDateTime;
1152 large.u.HighPart = user_time.dwHighDateTime;
1153 utime = large.QuadPart;
1154
1155 /* ktime and utime have a resolution of 100 nanoseconds */
1156 t = _PyTime_FromNanoseconds((ktime + utime) * 100);
1157 *tp = t;
1158 return 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001159#else
1160
Victor Stinnerc29b5852017-11-02 07:28:27 -07001161 /* clock_gettime */
Victor Stinnerec895392012-04-29 02:41:27 +02001162#if defined(HAVE_CLOCK_GETTIME) \
1163 && (defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_PROF))
Victor Stinnerc29b5852017-11-02 07:28:27 -07001164 struct timespec ts;
Victor Stinnerec895392012-04-29 02:41:27 +02001165#ifdef CLOCK_PROF
1166 const clockid_t clk_id = CLOCK_PROF;
1167 const char *function = "clock_gettime(CLOCK_PROF)";
1168#else
1169 const clockid_t clk_id = CLOCK_PROCESS_CPUTIME_ID;
1170 const char *function = "clock_gettime(CLOCK_PROCESS_CPUTIME_ID)";
1171#endif
1172
Victor Stinnerc29b5852017-11-02 07:28:27 -07001173 if (clock_gettime(clk_id, &ts) == 0) {
Victor Stinnerec895392012-04-29 02:41:27 +02001174 if (info) {
1175 struct timespec res;
1176 info->implementation = function;
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001177 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001178 info->adjustable = 0;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001179 if (clock_getres(clk_id, &res)) {
1180 PyErr_SetFromErrno(PyExc_OSError);
1181 return -1;
1182 }
1183 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
Victor Stinnerec895392012-04-29 02:41:27 +02001184 }
Victor Stinnerc29b5852017-11-02 07:28:27 -07001185
1186 if (_PyTime_FromTimespec(tp, &ts) < 0) {
1187 return -1;
1188 }
1189 return 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001190 }
1191#endif
1192
Victor Stinnerc29b5852017-11-02 07:28:27 -07001193 /* getrusage(RUSAGE_SELF) */
Victor Stinnerec895392012-04-29 02:41:27 +02001194#if defined(HAVE_SYS_RESOURCE_H)
Victor Stinnerc29b5852017-11-02 07:28:27 -07001195 struct rusage ru;
1196
Victor Stinnerec895392012-04-29 02:41:27 +02001197 if (getrusage(RUSAGE_SELF, &ru) == 0) {
Victor Stinnerc29b5852017-11-02 07:28:27 -07001198 _PyTime_t utime, stime;
1199
Victor Stinnerec895392012-04-29 02:41:27 +02001200 if (info) {
1201 info->implementation = "getrusage(RUSAGE_SELF)";
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001202 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001203 info->adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001204 info->resolution = 1e-6;
1205 }
Victor Stinnerc29b5852017-11-02 07:28:27 -07001206
1207 if (_PyTime_FromTimeval(&utime, &ru.ru_utime) < 0) {
1208 return -1;
1209 }
1210 if (_PyTime_FromTimeval(&stime, &ru.ru_stime) < 0) {
1211 return -1;
1212 }
1213
vrajivk8bf5fef2019-08-26 21:13:12 -07001214 _PyTime_t total = utime + stime;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001215 *tp = total;
1216 return 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001217 }
1218#endif
1219
Victor Stinnerc29b5852017-11-02 07:28:27 -07001220 /* times() */
Victor Stinnerec895392012-04-29 02:41:27 +02001221#ifdef HAVE_TIMES
Victor Stinnerc29b5852017-11-02 07:28:27 -07001222 struct tms t;
1223
Victor Stinnerec895392012-04-29 02:41:27 +02001224 if (times(&t) != (clock_t)-1) {
Victor Stinnerc29b5852017-11-02 07:28:27 -07001225 static long ticks_per_second = -1;
Victor Stinnerec895392012-04-29 02:41:27 +02001226
1227 if (ticks_per_second == -1) {
Victor Stinnerc29b5852017-11-02 07:28:27 -07001228 long freq;
Victor Stinnerec895392012-04-29 02:41:27 +02001229#if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinnerc29b5852017-11-02 07:28:27 -07001230 freq = sysconf(_SC_CLK_TCK);
1231 if (freq < 1) {
1232 freq = -1;
1233 }
Victor Stinnerec895392012-04-29 02:41:27 +02001234#elif defined(HZ)
Victor Stinnerc29b5852017-11-02 07:28:27 -07001235 freq = HZ;
Victor Stinnerec895392012-04-29 02:41:27 +02001236#else
Victor Stinnerc29b5852017-11-02 07:28:27 -07001237 freq = 60; /* magic fallback value; may be bogus */
Victor Stinnerec895392012-04-29 02:41:27 +02001238#endif
Victor Stinnerc29b5852017-11-02 07:28:27 -07001239
1240 if (freq != -1) {
1241 /* check that _PyTime_MulDiv(t, SEC_TO_NS, ticks_per_second)
1242 cannot overflow below */
Serhiy Storchakabfe4fd52018-02-09 17:31:26 +02001243#if LONG_MAX > _PyTime_MAX / SEC_TO_NS
Victor Stinnerc29b5852017-11-02 07:28:27 -07001244 if ((_PyTime_t)freq > _PyTime_MAX / SEC_TO_NS) {
1245 PyErr_SetString(PyExc_OverflowError,
1246 "_SC_CLK_TCK is too large");
1247 return -1;
1248 }
Serhiy Storchakabfe4fd52018-02-09 17:31:26 +02001249#endif
Victor Stinnerc29b5852017-11-02 07:28:27 -07001250
1251 ticks_per_second = freq;
1252 }
Victor Stinnerec895392012-04-29 02:41:27 +02001253 }
1254
1255 if (ticks_per_second != -1) {
Victor Stinnerec895392012-04-29 02:41:27 +02001256 if (info) {
1257 info->implementation = "times()";
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001258 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001259 info->adjustable = 0;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001260 info->resolution = 1.0 / (double)ticks_per_second;
Victor Stinnerec895392012-04-29 02:41:27 +02001261 }
Victor Stinnerc29b5852017-11-02 07:28:27 -07001262
1263 _PyTime_t total;
1264 total = _PyTime_MulDiv(t.tms_utime, SEC_TO_NS, ticks_per_second);
1265 total += _PyTime_MulDiv(t.tms_stime, SEC_TO_NS, ticks_per_second);
1266 *tp = total;
1267 return 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001268 }
1269 }
1270#endif
1271
Victor Stinnerc29b5852017-11-02 07:28:27 -07001272 /* clock */
Victor Stinner53e22bf2016-07-08 17:55:01 +02001273 /* Currently, Python 3 requires clock() to build: see issue #22624 */
Victor Stinnerc29b5852017-11-02 07:28:27 -07001274 return _PyTime_GetClockWithInfo(tp, info);
Victor Stinnerec895392012-04-29 02:41:27 +02001275#endif
1276}
1277
1278static PyObject *
1279time_process_time(PyObject *self, PyObject *unused)
1280{
Victor Stinnerc29b5852017-11-02 07:28:27 -07001281 _PyTime_t t;
1282 if (_PyTime_GetProcessTimeWithInfo(&t, NULL) < 0) {
1283 return NULL;
1284 }
1285 return _PyFloat_FromPyTime(t);
Victor Stinnerec895392012-04-29 02:41:27 +02001286}
1287
1288PyDoc_STRVAR(process_time_doc,
1289"process_time() -> float\n\
1290\n\
1291Process time for profiling: sum of the kernel and user-space CPU time.");
1292
Victor Stinnerc29b5852017-11-02 07:28:27 -07001293static PyObject *
1294time_process_time_ns(PyObject *self, PyObject *unused)
1295{
1296 _PyTime_t t;
1297 if (_PyTime_GetProcessTimeWithInfo(&t, NULL) < 0) {
1298 return NULL;
1299 }
1300 return _PyTime_AsNanosecondsObject(t);
1301}
1302
1303PyDoc_STRVAR(process_time_ns_doc,
1304"process_time() -> int\n\
1305\n\
1306Process time for profiling as nanoseconds:\n\
1307sum of the kernel and user-space CPU time.");
1308
Victor Stinnerec895392012-04-29 02:41:27 +02001309
Antoine Pitrou4bd41c92017-11-15 22:52:21 +01001310#if defined(MS_WINDOWS)
1311#define HAVE_THREAD_TIME
1312static int
1313_PyTime_GetThreadTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
1314{
1315 HANDLE thread;
1316 FILETIME creation_time, exit_time, kernel_time, user_time;
1317 ULARGE_INTEGER large;
1318 _PyTime_t ktime, utime, t;
1319 BOOL ok;
1320
1321 thread = GetCurrentThread();
1322 ok = GetThreadTimes(thread, &creation_time, &exit_time,
1323 &kernel_time, &user_time);
1324 if (!ok) {
1325 PyErr_SetFromWindowsErr(0);
1326 return -1;
1327 }
1328
1329 if (info) {
1330 info->implementation = "GetThreadTimes()";
1331 info->resolution = 1e-7;
1332 info->monotonic = 1;
1333 info->adjustable = 0;
1334 }
1335
1336 large.u.LowPart = kernel_time.dwLowDateTime;
1337 large.u.HighPart = kernel_time.dwHighDateTime;
1338 ktime = large.QuadPart;
1339
1340 large.u.LowPart = user_time.dwLowDateTime;
1341 large.u.HighPart = user_time.dwHighDateTime;
1342 utime = large.QuadPart;
1343
1344 /* ktime and utime have a resolution of 100 nanoseconds */
1345 t = _PyTime_FromNanoseconds((ktime + utime) * 100);
1346 *tp = t;
1347 return 0;
1348}
1349
Batuhan Taskaya45410862020-05-16 12:39:09 +03001350#elif defined(_AIX)
1351#define HAVE_THREAD_TIME
1352static int
1353_PyTime_GetThreadTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
1354{
1355 /* bpo-40192: On AIX, thread_cputime() is preferred: it has nanosecond
1356 resolution, whereas clock_gettime(CLOCK_THREAD_CPUTIME_ID)
1357 has a resolution of 10 ms. */
1358 thread_cputime_t tc;
1359 if (thread_cputime(-1, &tc) != 0) {
1360 PyErr_SetFromErrno(PyExc_OSError);
1361 return -1;
1362 }
1363
1364 if (info) {
1365 info->implementation = "thread_cputime()";
1366 info->monotonic = 1;
1367 info->adjustable = 0;
1368 info->resolution = 1e-9;
1369 }
1370 *tp = _PyTime_FromNanoseconds(tc.stime + tc.utime);
1371 return 0;
1372}
1373
Antoine Pitrou4bd41c92017-11-15 22:52:21 +01001374#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID)
1375#define HAVE_THREAD_TIME
1376static int
1377_PyTime_GetThreadTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
1378{
1379 struct timespec ts;
1380 const clockid_t clk_id = CLOCK_THREAD_CPUTIME_ID;
1381 const char *function = "clock_gettime(CLOCK_THREAD_CPUTIME_ID)";
1382
1383 if (clock_gettime(clk_id, &ts)) {
1384 PyErr_SetFromErrno(PyExc_OSError);
1385 return -1;
1386 }
1387 if (info) {
1388 struct timespec res;
1389 info->implementation = function;
1390 info->monotonic = 1;
1391 info->adjustable = 0;
1392 if (clock_getres(clk_id, &res)) {
1393 PyErr_SetFromErrno(PyExc_OSError);
1394 return -1;
1395 }
1396 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
1397 }
1398
1399 if (_PyTime_FromTimespec(tp, &ts) < 0) {
1400 return -1;
1401 }
1402 return 0;
1403}
1404#endif
1405
1406#ifdef HAVE_THREAD_TIME
1407static PyObject *
1408time_thread_time(PyObject *self, PyObject *unused)
1409{
1410 _PyTime_t t;
1411 if (_PyTime_GetThreadTimeWithInfo(&t, NULL) < 0) {
1412 return NULL;
1413 }
1414 return _PyFloat_FromPyTime(t);
1415}
1416
1417PyDoc_STRVAR(thread_time_doc,
1418"thread_time() -> float\n\
1419\n\
1420Thread time for profiling: sum of the kernel and user-space CPU time.");
1421
1422static PyObject *
1423time_thread_time_ns(PyObject *self, PyObject *unused)
1424{
1425 _PyTime_t t;
1426 if (_PyTime_GetThreadTimeWithInfo(&t, NULL) < 0) {
1427 return NULL;
1428 }
1429 return _PyTime_AsNanosecondsObject(t);
1430}
1431
1432PyDoc_STRVAR(thread_time_ns_doc,
1433"thread_time() -> int\n\
1434\n\
1435Thread time for profiling as nanoseconds:\n\
1436sum of the kernel and user-space CPU time.");
1437#endif
1438
1439
Victor Stinnerec895392012-04-29 02:41:27 +02001440static PyObject *
1441time_get_clock_info(PyObject *self, PyObject *args)
1442{
1443 char *name;
Victor Stinnerec895392012-04-29 02:41:27 +02001444 _Py_clock_info_t info;
Victor Stinnerbda4b882012-06-12 22:11:44 +02001445 PyObject *obj = NULL, *dict, *ns;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001446 _PyTime_t t;
Victor Stinnerec895392012-04-29 02:41:27 +02001447
Victor Stinnerc29b5852017-11-02 07:28:27 -07001448 if (!PyArg_ParseTuple(args, "s:get_clock_info", &name)) {
Victor Stinnerec895392012-04-29 02:41:27 +02001449 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001450 }
Victor Stinnerec895392012-04-29 02:41:27 +02001451
1452#ifdef Py_DEBUG
1453 info.implementation = NULL;
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001454 info.monotonic = -1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001455 info.adjustable = -1;
Victor Stinnerec895392012-04-29 02:41:27 +02001456 info.resolution = -1.0;
1457#else
1458 info.implementation = "";
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001459 info.monotonic = 0;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001460 info.adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001461 info.resolution = 1.0;
1462#endif
1463
Victor Stinnerc29b5852017-11-02 07:28:27 -07001464 if (strcmp(name, "time") == 0) {
1465 if (_PyTime_GetSystemClockWithInfo(&t, &info) < 0) {
1466 return NULL;
1467 }
1468 }
Victor Stinnerc29b5852017-11-02 07:28:27 -07001469 else if (strcmp(name, "monotonic") == 0) {
1470 if (_PyTime_GetMonotonicClockWithInfo(&t, &info) < 0) {
1471 return NULL;
1472 }
1473 }
1474 else if (strcmp(name, "perf_counter") == 0) {
1475 if (_PyTime_GetPerfCounterWithInfo(&t, &info) < 0) {
1476 return NULL;
1477 }
1478 }
1479 else if (strcmp(name, "process_time") == 0) {
1480 if (_PyTime_GetProcessTimeWithInfo(&t, &info) < 0) {
1481 return NULL;
1482 }
1483 }
Antoine Pitrou4bd41c92017-11-15 22:52:21 +01001484#ifdef HAVE_THREAD_TIME
1485 else if (strcmp(name, "thread_time") == 0) {
1486 if (_PyTime_GetThreadTimeWithInfo(&t, &info) < 0) {
1487 return NULL;
1488 }
1489 }
1490#endif
Victor Stinnerec895392012-04-29 02:41:27 +02001491 else {
1492 PyErr_SetString(PyExc_ValueError, "unknown clock");
1493 return NULL;
1494 }
Victor Stinnerec895392012-04-29 02:41:27 +02001495
Victor Stinnerbda4b882012-06-12 22:11:44 +02001496 dict = PyDict_New();
Victor Stinnerc29b5852017-11-02 07:28:27 -07001497 if (dict == NULL) {
Victor Stinnerec895392012-04-29 02:41:27 +02001498 return NULL;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001499 }
Victor Stinnerec895392012-04-29 02:41:27 +02001500
1501 assert(info.implementation != NULL);
1502 obj = PyUnicode_FromString(info.implementation);
Victor Stinnerc29b5852017-11-02 07:28:27 -07001503 if (obj == NULL) {
Victor Stinnerec895392012-04-29 02:41:27 +02001504 goto error;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001505 }
1506 if (PyDict_SetItemString(dict, "implementation", obj) == -1) {
Victor Stinnerbda4b882012-06-12 22:11:44 +02001507 goto error;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001508 }
Victor Stinnerbda4b882012-06-12 22:11:44 +02001509 Py_CLEAR(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001510
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001511 assert(info.monotonic != -1);
1512 obj = PyBool_FromLong(info.monotonic);
Victor Stinnerc29b5852017-11-02 07:28:27 -07001513 if (obj == NULL) {
Victor Stinnerec895392012-04-29 02:41:27 +02001514 goto error;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001515 }
1516 if (PyDict_SetItemString(dict, "monotonic", obj) == -1) {
Victor Stinnerbda4b882012-06-12 22:11:44 +02001517 goto error;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001518 }
Victor Stinnerbda4b882012-06-12 22:11:44 +02001519 Py_CLEAR(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001520
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001521 assert(info.adjustable != -1);
1522 obj = PyBool_FromLong(info.adjustable);
Victor Stinnerc29b5852017-11-02 07:28:27 -07001523 if (obj == NULL) {
Victor Stinnerec895392012-04-29 02:41:27 +02001524 goto error;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001525 }
1526 if (PyDict_SetItemString(dict, "adjustable", obj) == -1) {
Victor Stinnerbda4b882012-06-12 22:11:44 +02001527 goto error;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001528 }
Victor Stinnerbda4b882012-06-12 22:11:44 +02001529 Py_CLEAR(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001530
1531 assert(info.resolution > 0.0);
1532 assert(info.resolution <= 1.0);
1533 obj = PyFloat_FromDouble(info.resolution);
Victor Stinnerc29b5852017-11-02 07:28:27 -07001534 if (obj == NULL) {
Victor Stinnerec895392012-04-29 02:41:27 +02001535 goto error;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001536 }
1537 if (PyDict_SetItemString(dict, "resolution", obj) == -1) {
Victor Stinnerbda4b882012-06-12 22:11:44 +02001538 goto error;
Victor Stinnerc29b5852017-11-02 07:28:27 -07001539 }
Victor Stinnerbda4b882012-06-12 22:11:44 +02001540 Py_CLEAR(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001541
Victor Stinnerbda4b882012-06-12 22:11:44 +02001542 ns = _PyNamespace_New(dict);
1543 Py_DECREF(dict);
1544 return ns;
Victor Stinnerec895392012-04-29 02:41:27 +02001545
1546error:
Victor Stinnerbda4b882012-06-12 22:11:44 +02001547 Py_DECREF(dict);
1548 Py_XDECREF(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001549 return NULL;
1550}
1551
1552PyDoc_STRVAR(get_clock_info_doc,
1553"get_clock_info(name: str) -> dict\n\
1554\n\
1555Get information of the specified clock.");
Victor Stinner8b302012012-02-07 23:29:46 +01001556
Benjamin Petersonc510c6b2018-09-20 19:52:18 -07001557#ifndef HAVE_DECL_TZNAME
Victor Stinner1fb399b2018-09-17 13:56:17 -07001558static void
1559get_zone(char *zone, int n, struct tm *p)
1560{
1561#ifdef HAVE_STRUCT_TM_TM_ZONE
1562 strncpy(zone, p->tm_zone ? p->tm_zone : " ", n);
1563#else
1564 tzset();
1565 strftime(zone, n, "%Z", p);
1566#endif
1567}
1568
Victor Stinner503ce5c2018-12-01 00:39:36 +01001569static time_t
Victor Stinner1fb399b2018-09-17 13:56:17 -07001570get_gmtoff(time_t t, struct tm *p)
1571{
1572#ifdef HAVE_STRUCT_TM_TM_ZONE
1573 return p->tm_gmtoff;
1574#else
1575 return timegm(p) - t;
1576#endif
1577}
Benjamin Petersonc510c6b2018-09-20 19:52:18 -07001578#endif // !HAVE_DECL_TZNAME
Victor Stinner1fb399b2018-09-17 13:56:17 -07001579
Victor Stinner503ce5c2018-12-01 00:39:36 +01001580static int
Victor Stinner3bb150d2018-12-03 13:45:38 +01001581init_timezone(PyObject *m)
Victor Stinner503ce5c2018-12-01 00:39:36 +01001582{
1583 assert(!PyErr_Occurred());
1584
Martin v. Löwis1a214512008-06-11 05:26:20 +00001585 /* This code moved from PyInit_time wholesale to allow calling it from
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 time_tzset. In the future, some parts of it can be moved back
1587 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
1588 are), and the extraneous calls to tzset(3) should be removed.
1589 I haven't done this yet, as I don't want to change this code as
1590 little as possible when introducing the time.tzset and time.tzsetwall
1591 methods. This should simply be a method of doing the following once,
1592 at the top of this function and removing the call to tzset() from
1593 time_tzset():
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 #ifdef HAVE_TZSET
1596 tzset()
1597 #endif
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 And I'm lazy and hate C so nyer.
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001600 */
Benjamin Petersonc510c6b2018-09-20 19:52:18 -07001601#ifdef HAVE_DECL_TZNAME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 PyObject *otz0, *otz1;
1603 tzset();
Zackery Spytz6673dec2019-02-25 16:56:44 -07001604 PyModule_AddIntConstant(m, "timezone", _Py_timezone);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001605#ifdef HAVE_ALTZONE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001607#else
Zackery Spytz6673dec2019-02-25 16:56:44 -07001608 PyModule_AddIntConstant(m, "altzone", _Py_timezone-3600);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001609#endif
Zackery Spytz6673dec2019-02-25 16:56:44 -07001610 PyModule_AddIntConstant(m, "daylight", _Py_daylight);
Paul Monsonb4c7def2019-06-12 16:13:27 -07001611#ifdef MS_WINDOWS
1612 TIME_ZONE_INFORMATION tzinfo = {0};
1613 GetTimeZoneInformation(&tzinfo);
1614 otz0 = PyUnicode_FromWideChar(tzinfo.StandardName, -1);
1615 if (otz0 == NULL) {
1616 return -1;
1617 }
1618 otz1 = PyUnicode_FromWideChar(tzinfo.DaylightName, -1);
1619 if (otz1 == NULL) {
1620 Py_DECREF(otz0);
1621 return -1;
1622 }
1623#else
Zackery Spytz6673dec2019-02-25 16:56:44 -07001624 otz0 = PyUnicode_DecodeLocale(_Py_tzname[0], "surrogateescape");
Benjamin Petersonc510c6b2018-09-20 19:52:18 -07001625 if (otz0 == NULL) {
Victor Stinnerab661492018-12-03 12:02:43 +01001626 return -1;
Victor Stinner1fb399b2018-09-17 13:56:17 -07001627 }
Zackery Spytz6673dec2019-02-25 16:56:44 -07001628 otz1 = PyUnicode_DecodeLocale(_Py_tzname[1], "surrogateescape");
Benjamin Petersonc510c6b2018-09-20 19:52:18 -07001629 if (otz1 == NULL) {
1630 Py_DECREF(otz0);
Victor Stinnerab661492018-12-03 12:02:43 +01001631 return -1;
Benjamin Petersonc510c6b2018-09-20 19:52:18 -07001632 }
Paul Monsonb4c7def2019-06-12 16:13:27 -07001633#endif // MS_WINDOWS
Benjamin Petersonc510c6b2018-09-20 19:52:18 -07001634 PyObject *tzname_obj = Py_BuildValue("(NN)", otz0, otz1);
Victor Stinnerab661492018-12-03 12:02:43 +01001635 if (tzname_obj == NULL) {
1636 return -1;
1637 }
Benjamin Petersonc510c6b2018-09-20 19:52:18 -07001638 PyModule_AddObject(m, "tzname", tzname_obj);
1639#else // !HAVE_DECL_TZNAME
1640 static const time_t YEAR = (365 * 24 + 6) * 3600;
1641 time_t t;
1642 struct tm p;
Victor Stinner503ce5c2018-12-01 00:39:36 +01001643 time_t janzone_t, julyzone_t;
Benjamin Petersonc510c6b2018-09-20 19:52:18 -07001644 char janname[10], julyname[10];
1645 t = (time((time_t *)0) / YEAR) * YEAR;
1646 _PyTime_localtime(t, &p);
1647 get_zone(janname, 9, &p);
Victor Stinner503ce5c2018-12-01 00:39:36 +01001648 janzone_t = -get_gmtoff(t, &p);
Benjamin Petersonc510c6b2018-09-20 19:52:18 -07001649 janname[9] = '\0';
1650 t += YEAR/2;
1651 _PyTime_localtime(t, &p);
1652 get_zone(julyname, 9, &p);
Victor Stinner503ce5c2018-12-01 00:39:36 +01001653 julyzone_t = -get_gmtoff(t, &p);
Benjamin Petersonc510c6b2018-09-20 19:52:18 -07001654 julyname[9] = '\0';
1655
Victor Stinner503ce5c2018-12-01 00:39:36 +01001656 /* Sanity check, don't check for the validity of timezones.
1657 In practice, it should be more in range -12 hours .. +14 hours. */
1658#define MAX_TIMEZONE (48 * 3600)
1659 if (janzone_t < -MAX_TIMEZONE || janzone_t > MAX_TIMEZONE
1660 || julyzone_t < -MAX_TIMEZONE || julyzone_t > MAX_TIMEZONE)
1661 {
1662 PyErr_SetString(PyExc_RuntimeError, "invalid GMT offset");
1663 return -1;
1664 }
1665 int janzone = (int)janzone_t;
1666 int julyzone = (int)julyzone_t;
1667
Benjamin Petersonc510c6b2018-09-20 19:52:18 -07001668 PyObject *tzname_obj;
1669 if (janzone < julyzone) {
1670 /* DST is reversed in the southern hemisphere */
1671 PyModule_AddIntConstant(m, "timezone", julyzone);
1672 PyModule_AddIntConstant(m, "altzone", janzone);
1673 PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
1674 tzname_obj = Py_BuildValue("(zz)", julyname, janname);
1675 } else {
1676 PyModule_AddIntConstant(m, "timezone", janzone);
1677 PyModule_AddIntConstant(m, "altzone", julyzone);
1678 PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
1679 tzname_obj = Py_BuildValue("(zz)", janname, julyname);
1680 }
Victor Stinner503ce5c2018-12-01 00:39:36 +01001681 if (tzname_obj == NULL) {
1682 return -1;
1683 }
Benjamin Petersonc510c6b2018-09-20 19:52:18 -07001684 PyModule_AddObject(m, "tzname", tzname_obj);
1685#endif // !HAVE_DECL_TZNAME
Victor Stinner503ce5c2018-12-01 00:39:36 +01001686
1687 if (PyErr_Occurred()) {
1688 return -1;
1689 }
1690 return 0;
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001691}
1692
1693
1694static PyMethodDef time_methods[] = {
Victor Stinner4195b5c2012-02-08 23:03:19 +01001695 {"time", time_time, METH_NOARGS, time_doc},
Victor Stinnerc29b5852017-11-02 07:28:27 -07001696 {"time_ns", time_time_ns, METH_NOARGS, time_ns_doc},
Victor Stinnere0be4232011-10-25 13:06:09 +02001697#ifdef HAVE_CLOCK_GETTIME
Victor Stinner4195b5c2012-02-08 23:03:19 +01001698 {"clock_gettime", time_clock_gettime, METH_VARARGS, clock_gettime_doc},
Victor Stinnerc29b5852017-11-02 07:28:27 -07001699 {"clock_gettime_ns",time_clock_gettime_ns, METH_VARARGS, clock_gettime_ns_doc},
Benjamin Peterson37098cd2016-09-13 22:55:09 -07001700#endif
1701#ifdef HAVE_CLOCK_SETTIME
Victor Stinner30d79472012-04-03 00:45:07 +02001702 {"clock_settime", time_clock_settime, METH_VARARGS, clock_settime_doc},
Victor Stinnerc29b5852017-11-02 07:28:27 -07001703 {"clock_settime_ns",time_clock_settime_ns, METH_VARARGS, clock_settime_ns_doc},
Benjamin Peterson37098cd2016-09-13 22:55:09 -07001704#endif
1705#ifdef HAVE_CLOCK_GETRES
Victor Stinner4195b5c2012-02-08 23:03:19 +01001706 {"clock_getres", time_clock_getres, METH_VARARGS, clock_getres_doc},
Victor Stinnere0be4232011-10-25 13:06:09 +02001707#endif
pdoxe14679c2017-10-05 00:01:56 -07001708#ifdef HAVE_PTHREAD_GETCPUCLOCKID
1709 {"pthread_getcpuclockid", time_pthread_getcpuclockid, METH_VARARGS, pthread_getcpuclockid_doc},
1710#endif
Victor Stinnercb29f012015-03-27 13:31:18 +01001711 {"sleep", time_sleep, METH_O, sleep_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
1713 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
1714 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
1715 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001716#ifdef HAVE_MKTIME
Victor Stinner4195b5c2012-02-08 23:03:19 +01001717 {"mktime", time_mktime, METH_O, mktime_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001718#endif
1719#ifdef HAVE_STRFTIME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001721#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001723#ifdef HAVE_WORKING_TZSET
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 {"tzset", time_tzset, METH_NOARGS, tzset_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001725#endif
Victor Stinnerec895392012-04-29 02:41:27 +02001726 {"monotonic", time_monotonic, METH_NOARGS, monotonic_doc},
Victor Stinnerc29b5852017-11-02 07:28:27 -07001727 {"monotonic_ns", time_monotonic_ns, METH_NOARGS, monotonic_ns_doc},
Victor Stinnerec895392012-04-29 02:41:27 +02001728 {"process_time", time_process_time, METH_NOARGS, process_time_doc},
Victor Stinnerc29b5852017-11-02 07:28:27 -07001729 {"process_time_ns", time_process_time_ns, METH_NOARGS, process_time_ns_doc},
Antoine Pitrou4bd41c92017-11-15 22:52:21 +01001730#ifdef HAVE_THREAD_TIME
1731 {"thread_time", time_thread_time, METH_NOARGS, thread_time_doc},
1732 {"thread_time_ns", time_thread_time_ns, METH_NOARGS, thread_time_ns_doc},
1733#endif
Victor Stinnerec895392012-04-29 02:41:27 +02001734 {"perf_counter", time_perf_counter, METH_NOARGS, perf_counter_doc},
Victor Stinnerc29b5852017-11-02 07:28:27 -07001735 {"perf_counter_ns", time_perf_counter_ns, METH_NOARGS, perf_counter_ns_doc},
Victor Stinnerec895392012-04-29 02:41:27 +02001736 {"get_clock_info", time_get_clock_info, METH_VARARGS, get_clock_info_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 {NULL, NULL} /* sentinel */
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001738};
1739
1740
1741PyDoc_STRVAR(module_doc,
1742"This module provides various functions to manipulate time values.\n\
1743\n\
1744There are two standard representations of time. One is the number\n\
1745of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
1746or a floating point number (to represent fractions of seconds).\n\
1747The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
1748The actual value can be retrieved by calling gmtime(0).\n\
1749\n\
1750The other representation is a tuple of 9 integers giving local time.\n\
1751The tuple items are:\n\
Alexander Belopolsky03163ac2011-05-02 12:20:52 -04001752 year (including century, e.g. 1998)\n\
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001753 month (1-12)\n\
1754 day (1-31)\n\
1755 hours (0-23)\n\
1756 minutes (0-59)\n\
1757 seconds (0-59)\n\
1758 weekday (0-6, Monday is 0)\n\
1759 Julian day (day in the year, 1-366)\n\
1760 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
1761If the DST flag is 0, the time is given in the regular time zone;\n\
1762if it is 1, the time is given in the DST time zone;\n\
Cheryl Sabella703ff382017-10-11 09:29:14 -04001763if it is -1, mktime() should guess based on the date and time.\n");
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001764
1765
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001766static int
1767time_exec(PyObject *module)
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 /* Set, or reset, module variables like time.timezone */
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001770 if (init_timezone(module) < 0) {
1771 return -1;
Victor Stinner503ce5c2018-12-01 00:39:36 +01001772 }
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001773
Max Bélanger94451182018-10-20 17:07:54 -07001774#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_SETTIME) || defined(HAVE_CLOCK_GETRES)
1775
Benjamin Peterson37098cd2016-09-13 22:55:09 -07001776#ifdef CLOCK_REALTIME
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001777 if (PyModule_AddIntMacro(module, CLOCK_REALTIME) < 0) {
1778 return -1;
Hai Shi196f1eb2020-03-12 00:56:08 +08001779 }
Benjamin Peterson37098cd2016-09-13 22:55:09 -07001780#endif
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001781#ifdef CLOCK_MONOTONIC
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001782 if (PyModule_AddIntMacro(module, CLOCK_MONOTONIC) < 0) {
1783 return -1;
Hai Shi196f1eb2020-03-12 00:56:08 +08001784 }
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001785#endif
1786#ifdef CLOCK_MONOTONIC_RAW
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001787 if (PyModule_AddIntMacro(module, CLOCK_MONOTONIC_RAW) < 0) {
1788 return -1;
Hai Shi196f1eb2020-03-12 00:56:08 +08001789 }
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001790#endif
1791#ifdef CLOCK_HIGHRES
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001792 if (PyModule_AddIntMacro(module, CLOCK_HIGHRES) < 0) {
1793 return -1;
Hai Shi196f1eb2020-03-12 00:56:08 +08001794 }
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001795#endif
1796#ifdef CLOCK_PROCESS_CPUTIME_ID
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001797 if (PyModule_AddIntMacro(module, CLOCK_PROCESS_CPUTIME_ID) < 0) {
1798 return -1;
Hai Shi196f1eb2020-03-12 00:56:08 +08001799 }
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001800#endif
1801#ifdef CLOCK_THREAD_CPUTIME_ID
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001802 if (PyModule_AddIntMacro(module, CLOCK_THREAD_CPUTIME_ID) < 0) {
1803 return -1;
Hai Shi196f1eb2020-03-12 00:56:08 +08001804 }
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001805#endif
Victor Stinnera64ce972017-11-02 04:19:19 -07001806#ifdef CLOCK_PROF
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001807 if (PyModule_AddIntMacro(module, CLOCK_PROF) < 0) {
1808 return -1;
Hai Shi196f1eb2020-03-12 00:56:08 +08001809 }
Victor Stinnera64ce972017-11-02 04:19:19 -07001810#endif
1811#ifdef CLOCK_BOOTTIME
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001812 if (PyModule_AddIntMacro(module, CLOCK_BOOTTIME) < 0) {
1813 return -1;
Hai Shi196f1eb2020-03-12 00:56:08 +08001814 }
Victor Stinnera64ce972017-11-02 04:19:19 -07001815#endif
Russell Owen60000872020-03-23 20:41:40 -07001816#ifdef CLOCK_TAI
1817 if (PyModule_AddIntMacro(module, CLOCK_TAI) < 0) {
1818 return -1;
1819 }
1820#endif
Victor Stinnera64ce972017-11-02 04:19:19 -07001821#ifdef CLOCK_UPTIME
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001822 if (PyModule_AddIntMacro(module, CLOCK_UPTIME) < 0) {
1823 return -1;
Hai Shi196f1eb2020-03-12 00:56:08 +08001824 }
Victor Stinnera64ce972017-11-02 04:19:19 -07001825#endif
Joannah Nanjekye572168a2019-01-10 19:56:38 +03001826#ifdef CLOCK_UPTIME_RAW
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001827 if (PyModule_AddIntMacro(module, CLOCK_UPTIME_RAW) < 0) {
1828 return -1;
Hai Shi196f1eb2020-03-12 00:56:08 +08001829 }
Joannah Nanjekye572168a2019-01-10 19:56:38 +03001830#endif
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001831
Max Bélanger94451182018-10-20 17:07:54 -07001832#endif /* defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_SETTIME) || defined(HAVE_CLOCK_GETRES) */
1833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 if (!initialized) {
Victor Stinner1c8f0592013-07-22 22:24:54 +02001835 if (PyStructSequence_InitType2(&StructTimeType,
Hai Shi196f1eb2020-03-12 00:56:08 +08001836 &struct_time_type_desc) < 0) {
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001837 return -1;
Hai Shi196f1eb2020-03-12 00:56:08 +08001838 }
1839 }
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001840 if (PyModule_AddIntConstant(module, "_STRUCT_TM_ITEMS", 11)) {
1841 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 }
1843 Py_INCREF(&StructTimeType);
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001844 if (PyModule_AddObject(module, "struct_time", (PyObject*) &StructTimeType)) {
Hai Shi196f1eb2020-03-12 00:56:08 +08001845 Py_DECREF(&StructTimeType);
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001846 return -1;
Hai Shi196f1eb2020-03-12 00:56:08 +08001847 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 initialized = 1;
Benjamin Peterson5633c4f2018-09-14 09:09:04 -07001849
1850#if defined(__linux__) && !defined(__GLIBC__)
1851 struct tm tm;
Benjamin Petersonb93062b2018-09-14 10:39:13 -07001852 const time_t zero = 0;
1853 if (gmtime_r(&zero, &tm) != NULL)
Benjamin Peterson5633c4f2018-09-14 09:09:04 -07001854 utc_string = tm.tm_zone;
1855#endif
1856
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001857 return 0;
1858}
Hai Shi196f1eb2020-03-12 00:56:08 +08001859
Paulo Henrique Silvabd409bb2020-03-23 15:58:23 -03001860static struct PyModuleDef_Slot time_slots[] = {
1861 {Py_mod_exec, time_exec},
1862 {0, NULL}
1863};
1864
1865static struct PyModuleDef timemodule = {
1866 PyModuleDef_HEAD_INIT,
1867 "time",
1868 module_doc,
1869 0,
1870 time_methods,
1871 time_slots,
1872 NULL,
1873 NULL,
1874 NULL
1875};
1876
1877PyMODINIT_FUNC
1878PyInit_time(void)
1879{
1880 return PyModuleDef_Init(&timemodule);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001881}
1882
Victor Stinnercb29f012015-03-27 13:31:18 +01001883/* Implement pysleep() for various platforms.
Guido van Rossumb6775db1994-08-01 11:34:53 +00001884 When interrupted (or when another error occurs), return -1 and
1885 set an exception; else return 0. */
1886
1887static int
Victor Stinnercb29f012015-03-27 13:31:18 +01001888pysleep(_PyTime_t secs)
Guido van Rossum426035c1991-02-19 12:27:35 +00001889{
Victor Stinnercb29f012015-03-27 13:31:18 +01001890 _PyTime_t deadline, monotonic;
Victor Stinner79d68f92015-03-19 21:54:09 +01001891#ifndef MS_WINDOWS
1892 struct timeval timeout;
Victor Stinner79d68f92015-03-19 21:54:09 +01001893 int err = 0;
1894#else
Victor Stinnercb29f012015-03-27 13:31:18 +01001895 _PyTime_t millisecs;
Victor Stinner79d68f92015-03-19 21:54:09 +01001896 unsigned long ul_millis;
1897 DWORD rc;
1898 HANDLE hInterruptEvent;
Victor Stinner0c2fd892015-03-17 10:49:17 +01001899#endif
Victor Stinner79d68f92015-03-19 21:54:09 +01001900
Victor Stinnercb29f012015-03-27 13:31:18 +01001901 deadline = _PyTime_GetMonotonicClock() + secs;
Victor Stinner79d68f92015-03-19 21:54:09 +01001902
1903 do {
1904#ifndef MS_WINDOWS
Victor Stinner869e1772015-03-30 03:49:14 +02001905 if (_PyTime_AsTimeval(secs, &timeout, _PyTime_ROUND_CEILING) < 0)
Victor Stinnercb29f012015-03-27 13:31:18 +01001906 return -1;
Victor Stinner79d68f92015-03-19 21:54:09 +01001907
1908 Py_BEGIN_ALLOW_THREADS
1909 err = select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout);
1910 Py_END_ALLOW_THREADS
1911
1912 if (err == 0)
1913 break;
1914
1915 if (errno != EINTR) {
Victor Stinner0c2fd892015-03-17 10:49:17 +01001916 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 return -1;
1918 }
Victor Stinner79d68f92015-03-19 21:54:09 +01001919#else
Victor Stinner869e1772015-03-30 03:49:14 +02001920 millisecs = _PyTime_AsMilliseconds(secs, _PyTime_ROUND_CEILING);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 if (millisecs > (double)ULONG_MAX) {
1922 PyErr_SetString(PyExc_OverflowError,
1923 "sleep length is too large");
1924 return -1;
1925 }
Victor Stinner79d68f92015-03-19 21:54:09 +01001926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 /* Allow sleep(0) to maintain win32 semantics, and as decreed
1928 * by Guido, only the main thread can be interrupted.
1929 */
1930 ul_millis = (unsigned long)millisecs;
Victor Stinner79d68f92015-03-19 21:54:09 +01001931 if (ul_millis == 0 || !_PyOS_IsMainThread()) {
1932 Py_BEGIN_ALLOW_THREADS
Victor Stinner0eac1302015-03-20 03:06:12 +01001933 Sleep(ul_millis);
Victor Stinner79d68f92015-03-19 21:54:09 +01001934 Py_END_ALLOW_THREADS
1935 break;
Victor Stinner945c82e2015-03-12 16:19:01 +01001936 }
Victor Stinner79d68f92015-03-19 21:54:09 +01001937
1938 hInterruptEvent = _PyOS_SigintEvent();
1939 ResetEvent(hInterruptEvent);
1940
1941 Py_BEGIN_ALLOW_THREADS
1942 rc = WaitForSingleObjectEx(hInterruptEvent, ul_millis, FALSE);
Victor Stinner0c2fd892015-03-17 10:49:17 +01001943 Py_END_ALLOW_THREADS
Victor Stinner79d68f92015-03-19 21:54:09 +01001944
1945 if (rc != WAIT_OBJECT_0)
1946 break;
Victor Stinner945c82e2015-03-12 16:19:01 +01001947#endif
Victor Stinner0c2fd892015-03-17 10:49:17 +01001948
Victor Stinner79d68f92015-03-19 21:54:09 +01001949 /* sleep was interrupted by SIGINT */
1950 if (PyErr_CheckSignals())
1951 return -1;
1952
Victor Stinnercb29f012015-03-27 13:31:18 +01001953 monotonic = _PyTime_GetMonotonicClock();
1954 secs = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001955 if (secs < 0)
Victor Stinner79d68f92015-03-19 21:54:09 +01001956 break;
1957 /* retry with the recomputed delay */
1958 } while (1);
1959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +00001961}