blob: 347c8282d8ed31836fdd2d6c3577890fcdd72fbe [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
Guido van Rossum7bf22de1997-12-02 20:34:19 +000027#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000028#include <i86.h>
29#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000030#ifdef MS_WINDOWS
Mark Hammond975e3922002-07-16 01:29:19 +000031#define WIN32_LEAN_AND_MEAN
Guido van Rossum258ccd42001-03-02 06:53:29 +000032#include <windows.h>
Mark Hammond975e3922002-07-16 01:29:19 +000033#include "pythread.h"
Guido van Rossumcac6c721996-09-06 13:34:02 +000034#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000035#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000036
37/* Forward declarations */
Victor Stinnercb29f012015-03-27 13:31:18 +010038static int pysleep(_PyTime_t);
Victor Stinnerec895392012-04-29 02:41:27 +020039static PyObject* floattime(_Py_clock_info_t *info);
40
Barry Warsaw9a2a8a81996-12-06 23:32:14 +000041static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +010042time_time(PyObject *self, PyObject *unused)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043{
Victor Stinnerec895392012-04-29 02:41:27 +020044 return floattime(NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +000045}
46
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000047PyDoc_STRVAR(time_doc,
Victor Stinner4195b5c2012-02-08 23:03:19 +010048"time() -> floating point number\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +000049\n\
50Return the current time in seconds since the Epoch.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000051Fractions of a second may be present if the system clock provides them.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +000052
Victor Stinner85fdfa82012-01-27 00:38:48 +010053#if defined(HAVE_CLOCK)
54
55#ifndef CLOCKS_PER_SEC
56#ifdef CLK_TCK
57#define CLOCKS_PER_SEC CLK_TCK
58#else
59#define CLOCKS_PER_SEC 1000000
60#endif
61#endif
62
Victor Stinnera997c7b2017-10-10 02:51:50 -070063static PyObject*
64_PyFloat_FromPyTime(_PyTime_t t)
65{
66 double d = _PyTime_AsSecondsDouble(t);
67 return PyFloat_FromDouble(d);
68}
69
Victor Stinner4195b5c2012-02-08 23:03:19 +010070static PyObject *
Victor Stinnerec895392012-04-29 02:41:27 +020071floatclock(_Py_clock_info_t *info)
Victor Stinner85fdfa82012-01-27 00:38:48 +010072{
Victor Stinner4195b5c2012-02-08 23:03:19 +010073 clock_t value;
74 value = clock();
75 if (value == (clock_t)-1) {
Victor Stinner85fdfa82012-01-27 00:38:48 +010076 PyErr_SetString(PyExc_RuntimeError,
77 "the processor time used is not available "
78 "or its value cannot be represented");
Victor Stinner4195b5c2012-02-08 23:03:19 +010079 return NULL;
Victor Stinner85fdfa82012-01-27 00:38:48 +010080 }
Victor Stinnerec895392012-04-29 02:41:27 +020081 if (info) {
82 info->implementation = "clock()";
83 info->resolution = 1.0 / (double)CLOCKS_PER_SEC;
Benjamin Peterson49a69e42012-05-01 09:38:34 -040084 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +020085 info->adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +020086 }
Victor Stinner4195b5c2012-02-08 23:03:19 +010087 return PyFloat_FromDouble((double)value / CLOCKS_PER_SEC);
Victor Stinner85fdfa82012-01-27 00:38:48 +010088}
89#endif /* HAVE_CLOCK */
90
Victor Stinnercba9a0c2017-10-12 08:51:56 -070091static PyObject*
92perf_counter(_Py_clock_info_t *info)
93{
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -070094 _PyTime_t t;
95 if (_PyTime_GetPerfCounterWithInfo(&t, info) < 0) {
Victor Stinnercba9a0c2017-10-12 08:51:56 -070096 return NULL;
97 }
Victor Stinnerbdaeb7d2017-10-16 08:44:31 -070098 double d = _PyTime_AsSecondsDouble(t);
99 return PyFloat_FromDouble(d);
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700100}
101
Victor Stinnera997c7b2017-10-10 02:51:50 -0700102#if defined(MS_WINDOWS) || defined(HAVE_CLOCK)
Victor Stinnerec895392012-04-29 02:41:27 +0200103#define PYCLOCK
104static PyObject*
105pyclock(_Py_clock_info_t *info)
106{
Victor Stinner884d13a2017-10-17 14:46:45 -0700107 if (PyErr_WarnEx(PyExc_DeprecationWarning,
108 "time.clock has been deprecated in Python 3.3 and will "
109 "be removed from Python 3.8: "
110 "use time.perf_counter or time.process_time "
111 "instead", 1) < 0) {
112 return NULL;
113 }
Victor Stinnera997c7b2017-10-10 02:51:50 -0700114#ifdef MS_WINDOWS
Victor Stinnercba9a0c2017-10-12 08:51:56 -0700115 return perf_counter(info);
Victor Stinner54884492014-08-29 16:51:33 +0200116#else
Victor Stinnerec895392012-04-29 02:41:27 +0200117 return floatclock(info);
Victor Stinner54884492014-08-29 16:51:33 +0200118#endif
Victor Stinnerec895392012-04-29 02:41:27 +0200119}
120
Victor Stinner9122fdd2011-07-04 13:55:40 +0200121static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +0100122time_clock(PyObject *self, PyObject *unused)
Victor Stinner9122fdd2011-07-04 13:55:40 +0200123{
Victor Stinnerec895392012-04-29 02:41:27 +0200124 return pyclock(NULL);
Victor Stinner9122fdd2011-07-04 13:55:40 +0200125}
Victor Stinner9122fdd2011-07-04 13:55:40 +0200126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000127PyDoc_STRVAR(clock_doc,
Victor Stinner4195b5c2012-02-08 23:03:19 +0100128"clock() -> floating point number\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000129\n\
130Return the CPU time or real time since the start of the process or since\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000131the first call to clock(). This has as much precision as the system\n\
132records.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000133#endif
134
Victor Stinnere0be4232011-10-25 13:06:09 +0200135#ifdef HAVE_CLOCK_GETTIME
136static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +0100137time_clock_gettime(PyObject *self, PyObject *args)
Victor Stinnere0be4232011-10-25 13:06:09 +0200138{
139 int ret;
Victor Stinnerb8d01692012-04-13 23:44:05 +0200140 int clk_id;
Victor Stinnere0be4232011-10-25 13:06:09 +0200141 struct timespec tp;
142
Victor Stinner4195b5c2012-02-08 23:03:19 +0100143 if (!PyArg_ParseTuple(args, "i:clock_gettime", &clk_id))
Victor Stinnere0be4232011-10-25 13:06:09 +0200144 return NULL;
145
146 ret = clock_gettime((clockid_t)clk_id, &tp);
Antoine Pitrou2c085602012-01-18 01:41:44 +0100147 if (ret != 0) {
Victor Stinnera734af32014-07-31 13:07:17 +0200148 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrou2c085602012-01-18 01:41:44 +0100149 return NULL;
150 }
Victor Stinner4195b5c2012-02-08 23:03:19 +0100151 return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
Victor Stinnere0be4232011-10-25 13:06:09 +0200152}
153
154PyDoc_STRVAR(clock_gettime_doc,
Victor Stinner4195b5c2012-02-08 23:03:19 +0100155"clock_gettime(clk_id) -> floating point number\n\
Victor Stinnere0be4232011-10-25 13:06:09 +0200156\n\
157Return the time of the specified clock clk_id.");
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700158#endif /* HAVE_CLOCK_GETTIME */
Victor Stinner30d79472012-04-03 00:45:07 +0200159
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700160#ifdef HAVE_CLOCK_SETTIME
Victor Stinner30d79472012-04-03 00:45:07 +0200161static PyObject *
162time_clock_settime(PyObject *self, PyObject *args)
163{
Victor Stinnerb8d01692012-04-13 23:44:05 +0200164 int clk_id;
Victor Stinner30d79472012-04-03 00:45:07 +0200165 PyObject *obj;
Victor Stinnerb3b45442015-03-28 04:09:41 +0100166 _PyTime_t t;
Victor Stinner30d79472012-04-03 00:45:07 +0200167 struct timespec tp;
168 int ret;
169
170 if (!PyArg_ParseTuple(args, "iO:clock_settime", &clk_id, &obj))
171 return NULL;
172
Victor Stinner02937aa2015-03-28 05:02:39 +0100173 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_FLOOR) < 0)
Victor Stinner30d79472012-04-03 00:45:07 +0200174 return NULL;
Victor Stinnerb3b45442015-03-28 04:09:41 +0100175
176 if (_PyTime_AsTimespec(t, &tp) == -1)
177 return NULL;
Victor Stinner30d79472012-04-03 00:45:07 +0200178
179 ret = clock_settime((clockid_t)clk_id, &tp);
180 if (ret != 0) {
Victor Stinnera734af32014-07-31 13:07:17 +0200181 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinner30d79472012-04-03 00:45:07 +0200182 return NULL;
183 }
184 Py_RETURN_NONE;
185}
186
187PyDoc_STRVAR(clock_settime_doc,
188"clock_settime(clk_id, time)\n\
189\n\
190Set the time of the specified clock clk_id.");
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700191#endif /* HAVE_CLOCK_SETTIME */
Victor Stinnere0be4232011-10-25 13:06:09 +0200192
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700193#ifdef HAVE_CLOCK_GETRES
Victor Stinnere0be4232011-10-25 13:06:09 +0200194static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +0100195time_clock_getres(PyObject *self, PyObject *args)
Victor Stinnere0be4232011-10-25 13:06:09 +0200196{
197 int ret;
Victor Stinnerb8d01692012-04-13 23:44:05 +0200198 int clk_id;
Victor Stinnere0be4232011-10-25 13:06:09 +0200199 struct timespec tp;
200
Victor Stinner4195b5c2012-02-08 23:03:19 +0100201 if (!PyArg_ParseTuple(args, "i:clock_getres", &clk_id))
Victor Stinnere0be4232011-10-25 13:06:09 +0200202 return NULL;
203
204 ret = clock_getres((clockid_t)clk_id, &tp);
Antoine Pitrou2c085602012-01-18 01:41:44 +0100205 if (ret != 0) {
Victor Stinnera734af32014-07-31 13:07:17 +0200206 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrou2c085602012-01-18 01:41:44 +0100207 return NULL;
208 }
Victor Stinner4195b5c2012-02-08 23:03:19 +0100209
210 return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
Victor Stinnere0be4232011-10-25 13:06:09 +0200211}
212
213PyDoc_STRVAR(clock_getres_doc,
Victor Stinner4195b5c2012-02-08 23:03:19 +0100214"clock_getres(clk_id) -> floating point number\n\
Victor Stinnere0be4232011-10-25 13:06:09 +0200215\n\
216Return the resolution (precision) of the specified clock clk_id.");
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700217#endif /* HAVE_CLOCK_GETRES */
Victor Stinnere0be4232011-10-25 13:06:09 +0200218
pdoxe14679c2017-10-05 00:01:56 -0700219#ifdef HAVE_PTHREAD_GETCPUCLOCKID
220static PyObject *
221time_pthread_getcpuclockid(PyObject *self, PyObject *args)
222{
223 unsigned long thread_id;
224 int err;
225 clockid_t clk_id;
226 if (!PyArg_ParseTuple(args, "k:pthread_getcpuclockid", &thread_id)) {
227 return NULL;
228 }
229 err = pthread_getcpuclockid((pthread_t)thread_id, &clk_id);
230 if (err) {
231 errno = err;
232 PyErr_SetFromErrno(PyExc_OSError);
233 return NULL;
234 }
235 return PyLong_FromLong(clk_id);
236}
237
238PyDoc_STRVAR(pthread_getcpuclockid_doc,
239"pthread_getcpuclockid(thread_id) -> int\n\
240\n\
241Return the clk_id of a thread's CPU time clock.");
242#endif /* HAVE_PTHREAD_GETCPUCLOCKID */
243
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000244static PyObject *
Victor Stinnercb29f012015-03-27 13:31:18 +0100245time_sleep(PyObject *self, PyObject *obj)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246{
Victor Stinnercb29f012015-03-27 13:31:18 +0100247 _PyTime_t secs;
Pablo Galindo59af94f2017-10-18 08:13:09 +0100248 if (_PyTime_FromSecondsObject(&secs, obj, _PyTime_ROUND_TIMEOUT))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 return NULL;
Victor Stinner7f53a502011-07-05 22:00:25 +0200250 if (secs < 0) {
251 PyErr_SetString(PyExc_ValueError,
252 "sleep length must be non-negative");
253 return NULL;
254 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100255 if (pysleep(secs) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200257 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258}
259
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000260PyDoc_STRVAR(sleep_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000261"sleep(seconds)\n\
262\n\
263Delay execution for a given number of seconds. The argument may be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000264a floating point number for subsecond precision.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000265
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000266static PyStructSequence_Field struct_time_type_fields[] = {
Alexander Belopolsky69f3fd02010-06-05 15:04:51 +0000267 {"tm_year", "year, for example, 1993"},
268 {"tm_mon", "month of year, range [1, 12]"},
269 {"tm_mday", "day of month, range [1, 31]"},
270 {"tm_hour", "hours, range [0, 23]"},
271 {"tm_min", "minutes, range [0, 59]"},
272 {"tm_sec", "seconds, range [0, 61])"},
273 {"tm_wday", "day of week, range [0, 6], Monday is 0"},
274 {"tm_yday", "day of year, range [1, 366]"},
275 {"tm_isdst", "1 if summer time is in effect, 0 if not, and -1 if unknown"},
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400276 {"tm_zone", "abbreviation of timezone name"},
277 {"tm_gmtoff", "offset from UTC in seconds"},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000279};
280
281static PyStructSequence_Desc struct_time_type_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 "time.struct_time",
Alexander Belopolsky69f3fd02010-06-05 15:04:51 +0000283 "The time value as returned by gmtime(), localtime(), and strptime(), and\n"
284 " accepted by asctime(), mktime() and strftime(). May be considered as a\n"
285 " sequence of 9 integers.\n\n"
286 " Note that several fields' values are not the same as those defined by\n"
287 " the C language standard for struct tm. For example, the value of the\n"
288 " field tm_year is the actual year, not year - 1900. See individual\n"
289 " fields' descriptions for details.",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 struct_time_type_fields,
291 9,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000292};
Tim Peters9ad4b682002-02-13 05:14:18 +0000293
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000294static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000295static PyTypeObject StructTimeType;
296
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400297
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000298static PyObject *
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400299tmtotuple(struct tm *p
300#ifndef HAVE_STRUCT_TM_TM_ZONE
Victor Stinner0d659e52017-04-25 01:22:42 +0200301 , const char *zone, time_t gmtoff
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400302#endif
303)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 PyObject *v = PyStructSequence_New(&StructTimeType);
306 if (v == NULL)
307 return NULL;
Tim Peters9ad4b682002-02-13 05:14:18 +0000308
Christian Heimes217cfd12007-12-02 14:31:20 +0000309#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 SET(0, p->tm_year + 1900);
312 SET(1, p->tm_mon + 1); /* Want January == 1 */
313 SET(2, p->tm_mday);
314 SET(3, p->tm_hour);
315 SET(4, p->tm_min);
316 SET(5, p->tm_sec);
317 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
318 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
319 SET(8, p->tm_isdst);
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400320#ifdef HAVE_STRUCT_TM_TM_ZONE
321 PyStructSequence_SET_ITEM(v, 9,
322 PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape"));
323 SET(10, p->tm_gmtoff);
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400324#else
325 PyStructSequence_SET_ITEM(v, 9,
326 PyUnicode_DecodeLocale(zone, "surrogateescape"));
Victor Stinner0d659e52017-04-25 01:22:42 +0200327 PyStructSequence_SET_ITEM(v, 10, _PyLong_FromTime_t(gmtoff));
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400328#endif /* HAVE_STRUCT_TM_TM_ZONE */
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000329#undef SET
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 if (PyErr_Occurred()) {
331 Py_XDECREF(v);
332 return NULL;
333 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000336}
337
Fred Drakef901abd2004-08-03 17:58:55 +0000338/* Parse arg tuple that can contain an optional float-or-None value;
339 format needs to be "|O:name".
340 Returns non-zero on success (parallels PyArg_ParseTuple).
341*/
342static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200343parse_time_t_args(PyObject *args, const char *format, time_t *pwhen)
Fred Drakef901abd2004-08-03 17:58:55 +0000344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 PyObject *ot = NULL;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100346 time_t whent;
Fred Drakef901abd2004-08-03 17:58:55 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 if (!PyArg_ParseTuple(args, format, &ot))
349 return 0;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100350 if (ot == NULL || ot == Py_None) {
351 whent = time(NULL);
352 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 else {
Victor Stinner02937aa2015-03-28 05:02:39 +0100354 if (_PyTime_ObjectToTime_t(ot, &whent, _PyTime_ROUND_FLOOR) == -1)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100355 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 }
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100357 *pwhen = whent;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 return 1;
Fred Drakef901abd2004-08-03 17:58:55 +0000359}
360
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000361static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000362time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000363{
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100364 time_t when;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400365 struct tm buf;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100366
367 if (!parse_time_t_args(args, "|O:gmtime", &when))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 return NULL;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100369
370 errno = 0;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400371 if (_PyTime_gmtime(when, &buf) != 0)
372 return NULL;
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400373#ifdef HAVE_STRUCT_TM_TM_ZONE
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100374 return tmtotuple(&buf);
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400375#else
376 return tmtotuple(&buf, "UTC", 0);
377#endif
Guido van Rossum234f9421993-06-17 12:35:49 +0000378}
379
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400380#ifndef HAVE_TIMEGM
381static time_t
382timegm(struct tm *p)
383{
384 /* XXX: the following implementation will not work for tm_year < 1970.
385 but it is likely that platforms that don't have timegm do not support
386 negative timestamps anyways. */
387 return p->tm_sec + p->tm_min*60 + p->tm_hour*3600 + p->tm_yday*86400 +
388 (p->tm_year-70)*31536000 + ((p->tm_year-69)/4)*86400 -
389 ((p->tm_year-1)/100)*86400 + ((p->tm_year+299)/400)*86400;
390}
391#endif
392
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000393PyDoc_STRVAR(gmtime_doc,
Christian Heimes9a371592007-12-28 14:08:13 +0000394"gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,\n\
Fred Drake193a3f62002-03-12 21:38:49 +0000395 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000396\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000397Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400398GMT). When 'seconds' is not passed in, convert the current time instead.\n\
399\n\
400If the platform supports the tm_gmtoff and tm_zone, they are available as\n\
401attributes only.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000402
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000403static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000404time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000405{
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100406 time_t when;
407 struct tm buf;
408
409 if (!parse_time_t_args(args, "|O:localtime", &when))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 return NULL;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400411 if (_PyTime_localtime(when, &buf) != 0)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100412 return NULL;
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400413#ifdef HAVE_STRUCT_TM_TM_ZONE
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100414 return tmtotuple(&buf);
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400415#else
416 {
417 struct tm local = buf;
418 char zone[100];
Victor Stinner0d659e52017-04-25 01:22:42 +0200419 time_t gmtoff;
Steve Dowerc3c6f712016-12-14 11:22:05 -0800420 strftime(zone, sizeof(zone), "%Z", &buf);
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400421 gmtoff = timegm(&buf) - when;
422 return tmtotuple(&local, zone, gmtoff);
423 }
424#endif
Guido van Rossum234f9421993-06-17 12:35:49 +0000425}
426
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000427PyDoc_STRVAR(localtime_doc,
Christian Heimes9a371592007-12-28 14:08:13 +0000428"localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 tm_sec,tm_wday,tm_yday,tm_isdst)\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000430\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000431Convert seconds since the Epoch to a time tuple expressing local time.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000432When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000433
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000434/* Convert 9-item tuple to tm structure. Return 1 on success, set
435 * an exception and return 0 on error.
436 */
Guido van Rossum9e90a671993-06-24 11:10:19 +0000437static int
Oren Milman1d1d3e92017-08-20 18:35:36 +0300438gettmarg(PyObject *args, struct tm *p, const char *format)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 int y;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumb9081262007-08-25 03:14:09 +0000443
Alexander Belopolsky610e5442011-01-06 21:57:06 +0000444 if (!PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 PyErr_SetString(PyExc_TypeError,
446 "Tuple or struct_time argument required");
447 return 0;
448 }
Skip Montanaro41cfce92007-08-24 21:11:00 +0000449
Oren Milman1d1d3e92017-08-20 18:35:36 +0300450 if (!PyArg_ParseTuple(args, format,
Alexander Belopolsky610e5442011-01-06 21:57:06 +0000451 &y, &p->tm_mon, &p->tm_mday,
452 &p->tm_hour, &p->tm_min, &p->tm_sec,
453 &p->tm_wday, &p->tm_yday, &p->tm_isdst))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 p->tm_year = y - 1900;
456 p->tm_mon--;
457 p->tm_wday = (p->tm_wday + 1) % 7;
458 p->tm_yday--;
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400459#ifdef HAVE_STRUCT_TM_TM_ZONE
460 if (Py_TYPE(args) == &StructTimeType) {
461 PyObject *item;
462 item = PyTuple_GET_ITEM(args, 9);
Victor Stinner6e676952017-04-26 13:51:48 +0200463 p->tm_zone = item == Py_None ? NULL : (char*)PyUnicode_AsUTF8(item);
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400464 item = PyTuple_GET_ITEM(args, 10);
465 p->tm_gmtoff = item == Py_None ? 0 : PyLong_AsLong(item);
466 if (PyErr_Occurred())
467 return 0;
468 }
469#endif /* HAVE_STRUCT_TM_TM_ZONE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 return 1;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000471}
472
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000473/* Check values of the struct tm fields before it is passed to strftime() and
474 * asctime(). Return 1 if all values are valid, otherwise set an exception
475 * and returns 0.
476 */
Victor Stinneref128102010-10-07 01:00:52 +0000477static int
478checktm(struct tm* buf)
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000479{
Victor Stinneref128102010-10-07 01:00:52 +0000480 /* Checks added to make sure strftime() and asctime() does not crash Python by
481 indexing blindly into some array for a textual representation
482 by some bad index (fixes bug #897625 and #6608).
483
484 Also support values of zero from Python code for arguments in which
485 that is out of range by forcing that value to the lowest value that
486 is valid (fixed bug #1520914).
487
488 Valid ranges based on what is allowed in struct tm:
489
490 - tm_year: [0, max(int)] (1)
491 - tm_mon: [0, 11] (2)
492 - tm_mday: [1, 31]
493 - tm_hour: [0, 23]
494 - tm_min: [0, 59]
495 - tm_sec: [0, 60]
496 - tm_wday: [0, 6] (1)
497 - tm_yday: [0, 365] (2)
498 - tm_isdst: [-max(int), max(int)]
499
500 (1) gettmarg() handles bounds-checking.
501 (2) Python's acceptable range is one greater than the range in C,
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000502 thus need to check against automatic decrement by gettmarg().
503 */
504 if (buf->tm_mon == -1)
505 buf->tm_mon = 0;
506 else if (buf->tm_mon < 0 || buf->tm_mon > 11) {
507 PyErr_SetString(PyExc_ValueError, "month out of range");
508 return 0;
509 }
510 if (buf->tm_mday == 0)
511 buf->tm_mday = 1;
512 else if (buf->tm_mday < 0 || buf->tm_mday > 31) {
513 PyErr_SetString(PyExc_ValueError, "day of month out of range");
514 return 0;
515 }
516 if (buf->tm_hour < 0 || buf->tm_hour > 23) {
517 PyErr_SetString(PyExc_ValueError, "hour out of range");
518 return 0;
519 }
520 if (buf->tm_min < 0 || buf->tm_min > 59) {
521 PyErr_SetString(PyExc_ValueError, "minute out of range");
522 return 0;
523 }
524 if (buf->tm_sec < 0 || buf->tm_sec > 61) {
525 PyErr_SetString(PyExc_ValueError, "seconds out of range");
526 return 0;
527 }
528 /* tm_wday does not need checking of its upper-bound since taking
529 ``% 7`` in gettmarg() automatically restricts the range. */
530 if (buf->tm_wday < 0) {
531 PyErr_SetString(PyExc_ValueError, "day of week out of range");
532 return 0;
533 }
534 if (buf->tm_yday == -1)
535 buf->tm_yday = 0;
536 else if (buf->tm_yday < 0 || buf->tm_yday > 365) {
537 PyErr_SetString(PyExc_ValueError, "day of year out of range");
538 return 0;
539 }
540 return 1;
541}
542
Victor Stinnerc1f32ca2011-10-14 02:36:13 +0200543#ifdef MS_WINDOWS
544 /* wcsftime() doesn't format correctly time zones, see issue #10653 */
545# undef HAVE_WCSFTIME
546#endif
Alexander Belopolskycf774542012-10-02 18:39:16 -0400547#define STRFTIME_FORMAT_CODES \
548"Commonly used format codes:\n\
549\n\
550%Y Year with century as a decimal number.\n\
551%m Month as a decimal number [01,12].\n\
552%d Day of the month as a decimal number [01,31].\n\
553%H Hour (24-hour clock) as a decimal number [00,23].\n\
554%M Minute as a decimal number [00,59].\n\
555%S Second as a decimal number [00,61].\n\
556%z Time zone offset from UTC.\n\
557%a Locale's abbreviated weekday name.\n\
558%A Locale's full weekday name.\n\
559%b Locale's abbreviated month name.\n\
560%B Locale's full month name.\n\
561%c Locale's appropriate date and time representation.\n\
562%I Hour (12-hour clock) as a decimal number [01,12].\n\
563%p Locale's equivalent of either AM or PM.\n\
564\n\
565Other codes may be available on your platform. See documentation for\n\
566the C library strftime function.\n"
Victor Stinnerc1f32ca2011-10-14 02:36:13 +0200567
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000568#ifdef HAVE_STRFTIME
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000569#ifdef HAVE_WCSFTIME
570#define time_char wchar_t
571#define format_time wcsftime
572#define time_strlen wcslen
573#else
574#define time_char char
575#define format_time strftime
576#define time_strlen strlen
577#endif
578
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000579static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000580time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 PyObject *tup = NULL;
583 struct tm buf;
584 const time_char *fmt;
Victor Stinnerb2904782010-09-29 10:34:19 +0000585#ifdef HAVE_WCSFTIME
586 wchar_t *format;
587#else
588 PyObject *format;
589#endif
Victor Stinneref128102010-10-07 01:00:52 +0000590 PyObject *format_arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 size_t fmtlen, buflen;
Victor Stinnerb2904782010-09-29 10:34:19 +0000592 time_char *outbuf = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 size_t i;
Victor Stinnerb2904782010-09-29 10:34:19 +0000594 PyObject *ret = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 /* Will always expect a unicode string to be passed as format.
599 Given that there's no str type anymore in py3k this seems safe.
600 */
Victor Stinneref128102010-10-07 01:00:52 +0000601 if (!PyArg_ParseTuple(args, "U|O:strftime", &format_arg, &tup))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 if (tup == NULL) {
605 time_t tt = time(NULL);
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400606 if (_PyTime_localtime(tt, &buf) != 0)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100607 return NULL;
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000608 }
Oren Milman1d1d3e92017-08-20 18:35:36 +0300609 else if (!gettmarg(tup, &buf,
610 "iiiiiiiii;strftime(): illegal time tuple argument") ||
611 !checktm(&buf))
612 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 return NULL;
Oren Milman1d1d3e92017-08-20 18:35:36 +0300614 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000615
Victor Stinner36b82d82013-06-25 02:33:53 +0200616#if defined(_MSC_VER) || defined(sun) || defined(_AIX)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000617 if (buf.tm_year + 1900 < 1 || 9999 < buf.tm_year + 1900) {
Victor Stinner6f0e4f92011-03-21 02:14:53 +0100618 PyErr_SetString(PyExc_ValueError,
619 "strftime() requires year in [1; 9999]");
Alexander Belopolsky0dd06f42011-01-08 01:23:02 +0000620 return NULL;
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000621 }
Victor Stinner73ea29c2011-01-08 01:56:31 +0000622#endif
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 /* Normalize tm_isdst just in case someone foolishly implements %Z
625 based on the assumption that tm_isdst falls within the range of
626 [-1, 1] */
627 if (buf.tm_isdst < -1)
628 buf.tm_isdst = -1;
629 else if (buf.tm_isdst > 1)
630 buf.tm_isdst = 1;
Brett Cannond1080a32004-03-02 04:38:10 +0000631
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000632#ifdef HAVE_WCSFTIME
Victor Stinnerbeb4135b2010-10-07 01:02:42 +0000633 format = PyUnicode_AsWideCharString(format_arg, NULL);
Victor Stinnerb2904782010-09-29 10:34:19 +0000634 if (format == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 return NULL;
Victor Stinnerb2904782010-09-29 10:34:19 +0000636 fmt = format;
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000637#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 /* Convert the unicode string to an ascii one */
Victor Stinner1b579672011-12-17 05:47:23 +0100639 format = PyUnicode_EncodeLocale(format_arg, "surrogateescape");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 if (format == NULL)
641 return NULL;
642 fmt = PyBytes_AS_STRING(format);
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000643#endif
Amaury Forgeot d'Arcb5be6d42009-03-02 23:52:57 +0000644
Stefan Krah4aea7d32012-02-27 16:30:26 +0100645#if defined(MS_WINDOWS) && !defined(HAVE_WCSFTIME)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 /* check that the format string contains only valid directives */
Steve Dowere5b58952015-09-06 19:20:51 -0700647 for (outbuf = strchr(fmt, '%');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 outbuf != NULL;
Victor Stinner5a3ff792011-10-16 19:08:23 +0200649 outbuf = strchr(outbuf+2, '%'))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 {
Steve Dowere5b58952015-09-06 19:20:51 -0700651 if (outbuf[1] == '#')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 ++outbuf; /* not documented by python, */
Steve Dowere5b58952015-09-06 19:20:51 -0700653 if (outbuf[1] == '\0')
654 break;
655 if ((outbuf[1] == 'y') && buf.tm_year < 0) {
Tim Golden6e51b8f2013-11-12 12:36:54 +0000656 PyErr_SetString(PyExc_ValueError,
657 "format %y requires year >= 1900 on Windows");
658 Py_DECREF(format);
659 return NULL;
660 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 }
Victor Stinner93965f72013-11-23 14:59:33 +0100662#elif (defined(_AIX) || defined(sun)) && defined(HAVE_WCSFTIME)
Steve Dowere5b58952015-09-06 19:20:51 -0700663 for (outbuf = wcschr(fmt, '%');
Victor Stinner55329f82013-11-17 23:39:21 +0100664 outbuf != NULL;
665 outbuf = wcschr(outbuf+2, '%'))
666 {
Steve Dowere5b58952015-09-06 19:20:51 -0700667 if (outbuf[1] == L'\0')
668 break;
Victor Stinner55329f82013-11-17 23:39:21 +0100669 /* Issue #19634: On AIX, wcsftime("y", (1899, 1, 1, 0, 0, 0, 0, 0, 0))
670 returns "0/" instead of "99" */
671 if (outbuf[1] == L'y' && buf.tm_year < 0) {
672 PyErr_SetString(PyExc_ValueError,
673 "format %y requires year >= 1900 on AIX");
Victor Stinner55329f82013-11-17 23:39:21 +0100674 return NULL;
675 }
676 }
Amaury Forgeot d'Arcb5be6d42009-03-02 23:52:57 +0000677#endif
678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 fmtlen = time_strlen(fmt);
Guido van Rossumc222ec21999-02-23 00:00:10 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 /* I hate these functions that presume you know how big the output
682 * will be ahead of time...
683 */
684 for (i = 1024; ; i += i) {
685 outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char));
686 if (outbuf == NULL) {
Victor Stinnerb2904782010-09-29 10:34:19 +0000687 PyErr_NoMemory();
688 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 }
Steve Dower57ab1cd2015-09-22 14:51:42 -0700690#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
691 errno = 0;
692#endif
Steve Dowerd9ef74e2015-05-22 15:08:34 -0700693 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 buflen = format_time(outbuf, i, fmt, &buf);
Steve Dowerd9ef74e2015-05-22 15:08:34 -0700695 _Py_END_SUPPRESS_IPH
Victor Stinner136ea492011-12-17 22:37:18 +0100696#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Steve Dower97cded92015-09-08 19:12:51 -0700697 /* VisualStudio .NET 2005 does this properly */
698 if (buflen == 0 && errno == EINVAL) {
699 PyErr_SetString(PyExc_ValueError, "Invalid format string");
700 PyMem_Free(outbuf);
701 break;
702 }
Victor Stinner136ea492011-12-17 22:37:18 +0100703#endif
Steve Dower97cded92015-09-08 19:12:51 -0700704 if (buflen > 0 || i >= 256 * fmtlen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 /* If the buffer is 256 times as long as the format,
706 it's probably not failing for lack of room!
707 More likely, the format yields an empty result,
708 e.g. an empty format, or %Z when the timezone
709 is unknown. */
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000710#ifdef HAVE_WCSFTIME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 ret = PyUnicode_FromWideChar(outbuf, buflen);
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000712#else
Victor Stinner1b579672011-12-17 05:47:23 +0100713 ret = PyUnicode_DecodeLocaleAndSize(outbuf, buflen,
714 "surrogateescape");
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000715#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 PyMem_Free(outbuf);
Victor Stinnerb2904782010-09-29 10:34:19 +0000717 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 }
719 PyMem_Free(outbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 }
Victor Stinnerb2904782010-09-29 10:34:19 +0000721#ifdef HAVE_WCSFTIME
722 PyMem_Free(format);
723#else
724 Py_DECREF(format);
725#endif
726 return ret;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000727}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000728
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000729#undef time_char
730#undef format_time
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000731PyDoc_STRVAR(strftime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000732"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000733\n\
734Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000735See the library reference manual for formatting codes. When the time tuple\n\
Alexander Belopolskycf774542012-10-02 18:39:16 -0400736is not present, current time as returned by localtime() is used.\n\
737\n" STRFTIME_FORMAT_CODES);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000738#endif /* HAVE_STRFTIME */
739
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000740static PyObject *
741time_strptime(PyObject *self, PyObject *args)
742{
Victor Stinnerdbe28d22016-12-09 00:38:53 +0100743 PyObject *module, *func, *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200744 _Py_IDENTIFIER(_strptime_time);
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000745
Victor Stinnerdbe28d22016-12-09 00:38:53 +0100746 module = PyImport_ImportModuleNoBlock("_strptime");
747 if (!module)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 return NULL;
Victor Stinnerdbe28d22016-12-09 00:38:53 +0100749
750 func = _PyObject_GetAttrId(module, &PyId__strptime_time);
751 Py_DECREF(module);
752 if (!func) {
753 return NULL;
754 }
755
756 result = PyObject_Call(func, args, NULL);
757 Py_DECREF(func);
758 return result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000759}
760
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000761
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000762PyDoc_STRVAR(strptime_doc,
Brett Cannon20def8b2003-07-01 05:16:08 +0000763"strptime(string, format) -> struct_time\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000764\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000765Parse a string to a time tuple according to a format specification.\n\
Alexander Belopolskycf774542012-10-02 18:39:16 -0400766See the library reference manual for formatting codes (same as\n\
767strftime()).\n\
768\n" STRFTIME_FORMAT_CODES);
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000769
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000770static PyObject *
771_asctime(struct tm *timeptr)
772{
773 /* Inspired by Open Group reference implementation available at
774 * http://pubs.opengroup.org/onlinepubs/009695399/functions/asctime.html */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200775 static const char wday_name[7][4] = {
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000776 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
777 };
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200778 static const char mon_name[12][4] = {
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000779 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
780 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
781 };
Victor Stinner499dfcf2011-03-21 13:26:24 +0100782 return PyUnicode_FromFormat(
783 "%s %s%3d %.2d:%.2d:%.2d %d",
784 wday_name[timeptr->tm_wday],
785 mon_name[timeptr->tm_mon],
786 timeptr->tm_mday, timeptr->tm_hour,
787 timeptr->tm_min, timeptr->tm_sec,
788 1900 + timeptr->tm_year);
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000789}
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000790
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000791static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000792time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 PyObject *tup = NULL;
795 struct tm buf;
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup))
798 return NULL;
799 if (tup == NULL) {
800 time_t tt = time(NULL);
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400801 if (_PyTime_localtime(tt, &buf) != 0)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100802 return NULL;
Oren Milman1d1d3e92017-08-20 18:35:36 +0300803 }
804 else if (!gettmarg(tup, &buf,
805 "iiiiiiiii;asctime(): illegal time tuple argument") ||
806 !checktm(&buf))
807 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 return NULL;
Oren Milman1d1d3e92017-08-20 18:35:36 +0300809 }
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000810 return _asctime(&buf);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000811}
812
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000813PyDoc_STRVAR(asctime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000814"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000815\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000816Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
817When the time tuple is not present, current time as returned by localtime()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000818is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000819
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000820static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000821time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 time_t tt;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100824 struct tm buf;
825 if (!parse_time_t_args(args, "|O:ctime", &tt))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 return NULL;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400827 if (_PyTime_localtime(tt, &buf) != 0)
Alexander Belopolsky5da468f2011-01-04 17:15:52 +0000828 return NULL;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100829 return _asctime(&buf);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000830}
831
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000832PyDoc_STRVAR(ctime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000833"ctime(seconds) -> string\n\
834\n\
835Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000836This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000837not present, current time as returned by localtime() is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000838
Guido van Rossum60cd8131998-03-06 17:16:21 +0000839#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000840static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +0100841time_mktime(PyObject *self, PyObject *tup)
Guido van Rossum234f9421993-06-17 12:35:49 +0000842{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 struct tm buf;
844 time_t tt;
Oren Milman1d1d3e92017-08-20 18:35:36 +0300845 if (!gettmarg(tup, &buf,
846 "iiiiiiiii;mktime(): illegal time tuple argument"))
847 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 return NULL;
Oren Milman1d1d3e92017-08-20 18:35:36 +0300849 }
Victor Stinner1ac42612014-02-21 09:27:17 +0100850#ifdef _AIX
851 /* year < 1902 or year > 2037 */
852 if (buf.tm_year < 2 || buf.tm_year > 137) {
853 /* Issue #19748: On AIX, mktime() doesn't report overflow error for
854 * timestamp < -2^31 or timestamp > 2**31-1. */
855 PyErr_SetString(PyExc_OverflowError,
856 "mktime argument out of range");
857 return NULL;
858 }
859#else
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000860 buf.tm_wday = -1; /* sentinel; original value ignored */
Victor Stinner1ac42612014-02-21 09:27:17 +0100861#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 tt = mktime(&buf);
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000863 /* Return value of -1 does not necessarily mean an error, but tm_wday
Ezio Melotti13925002011-03-16 11:05:33 +0200864 * cannot remain set to -1 if mktime succeeded. */
Victor Stinner93037492013-06-25 22:54:35 +0200865 if (tt == (time_t)(-1)
866#ifndef _AIX
867 /* Return value of -1 does not necessarily mean an error, but
868 * tm_wday cannot remain set to -1 if mktime succeeded. */
869 && buf.tm_wday == -1
870#else
871 /* on AIX, tm_wday is always sets, even on error */
872#endif
873 )
874 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 PyErr_SetString(PyExc_OverflowError,
876 "mktime argument out of range");
877 return NULL;
878 }
Victor Stinner4195b5c2012-02-08 23:03:19 +0100879 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000880}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000881
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000882PyDoc_STRVAR(mktime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000883"mktime(tuple) -> floating point number\n\
884\n\
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400885Convert a time tuple in local time to seconds since the Epoch.\n\
886Note that mktime(gmtime(0)) will not generally return zero for most\n\
887time zones; instead the returned value will either be equal to that\n\
888of the timezone or altzone attributes on the time module.");
Guido van Rossum60cd8131998-03-06 17:16:21 +0000889#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000890
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000891#ifdef HAVE_WORKING_TZSET
Martin v. Löwis1a214512008-06-11 05:26:20 +0000892static void PyInit_timezone(PyObject *module);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000893
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000894static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000895time_tzset(PyObject *self, PyObject *unused)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 PyObject* m;
Fred Drake9bb74322002-04-01 14:49:59 +0000898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 m = PyImport_ImportModuleNoBlock("time");
900 if (m == NULL) {
901 return NULL;
902 }
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 tzset();
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 /* Reset timezone, altzone, daylight and tzname */
907 PyInit_timezone(m);
908 Py_DECREF(m);
Victor Stinner2ff51b82013-07-17 21:42:45 +0200909 if (PyErr_Occurred())
910 return NULL;
Tim Peters1b6f7a92004-06-20 02:50:16 +0000911
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200912 Py_RETURN_NONE;
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000913}
914
915PyDoc_STRVAR(tzset_doc,
R. David Murray4d55bf92010-12-14 00:55:46 +0000916"tzset()\n\
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000917\n\
918Initialize, or reinitialize, the local timezone to the value stored in\n\
919os.environ['TZ']. The TZ environment variable should be specified in\n\
Neal Norwitzdc8e1942004-07-20 22:34:37 +0000920standard Unix timezone format as documented in the tzset man page\n\
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000921(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
922fall back to UTC. If the TZ environment variable is not set, the local\n\
923timezone is set to the systems best guess of wallclock time.\n\
924Changing the TZ environment variable without calling tzset *may* change\n\
925the local timezone used by methods such as localtime, but this behaviour\n\
926should not be relied on.");
927#endif /* HAVE_WORKING_TZSET */
928
Victor Stinnerae586492014-09-02 23:18:25 +0200929static PyObject *
Victor Stinnerec895392012-04-29 02:41:27 +0200930pymonotonic(_Py_clock_info_t *info)
Victor Stinnerb94b2662012-01-18 01:50:21 +0100931{
Victor Stinner4bfb4602015-03-27 22:27:24 +0100932 _PyTime_t t;
Victor Stinner4bfb4602015-03-27 22:27:24 +0100933 if (_PyTime_GetMonotonicClockWithInfo(&t, info) < 0) {
Victor Stinnerae586492014-09-02 23:18:25 +0200934 assert(info != NULL);
Victor Stinner071eca32012-03-15 01:17:09 +0100935 return NULL;
936 }
Victor Stinnera997c7b2017-10-10 02:51:50 -0700937 return _PyFloat_FromPyTime(t);
Victor Stinner8b302012012-02-07 23:29:46 +0100938}
939
Victor Stinner071eca32012-03-15 01:17:09 +0100940static PyObject *
Victor Stinnerec895392012-04-29 02:41:27 +0200941time_monotonic(PyObject *self, PyObject *unused)
Victor Stinner071eca32012-03-15 01:17:09 +0100942{
Victor Stinnerec895392012-04-29 02:41:27 +0200943 return pymonotonic(NULL);
Victor Stinner071eca32012-03-15 01:17:09 +0100944}
945
Victor Stinnerec895392012-04-29 02:41:27 +0200946PyDoc_STRVAR(monotonic_doc,
947"monotonic() -> float\n\
Victor Stinner8b302012012-02-07 23:29:46 +0100948\n\
Victor Stinnerec895392012-04-29 02:41:27 +0200949Monotonic clock, cannot go backward.");
Victor Stinnerec919cc2012-03-15 00:58:32 +0100950
Victor Stinnerec895392012-04-29 02:41:27 +0200951static PyObject *
952time_perf_counter(PyObject *self, PyObject *unused)
953{
954 return perf_counter(NULL);
955}
956
957PyDoc_STRVAR(perf_counter_doc,
958"perf_counter() -> float\n\
959\n\
960Performance counter for benchmarking.");
961
962static PyObject*
963py_process_time(_Py_clock_info_t *info)
964{
965#if defined(MS_WINDOWS)
966 HANDLE process;
967 FILETIME creation_time, exit_time, kernel_time, user_time;
968 ULARGE_INTEGER large;
969 double total;
970 BOOL ok;
971
972 process = GetCurrentProcess();
973 ok = GetProcessTimes(process, &creation_time, &exit_time, &kernel_time, &user_time);
974 if (!ok)
975 return PyErr_SetFromWindowsErr(0);
976
977 large.u.LowPart = kernel_time.dwLowDateTime;
978 large.u.HighPart = kernel_time.dwHighDateTime;
979 total = (double)large.QuadPart;
980 large.u.LowPart = user_time.dwLowDateTime;
981 large.u.HighPart = user_time.dwHighDateTime;
982 total += (double)large.QuadPart;
983 if (info) {
984 info->implementation = "GetProcessTimes()";
985 info->resolution = 1e-7;
Benjamin Peterson49a69e42012-05-01 09:38:34 -0400986 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +0200987 info->adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +0200988 }
989 return PyFloat_FromDouble(total * 1e-7);
990#else
991
992#if defined(HAVE_SYS_RESOURCE_H)
993 struct rusage ru;
994#endif
995#ifdef HAVE_TIMES
996 struct tms t;
997 static long ticks_per_second = -1;
998#endif
999
1000#if defined(HAVE_CLOCK_GETTIME) \
1001 && (defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_PROF))
1002 struct timespec tp;
1003#ifdef CLOCK_PROF
1004 const clockid_t clk_id = CLOCK_PROF;
1005 const char *function = "clock_gettime(CLOCK_PROF)";
1006#else
1007 const clockid_t clk_id = CLOCK_PROCESS_CPUTIME_ID;
1008 const char *function = "clock_gettime(CLOCK_PROCESS_CPUTIME_ID)";
1009#endif
1010
1011 if (clock_gettime(clk_id, &tp) == 0) {
1012 if (info) {
1013 struct timespec res;
1014 info->implementation = function;
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001015 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001016 info->adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001017 if (clock_getres(clk_id, &res) == 0)
1018 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
1019 else
1020 info->resolution = 1e-9;
1021 }
1022 return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
1023 }
1024#endif
1025
1026#if defined(HAVE_SYS_RESOURCE_H)
1027 if (getrusage(RUSAGE_SELF, &ru) == 0) {
1028 double total;
1029 total = ru.ru_utime.tv_sec + ru.ru_utime.tv_usec * 1e-6;
1030 total += ru.ru_stime.tv_sec + ru.ru_stime.tv_usec * 1e-6;
1031 if (info) {
1032 info->implementation = "getrusage(RUSAGE_SELF)";
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001033 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001034 info->adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001035 info->resolution = 1e-6;
1036 }
1037 return PyFloat_FromDouble(total);
1038 }
1039#endif
1040
1041#ifdef HAVE_TIMES
1042 if (times(&t) != (clock_t)-1) {
1043 double total;
1044
1045 if (ticks_per_second == -1) {
1046#if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
1047 ticks_per_second = sysconf(_SC_CLK_TCK);
1048 if (ticks_per_second < 1)
1049 ticks_per_second = -1;
1050#elif defined(HZ)
1051 ticks_per_second = HZ;
1052#else
1053 ticks_per_second = 60; /* magic fallback value; may be bogus */
1054#endif
1055 }
1056
1057 if (ticks_per_second != -1) {
1058 total = (double)t.tms_utime / ticks_per_second;
1059 total += (double)t.tms_stime / ticks_per_second;
1060 if (info) {
1061 info->implementation = "times()";
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001062 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001063 info->adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001064 info->resolution = 1.0 / ticks_per_second;
1065 }
1066 return PyFloat_FromDouble(total);
1067 }
1068 }
1069#endif
1070
Victor Stinner53e22bf2016-07-08 17:55:01 +02001071 /* Currently, Python 3 requires clock() to build: see issue #22624 */
Victor Stinnerec895392012-04-29 02:41:27 +02001072 return floatclock(info);
1073#endif
1074}
1075
1076static PyObject *
1077time_process_time(PyObject *self, PyObject *unused)
1078{
1079 return py_process_time(NULL);
1080}
1081
1082PyDoc_STRVAR(process_time_doc,
1083"process_time() -> float\n\
1084\n\
1085Process time for profiling: sum of the kernel and user-space CPU time.");
1086
1087
Victor Stinnerec895392012-04-29 02:41:27 +02001088static PyObject *
1089time_get_clock_info(PyObject *self, PyObject *args)
1090{
1091 char *name;
Victor Stinnerec895392012-04-29 02:41:27 +02001092 _Py_clock_info_t info;
Victor Stinnerbda4b882012-06-12 22:11:44 +02001093 PyObject *obj = NULL, *dict, *ns;
Victor Stinnerec895392012-04-29 02:41:27 +02001094
1095 if (!PyArg_ParseTuple(args, "s:get_clock_info", &name))
1096 return NULL;
1097
1098#ifdef Py_DEBUG
1099 info.implementation = NULL;
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001100 info.monotonic = -1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001101 info.adjustable = -1;
Victor Stinnerec895392012-04-29 02:41:27 +02001102 info.resolution = -1.0;
1103#else
1104 info.implementation = "";
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001105 info.monotonic = 0;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001106 info.adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001107 info.resolution = 1.0;
1108#endif
1109
1110 if (strcmp(name, "time") == 0)
1111 obj = floattime(&info);
1112#ifdef PYCLOCK
1113 else if (strcmp(name, "clock") == 0)
1114 obj = pyclock(&info);
1115#endif
Victor Stinnerec895392012-04-29 02:41:27 +02001116 else if (strcmp(name, "monotonic") == 0)
1117 obj = pymonotonic(&info);
Victor Stinnerec895392012-04-29 02:41:27 +02001118 else if (strcmp(name, "perf_counter") == 0)
1119 obj = perf_counter(&info);
1120 else if (strcmp(name, "process_time") == 0)
1121 obj = py_process_time(&info);
1122 else {
1123 PyErr_SetString(PyExc_ValueError, "unknown clock");
1124 return NULL;
1125 }
1126 if (obj == NULL)
1127 return NULL;
1128 Py_DECREF(obj);
1129
Victor Stinnerbda4b882012-06-12 22:11:44 +02001130 dict = PyDict_New();
1131 if (dict == NULL)
Victor Stinnerec895392012-04-29 02:41:27 +02001132 return NULL;
1133
1134 assert(info.implementation != NULL);
1135 obj = PyUnicode_FromString(info.implementation);
1136 if (obj == NULL)
1137 goto error;
Victor Stinnerbda4b882012-06-12 22:11:44 +02001138 if (PyDict_SetItemString(dict, "implementation", obj) == -1)
1139 goto error;
1140 Py_CLEAR(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001141
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001142 assert(info.monotonic != -1);
1143 obj = PyBool_FromLong(info.monotonic);
Victor Stinnerec895392012-04-29 02:41:27 +02001144 if (obj == NULL)
1145 goto error;
Victor Stinnerbda4b882012-06-12 22:11:44 +02001146 if (PyDict_SetItemString(dict, "monotonic", obj) == -1)
1147 goto error;
1148 Py_CLEAR(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001149
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001150 assert(info.adjustable != -1);
1151 obj = PyBool_FromLong(info.adjustable);
Victor Stinnerec895392012-04-29 02:41:27 +02001152 if (obj == NULL)
1153 goto error;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001154 if (PyDict_SetItemString(dict, "adjustable", obj) == -1)
Victor Stinnerbda4b882012-06-12 22:11:44 +02001155 goto error;
1156 Py_CLEAR(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001157
1158 assert(info.resolution > 0.0);
1159 assert(info.resolution <= 1.0);
1160 obj = PyFloat_FromDouble(info.resolution);
1161 if (obj == NULL)
1162 goto error;
Victor Stinnerbda4b882012-06-12 22:11:44 +02001163 if (PyDict_SetItemString(dict, "resolution", obj) == -1)
1164 goto error;
1165 Py_CLEAR(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001166
Victor Stinnerbda4b882012-06-12 22:11:44 +02001167 ns = _PyNamespace_New(dict);
1168 Py_DECREF(dict);
1169 return ns;
Victor Stinnerec895392012-04-29 02:41:27 +02001170
1171error:
Victor Stinnerbda4b882012-06-12 22:11:44 +02001172 Py_DECREF(dict);
1173 Py_XDECREF(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001174 return NULL;
1175}
1176
1177PyDoc_STRVAR(get_clock_info_doc,
1178"get_clock_info(name: str) -> dict\n\
1179\n\
1180Get information of the specified clock.");
Victor Stinner8b302012012-02-07 23:29:46 +01001181
Victor Stinner8f5cdfa2017-04-20 13:41:09 +02001182#if !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__)
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001183static void
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001184get_zone(char *zone, int n, struct tm *p)
1185{
1186#ifdef HAVE_STRUCT_TM_TM_ZONE
1187 strncpy(zone, p->tm_zone ? p->tm_zone : " ", n);
1188#else
1189 tzset();
1190 strftime(zone, n, "%Z", p);
1191#endif
1192}
1193
1194static int
1195get_gmtoff(time_t t, struct tm *p)
1196{
1197#ifdef HAVE_STRUCT_TM_TM_ZONE
1198 return p->tm_gmtoff;
1199#else
1200 return timegm(p) - t;
1201#endif
1202}
Victor Stinner8f5cdfa2017-04-20 13:41:09 +02001203#endif /* !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__) */
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001204
1205static void
Martin v. Löwis1a214512008-06-11 05:26:20 +00001206PyInit_timezone(PyObject *m) {
1207 /* This code moved from PyInit_time wholesale to allow calling it from
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 time_tzset. In the future, some parts of it can be moved back
1209 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
1210 are), and the extraneous calls to tzset(3) should be removed.
1211 I haven't done this yet, as I don't want to change this code as
1212 little as possible when introducing the time.tzset and time.tzsetwall
1213 methods. This should simply be a method of doing the following once,
1214 at the top of this function and removing the call to tzset() from
1215 time_tzset():
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 #ifdef HAVE_TZSET
1218 tzset()
1219 #endif
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 And I'm lazy and hate C so nyer.
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001222 */
Guido van Rossum10b164a2001-09-25 13:59:01 +00001223#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 PyObject *otz0, *otz1;
1225 tzset();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001227#ifdef HAVE_ALTZONE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001229#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001231#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 PyModule_AddIntConstant(m, "daylight", daylight);
Victor Stinner1b579672011-12-17 05:47:23 +01001233 otz0 = PyUnicode_DecodeLocale(tzname[0], "surrogateescape");
1234 otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
Guido van Rossum10b164a2001-09-25 13:59:01 +00001236#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 {
Guido van Rossum234f9421993-06-17 12:35:49 +00001238#define YEAR ((time_t)((365 * 24 + 6) * 3600))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 time_t t;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001240 struct tm p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 long janzone, julyzone;
1242 char janname[10], julyname[10];
1243 t = (time((time_t *)0) / YEAR) * YEAR;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001244 _PyTime_localtime(t, &p);
1245 get_zone(janname, 9, &p);
1246 janzone = -get_gmtoff(t, &p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 janname[9] = '\0';
1248 t += YEAR/2;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001249 _PyTime_localtime(t, &p);
1250 get_zone(julyname, 9, &p);
1251 julyzone = -get_gmtoff(t, &p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 if( janzone < julyzone ) {
1255 /* DST is reversed in the southern hemisphere */
1256 PyModule_AddIntConstant(m, "timezone", julyzone);
1257 PyModule_AddIntConstant(m, "altzone", janzone);
1258 PyModule_AddIntConstant(m, "daylight",
1259 janzone != julyzone);
1260 PyModule_AddObject(m, "tzname",
1261 Py_BuildValue("(zz)",
1262 julyname, janname));
1263 } else {
1264 PyModule_AddIntConstant(m, "timezone", janzone);
1265 PyModule_AddIntConstant(m, "altzone", julyzone);
1266 PyModule_AddIntConstant(m, "daylight",
1267 janzone != julyzone);
1268 PyModule_AddObject(m, "tzname",
1269 Py_BuildValue("(zz)",
1270 janname, julyname));
1271 }
1272 }
Tim Peters26ae7cd2001-03-20 03:26:49 +00001273#ifdef __CYGWIN__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 tzset();
1275 PyModule_AddIntConstant(m, "timezone", _timezone);
1276 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
1277 PyModule_AddIntConstant(m, "daylight", _daylight);
1278 PyModule_AddObject(m, "tzname",
1279 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +00001280#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +00001281#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001282}
1283
1284
1285static PyMethodDef time_methods[] = {
Victor Stinner4195b5c2012-02-08 23:03:19 +01001286 {"time", time_time, METH_NOARGS, time_doc},
Victor Stinnerec895392012-04-29 02:41:27 +02001287#ifdef PYCLOCK
Victor Stinner4195b5c2012-02-08 23:03:19 +01001288 {"clock", time_clock, METH_NOARGS, clock_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001289#endif
Victor Stinnere0be4232011-10-25 13:06:09 +02001290#ifdef HAVE_CLOCK_GETTIME
Victor Stinner4195b5c2012-02-08 23:03:19 +01001291 {"clock_gettime", time_clock_gettime, METH_VARARGS, clock_gettime_doc},
Benjamin Peterson37098cd2016-09-13 22:55:09 -07001292#endif
1293#ifdef HAVE_CLOCK_SETTIME
Victor Stinner30d79472012-04-03 00:45:07 +02001294 {"clock_settime", time_clock_settime, METH_VARARGS, clock_settime_doc},
Benjamin Peterson37098cd2016-09-13 22:55:09 -07001295#endif
1296#ifdef HAVE_CLOCK_GETRES
Victor Stinner4195b5c2012-02-08 23:03:19 +01001297 {"clock_getres", time_clock_getres, METH_VARARGS, clock_getres_doc},
Victor Stinnere0be4232011-10-25 13:06:09 +02001298#endif
pdoxe14679c2017-10-05 00:01:56 -07001299#ifdef HAVE_PTHREAD_GETCPUCLOCKID
1300 {"pthread_getcpuclockid", time_pthread_getcpuclockid, METH_VARARGS, pthread_getcpuclockid_doc},
1301#endif
Victor Stinnercb29f012015-03-27 13:31:18 +01001302 {"sleep", time_sleep, METH_O, sleep_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
1304 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
1305 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
1306 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001307#ifdef HAVE_MKTIME
Victor Stinner4195b5c2012-02-08 23:03:19 +01001308 {"mktime", time_mktime, METH_O, mktime_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001309#endif
1310#ifdef HAVE_STRFTIME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001312#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001314#ifdef HAVE_WORKING_TZSET
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 {"tzset", time_tzset, METH_NOARGS, tzset_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001316#endif
Victor Stinnerec895392012-04-29 02:41:27 +02001317 {"monotonic", time_monotonic, METH_NOARGS, monotonic_doc},
Victor Stinnerec895392012-04-29 02:41:27 +02001318 {"process_time", time_process_time, METH_NOARGS, process_time_doc},
1319 {"perf_counter", time_perf_counter, METH_NOARGS, perf_counter_doc},
1320 {"get_clock_info", time_get_clock_info, METH_VARARGS, get_clock_info_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 {NULL, NULL} /* sentinel */
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001322};
1323
1324
1325PyDoc_STRVAR(module_doc,
1326"This module provides various functions to manipulate time values.\n\
1327\n\
1328There are two standard representations of time. One is the number\n\
1329of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
1330or a floating point number (to represent fractions of seconds).\n\
1331The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
1332The actual value can be retrieved by calling gmtime(0).\n\
1333\n\
1334The other representation is a tuple of 9 integers giving local time.\n\
1335The tuple items are:\n\
Alexander Belopolsky03163ac2011-05-02 12:20:52 -04001336 year (including century, e.g. 1998)\n\
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001337 month (1-12)\n\
1338 day (1-31)\n\
1339 hours (0-23)\n\
1340 minutes (0-59)\n\
1341 seconds (0-59)\n\
1342 weekday (0-6, Monday is 0)\n\
1343 Julian day (day in the year, 1-366)\n\
1344 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
1345If the DST flag is 0, the time is given in the regular time zone;\n\
1346if it is 1, the time is given in the DST time zone;\n\
Cheryl Sabella703ff382017-10-11 09:29:14 -04001347if it is -1, mktime() should guess based on the date and time.\n");
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001348
1349
Martin v. Löwis1a214512008-06-11 05:26:20 +00001350
1351static struct PyModuleDef timemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 PyModuleDef_HEAD_INIT,
1353 "time",
1354 module_doc,
1355 -1,
1356 time_methods,
1357 NULL,
1358 NULL,
1359 NULL,
1360 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001361};
1362
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001363PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001364PyInit_time(void)
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 PyObject *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 m = PyModule_Create(&timemodule);
1368 if (m == NULL)
1369 return NULL;
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 /* Set, or reset, module variables like time.timezone */
1372 PyInit_timezone(m);
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001373
Benjamin Peterson37098cd2016-09-13 22:55:09 -07001374#ifdef CLOCK_REALTIME
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001375 PyModule_AddIntMacro(m, CLOCK_REALTIME);
Benjamin Peterson37098cd2016-09-13 22:55:09 -07001376#endif
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001377#ifdef CLOCK_MONOTONIC
1378 PyModule_AddIntMacro(m, CLOCK_MONOTONIC);
1379#endif
1380#ifdef CLOCK_MONOTONIC_RAW
1381 PyModule_AddIntMacro(m, CLOCK_MONOTONIC_RAW);
1382#endif
1383#ifdef CLOCK_HIGHRES
1384 PyModule_AddIntMacro(m, CLOCK_HIGHRES);
1385#endif
1386#ifdef CLOCK_PROCESS_CPUTIME_ID
1387 PyModule_AddIntMacro(m, CLOCK_PROCESS_CPUTIME_ID);
1388#endif
1389#ifdef CLOCK_THREAD_CPUTIME_ID
1390 PyModule_AddIntMacro(m, CLOCK_THREAD_CPUTIME_ID);
1391#endif
Victor Stinnera64ce972017-11-02 04:19:19 -07001392#ifdef CLOCK_PROF
1393 PyModule_AddIntMacro(m, CLOCK_PROF);
1394#endif
1395#ifdef CLOCK_BOOTTIME
1396 PyModule_AddIntMacro(m, CLOCK_BOOTTIME);
1397#endif
1398#ifdef CLOCK_UPTIME
1399 PyModule_AddIntMacro(m, CLOCK_UPTIME);
1400#endif
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 if (!initialized) {
Victor Stinner1c8f0592013-07-22 22:24:54 +02001403 if (PyStructSequence_InitType2(&StructTimeType,
1404 &struct_time_type_desc) < 0)
1405 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 }
1407 Py_INCREF(&StructTimeType);
Alexander Belopolskyc142bba2012-06-13 22:15:26 -04001408 PyModule_AddIntConstant(m, "_STRUCT_TM_ITEMS", 11);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
1410 initialized = 1;
1411 return m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001412}
1413
Victor Stinner071eca32012-03-15 01:17:09 +01001414static PyObject*
Victor Stinnerec895392012-04-29 02:41:27 +02001415floattime(_Py_clock_info_t *info)
Victor Stinner4195b5c2012-02-08 23:03:19 +01001416{
Victor Stinnera47b8812015-03-27 18:16:17 +01001417 _PyTime_t t;
Victor Stinnera47b8812015-03-27 18:16:17 +01001418 if (_PyTime_GetSystemClockWithInfo(&t, info) < 0) {
Victor Stinner00111242014-08-29 16:31:59 +02001419 assert(info != NULL);
1420 return NULL;
1421 }
Victor Stinnera997c7b2017-10-10 02:51:50 -07001422 return _PyFloat_FromPyTime(t);
Victor Stinner4195b5c2012-02-08 23:03:19 +01001423}
1424
1425
Victor Stinnercb29f012015-03-27 13:31:18 +01001426/* Implement pysleep() for various platforms.
Guido van Rossumb6775db1994-08-01 11:34:53 +00001427 When interrupted (or when another error occurs), return -1 and
1428 set an exception; else return 0. */
1429
1430static int
Victor Stinnercb29f012015-03-27 13:31:18 +01001431pysleep(_PyTime_t secs)
Guido van Rossum426035c1991-02-19 12:27:35 +00001432{
Victor Stinnercb29f012015-03-27 13:31:18 +01001433 _PyTime_t deadline, monotonic;
Victor Stinner79d68f92015-03-19 21:54:09 +01001434#ifndef MS_WINDOWS
1435 struct timeval timeout;
Victor Stinner79d68f92015-03-19 21:54:09 +01001436 int err = 0;
1437#else
Victor Stinnercb29f012015-03-27 13:31:18 +01001438 _PyTime_t millisecs;
Victor Stinner79d68f92015-03-19 21:54:09 +01001439 unsigned long ul_millis;
1440 DWORD rc;
1441 HANDLE hInterruptEvent;
Victor Stinner0c2fd892015-03-17 10:49:17 +01001442#endif
Victor Stinner79d68f92015-03-19 21:54:09 +01001443
Victor Stinnercb29f012015-03-27 13:31:18 +01001444 deadline = _PyTime_GetMonotonicClock() + secs;
Victor Stinner79d68f92015-03-19 21:54:09 +01001445
1446 do {
1447#ifndef MS_WINDOWS
Victor Stinner869e1772015-03-30 03:49:14 +02001448 if (_PyTime_AsTimeval(secs, &timeout, _PyTime_ROUND_CEILING) < 0)
Victor Stinnercb29f012015-03-27 13:31:18 +01001449 return -1;
Victor Stinner79d68f92015-03-19 21:54:09 +01001450
1451 Py_BEGIN_ALLOW_THREADS
1452 err = select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout);
1453 Py_END_ALLOW_THREADS
1454
1455 if (err == 0)
1456 break;
1457
1458 if (errno != EINTR) {
Victor Stinner0c2fd892015-03-17 10:49:17 +01001459 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 return -1;
1461 }
Victor Stinner79d68f92015-03-19 21:54:09 +01001462#else
Victor Stinner869e1772015-03-30 03:49:14 +02001463 millisecs = _PyTime_AsMilliseconds(secs, _PyTime_ROUND_CEILING);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 if (millisecs > (double)ULONG_MAX) {
1465 PyErr_SetString(PyExc_OverflowError,
1466 "sleep length is too large");
1467 return -1;
1468 }
Victor Stinner79d68f92015-03-19 21:54:09 +01001469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 /* Allow sleep(0) to maintain win32 semantics, and as decreed
1471 * by Guido, only the main thread can be interrupted.
1472 */
1473 ul_millis = (unsigned long)millisecs;
Victor Stinner79d68f92015-03-19 21:54:09 +01001474 if (ul_millis == 0 || !_PyOS_IsMainThread()) {
1475 Py_BEGIN_ALLOW_THREADS
Victor Stinner0eac1302015-03-20 03:06:12 +01001476 Sleep(ul_millis);
Victor Stinner79d68f92015-03-19 21:54:09 +01001477 Py_END_ALLOW_THREADS
1478 break;
Victor Stinner945c82e2015-03-12 16:19:01 +01001479 }
Victor Stinner79d68f92015-03-19 21:54:09 +01001480
1481 hInterruptEvent = _PyOS_SigintEvent();
1482 ResetEvent(hInterruptEvent);
1483
1484 Py_BEGIN_ALLOW_THREADS
1485 rc = WaitForSingleObjectEx(hInterruptEvent, ul_millis, FALSE);
Victor Stinner0c2fd892015-03-17 10:49:17 +01001486 Py_END_ALLOW_THREADS
Victor Stinner79d68f92015-03-19 21:54:09 +01001487
1488 if (rc != WAIT_OBJECT_0)
1489 break;
Victor Stinner945c82e2015-03-12 16:19:01 +01001490#endif
Victor Stinner0c2fd892015-03-17 10:49:17 +01001491
Victor Stinner79d68f92015-03-19 21:54:09 +01001492 /* sleep was interrupted by SIGINT */
1493 if (PyErr_CheckSignals())
1494 return -1;
1495
Victor Stinnercb29f012015-03-27 13:31:18 +01001496 monotonic = _PyTime_GetMonotonicClock();
1497 secs = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001498 if (secs < 0)
Victor Stinner79d68f92015-03-19 21:54:09 +01001499 break;
1500 /* retry with the recomputed delay */
1501 } while (1);
1502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +00001504}