blob: 36a95bbcedd6fefa0de109ff055a24438badde49 [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
Guido van Rossum7bf22de1997-12-02 20:34:19 +000023#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000024#include <i86.h>
25#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000026#ifdef MS_WINDOWS
Mark Hammond975e3922002-07-16 01:29:19 +000027#define WIN32_LEAN_AND_MEAN
Guido van Rossum258ccd42001-03-02 06:53:29 +000028#include <windows.h>
Mark Hammond975e3922002-07-16 01:29:19 +000029#include "pythread.h"
Guido van Rossumcac6c721996-09-06 13:34:02 +000030#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000031#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000032
33/* Forward declarations */
Victor Stinnercb29f012015-03-27 13:31:18 +010034static int pysleep(_PyTime_t);
Victor Stinnerec895392012-04-29 02:41:27 +020035static PyObject* floattime(_Py_clock_info_t *info);
36
Barry Warsaw9a2a8a81996-12-06 23:32:14 +000037static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +010038time_time(PyObject *self, PyObject *unused)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039{
Victor Stinnerec895392012-04-29 02:41:27 +020040 return floattime(NULL);
Guido van Rossumb6775db1994-08-01 11:34:53 +000041}
42
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000043PyDoc_STRVAR(time_doc,
Victor Stinner4195b5c2012-02-08 23:03:19 +010044"time() -> floating point number\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +000045\n\
46Return the current time in seconds since the Epoch.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000047Fractions of a second may be present if the system clock provides them.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +000048
Victor Stinner85fdfa82012-01-27 00:38:48 +010049#if defined(HAVE_CLOCK)
50
51#ifndef CLOCKS_PER_SEC
52#ifdef CLK_TCK
53#define CLOCKS_PER_SEC CLK_TCK
54#else
55#define CLOCKS_PER_SEC 1000000
56#endif
57#endif
58
Victor Stinner4195b5c2012-02-08 23:03:19 +010059static PyObject *
Victor Stinnerec895392012-04-29 02:41:27 +020060floatclock(_Py_clock_info_t *info)
Victor Stinner85fdfa82012-01-27 00:38:48 +010061{
Victor Stinner4195b5c2012-02-08 23:03:19 +010062 clock_t value;
63 value = clock();
64 if (value == (clock_t)-1) {
Victor Stinner85fdfa82012-01-27 00:38:48 +010065 PyErr_SetString(PyExc_RuntimeError,
66 "the processor time used is not available "
67 "or its value cannot be represented");
Victor Stinner4195b5c2012-02-08 23:03:19 +010068 return NULL;
Victor Stinner85fdfa82012-01-27 00:38:48 +010069 }
Victor Stinnerec895392012-04-29 02:41:27 +020070 if (info) {
71 info->implementation = "clock()";
72 info->resolution = 1.0 / (double)CLOCKS_PER_SEC;
Benjamin Peterson49a69e42012-05-01 09:38:34 -040073 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +020074 info->adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +020075 }
Victor Stinner4195b5c2012-02-08 23:03:19 +010076 return PyFloat_FromDouble((double)value / CLOCKS_PER_SEC);
Victor Stinner85fdfa82012-01-27 00:38:48 +010077}
78#endif /* HAVE_CLOCK */
79
Victor Stinnerf427a142014-10-22 12:33:23 +020080#ifdef MS_WINDOWS
Victor Stinnerec895392012-04-29 02:41:27 +020081#define WIN32_PERF_COUNTER
Victor Stinner9122fdd2011-07-04 13:55:40 +020082/* Win32 has better clock replacement; we have our own version, due to Mark
83 Hammond and Tim Peters */
Victor Stinner54884492014-08-29 16:51:33 +020084static PyObject*
85win_perf_counter(_Py_clock_info_t *info)
Guido van Rossum3917c221997-04-02 05:35:28 +000086{
Victor Stinner8b302012012-02-07 23:29:46 +010087 static LONGLONG cpu_frequency = 0;
Victor Stinner4195b5c2012-02-08 23:03:19 +010088 static LONGLONG ctrStart;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 LARGE_INTEGER now;
Victor Stinner4195b5c2012-02-08 23:03:19 +010090 double diff;
Guido van Rossum3917c221997-04-02 05:35:28 +000091
Victor Stinner8b302012012-02-07 23:29:46 +010092 if (cpu_frequency == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 LARGE_INTEGER freq;
Victor Stinner8b302012012-02-07 23:29:46 +010094 QueryPerformanceCounter(&now);
Victor Stinner4195b5c2012-02-08 23:03:19 +010095 ctrStart = now.QuadPart;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
Victor Stinner54884492014-08-29 16:51:33 +020097 PyErr_SetFromWindowsErr(0);
98 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 }
Victor Stinner8b302012012-02-07 23:29:46 +0100100 cpu_frequency = freq.QuadPart;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 }
102 QueryPerformanceCounter(&now);
Victor Stinner4195b5c2012-02-08 23:03:19 +0100103 diff = (double)(now.QuadPart - ctrStart);
Victor Stinnerec895392012-04-29 02:41:27 +0200104 if (info) {
105 info->implementation = "QueryPerformanceCounter()";
106 info->resolution = 1.0 / (double)cpu_frequency;
Benjamin Peterson49a69e42012-05-01 09:38:34 -0400107 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +0200108 info->adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +0200109 }
Victor Stinner54884492014-08-29 16:51:33 +0200110 return PyFloat_FromDouble(diff / (double)cpu_frequency);
Guido van Rossum3917c221997-04-02 05:35:28 +0000111}
Victor Stinnerf427a142014-10-22 12:33:23 +0200112#endif /* MS_WINDOWS */
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000113
Victor Stinnerec895392012-04-29 02:41:27 +0200114#if defined(WIN32_PERF_COUNTER) || defined(HAVE_CLOCK)
115#define PYCLOCK
116static PyObject*
117pyclock(_Py_clock_info_t *info)
118{
119#ifdef WIN32_PERF_COUNTER
Victor Stinner54884492014-08-29 16:51:33 +0200120 return win_perf_counter(info);
121#else
Victor Stinnerec895392012-04-29 02:41:27 +0200122 return floatclock(info);
Victor Stinner54884492014-08-29 16:51:33 +0200123#endif
Victor Stinnerec895392012-04-29 02:41:27 +0200124}
125
Victor Stinner9122fdd2011-07-04 13:55:40 +0200126static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +0100127time_clock(PyObject *self, PyObject *unused)
Victor Stinner9122fdd2011-07-04 13:55:40 +0200128{
Victor Stinnerec895392012-04-29 02:41:27 +0200129 return pyclock(NULL);
Victor Stinner9122fdd2011-07-04 13:55:40 +0200130}
Victor Stinner9122fdd2011-07-04 13:55:40 +0200131
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000132PyDoc_STRVAR(clock_doc,
Victor Stinner4195b5c2012-02-08 23:03:19 +0100133"clock() -> floating point number\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000134\n\
135Return the CPU time or real time since the start of the process or since\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000136the first call to clock(). This has as much precision as the system\n\
137records.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000138#endif
139
Victor Stinnere0be4232011-10-25 13:06:09 +0200140#ifdef HAVE_CLOCK_GETTIME
141static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +0100142time_clock_gettime(PyObject *self, PyObject *args)
Victor Stinnere0be4232011-10-25 13:06:09 +0200143{
144 int ret;
Victor Stinnerb8d01692012-04-13 23:44:05 +0200145 int clk_id;
Victor Stinnere0be4232011-10-25 13:06:09 +0200146 struct timespec tp;
147
Victor Stinner4195b5c2012-02-08 23:03:19 +0100148 if (!PyArg_ParseTuple(args, "i:clock_gettime", &clk_id))
Victor Stinnere0be4232011-10-25 13:06:09 +0200149 return NULL;
150
151 ret = clock_gettime((clockid_t)clk_id, &tp);
Antoine Pitrou2c085602012-01-18 01:41:44 +0100152 if (ret != 0) {
Victor Stinnera734af32014-07-31 13:07:17 +0200153 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrou2c085602012-01-18 01:41:44 +0100154 return NULL;
155 }
Victor Stinner4195b5c2012-02-08 23:03:19 +0100156 return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
Victor Stinnere0be4232011-10-25 13:06:09 +0200157}
158
159PyDoc_STRVAR(clock_gettime_doc,
Victor Stinner4195b5c2012-02-08 23:03:19 +0100160"clock_gettime(clk_id) -> floating point number\n\
Victor Stinnere0be4232011-10-25 13:06:09 +0200161\n\
162Return the time of the specified clock clk_id.");
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700163#endif /* HAVE_CLOCK_GETTIME */
Victor Stinner30d79472012-04-03 00:45:07 +0200164
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700165#ifdef HAVE_CLOCK_SETTIME
Victor Stinner30d79472012-04-03 00:45:07 +0200166static PyObject *
167time_clock_settime(PyObject *self, PyObject *args)
168{
Victor Stinnerb8d01692012-04-13 23:44:05 +0200169 int clk_id;
Victor Stinner30d79472012-04-03 00:45:07 +0200170 PyObject *obj;
Victor Stinnerb3b45442015-03-28 04:09:41 +0100171 _PyTime_t t;
Victor Stinner30d79472012-04-03 00:45:07 +0200172 struct timespec tp;
173 int ret;
174
175 if (!PyArg_ParseTuple(args, "iO:clock_settime", &clk_id, &obj))
176 return NULL;
177
Victor Stinner02937aa2015-03-28 05:02:39 +0100178 if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_FLOOR) < 0)
Victor Stinner30d79472012-04-03 00:45:07 +0200179 return NULL;
Victor Stinnerb3b45442015-03-28 04:09:41 +0100180
181 if (_PyTime_AsTimespec(t, &tp) == -1)
182 return NULL;
Victor Stinner30d79472012-04-03 00:45:07 +0200183
184 ret = clock_settime((clockid_t)clk_id, &tp);
185 if (ret != 0) {
Victor Stinnera734af32014-07-31 13:07:17 +0200186 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinner30d79472012-04-03 00:45:07 +0200187 return NULL;
188 }
189 Py_RETURN_NONE;
190}
191
192PyDoc_STRVAR(clock_settime_doc,
193"clock_settime(clk_id, time)\n\
194\n\
195Set the time of the specified clock clk_id.");
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700196#endif /* HAVE_CLOCK_SETTIME */
Victor Stinnere0be4232011-10-25 13:06:09 +0200197
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700198#ifdef HAVE_CLOCK_GETRES
Victor Stinnere0be4232011-10-25 13:06:09 +0200199static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +0100200time_clock_getres(PyObject *self, PyObject *args)
Victor Stinnere0be4232011-10-25 13:06:09 +0200201{
202 int ret;
Victor Stinnerb8d01692012-04-13 23:44:05 +0200203 int clk_id;
Victor Stinnere0be4232011-10-25 13:06:09 +0200204 struct timespec tp;
205
Victor Stinner4195b5c2012-02-08 23:03:19 +0100206 if (!PyArg_ParseTuple(args, "i:clock_getres", &clk_id))
Victor Stinnere0be4232011-10-25 13:06:09 +0200207 return NULL;
208
209 ret = clock_getres((clockid_t)clk_id, &tp);
Antoine Pitrou2c085602012-01-18 01:41:44 +0100210 if (ret != 0) {
Victor Stinnera734af32014-07-31 13:07:17 +0200211 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrou2c085602012-01-18 01:41:44 +0100212 return NULL;
213 }
Victor Stinner4195b5c2012-02-08 23:03:19 +0100214
215 return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
Victor Stinnere0be4232011-10-25 13:06:09 +0200216}
217
218PyDoc_STRVAR(clock_getres_doc,
Victor Stinner4195b5c2012-02-08 23:03:19 +0100219"clock_getres(clk_id) -> floating point number\n\
Victor Stinnere0be4232011-10-25 13:06:09 +0200220\n\
221Return the resolution (precision) of the specified clock clk_id.");
Benjamin Peterson37098cd2016-09-13 22:55:09 -0700222#endif /* HAVE_CLOCK_GETRES */
Victor Stinnere0be4232011-10-25 13:06:09 +0200223
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000224static PyObject *
Victor Stinnercb29f012015-03-27 13:31:18 +0100225time_sleep(PyObject *self, PyObject *obj)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226{
Victor Stinnercb29f012015-03-27 13:31:18 +0100227 _PyTime_t secs;
Victor Stinner869e1772015-03-30 03:49:14 +0200228 if (_PyTime_FromSecondsObject(&secs, obj, _PyTime_ROUND_CEILING))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 return NULL;
Victor Stinner7f53a502011-07-05 22:00:25 +0200230 if (secs < 0) {
231 PyErr_SetString(PyExc_ValueError,
232 "sleep length must be non-negative");
233 return NULL;
234 }
Victor Stinnercb29f012015-03-27 13:31:18 +0100235 if (pysleep(secs) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 return NULL;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200237 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238}
239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000240PyDoc_STRVAR(sleep_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000241"sleep(seconds)\n\
242\n\
243Delay execution for a given number of seconds. The argument may be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000244a floating point number for subsecond precision.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000245
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000246static PyStructSequence_Field struct_time_type_fields[] = {
Alexander Belopolsky69f3fd02010-06-05 15:04:51 +0000247 {"tm_year", "year, for example, 1993"},
248 {"tm_mon", "month of year, range [1, 12]"},
249 {"tm_mday", "day of month, range [1, 31]"},
250 {"tm_hour", "hours, range [0, 23]"},
251 {"tm_min", "minutes, range [0, 59]"},
252 {"tm_sec", "seconds, range [0, 61])"},
253 {"tm_wday", "day of week, range [0, 6], Monday is 0"},
254 {"tm_yday", "day of year, range [1, 366]"},
255 {"tm_isdst", "1 if summer time is in effect, 0 if not, and -1 if unknown"},
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400256 {"tm_zone", "abbreviation of timezone name"},
257 {"tm_gmtoff", "offset from UTC in seconds"},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000259};
260
261static PyStructSequence_Desc struct_time_type_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 "time.struct_time",
Alexander Belopolsky69f3fd02010-06-05 15:04:51 +0000263 "The time value as returned by gmtime(), localtime(), and strptime(), and\n"
264 " accepted by asctime(), mktime() and strftime(). May be considered as a\n"
265 " sequence of 9 integers.\n\n"
266 " Note that several fields' values are not the same as those defined by\n"
267 " the C language standard for struct tm. For example, the value of the\n"
268 " field tm_year is the actual year, not year - 1900. See individual\n"
269 " fields' descriptions for details.",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 struct_time_type_fields,
271 9,
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000272};
Tim Peters9ad4b682002-02-13 05:14:18 +0000273
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000274static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000275static PyTypeObject StructTimeType;
276
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400277
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000278static PyObject *
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400279tmtotuple(struct tm *p
280#ifndef HAVE_STRUCT_TM_TM_ZONE
Victor Stinner0d659e52017-04-25 01:22:42 +0200281 , const char *zone, time_t gmtoff
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400282#endif
283)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 PyObject *v = PyStructSequence_New(&StructTimeType);
286 if (v == NULL)
287 return NULL;
Tim Peters9ad4b682002-02-13 05:14:18 +0000288
Christian Heimes217cfd12007-12-02 14:31:20 +0000289#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 SET(0, p->tm_year + 1900);
292 SET(1, p->tm_mon + 1); /* Want January == 1 */
293 SET(2, p->tm_mday);
294 SET(3, p->tm_hour);
295 SET(4, p->tm_min);
296 SET(5, p->tm_sec);
297 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
298 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
299 SET(8, p->tm_isdst);
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400300#ifdef HAVE_STRUCT_TM_TM_ZONE
301 PyStructSequence_SET_ITEM(v, 9,
302 PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape"));
303 SET(10, p->tm_gmtoff);
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400304#else
305 PyStructSequence_SET_ITEM(v, 9,
306 PyUnicode_DecodeLocale(zone, "surrogateescape"));
Victor Stinner0d659e52017-04-25 01:22:42 +0200307 PyStructSequence_SET_ITEM(v, 10, _PyLong_FromTime_t(gmtoff));
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400308#endif /* HAVE_STRUCT_TM_TM_ZONE */
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000309#undef SET
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 if (PyErr_Occurred()) {
311 Py_XDECREF(v);
312 return NULL;
313 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000316}
317
Fred Drakef901abd2004-08-03 17:58:55 +0000318/* Parse arg tuple that can contain an optional float-or-None value;
319 format needs to be "|O:name".
320 Returns non-zero on success (parallels PyArg_ParseTuple).
321*/
322static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200323parse_time_t_args(PyObject *args, const char *format, time_t *pwhen)
Fred Drakef901abd2004-08-03 17:58:55 +0000324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 PyObject *ot = NULL;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100326 time_t whent;
Fred Drakef901abd2004-08-03 17:58:55 +0000327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 if (!PyArg_ParseTuple(args, format, &ot))
329 return 0;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100330 if (ot == NULL || ot == Py_None) {
331 whent = time(NULL);
332 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 else {
Victor Stinner02937aa2015-03-28 05:02:39 +0100334 if (_PyTime_ObjectToTime_t(ot, &whent, _PyTime_ROUND_FLOOR) == -1)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100335 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 }
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100337 *pwhen = whent;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 return 1;
Fred Drakef901abd2004-08-03 17:58:55 +0000339}
340
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000341static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000342time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000343{
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100344 time_t when;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400345 struct tm buf;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100346
347 if (!parse_time_t_args(args, "|O:gmtime", &when))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 return NULL;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100349
350 errno = 0;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400351 if (_PyTime_gmtime(when, &buf) != 0)
352 return NULL;
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400353#ifdef HAVE_STRUCT_TM_TM_ZONE
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100354 return tmtotuple(&buf);
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400355#else
356 return tmtotuple(&buf, "UTC", 0);
357#endif
Guido van Rossum234f9421993-06-17 12:35:49 +0000358}
359
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400360#ifndef HAVE_TIMEGM
361static time_t
362timegm(struct tm *p)
363{
364 /* XXX: the following implementation will not work for tm_year < 1970.
365 but it is likely that platforms that don't have timegm do not support
366 negative timestamps anyways. */
367 return p->tm_sec + p->tm_min*60 + p->tm_hour*3600 + p->tm_yday*86400 +
368 (p->tm_year-70)*31536000 + ((p->tm_year-69)/4)*86400 -
369 ((p->tm_year-1)/100)*86400 + ((p->tm_year+299)/400)*86400;
370}
371#endif
372
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000373PyDoc_STRVAR(gmtime_doc,
Christian Heimes9a371592007-12-28 14:08:13 +0000374"gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,\n\
Fred Drake193a3f62002-03-12 21:38:49 +0000375 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000376\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000377Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400378GMT). When 'seconds' is not passed in, convert the current time instead.\n\
379\n\
380If the platform supports the tm_gmtoff and tm_zone, they are available as\n\
381attributes only.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000382
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000383static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000384time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000385{
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100386 time_t when;
387 struct tm buf;
388
389 if (!parse_time_t_args(args, "|O:localtime", &when))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 return NULL;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400391 if (_PyTime_localtime(when, &buf) != 0)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100392 return NULL;
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400393#ifdef HAVE_STRUCT_TM_TM_ZONE
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100394 return tmtotuple(&buf);
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400395#else
396 {
397 struct tm local = buf;
398 char zone[100];
Victor Stinner0d659e52017-04-25 01:22:42 +0200399 time_t gmtoff;
Steve Dowerc3c6f712016-12-14 11:22:05 -0800400 strftime(zone, sizeof(zone), "%Z", &buf);
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -0400401 gmtoff = timegm(&buf) - when;
402 return tmtotuple(&local, zone, gmtoff);
403 }
404#endif
Guido van Rossum234f9421993-06-17 12:35:49 +0000405}
406
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000407PyDoc_STRVAR(localtime_doc,
Christian Heimes9a371592007-12-28 14:08:13 +0000408"localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,\n\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 tm_sec,tm_wday,tm_yday,tm_isdst)\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000410\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000411Convert seconds since the Epoch to a time tuple expressing local time.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000412When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000413
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000414/* Convert 9-item tuple to tm structure. Return 1 on success, set
415 * an exception and return 0 on error.
416 */
Guido van Rossum9e90a671993-06-24 11:10:19 +0000417static int
Oren Milman1d1d3e92017-08-20 18:35:36 +0300418gettmarg(PyObject *args, struct tm *p, const char *format)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 int y;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumb9081262007-08-25 03:14:09 +0000423
Alexander Belopolsky610e5442011-01-06 21:57:06 +0000424 if (!PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 PyErr_SetString(PyExc_TypeError,
426 "Tuple or struct_time argument required");
427 return 0;
428 }
Skip Montanaro41cfce92007-08-24 21:11:00 +0000429
Oren Milman1d1d3e92017-08-20 18:35:36 +0300430 if (!PyArg_ParseTuple(args, format,
Alexander Belopolsky610e5442011-01-06 21:57:06 +0000431 &y, &p->tm_mon, &p->tm_mday,
432 &p->tm_hour, &p->tm_min, &p->tm_sec,
433 &p->tm_wday, &p->tm_yday, &p->tm_isdst))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 p->tm_year = y - 1900;
436 p->tm_mon--;
437 p->tm_wday = (p->tm_wday + 1) % 7;
438 p->tm_yday--;
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400439#ifdef HAVE_STRUCT_TM_TM_ZONE
440 if (Py_TYPE(args) == &StructTimeType) {
441 PyObject *item;
442 item = PyTuple_GET_ITEM(args, 9);
Victor Stinner6e676952017-04-26 13:51:48 +0200443 p->tm_zone = item == Py_None ? NULL : (char*)PyUnicode_AsUTF8(item);
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400444 item = PyTuple_GET_ITEM(args, 10);
445 p->tm_gmtoff = item == Py_None ? 0 : PyLong_AsLong(item);
446 if (PyErr_Occurred())
447 return 0;
448 }
449#endif /* HAVE_STRUCT_TM_TM_ZONE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 return 1;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000451}
452
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000453/* Check values of the struct tm fields before it is passed to strftime() and
454 * asctime(). Return 1 if all values are valid, otherwise set an exception
455 * and returns 0.
456 */
Victor Stinneref128102010-10-07 01:00:52 +0000457static int
458checktm(struct tm* buf)
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000459{
Victor Stinneref128102010-10-07 01:00:52 +0000460 /* Checks added to make sure strftime() and asctime() does not crash Python by
461 indexing blindly into some array for a textual representation
462 by some bad index (fixes bug #897625 and #6608).
463
464 Also support values of zero from Python code for arguments in which
465 that is out of range by forcing that value to the lowest value that
466 is valid (fixed bug #1520914).
467
468 Valid ranges based on what is allowed in struct tm:
469
470 - tm_year: [0, max(int)] (1)
471 - tm_mon: [0, 11] (2)
472 - tm_mday: [1, 31]
473 - tm_hour: [0, 23]
474 - tm_min: [0, 59]
475 - tm_sec: [0, 60]
476 - tm_wday: [0, 6] (1)
477 - tm_yday: [0, 365] (2)
478 - tm_isdst: [-max(int), max(int)]
479
480 (1) gettmarg() handles bounds-checking.
481 (2) Python's acceptable range is one greater than the range in C,
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000482 thus need to check against automatic decrement by gettmarg().
483 */
484 if (buf->tm_mon == -1)
485 buf->tm_mon = 0;
486 else if (buf->tm_mon < 0 || buf->tm_mon > 11) {
487 PyErr_SetString(PyExc_ValueError, "month out of range");
488 return 0;
489 }
490 if (buf->tm_mday == 0)
491 buf->tm_mday = 1;
492 else if (buf->tm_mday < 0 || buf->tm_mday > 31) {
493 PyErr_SetString(PyExc_ValueError, "day of month out of range");
494 return 0;
495 }
496 if (buf->tm_hour < 0 || buf->tm_hour > 23) {
497 PyErr_SetString(PyExc_ValueError, "hour out of range");
498 return 0;
499 }
500 if (buf->tm_min < 0 || buf->tm_min > 59) {
501 PyErr_SetString(PyExc_ValueError, "minute out of range");
502 return 0;
503 }
504 if (buf->tm_sec < 0 || buf->tm_sec > 61) {
505 PyErr_SetString(PyExc_ValueError, "seconds out of range");
506 return 0;
507 }
508 /* tm_wday does not need checking of its upper-bound since taking
509 ``% 7`` in gettmarg() automatically restricts the range. */
510 if (buf->tm_wday < 0) {
511 PyErr_SetString(PyExc_ValueError, "day of week out of range");
512 return 0;
513 }
514 if (buf->tm_yday == -1)
515 buf->tm_yday = 0;
516 else if (buf->tm_yday < 0 || buf->tm_yday > 365) {
517 PyErr_SetString(PyExc_ValueError, "day of year out of range");
518 return 0;
519 }
520 return 1;
521}
522
Victor Stinnerc1f32ca2011-10-14 02:36:13 +0200523#ifdef MS_WINDOWS
524 /* wcsftime() doesn't format correctly time zones, see issue #10653 */
525# undef HAVE_WCSFTIME
526#endif
Alexander Belopolskycf774542012-10-02 18:39:16 -0400527#define STRFTIME_FORMAT_CODES \
528"Commonly used format codes:\n\
529\n\
530%Y Year with century as a decimal number.\n\
531%m Month as a decimal number [01,12].\n\
532%d Day of the month as a decimal number [01,31].\n\
533%H Hour (24-hour clock) as a decimal number [00,23].\n\
534%M Minute as a decimal number [00,59].\n\
535%S Second as a decimal number [00,61].\n\
536%z Time zone offset from UTC.\n\
537%a Locale's abbreviated weekday name.\n\
538%A Locale's full weekday name.\n\
539%b Locale's abbreviated month name.\n\
540%B Locale's full month name.\n\
541%c Locale's appropriate date and time representation.\n\
542%I Hour (12-hour clock) as a decimal number [01,12].\n\
543%p Locale's equivalent of either AM or PM.\n\
544\n\
545Other codes may be available on your platform. See documentation for\n\
546the C library strftime function.\n"
Victor Stinnerc1f32ca2011-10-14 02:36:13 +0200547
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000548#ifdef HAVE_STRFTIME
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000549#ifdef HAVE_WCSFTIME
550#define time_char wchar_t
551#define format_time wcsftime
552#define time_strlen wcslen
553#else
554#define time_char char
555#define format_time strftime
556#define time_strlen strlen
557#endif
558
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000559static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000560time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 PyObject *tup = NULL;
563 struct tm buf;
564 const time_char *fmt;
Victor Stinnerb2904782010-09-29 10:34:19 +0000565#ifdef HAVE_WCSFTIME
566 wchar_t *format;
567#else
568 PyObject *format;
569#endif
Victor Stinneref128102010-10-07 01:00:52 +0000570 PyObject *format_arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 size_t fmtlen, buflen;
Victor Stinnerb2904782010-09-29 10:34:19 +0000572 time_char *outbuf = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 size_t i;
Victor Stinnerb2904782010-09-29 10:34:19 +0000574 PyObject *ret = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 /* Will always expect a unicode string to be passed as format.
579 Given that there's no str type anymore in py3k this seems safe.
580 */
Victor Stinneref128102010-10-07 01:00:52 +0000581 if (!PyArg_ParseTuple(args, "U|O:strftime", &format_arg, &tup))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 if (tup == NULL) {
585 time_t tt = time(NULL);
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400586 if (_PyTime_localtime(tt, &buf) != 0)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100587 return NULL;
Alexander Belopolsky38e29962010-10-01 14:18:49 +0000588 }
Oren Milman1d1d3e92017-08-20 18:35:36 +0300589 else if (!gettmarg(tup, &buf,
590 "iiiiiiiii;strftime(): illegal time tuple argument") ||
591 !checktm(&buf))
592 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 return NULL;
Oren Milman1d1d3e92017-08-20 18:35:36 +0300594 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000595
Victor Stinner36b82d82013-06-25 02:33:53 +0200596#if defined(_MSC_VER) || defined(sun) || defined(_AIX)
Victor Stinner73ea29c2011-01-08 01:56:31 +0000597 if (buf.tm_year + 1900 < 1 || 9999 < buf.tm_year + 1900) {
Victor Stinner6f0e4f92011-03-21 02:14:53 +0100598 PyErr_SetString(PyExc_ValueError,
599 "strftime() requires year in [1; 9999]");
Alexander Belopolsky0dd06f42011-01-08 01:23:02 +0000600 return NULL;
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000601 }
Victor Stinner73ea29c2011-01-08 01:56:31 +0000602#endif
Alexander Belopolskyc64708a2011-01-07 19:59:19 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 /* Normalize tm_isdst just in case someone foolishly implements %Z
605 based on the assumption that tm_isdst falls within the range of
606 [-1, 1] */
607 if (buf.tm_isdst < -1)
608 buf.tm_isdst = -1;
609 else if (buf.tm_isdst > 1)
610 buf.tm_isdst = 1;
Brett Cannond1080a32004-03-02 04:38:10 +0000611
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000612#ifdef HAVE_WCSFTIME
Victor Stinnerbeb4135b2010-10-07 01:02:42 +0000613 format = PyUnicode_AsWideCharString(format_arg, NULL);
Victor Stinnerb2904782010-09-29 10:34:19 +0000614 if (format == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 return NULL;
Victor Stinnerb2904782010-09-29 10:34:19 +0000616 fmt = format;
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000617#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 /* Convert the unicode string to an ascii one */
Victor Stinner1b579672011-12-17 05:47:23 +0100619 format = PyUnicode_EncodeLocale(format_arg, "surrogateescape");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 if (format == NULL)
621 return NULL;
622 fmt = PyBytes_AS_STRING(format);
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000623#endif
Amaury Forgeot d'Arcb5be6d42009-03-02 23:52:57 +0000624
Stefan Krah4aea7d32012-02-27 16:30:26 +0100625#if defined(MS_WINDOWS) && !defined(HAVE_WCSFTIME)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 /* check that the format string contains only valid directives */
Steve Dowere5b58952015-09-06 19:20:51 -0700627 for (outbuf = strchr(fmt, '%');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 outbuf != NULL;
Victor Stinner5a3ff792011-10-16 19:08:23 +0200629 outbuf = strchr(outbuf+2, '%'))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 {
Steve Dowere5b58952015-09-06 19:20:51 -0700631 if (outbuf[1] == '#')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 ++outbuf; /* not documented by python, */
Steve Dowere5b58952015-09-06 19:20:51 -0700633 if (outbuf[1] == '\0')
634 break;
635 if ((outbuf[1] == 'y') && buf.tm_year < 0) {
Tim Golden6e51b8f2013-11-12 12:36:54 +0000636 PyErr_SetString(PyExc_ValueError,
637 "format %y requires year >= 1900 on Windows");
638 Py_DECREF(format);
639 return NULL;
640 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 }
Victor Stinner93965f72013-11-23 14:59:33 +0100642#elif (defined(_AIX) || defined(sun)) && defined(HAVE_WCSFTIME)
Steve Dowere5b58952015-09-06 19:20:51 -0700643 for (outbuf = wcschr(fmt, '%');
Victor Stinner55329f82013-11-17 23:39:21 +0100644 outbuf != NULL;
645 outbuf = wcschr(outbuf+2, '%'))
646 {
Steve Dowere5b58952015-09-06 19:20:51 -0700647 if (outbuf[1] == L'\0')
648 break;
Victor Stinner55329f82013-11-17 23:39:21 +0100649 /* Issue #19634: On AIX, wcsftime("y", (1899, 1, 1, 0, 0, 0, 0, 0, 0))
650 returns "0/" instead of "99" */
651 if (outbuf[1] == L'y' && buf.tm_year < 0) {
652 PyErr_SetString(PyExc_ValueError,
653 "format %y requires year >= 1900 on AIX");
Victor Stinner55329f82013-11-17 23:39:21 +0100654 return NULL;
655 }
656 }
Amaury Forgeot d'Arcb5be6d42009-03-02 23:52:57 +0000657#endif
658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 fmtlen = time_strlen(fmt);
Guido van Rossumc222ec21999-02-23 00:00:10 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 /* I hate these functions that presume you know how big the output
662 * will be ahead of time...
663 */
664 for (i = 1024; ; i += i) {
665 outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char));
666 if (outbuf == NULL) {
Victor Stinnerb2904782010-09-29 10:34:19 +0000667 PyErr_NoMemory();
668 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 }
Steve Dower57ab1cd2015-09-22 14:51:42 -0700670#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
671 errno = 0;
672#endif
Steve Dowerd9ef74e2015-05-22 15:08:34 -0700673 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 buflen = format_time(outbuf, i, fmt, &buf);
Steve Dowerd9ef74e2015-05-22 15:08:34 -0700675 _Py_END_SUPPRESS_IPH
Victor Stinner136ea492011-12-17 22:37:18 +0100676#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
Steve Dower97cded92015-09-08 19:12:51 -0700677 /* VisualStudio .NET 2005 does this properly */
678 if (buflen == 0 && errno == EINVAL) {
679 PyErr_SetString(PyExc_ValueError, "Invalid format string");
680 PyMem_Free(outbuf);
681 break;
682 }
Victor Stinner136ea492011-12-17 22:37:18 +0100683#endif
Steve Dower97cded92015-09-08 19:12:51 -0700684 if (buflen > 0 || i >= 256 * fmtlen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 /* If the buffer is 256 times as long as the format,
686 it's probably not failing for lack of room!
687 More likely, the format yields an empty result,
688 e.g. an empty format, or %Z when the timezone
689 is unknown. */
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000690#ifdef HAVE_WCSFTIME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 ret = PyUnicode_FromWideChar(outbuf, buflen);
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000692#else
Victor Stinner1b579672011-12-17 05:47:23 +0100693 ret = PyUnicode_DecodeLocaleAndSize(outbuf, buflen,
694 "surrogateescape");
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000695#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 PyMem_Free(outbuf);
Victor Stinnerb2904782010-09-29 10:34:19 +0000697 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 }
699 PyMem_Free(outbuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 }
Victor Stinnerb2904782010-09-29 10:34:19 +0000701#ifdef HAVE_WCSFTIME
702 PyMem_Free(format);
703#else
704 Py_DECREF(format);
705#endif
706 return ret;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000707}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000708
Martin v. Löwis1b01ccd2009-05-30 06:13:40 +0000709#undef time_char
710#undef format_time
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000711PyDoc_STRVAR(strftime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000712"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000713\n\
714Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000715See the library reference manual for formatting codes. When the time tuple\n\
Alexander Belopolskycf774542012-10-02 18:39:16 -0400716is not present, current time as returned by localtime() is used.\n\
717\n" STRFTIME_FORMAT_CODES);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000718#endif /* HAVE_STRFTIME */
719
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000720static PyObject *
721time_strptime(PyObject *self, PyObject *args)
722{
Victor Stinnerdbe28d22016-12-09 00:38:53 +0100723 PyObject *module, *func, *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200724 _Py_IDENTIFIER(_strptime_time);
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000725
Victor Stinnerdbe28d22016-12-09 00:38:53 +0100726 module = PyImport_ImportModuleNoBlock("_strptime");
727 if (!module)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 return NULL;
Victor Stinnerdbe28d22016-12-09 00:38:53 +0100729
730 func = _PyObject_GetAttrId(module, &PyId__strptime_time);
731 Py_DECREF(module);
732 if (!func) {
733 return NULL;
734 }
735
736 result = PyObject_Call(func, args, NULL);
737 Py_DECREF(func);
738 return result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000739}
740
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000742PyDoc_STRVAR(strptime_doc,
Brett Cannon20def8b2003-07-01 05:16:08 +0000743"strptime(string, format) -> struct_time\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000744\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000745Parse a string to a time tuple according to a format specification.\n\
Alexander Belopolskycf774542012-10-02 18:39:16 -0400746See the library reference manual for formatting codes (same as\n\
747strftime()).\n\
748\n" STRFTIME_FORMAT_CODES);
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000749
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000750static PyObject *
751_asctime(struct tm *timeptr)
752{
753 /* Inspired by Open Group reference implementation available at
754 * http://pubs.opengroup.org/onlinepubs/009695399/functions/asctime.html */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200755 static const char wday_name[7][4] = {
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000756 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
757 };
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200758 static const char mon_name[12][4] = {
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000759 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
760 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
761 };
Victor Stinner499dfcf2011-03-21 13:26:24 +0100762 return PyUnicode_FromFormat(
763 "%s %s%3d %.2d:%.2d:%.2d %d",
764 wday_name[timeptr->tm_wday],
765 mon_name[timeptr->tm_mon],
766 timeptr->tm_mday, timeptr->tm_hour,
767 timeptr->tm_min, timeptr->tm_sec,
768 1900 + timeptr->tm_year);
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000769}
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000770
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000771static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000772time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 PyObject *tup = NULL;
775 struct tm buf;
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup))
778 return NULL;
779 if (tup == NULL) {
780 time_t tt = time(NULL);
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400781 if (_PyTime_localtime(tt, &buf) != 0)
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100782 return NULL;
Oren Milman1d1d3e92017-08-20 18:35:36 +0300783 }
784 else if (!gettmarg(tup, &buf,
785 "iiiiiiiii;asctime(): illegal time tuple argument") ||
786 !checktm(&buf))
787 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 return NULL;
Oren Milman1d1d3e92017-08-20 18:35:36 +0300789 }
Alexander Belopolskyb9588b52011-01-04 16:34:30 +0000790 return _asctime(&buf);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000791}
792
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000793PyDoc_STRVAR(asctime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000794"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000795\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000796Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
797When the time tuple is not present, current time as returned by localtime()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000798is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000799
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000800static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000801time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 time_t tt;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100804 struct tm buf;
805 if (!parse_time_t_args(args, "|O:ctime", &tt))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 return NULL;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -0400807 if (_PyTime_localtime(tt, &buf) != 0)
Alexander Belopolsky5da468f2011-01-04 17:15:52 +0000808 return NULL;
Victor Stinnerc1b5d342012-01-27 00:08:48 +0100809 return _asctime(&buf);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000810}
811
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000812PyDoc_STRVAR(ctime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000813"ctime(seconds) -> string\n\
814\n\
815Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000816This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000817not present, current time as returned by localtime() is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000818
Guido van Rossum60cd8131998-03-06 17:16:21 +0000819#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000820static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +0100821time_mktime(PyObject *self, PyObject *tup)
Guido van Rossum234f9421993-06-17 12:35:49 +0000822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 struct tm buf;
824 time_t tt;
Oren Milman1d1d3e92017-08-20 18:35:36 +0300825 if (!gettmarg(tup, &buf,
826 "iiiiiiiii;mktime(): illegal time tuple argument"))
827 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 return NULL;
Oren Milman1d1d3e92017-08-20 18:35:36 +0300829 }
Victor Stinner1ac42612014-02-21 09:27:17 +0100830#ifdef _AIX
831 /* year < 1902 or year > 2037 */
832 if (buf.tm_year < 2 || buf.tm_year > 137) {
833 /* Issue #19748: On AIX, mktime() doesn't report overflow error for
834 * timestamp < -2^31 or timestamp > 2**31-1. */
835 PyErr_SetString(PyExc_OverflowError,
836 "mktime argument out of range");
837 return NULL;
838 }
839#else
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000840 buf.tm_wday = -1; /* sentinel; original value ignored */
Victor Stinner1ac42612014-02-21 09:27:17 +0100841#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 tt = mktime(&buf);
Alexander Belopolskyb7d40d12011-01-11 01:21:25 +0000843 /* Return value of -1 does not necessarily mean an error, but tm_wday
Ezio Melotti13925002011-03-16 11:05:33 +0200844 * cannot remain set to -1 if mktime succeeded. */
Victor Stinner93037492013-06-25 22:54:35 +0200845 if (tt == (time_t)(-1)
846#ifndef _AIX
847 /* Return value of -1 does not necessarily mean an error, but
848 * tm_wday cannot remain set to -1 if mktime succeeded. */
849 && buf.tm_wday == -1
850#else
851 /* on AIX, tm_wday is always sets, even on error */
852#endif
853 )
854 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 PyErr_SetString(PyExc_OverflowError,
856 "mktime argument out of range");
857 return NULL;
858 }
Victor Stinner4195b5c2012-02-08 23:03:19 +0100859 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000860}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000861
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000862PyDoc_STRVAR(mktime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000863"mktime(tuple) -> floating point number\n\
864\n\
Alexander Belopolskyc142bba2012-06-13 22:15:26 -0400865Convert a time tuple in local time to seconds since the Epoch.\n\
866Note that mktime(gmtime(0)) will not generally return zero for most\n\
867time zones; instead the returned value will either be equal to that\n\
868of the timezone or altzone attributes on the time module.");
Guido van Rossum60cd8131998-03-06 17:16:21 +0000869#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000870
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000871#ifdef HAVE_WORKING_TZSET
Martin v. Löwis1a214512008-06-11 05:26:20 +0000872static void PyInit_timezone(PyObject *module);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000873
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000874static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000875time_tzset(PyObject *self, PyObject *unused)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 PyObject* m;
Fred Drake9bb74322002-04-01 14:49:59 +0000878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 m = PyImport_ImportModuleNoBlock("time");
880 if (m == NULL) {
881 return NULL;
882 }
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 tzset();
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 /* Reset timezone, altzone, daylight and tzname */
887 PyInit_timezone(m);
888 Py_DECREF(m);
Victor Stinner2ff51b82013-07-17 21:42:45 +0200889 if (PyErr_Occurred())
890 return NULL;
Tim Peters1b6f7a92004-06-20 02:50:16 +0000891
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200892 Py_RETURN_NONE;
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000893}
894
895PyDoc_STRVAR(tzset_doc,
R. David Murray4d55bf92010-12-14 00:55:46 +0000896"tzset()\n\
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000897\n\
898Initialize, or reinitialize, the local timezone to the value stored in\n\
899os.environ['TZ']. The TZ environment variable should be specified in\n\
Neal Norwitzdc8e1942004-07-20 22:34:37 +0000900standard Unix timezone format as documented in the tzset man page\n\
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000901(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
902fall back to UTC. If the TZ environment variable is not set, the local\n\
903timezone is set to the systems best guess of wallclock time.\n\
904Changing the TZ environment variable without calling tzset *may* change\n\
905the local timezone used by methods such as localtime, but this behaviour\n\
906should not be relied on.");
907#endif /* HAVE_WORKING_TZSET */
908
Victor Stinnerae586492014-09-02 23:18:25 +0200909static PyObject *
Victor Stinnerec895392012-04-29 02:41:27 +0200910pymonotonic(_Py_clock_info_t *info)
Victor Stinnerb94b2662012-01-18 01:50:21 +0100911{
Victor Stinner4bfb4602015-03-27 22:27:24 +0100912 _PyTime_t t;
913 double d;
914 if (_PyTime_GetMonotonicClockWithInfo(&t, info) < 0) {
Victor Stinnerae586492014-09-02 23:18:25 +0200915 assert(info != NULL);
Victor Stinner071eca32012-03-15 01:17:09 +0100916 return NULL;
917 }
Victor Stinner4bfb4602015-03-27 22:27:24 +0100918 d = _PyTime_AsSecondsDouble(t);
919 return PyFloat_FromDouble(d);
Victor Stinner8b302012012-02-07 23:29:46 +0100920}
921
Victor Stinner071eca32012-03-15 01:17:09 +0100922static PyObject *
Victor Stinnerec895392012-04-29 02:41:27 +0200923time_monotonic(PyObject *self, PyObject *unused)
Victor Stinner071eca32012-03-15 01:17:09 +0100924{
Victor Stinnerec895392012-04-29 02:41:27 +0200925 return pymonotonic(NULL);
Victor Stinner071eca32012-03-15 01:17:09 +0100926}
927
Victor Stinnerec895392012-04-29 02:41:27 +0200928PyDoc_STRVAR(monotonic_doc,
929"monotonic() -> float\n\
Victor Stinner8b302012012-02-07 23:29:46 +0100930\n\
Victor Stinnerec895392012-04-29 02:41:27 +0200931Monotonic clock, cannot go backward.");
Victor Stinnerec919cc2012-03-15 00:58:32 +0100932
Victor Stinnerec895392012-04-29 02:41:27 +0200933static PyObject*
934perf_counter(_Py_clock_info_t *info)
935{
Victor Stinner54884492014-08-29 16:51:33 +0200936#ifdef WIN32_PERF_COUNTER
937 return win_perf_counter(info);
938#else
Victor Stinnerae586492014-09-02 23:18:25 +0200939 return pymonotonic(info);
Victor Stinner54884492014-08-29 16:51:33 +0200940#endif
Victor Stinnerec895392012-04-29 02:41:27 +0200941}
942
943static PyObject *
944time_perf_counter(PyObject *self, PyObject *unused)
945{
946 return perf_counter(NULL);
947}
948
949PyDoc_STRVAR(perf_counter_doc,
950"perf_counter() -> float\n\
951\n\
952Performance counter for benchmarking.");
953
954static PyObject*
955py_process_time(_Py_clock_info_t *info)
956{
957#if defined(MS_WINDOWS)
958 HANDLE process;
959 FILETIME creation_time, exit_time, kernel_time, user_time;
960 ULARGE_INTEGER large;
961 double total;
962 BOOL ok;
963
964 process = GetCurrentProcess();
965 ok = GetProcessTimes(process, &creation_time, &exit_time, &kernel_time, &user_time);
966 if (!ok)
967 return PyErr_SetFromWindowsErr(0);
968
969 large.u.LowPart = kernel_time.dwLowDateTime;
970 large.u.HighPart = kernel_time.dwHighDateTime;
971 total = (double)large.QuadPart;
972 large.u.LowPart = user_time.dwLowDateTime;
973 large.u.HighPart = user_time.dwHighDateTime;
974 total += (double)large.QuadPart;
975 if (info) {
976 info->implementation = "GetProcessTimes()";
977 info->resolution = 1e-7;
Benjamin Peterson49a69e42012-05-01 09:38:34 -0400978 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +0200979 info->adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +0200980 }
981 return PyFloat_FromDouble(total * 1e-7);
982#else
983
984#if defined(HAVE_SYS_RESOURCE_H)
985 struct rusage ru;
986#endif
987#ifdef HAVE_TIMES
988 struct tms t;
989 static long ticks_per_second = -1;
990#endif
991
992#if defined(HAVE_CLOCK_GETTIME) \
993 && (defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_PROF))
994 struct timespec tp;
995#ifdef CLOCK_PROF
996 const clockid_t clk_id = CLOCK_PROF;
997 const char *function = "clock_gettime(CLOCK_PROF)";
998#else
999 const clockid_t clk_id = CLOCK_PROCESS_CPUTIME_ID;
1000 const char *function = "clock_gettime(CLOCK_PROCESS_CPUTIME_ID)";
1001#endif
1002
1003 if (clock_gettime(clk_id, &tp) == 0) {
1004 if (info) {
1005 struct timespec res;
1006 info->implementation = function;
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001007 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001008 info->adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001009 if (clock_getres(clk_id, &res) == 0)
1010 info->resolution = res.tv_sec + res.tv_nsec * 1e-9;
1011 else
1012 info->resolution = 1e-9;
1013 }
1014 return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
1015 }
1016#endif
1017
1018#if defined(HAVE_SYS_RESOURCE_H)
1019 if (getrusage(RUSAGE_SELF, &ru) == 0) {
1020 double total;
1021 total = ru.ru_utime.tv_sec + ru.ru_utime.tv_usec * 1e-6;
1022 total += ru.ru_stime.tv_sec + ru.ru_stime.tv_usec * 1e-6;
1023 if (info) {
1024 info->implementation = "getrusage(RUSAGE_SELF)";
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001025 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001026 info->adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001027 info->resolution = 1e-6;
1028 }
1029 return PyFloat_FromDouble(total);
1030 }
1031#endif
1032
1033#ifdef HAVE_TIMES
1034 if (times(&t) != (clock_t)-1) {
1035 double total;
1036
1037 if (ticks_per_second == -1) {
1038#if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
1039 ticks_per_second = sysconf(_SC_CLK_TCK);
1040 if (ticks_per_second < 1)
1041 ticks_per_second = -1;
1042#elif defined(HZ)
1043 ticks_per_second = HZ;
1044#else
1045 ticks_per_second = 60; /* magic fallback value; may be bogus */
1046#endif
1047 }
1048
1049 if (ticks_per_second != -1) {
1050 total = (double)t.tms_utime / ticks_per_second;
1051 total += (double)t.tms_stime / ticks_per_second;
1052 if (info) {
1053 info->implementation = "times()";
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001054 info->monotonic = 1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001055 info->adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001056 info->resolution = 1.0 / ticks_per_second;
1057 }
1058 return PyFloat_FromDouble(total);
1059 }
1060 }
1061#endif
1062
Victor Stinner53e22bf2016-07-08 17:55:01 +02001063 /* Currently, Python 3 requires clock() to build: see issue #22624 */
Victor Stinnerec895392012-04-29 02:41:27 +02001064 return floatclock(info);
1065#endif
1066}
1067
1068static PyObject *
1069time_process_time(PyObject *self, PyObject *unused)
1070{
1071 return py_process_time(NULL);
1072}
1073
1074PyDoc_STRVAR(process_time_doc,
1075"process_time() -> float\n\
1076\n\
1077Process time for profiling: sum of the kernel and user-space CPU time.");
1078
1079
Victor Stinnerec895392012-04-29 02:41:27 +02001080static PyObject *
1081time_get_clock_info(PyObject *self, PyObject *args)
1082{
1083 char *name;
Victor Stinnerec895392012-04-29 02:41:27 +02001084 _Py_clock_info_t info;
Victor Stinnerbda4b882012-06-12 22:11:44 +02001085 PyObject *obj = NULL, *dict, *ns;
Victor Stinnerec895392012-04-29 02:41:27 +02001086
1087 if (!PyArg_ParseTuple(args, "s:get_clock_info", &name))
1088 return NULL;
1089
1090#ifdef Py_DEBUG
1091 info.implementation = NULL;
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001092 info.monotonic = -1;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001093 info.adjustable = -1;
Victor Stinnerec895392012-04-29 02:41:27 +02001094 info.resolution = -1.0;
1095#else
1096 info.implementation = "";
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001097 info.monotonic = 0;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001098 info.adjustable = 0;
Victor Stinnerec895392012-04-29 02:41:27 +02001099 info.resolution = 1.0;
1100#endif
1101
1102 if (strcmp(name, "time") == 0)
1103 obj = floattime(&info);
1104#ifdef PYCLOCK
1105 else if (strcmp(name, "clock") == 0)
1106 obj = pyclock(&info);
1107#endif
Victor Stinnerec895392012-04-29 02:41:27 +02001108 else if (strcmp(name, "monotonic") == 0)
1109 obj = pymonotonic(&info);
Victor Stinnerec895392012-04-29 02:41:27 +02001110 else if (strcmp(name, "perf_counter") == 0)
1111 obj = perf_counter(&info);
1112 else if (strcmp(name, "process_time") == 0)
1113 obj = py_process_time(&info);
1114 else {
1115 PyErr_SetString(PyExc_ValueError, "unknown clock");
1116 return NULL;
1117 }
1118 if (obj == NULL)
1119 return NULL;
1120 Py_DECREF(obj);
1121
Victor Stinnerbda4b882012-06-12 22:11:44 +02001122 dict = PyDict_New();
1123 if (dict == NULL)
Victor Stinnerec895392012-04-29 02:41:27 +02001124 return NULL;
1125
1126 assert(info.implementation != NULL);
1127 obj = PyUnicode_FromString(info.implementation);
1128 if (obj == NULL)
1129 goto error;
Victor Stinnerbda4b882012-06-12 22:11:44 +02001130 if (PyDict_SetItemString(dict, "implementation", obj) == -1)
1131 goto error;
1132 Py_CLEAR(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001133
Benjamin Peterson49a69e42012-05-01 09:38:34 -04001134 assert(info.monotonic != -1);
1135 obj = PyBool_FromLong(info.monotonic);
Victor Stinnerec895392012-04-29 02:41:27 +02001136 if (obj == NULL)
1137 goto error;
Victor Stinnerbda4b882012-06-12 22:11:44 +02001138 if (PyDict_SetItemString(dict, "monotonic", obj) == -1)
1139 goto error;
1140 Py_CLEAR(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001141
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001142 assert(info.adjustable != -1);
1143 obj = PyBool_FromLong(info.adjustable);
Victor Stinnerec895392012-04-29 02:41:27 +02001144 if (obj == NULL)
1145 goto error;
Victor Stinner2b89fdf2012-06-12 22:46:37 +02001146 if (PyDict_SetItemString(dict, "adjustable", obj) == -1)
Victor Stinnerbda4b882012-06-12 22:11:44 +02001147 goto error;
1148 Py_CLEAR(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001149
1150 assert(info.resolution > 0.0);
1151 assert(info.resolution <= 1.0);
1152 obj = PyFloat_FromDouble(info.resolution);
1153 if (obj == NULL)
1154 goto error;
Victor Stinnerbda4b882012-06-12 22:11:44 +02001155 if (PyDict_SetItemString(dict, "resolution", obj) == -1)
1156 goto error;
1157 Py_CLEAR(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001158
Victor Stinnerbda4b882012-06-12 22:11:44 +02001159 ns = _PyNamespace_New(dict);
1160 Py_DECREF(dict);
1161 return ns;
Victor Stinnerec895392012-04-29 02:41:27 +02001162
1163error:
Victor Stinnerbda4b882012-06-12 22:11:44 +02001164 Py_DECREF(dict);
1165 Py_XDECREF(obj);
Victor Stinnerec895392012-04-29 02:41:27 +02001166 return NULL;
1167}
1168
1169PyDoc_STRVAR(get_clock_info_doc,
1170"get_clock_info(name: str) -> dict\n\
1171\n\
1172Get information of the specified clock.");
Victor Stinner8b302012012-02-07 23:29:46 +01001173
Victor Stinner8f5cdfa2017-04-20 13:41:09 +02001174#if !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__)
Martin v. Löwisd218dc12008-04-07 03:17:54 +00001175static void
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001176get_zone(char *zone, int n, struct tm *p)
1177{
1178#ifdef HAVE_STRUCT_TM_TM_ZONE
1179 strncpy(zone, p->tm_zone ? p->tm_zone : " ", n);
1180#else
1181 tzset();
1182 strftime(zone, n, "%Z", p);
1183#endif
1184}
1185
1186static int
1187get_gmtoff(time_t t, struct tm *p)
1188{
1189#ifdef HAVE_STRUCT_TM_TM_ZONE
1190 return p->tm_gmtoff;
1191#else
1192 return timegm(p) - t;
1193#endif
1194}
Victor Stinner8f5cdfa2017-04-20 13:41:09 +02001195#endif /* !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__) */
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001196
1197static void
Martin v. Löwis1a214512008-06-11 05:26:20 +00001198PyInit_timezone(PyObject *m) {
1199 /* This code moved from PyInit_time wholesale to allow calling it from
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 time_tzset. In the future, some parts of it can be moved back
1201 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
1202 are), and the extraneous calls to tzset(3) should be removed.
1203 I haven't done this yet, as I don't want to change this code as
1204 little as possible when introducing the time.tzset and time.tzsetwall
1205 methods. This should simply be a method of doing the following once,
1206 at the top of this function and removing the call to tzset() from
1207 time_tzset():
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 #ifdef HAVE_TZSET
1210 tzset()
1211 #endif
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 And I'm lazy and hate C so nyer.
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001214 */
Guido van Rossum10b164a2001-09-25 13:59:01 +00001215#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 PyObject *otz0, *otz1;
1217 tzset();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001219#ifdef HAVE_ALTZONE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001221#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001223#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 PyModule_AddIntConstant(m, "daylight", daylight);
Victor Stinner1b579672011-12-17 05:47:23 +01001225 otz0 = PyUnicode_DecodeLocale(tzname[0], "surrogateescape");
1226 otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
Guido van Rossum10b164a2001-09-25 13:59:01 +00001228#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 {
Guido van Rossum234f9421993-06-17 12:35:49 +00001230#define YEAR ((time_t)((365 * 24 + 6) * 3600))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 time_t t;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001232 struct tm p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 long janzone, julyzone;
1234 char janname[10], julyname[10];
1235 t = (time((time_t *)0) / YEAR) * YEAR;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001236 _PyTime_localtime(t, &p);
1237 get_zone(janname, 9, &p);
1238 janzone = -get_gmtoff(t, &p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 janname[9] = '\0';
1240 t += YEAR/2;
Alexander Belopolsky3e7a3cb2016-09-28 17:31:35 -04001241 _PyTime_localtime(t, &p);
1242 get_zone(julyname, 9, &p);
1243 julyzone = -get_gmtoff(t, &p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +00001245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 if( janzone < julyzone ) {
1247 /* DST is reversed in the southern hemisphere */
1248 PyModule_AddIntConstant(m, "timezone", julyzone);
1249 PyModule_AddIntConstant(m, "altzone", janzone);
1250 PyModule_AddIntConstant(m, "daylight",
1251 janzone != julyzone);
1252 PyModule_AddObject(m, "tzname",
1253 Py_BuildValue("(zz)",
1254 julyname, janname));
1255 } else {
1256 PyModule_AddIntConstant(m, "timezone", janzone);
1257 PyModule_AddIntConstant(m, "altzone", julyzone);
1258 PyModule_AddIntConstant(m, "daylight",
1259 janzone != julyzone);
1260 PyModule_AddObject(m, "tzname",
1261 Py_BuildValue("(zz)",
1262 janname, julyname));
1263 }
1264 }
Tim Peters26ae7cd2001-03-20 03:26:49 +00001265#ifdef __CYGWIN__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 tzset();
1267 PyModule_AddIntConstant(m, "timezone", _timezone);
1268 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
1269 PyModule_AddIntConstant(m, "daylight", _daylight);
1270 PyModule_AddObject(m, "tzname",
1271 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +00001272#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +00001273#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001274}
1275
1276
1277static PyMethodDef time_methods[] = {
Victor Stinner4195b5c2012-02-08 23:03:19 +01001278 {"time", time_time, METH_NOARGS, time_doc},
Victor Stinnerec895392012-04-29 02:41:27 +02001279#ifdef PYCLOCK
Victor Stinner4195b5c2012-02-08 23:03:19 +01001280 {"clock", time_clock, METH_NOARGS, clock_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001281#endif
Victor Stinnere0be4232011-10-25 13:06:09 +02001282#ifdef HAVE_CLOCK_GETTIME
Victor Stinner4195b5c2012-02-08 23:03:19 +01001283 {"clock_gettime", time_clock_gettime, METH_VARARGS, clock_gettime_doc},
Benjamin Peterson37098cd2016-09-13 22:55:09 -07001284#endif
1285#ifdef HAVE_CLOCK_SETTIME
Victor Stinner30d79472012-04-03 00:45:07 +02001286 {"clock_settime", time_clock_settime, METH_VARARGS, clock_settime_doc},
Benjamin Peterson37098cd2016-09-13 22:55:09 -07001287#endif
1288#ifdef HAVE_CLOCK_GETRES
Victor Stinner4195b5c2012-02-08 23:03:19 +01001289 {"clock_getres", time_clock_getres, METH_VARARGS, clock_getres_doc},
Victor Stinnere0be4232011-10-25 13:06:09 +02001290#endif
Victor Stinnercb29f012015-03-27 13:31:18 +01001291 {"sleep", time_sleep, METH_O, sleep_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
1293 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
1294 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
1295 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001296#ifdef HAVE_MKTIME
Victor Stinner4195b5c2012-02-08 23:03:19 +01001297 {"mktime", time_mktime, METH_O, mktime_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001298#endif
1299#ifdef HAVE_STRFTIME
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001301#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001303#ifdef HAVE_WORKING_TZSET
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 {"tzset", time_tzset, METH_NOARGS, tzset_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001305#endif
Victor Stinnerec895392012-04-29 02:41:27 +02001306 {"monotonic", time_monotonic, METH_NOARGS, monotonic_doc},
Victor Stinnerec895392012-04-29 02:41:27 +02001307 {"process_time", time_process_time, METH_NOARGS, process_time_doc},
1308 {"perf_counter", time_perf_counter, METH_NOARGS, perf_counter_doc},
1309 {"get_clock_info", time_get_clock_info, METH_VARARGS, get_clock_info_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 {NULL, NULL} /* sentinel */
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001311};
1312
1313
1314PyDoc_STRVAR(module_doc,
1315"This module provides various functions to manipulate time values.\n\
1316\n\
1317There are two standard representations of time. One is the number\n\
1318of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
1319or a floating point number (to represent fractions of seconds).\n\
1320The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
1321The actual value can be retrieved by calling gmtime(0).\n\
1322\n\
1323The other representation is a tuple of 9 integers giving local time.\n\
1324The tuple items are:\n\
Alexander Belopolsky03163ac2011-05-02 12:20:52 -04001325 year (including century, e.g. 1998)\n\
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001326 month (1-12)\n\
1327 day (1-31)\n\
1328 hours (0-23)\n\
1329 minutes (0-59)\n\
1330 seconds (0-59)\n\
1331 weekday (0-6, Monday is 0)\n\
1332 Julian day (day in the year, 1-366)\n\
1333 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
1334If the DST flag is 0, the time is given in the regular time zone;\n\
1335if it is 1, the time is given in the DST time zone;\n\
1336if it is -1, mktime() should guess based on the date and time.\n\
1337\n\
1338Variables:\n\
1339\n\
1340timezone -- difference in seconds between UTC and local standard time\n\
1341altzone -- difference in seconds between UTC and local DST time\n\
1342daylight -- whether local time should reflect DST\n\
1343tzname -- tuple of (standard time zone name, DST time zone name)\n\
1344\n\
1345Functions:\n\
1346\n\
1347time() -- return current time in seconds since the Epoch as a float\n\
1348clock() -- return CPU time since process start as a float\n\
1349sleep() -- delay for a number of seconds given as a float\n\
1350gmtime() -- convert seconds since Epoch to UTC tuple\n\
1351localtime() -- convert seconds since Epoch to local time tuple\n\
1352asctime() -- convert time tuple to string\n\
1353ctime() -- convert time in seconds to string\n\
1354mktime() -- convert local time tuple to seconds since Epoch\n\
1355strftime() -- convert time tuple to string according to format specification\n\
1356strptime() -- parse string to time tuple according to format specification\n\
1357tzset() -- change the local timezone");
1358
1359
Martin v. Löwis1a214512008-06-11 05:26:20 +00001360
1361static struct PyModuleDef timemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 PyModuleDef_HEAD_INIT,
1363 "time",
1364 module_doc,
1365 -1,
1366 time_methods,
1367 NULL,
1368 NULL,
1369 NULL,
1370 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001371};
1372
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001373PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001374PyInit_time(void)
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 PyObject *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 m = PyModule_Create(&timemodule);
1378 if (m == NULL)
1379 return NULL;
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 /* Set, or reset, module variables like time.timezone */
1382 PyInit_timezone(m);
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001383
Benjamin Peterson37098cd2016-09-13 22:55:09 -07001384#ifdef CLOCK_REALTIME
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001385 PyModule_AddIntMacro(m, CLOCK_REALTIME);
Benjamin Peterson37098cd2016-09-13 22:55:09 -07001386#endif
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001387#ifdef CLOCK_MONOTONIC
1388 PyModule_AddIntMacro(m, CLOCK_MONOTONIC);
1389#endif
1390#ifdef CLOCK_MONOTONIC_RAW
1391 PyModule_AddIntMacro(m, CLOCK_MONOTONIC_RAW);
1392#endif
1393#ifdef CLOCK_HIGHRES
1394 PyModule_AddIntMacro(m, CLOCK_HIGHRES);
1395#endif
1396#ifdef CLOCK_PROCESS_CPUTIME_ID
1397 PyModule_AddIntMacro(m, CLOCK_PROCESS_CPUTIME_ID);
1398#endif
1399#ifdef CLOCK_THREAD_CPUTIME_ID
1400 PyModule_AddIntMacro(m, CLOCK_THREAD_CPUTIME_ID);
1401#endif
Alexander Belopolsky18f3a9b2016-09-11 22:55:16 -04001402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 if (!initialized) {
Victor Stinner1c8f0592013-07-22 22:24:54 +02001404 if (PyStructSequence_InitType2(&StructTimeType,
1405 &struct_time_type_desc) < 0)
1406 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 }
1408 Py_INCREF(&StructTimeType);
Alexander Belopolskyc142bba2012-06-13 22:15:26 -04001409 PyModule_AddIntConstant(m, "_STRUCT_TM_ITEMS", 11);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
1411 initialized = 1;
1412 return m;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001413}
1414
Victor Stinner071eca32012-03-15 01:17:09 +01001415static PyObject*
Victor Stinnerec895392012-04-29 02:41:27 +02001416floattime(_Py_clock_info_t *info)
Victor Stinner4195b5c2012-02-08 23:03:19 +01001417{
Victor Stinnera47b8812015-03-27 18:16:17 +01001418 _PyTime_t t;
1419 double d;
1420 if (_PyTime_GetSystemClockWithInfo(&t, info) < 0) {
Victor Stinner00111242014-08-29 16:31:59 +02001421 assert(info != NULL);
1422 return NULL;
1423 }
Victor Stinnera47b8812015-03-27 18:16:17 +01001424 d = _PyTime_AsSecondsDouble(t);
1425 return PyFloat_FromDouble(d);
Victor Stinner4195b5c2012-02-08 23:03:19 +01001426}
1427
1428
Victor Stinnercb29f012015-03-27 13:31:18 +01001429/* Implement pysleep() for various platforms.
Guido van Rossumb6775db1994-08-01 11:34:53 +00001430 When interrupted (or when another error occurs), return -1 and
1431 set an exception; else return 0. */
1432
1433static int
Victor Stinnercb29f012015-03-27 13:31:18 +01001434pysleep(_PyTime_t secs)
Guido van Rossum426035c1991-02-19 12:27:35 +00001435{
Victor Stinnercb29f012015-03-27 13:31:18 +01001436 _PyTime_t deadline, monotonic;
Victor Stinner79d68f92015-03-19 21:54:09 +01001437#ifndef MS_WINDOWS
1438 struct timeval timeout;
Victor Stinner79d68f92015-03-19 21:54:09 +01001439 int err = 0;
1440#else
Victor Stinnercb29f012015-03-27 13:31:18 +01001441 _PyTime_t millisecs;
Victor Stinner79d68f92015-03-19 21:54:09 +01001442 unsigned long ul_millis;
1443 DWORD rc;
1444 HANDLE hInterruptEvent;
Victor Stinner0c2fd892015-03-17 10:49:17 +01001445#endif
Victor Stinner79d68f92015-03-19 21:54:09 +01001446
Victor Stinnercb29f012015-03-27 13:31:18 +01001447 deadline = _PyTime_GetMonotonicClock() + secs;
Victor Stinner79d68f92015-03-19 21:54:09 +01001448
1449 do {
1450#ifndef MS_WINDOWS
Victor Stinner869e1772015-03-30 03:49:14 +02001451 if (_PyTime_AsTimeval(secs, &timeout, _PyTime_ROUND_CEILING) < 0)
Victor Stinnercb29f012015-03-27 13:31:18 +01001452 return -1;
Victor Stinner79d68f92015-03-19 21:54:09 +01001453
1454 Py_BEGIN_ALLOW_THREADS
1455 err = select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout);
1456 Py_END_ALLOW_THREADS
1457
1458 if (err == 0)
1459 break;
1460
1461 if (errno != EINTR) {
Victor Stinner0c2fd892015-03-17 10:49:17 +01001462 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 return -1;
1464 }
Victor Stinner79d68f92015-03-19 21:54:09 +01001465#else
Victor Stinner869e1772015-03-30 03:49:14 +02001466 millisecs = _PyTime_AsMilliseconds(secs, _PyTime_ROUND_CEILING);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 if (millisecs > (double)ULONG_MAX) {
1468 PyErr_SetString(PyExc_OverflowError,
1469 "sleep length is too large");
1470 return -1;
1471 }
Victor Stinner79d68f92015-03-19 21:54:09 +01001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 /* Allow sleep(0) to maintain win32 semantics, and as decreed
1474 * by Guido, only the main thread can be interrupted.
1475 */
1476 ul_millis = (unsigned long)millisecs;
Victor Stinner79d68f92015-03-19 21:54:09 +01001477 if (ul_millis == 0 || !_PyOS_IsMainThread()) {
1478 Py_BEGIN_ALLOW_THREADS
Victor Stinner0eac1302015-03-20 03:06:12 +01001479 Sleep(ul_millis);
Victor Stinner79d68f92015-03-19 21:54:09 +01001480 Py_END_ALLOW_THREADS
1481 break;
Victor Stinner945c82e2015-03-12 16:19:01 +01001482 }
Victor Stinner79d68f92015-03-19 21:54:09 +01001483
1484 hInterruptEvent = _PyOS_SigintEvent();
1485 ResetEvent(hInterruptEvent);
1486
1487 Py_BEGIN_ALLOW_THREADS
1488 rc = WaitForSingleObjectEx(hInterruptEvent, ul_millis, FALSE);
Victor Stinner0c2fd892015-03-17 10:49:17 +01001489 Py_END_ALLOW_THREADS
Victor Stinner79d68f92015-03-19 21:54:09 +01001490
1491 if (rc != WAIT_OBJECT_0)
1492 break;
Victor Stinner945c82e2015-03-12 16:19:01 +01001493#endif
Victor Stinner0c2fd892015-03-17 10:49:17 +01001494
Victor Stinner79d68f92015-03-19 21:54:09 +01001495 /* sleep was interrupted by SIGINT */
1496 if (PyErr_CheckSignals())
1497 return -1;
1498
Victor Stinnercb29f012015-03-27 13:31:18 +01001499 monotonic = _PyTime_GetMonotonicClock();
1500 secs = deadline - monotonic;
Victor Stinner6aa446c2015-03-30 21:33:51 +02001501 if (secs < 0)
Victor Stinner79d68f92015-03-19 21:54:09 +01001502 break;
1503 /* retry with the recomputed delay */
1504 } while (1);
1505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +00001507}