Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Time module */ |
| 2 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 5 | #include <ctype.h> |
| 6 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 7 | #ifdef HAVE_SYS_TIMES_H |
| 8 | #include <sys/times.h> |
| 9 | #endif |
| 10 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12 | #include <sys/types.h> |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 13 | #endif |
| 14 | |
| 15 | #if defined(HAVE_SYS_RESOURCE_H) |
| 16 | #include <sys/resource.h> |
| 17 | #endif |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 19 | #ifdef QUICKWIN |
| 20 | #include <io.h> |
| 21 | #endif |
| 22 | |
pdox | e14679c | 2017-10-05 00:01:56 -0700 | [diff] [blame] | 23 | #if defined(HAVE_PTHREAD_H) |
| 24 | # include <pthread.h> |
| 25 | #endif |
| 26 | |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 27 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 28 | #include <i86.h> |
| 29 | #else |
Guido van Rossum | cac6c72 | 1996-09-06 13:34:02 +0000 | [diff] [blame] | 30 | #ifdef MS_WINDOWS |
Mark Hammond | 975e392 | 2002-07-16 01:29:19 +0000 | [diff] [blame] | 31 | #define WIN32_LEAN_AND_MEAN |
Guido van Rossum | 258ccd4 | 2001-03-02 06:53:29 +0000 | [diff] [blame] | 32 | #include <windows.h> |
Mark Hammond | 975e392 | 2002-07-16 01:29:19 +0000 | [diff] [blame] | 33 | #include "pythread.h" |
Guido van Rossum | cac6c72 | 1996-09-06 13:34:02 +0000 | [diff] [blame] | 34 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 35 | #endif /* !__WATCOMC__ || __QNX__ */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 36 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 37 | #define SEC_TO_NS (1000 * 1000 * 1000) |
| 38 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 39 | /* Forward declarations */ |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 40 | static int pysleep(_PyTime_t); |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 41 | |
Victor Stinner | 85fdfa8 | 2012-01-27 00:38:48 +0100 | [diff] [blame] | 42 | |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 43 | static PyObject* |
| 44 | _PyFloat_FromPyTime(_PyTime_t t) |
| 45 | { |
| 46 | double d = _PyTime_AsSecondsDouble(t); |
| 47 | return PyFloat_FromDouble(d); |
| 48 | } |
| 49 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 50 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 51 | static PyObject * |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 52 | time_time(PyObject *self, PyObject *unused) |
Victor Stinner | 85fdfa8 | 2012-01-27 00:38:48 +0100 | [diff] [blame] | 53 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 54 | _PyTime_t t = _PyTime_GetSystemClock(); |
| 55 | return _PyFloat_FromPyTime(t); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | PyDoc_STRVAR(time_doc, |
| 60 | "time() -> floating point number\n\ |
| 61 | \n\ |
| 62 | Return the current time in seconds since the Epoch.\n\ |
| 63 | Fractions of a second may be present if the system clock provides them."); |
| 64 | |
| 65 | static PyObject * |
| 66 | time_time_ns(PyObject *self, PyObject *unused) |
| 67 | { |
| 68 | _PyTime_t t = _PyTime_GetSystemClock(); |
| 69 | return _PyTime_AsNanosecondsObject(t); |
| 70 | } |
| 71 | |
| 72 | PyDoc_STRVAR(time_ns_doc, |
| 73 | "time_ns() -> int\n\ |
| 74 | \n\ |
| 75 | Return the current time in nanoseconds since the Epoch."); |
| 76 | |
| 77 | #if defined(HAVE_CLOCK) |
| 78 | |
| 79 | #ifndef CLOCKS_PER_SEC |
| 80 | # ifdef CLK_TCK |
| 81 | # define CLOCKS_PER_SEC CLK_TCK |
| 82 | # else |
| 83 | # define CLOCKS_PER_SEC 1000000 |
| 84 | # endif |
| 85 | #endif |
| 86 | |
| 87 | static int |
| 88 | _PyTime_GetClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info) |
| 89 | { |
| 90 | static int initialized = 0; |
| 91 | clock_t ticks; |
| 92 | |
| 93 | if (!initialized) { |
| 94 | initialized = 1; |
| 95 | |
| 96 | /* must sure that _PyTime_MulDiv(ticks, SEC_TO_NS, CLOCKS_PER_SEC) |
| 97 | above cannot overflow */ |
| 98 | if ((_PyTime_t)CLOCKS_PER_SEC > _PyTime_MAX / SEC_TO_NS) { |
| 99 | PyErr_SetString(PyExc_OverflowError, |
| 100 | "CLOCKS_PER_SEC is too large"); |
| 101 | return -1; |
| 102 | } |
Victor Stinner | 85fdfa8 | 2012-01-27 00:38:48 +0100 | [diff] [blame] | 103 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 104 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 105 | if (info) { |
| 106 | info->implementation = "clock()"; |
| 107 | info->resolution = 1.0 / (double)CLOCKS_PER_SEC; |
Benjamin Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 108 | info->monotonic = 1; |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 109 | info->adjustable = 0; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 110 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 111 | |
| 112 | ticks = clock(); |
| 113 | if (ticks == (clock_t)-1) { |
| 114 | PyErr_SetString(PyExc_RuntimeError, |
| 115 | "the processor time used is not available " |
| 116 | "or its value cannot be represented"); |
| 117 | return -1; |
| 118 | } |
| 119 | *tp = _PyTime_MulDiv(ticks, SEC_TO_NS, (_PyTime_t)CLOCKS_PER_SEC); |
| 120 | return 0; |
Victor Stinner | 85fdfa8 | 2012-01-27 00:38:48 +0100 | [diff] [blame] | 121 | } |
| 122 | #endif /* HAVE_CLOCK */ |
| 123 | |
Victor Stinner | cba9a0c | 2017-10-12 08:51:56 -0700 | [diff] [blame] | 124 | static PyObject* |
| 125 | perf_counter(_Py_clock_info_t *info) |
| 126 | { |
Victor Stinner | bdaeb7d | 2017-10-16 08:44:31 -0700 | [diff] [blame] | 127 | _PyTime_t t; |
| 128 | if (_PyTime_GetPerfCounterWithInfo(&t, info) < 0) { |
Victor Stinner | cba9a0c | 2017-10-12 08:51:56 -0700 | [diff] [blame] | 129 | return NULL; |
| 130 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 131 | return _PyFloat_FromPyTime(t); |
Victor Stinner | cba9a0c | 2017-10-12 08:51:56 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 134 | #if defined(MS_WINDOWS) || defined(HAVE_CLOCK) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 135 | #define PYCLOCK |
| 136 | static PyObject* |
| 137 | pyclock(_Py_clock_info_t *info) |
| 138 | { |
Victor Stinner | 884d13a | 2017-10-17 14:46:45 -0700 | [diff] [blame] | 139 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 140 | "time.clock has been deprecated in Python 3.3 and will " |
| 141 | "be removed from Python 3.8: " |
| 142 | "use time.perf_counter or time.process_time " |
| 143 | "instead", 1) < 0) { |
| 144 | return NULL; |
| 145 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 146 | |
Victor Stinner | a997c7b | 2017-10-10 02:51:50 -0700 | [diff] [blame] | 147 | #ifdef MS_WINDOWS |
Victor Stinner | cba9a0c | 2017-10-12 08:51:56 -0700 | [diff] [blame] | 148 | return perf_counter(info); |
Victor Stinner | 5488449 | 2014-08-29 16:51:33 +0200 | [diff] [blame] | 149 | #else |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 150 | _PyTime_t t; |
| 151 | if (_PyTime_GetClockWithInfo(&t, info) < 0) { |
| 152 | return NULL; |
| 153 | } |
| 154 | return _PyFloat_FromPyTime(t); |
Victor Stinner | 5488449 | 2014-08-29 16:51:33 +0200 | [diff] [blame] | 155 | #endif |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Victor Stinner | 9122fdd | 2011-07-04 13:55:40 +0200 | [diff] [blame] | 158 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 159 | time_clock(PyObject *self, PyObject *unused) |
Victor Stinner | 9122fdd | 2011-07-04 13:55:40 +0200 | [diff] [blame] | 160 | { |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 161 | return pyclock(NULL); |
Victor Stinner | 9122fdd | 2011-07-04 13:55:40 +0200 | [diff] [blame] | 162 | } |
Victor Stinner | 9122fdd | 2011-07-04 13:55:40 +0200 | [diff] [blame] | 163 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 164 | PyDoc_STRVAR(clock_doc, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 165 | "clock() -> floating point number\n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 166 | \n\ |
| 167 | Return the CPU time or real time since the start of the process or since\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 168 | the first call to clock(). This has as much precision as the system\n\ |
| 169 | records."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 170 | #endif |
| 171 | |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 172 | #ifdef HAVE_CLOCK_GETTIME |
| 173 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 174 | time_clock_gettime(PyObject *self, PyObject *args) |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 175 | { |
| 176 | int ret; |
Victor Stinner | b8d0169 | 2012-04-13 23:44:05 +0200 | [diff] [blame] | 177 | int clk_id; |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 178 | struct timespec tp; |
| 179 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 180 | if (!PyArg_ParseTuple(args, "i:clock_gettime", &clk_id)) { |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 181 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 182 | } |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 183 | |
| 184 | ret = clock_gettime((clockid_t)clk_id, &tp); |
Antoine Pitrou | 2c08560 | 2012-01-18 01:41:44 +0100 | [diff] [blame] | 185 | if (ret != 0) { |
Victor Stinner | a734af3 | 2014-07-31 13:07:17 +0200 | [diff] [blame] | 186 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | 2c08560 | 2012-01-18 01:41:44 +0100 | [diff] [blame] | 187 | return NULL; |
| 188 | } |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 189 | return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | PyDoc_STRVAR(clock_gettime_doc, |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 193 | "clock_gettime(clk_id) -> float\n\ |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 194 | \n\ |
| 195 | Return the time of the specified clock clk_id."); |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 196 | |
| 197 | static PyObject * |
| 198 | time_clock_gettime_ns(PyObject *self, PyObject *args) |
| 199 | { |
| 200 | int ret; |
| 201 | int clk_id; |
| 202 | struct timespec ts; |
| 203 | _PyTime_t t; |
| 204 | |
| 205 | if (!PyArg_ParseTuple(args, "i:clock_gettime", &clk_id)) { |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | ret = clock_gettime((clockid_t)clk_id, &ts); |
| 210 | if (ret != 0) { |
| 211 | PyErr_SetFromErrno(PyExc_OSError); |
| 212 | return NULL; |
| 213 | } |
| 214 | if (_PyTime_FromTimespec(&t, &ts) < 0) { |
| 215 | return NULL; |
| 216 | } |
| 217 | return _PyTime_AsNanosecondsObject(t); |
| 218 | } |
| 219 | |
| 220 | PyDoc_STRVAR(clock_gettime_ns_doc, |
| 221 | "clock_gettime_ns(clk_id) -> int\n\ |
| 222 | \n\ |
| 223 | Return the time of the specified clock clk_id as nanoseconds."); |
Benjamin Peterson | 37098cd | 2016-09-13 22:55:09 -0700 | [diff] [blame] | 224 | #endif /* HAVE_CLOCK_GETTIME */ |
Victor Stinner | 30d7947 | 2012-04-03 00:45:07 +0200 | [diff] [blame] | 225 | |
Benjamin Peterson | 37098cd | 2016-09-13 22:55:09 -0700 | [diff] [blame] | 226 | #ifdef HAVE_CLOCK_SETTIME |
Victor Stinner | 30d7947 | 2012-04-03 00:45:07 +0200 | [diff] [blame] | 227 | static PyObject * |
| 228 | time_clock_settime(PyObject *self, PyObject *args) |
| 229 | { |
Victor Stinner | b8d0169 | 2012-04-13 23:44:05 +0200 | [diff] [blame] | 230 | int clk_id; |
Victor Stinner | 30d7947 | 2012-04-03 00:45:07 +0200 | [diff] [blame] | 231 | PyObject *obj; |
Victor Stinner | b3b4544 | 2015-03-28 04:09:41 +0100 | [diff] [blame] | 232 | _PyTime_t t; |
Victor Stinner | 30d7947 | 2012-04-03 00:45:07 +0200 | [diff] [blame] | 233 | struct timespec tp; |
| 234 | int ret; |
| 235 | |
| 236 | if (!PyArg_ParseTuple(args, "iO:clock_settime", &clk_id, &obj)) |
| 237 | return NULL; |
| 238 | |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 239 | if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_FLOOR) < 0) |
Victor Stinner | 30d7947 | 2012-04-03 00:45:07 +0200 | [diff] [blame] | 240 | return NULL; |
Victor Stinner | b3b4544 | 2015-03-28 04:09:41 +0100 | [diff] [blame] | 241 | |
| 242 | if (_PyTime_AsTimespec(t, &tp) == -1) |
| 243 | return NULL; |
Victor Stinner | 30d7947 | 2012-04-03 00:45:07 +0200 | [diff] [blame] | 244 | |
| 245 | ret = clock_settime((clockid_t)clk_id, &tp); |
| 246 | if (ret != 0) { |
Victor Stinner | a734af3 | 2014-07-31 13:07:17 +0200 | [diff] [blame] | 247 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | 30d7947 | 2012-04-03 00:45:07 +0200 | [diff] [blame] | 248 | return NULL; |
| 249 | } |
| 250 | Py_RETURN_NONE; |
| 251 | } |
| 252 | |
| 253 | PyDoc_STRVAR(clock_settime_doc, |
| 254 | "clock_settime(clk_id, time)\n\ |
| 255 | \n\ |
| 256 | Set the time of the specified clock clk_id."); |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 257 | |
| 258 | static PyObject * |
| 259 | time_clock_settime_ns(PyObject *self, PyObject *args) |
| 260 | { |
| 261 | int clk_id; |
| 262 | PyObject *obj; |
| 263 | _PyTime_t t; |
| 264 | struct timespec ts; |
| 265 | int ret; |
| 266 | |
| 267 | if (!PyArg_ParseTuple(args, "iO:clock_settime", &clk_id, &obj)) { |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { |
| 272 | return NULL; |
| 273 | } |
| 274 | if (_PyTime_AsTimespec(t, &ts) == -1) { |
| 275 | return NULL; |
| 276 | } |
| 277 | |
| 278 | ret = clock_settime((clockid_t)clk_id, &ts); |
| 279 | if (ret != 0) { |
| 280 | PyErr_SetFromErrno(PyExc_OSError); |
| 281 | return NULL; |
| 282 | } |
| 283 | Py_RETURN_NONE; |
| 284 | } |
| 285 | |
| 286 | PyDoc_STRVAR(clock_settime_ns_doc, |
| 287 | "clock_settime_ns(clk_id, time)\n\ |
| 288 | \n\ |
| 289 | Set the time of the specified clock clk_id with nanoseconds."); |
Benjamin Peterson | 37098cd | 2016-09-13 22:55:09 -0700 | [diff] [blame] | 290 | #endif /* HAVE_CLOCK_SETTIME */ |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 291 | |
Benjamin Peterson | 37098cd | 2016-09-13 22:55:09 -0700 | [diff] [blame] | 292 | #ifdef HAVE_CLOCK_GETRES |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 293 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 294 | time_clock_getres(PyObject *self, PyObject *args) |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 295 | { |
| 296 | int ret; |
Victor Stinner | b8d0169 | 2012-04-13 23:44:05 +0200 | [diff] [blame] | 297 | int clk_id; |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 298 | struct timespec tp; |
| 299 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 300 | if (!PyArg_ParseTuple(args, "i:clock_getres", &clk_id)) |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 301 | return NULL; |
| 302 | |
| 303 | ret = clock_getres((clockid_t)clk_id, &tp); |
Antoine Pitrou | 2c08560 | 2012-01-18 01:41:44 +0100 | [diff] [blame] | 304 | if (ret != 0) { |
Victor Stinner | a734af3 | 2014-07-31 13:07:17 +0200 | [diff] [blame] | 305 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | 2c08560 | 2012-01-18 01:41:44 +0100 | [diff] [blame] | 306 | return NULL; |
| 307 | } |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 308 | |
| 309 | return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | PyDoc_STRVAR(clock_getres_doc, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 313 | "clock_getres(clk_id) -> floating point number\n\ |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 314 | \n\ |
| 315 | Return the resolution (precision) of the specified clock clk_id."); |
Benjamin Peterson | 37098cd | 2016-09-13 22:55:09 -0700 | [diff] [blame] | 316 | #endif /* HAVE_CLOCK_GETRES */ |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 317 | |
pdox | e14679c | 2017-10-05 00:01:56 -0700 | [diff] [blame] | 318 | #ifdef HAVE_PTHREAD_GETCPUCLOCKID |
| 319 | static PyObject * |
| 320 | time_pthread_getcpuclockid(PyObject *self, PyObject *args) |
| 321 | { |
| 322 | unsigned long thread_id; |
| 323 | int err; |
| 324 | clockid_t clk_id; |
| 325 | if (!PyArg_ParseTuple(args, "k:pthread_getcpuclockid", &thread_id)) { |
| 326 | return NULL; |
| 327 | } |
| 328 | err = pthread_getcpuclockid((pthread_t)thread_id, &clk_id); |
| 329 | if (err) { |
| 330 | errno = err; |
| 331 | PyErr_SetFromErrno(PyExc_OSError); |
| 332 | return NULL; |
| 333 | } |
| 334 | return PyLong_FromLong(clk_id); |
| 335 | } |
| 336 | |
| 337 | PyDoc_STRVAR(pthread_getcpuclockid_doc, |
| 338 | "pthread_getcpuclockid(thread_id) -> int\n\ |
| 339 | \n\ |
| 340 | Return the clk_id of a thread's CPU time clock."); |
| 341 | #endif /* HAVE_PTHREAD_GETCPUCLOCKID */ |
| 342 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 343 | static PyObject * |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 344 | time_sleep(PyObject *self, PyObject *obj) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 345 | { |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 346 | _PyTime_t secs; |
Pablo Galindo | 59af94f | 2017-10-18 08:13:09 +0100 | [diff] [blame] | 347 | if (_PyTime_FromSecondsObject(&secs, obj, _PyTime_ROUND_TIMEOUT)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | return NULL; |
Victor Stinner | 7f53a50 | 2011-07-05 22:00:25 +0200 | [diff] [blame] | 349 | if (secs < 0) { |
| 350 | PyErr_SetString(PyExc_ValueError, |
| 351 | "sleep length must be non-negative"); |
| 352 | return NULL; |
| 353 | } |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 354 | if (pysleep(secs) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | return NULL; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 356 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 359 | PyDoc_STRVAR(sleep_doc, |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 360 | "sleep(seconds)\n\ |
| 361 | \n\ |
| 362 | Delay execution for a given number of seconds. The argument may be\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 363 | a floating point number for subsecond precision."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 364 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 365 | static PyStructSequence_Field struct_time_type_fields[] = { |
Alexander Belopolsky | 69f3fd0 | 2010-06-05 15:04:51 +0000 | [diff] [blame] | 366 | {"tm_year", "year, for example, 1993"}, |
| 367 | {"tm_mon", "month of year, range [1, 12]"}, |
| 368 | {"tm_mday", "day of month, range [1, 31]"}, |
| 369 | {"tm_hour", "hours, range [0, 23]"}, |
| 370 | {"tm_min", "minutes, range [0, 59]"}, |
| 371 | {"tm_sec", "seconds, range [0, 61])"}, |
| 372 | {"tm_wday", "day of week, range [0, 6], Monday is 0"}, |
| 373 | {"tm_yday", "day of year, range [1, 366]"}, |
| 374 | {"tm_isdst", "1 if summer time is in effect, 0 if not, and -1 if unknown"}, |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 375 | {"tm_zone", "abbreviation of timezone name"}, |
| 376 | {"tm_gmtoff", "offset from UTC in seconds"}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 378 | }; |
| 379 | |
| 380 | static PyStructSequence_Desc struct_time_type_desc = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | "time.struct_time", |
Alexander Belopolsky | 69f3fd0 | 2010-06-05 15:04:51 +0000 | [diff] [blame] | 382 | "The time value as returned by gmtime(), localtime(), and strptime(), and\n" |
| 383 | " accepted by asctime(), mktime() and strftime(). May be considered as a\n" |
| 384 | " sequence of 9 integers.\n\n" |
| 385 | " Note that several fields' values are not the same as those defined by\n" |
| 386 | " the C language standard for struct tm. For example, the value of the\n" |
| 387 | " field tm_year is the actual year, not year - 1900. See individual\n" |
| 388 | " fields' descriptions for details.", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | struct_time_type_fields, |
| 390 | 9, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 391 | }; |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 392 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 393 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 394 | static PyTypeObject StructTimeType; |
| 395 | |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 396 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 397 | static PyObject * |
Alexander Belopolsky | 18f3a9b | 2016-09-11 22:55:16 -0400 | [diff] [blame] | 398 | tmtotuple(struct tm *p |
| 399 | #ifndef HAVE_STRUCT_TM_TM_ZONE |
Victor Stinner | 0d659e5 | 2017-04-25 01:22:42 +0200 | [diff] [blame] | 400 | , const char *zone, time_t gmtoff |
Alexander Belopolsky | 18f3a9b | 2016-09-11 22:55:16 -0400 | [diff] [blame] | 401 | #endif |
| 402 | ) |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 403 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | PyObject *v = PyStructSequence_New(&StructTimeType); |
| 405 | if (v == NULL) |
| 406 | return NULL; |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 407 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 408 | #define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val)) |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | SET(0, p->tm_year + 1900); |
| 411 | SET(1, p->tm_mon + 1); /* Want January == 1 */ |
| 412 | SET(2, p->tm_mday); |
| 413 | SET(3, p->tm_hour); |
| 414 | SET(4, p->tm_min); |
| 415 | SET(5, p->tm_sec); |
| 416 | SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */ |
| 417 | SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */ |
| 418 | SET(8, p->tm_isdst); |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 419 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
| 420 | PyStructSequence_SET_ITEM(v, 9, |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 421 | PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape")); |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 422 | SET(10, p->tm_gmtoff); |
Alexander Belopolsky | 18f3a9b | 2016-09-11 22:55:16 -0400 | [diff] [blame] | 423 | #else |
| 424 | PyStructSequence_SET_ITEM(v, 9, |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 425 | PyUnicode_DecodeLocale(zone, "surrogateescape")); |
Victor Stinner | 0d659e5 | 2017-04-25 01:22:42 +0200 | [diff] [blame] | 426 | PyStructSequence_SET_ITEM(v, 10, _PyLong_FromTime_t(gmtoff)); |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 427 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 428 | #undef SET |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | if (PyErr_Occurred()) { |
| 430 | Py_XDECREF(v); |
| 431 | return NULL; |
| 432 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | return v; |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 437 | /* Parse arg tuple that can contain an optional float-or-None value; |
| 438 | format needs to be "|O:name". |
| 439 | Returns non-zero on success (parallels PyArg_ParseTuple). |
| 440 | */ |
| 441 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 442 | parse_time_t_args(PyObject *args, const char *format, time_t *pwhen) |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | PyObject *ot = NULL; |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 445 | time_t whent; |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | if (!PyArg_ParseTuple(args, format, &ot)) |
| 448 | return 0; |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 449 | if (ot == NULL || ot == Py_None) { |
| 450 | whent = time(NULL); |
| 451 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | else { |
Victor Stinner | 02937aa | 2015-03-28 05:02:39 +0100 | [diff] [blame] | 453 | if (_PyTime_ObjectToTime_t(ot, &whent, _PyTime_ROUND_FLOOR) == -1) |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 454 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | } |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 456 | *pwhen = whent; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 457 | return 1; |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 460 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 461 | time_gmtime(PyObject *self, PyObject *args) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 462 | { |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 463 | time_t when; |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 464 | struct tm buf; |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 465 | |
| 466 | if (!parse_time_t_args(args, "|O:gmtime", &when)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | return NULL; |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 468 | |
| 469 | errno = 0; |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 470 | if (_PyTime_gmtime(when, &buf) != 0) |
| 471 | return NULL; |
Alexander Belopolsky | 18f3a9b | 2016-09-11 22:55:16 -0400 | [diff] [blame] | 472 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 473 | return tmtotuple(&buf); |
Alexander Belopolsky | 18f3a9b | 2016-09-11 22:55:16 -0400 | [diff] [blame] | 474 | #else |
| 475 | return tmtotuple(&buf, "UTC", 0); |
| 476 | #endif |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Alexander Belopolsky | 18f3a9b | 2016-09-11 22:55:16 -0400 | [diff] [blame] | 479 | #ifndef HAVE_TIMEGM |
| 480 | static time_t |
| 481 | timegm(struct tm *p) |
| 482 | { |
| 483 | /* XXX: the following implementation will not work for tm_year < 1970. |
| 484 | but it is likely that platforms that don't have timegm do not support |
| 485 | negative timestamps anyways. */ |
| 486 | return p->tm_sec + p->tm_min*60 + p->tm_hour*3600 + p->tm_yday*86400 + |
| 487 | (p->tm_year-70)*31536000 + ((p->tm_year-69)/4)*86400 - |
| 488 | ((p->tm_year-1)/100)*86400 + ((p->tm_year+299)/400)*86400; |
| 489 | } |
| 490 | #endif |
| 491 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 492 | PyDoc_STRVAR(gmtime_doc, |
Christian Heimes | 9a37159 | 2007-12-28 14:08:13 +0000 | [diff] [blame] | 493 | "gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,\n\ |
Fred Drake | 193a3f6 | 2002-03-12 21:38:49 +0000 | [diff] [blame] | 494 | tm_sec, tm_wday, tm_yday, tm_isdst)\n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 495 | \n\ |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 496 | Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\ |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 497 | GMT). When 'seconds' is not passed in, convert the current time instead.\n\ |
| 498 | \n\ |
| 499 | If the platform supports the tm_gmtoff and tm_zone, they are available as\n\ |
| 500 | attributes only."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 501 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 502 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 503 | time_localtime(PyObject *self, PyObject *args) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 504 | { |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 505 | time_t when; |
| 506 | struct tm buf; |
| 507 | |
| 508 | if (!parse_time_t_args(args, "|O:localtime", &when)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | return NULL; |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 510 | if (_PyTime_localtime(when, &buf) != 0) |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 511 | return NULL; |
Alexander Belopolsky | 18f3a9b | 2016-09-11 22:55:16 -0400 | [diff] [blame] | 512 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 513 | return tmtotuple(&buf); |
Alexander Belopolsky | 18f3a9b | 2016-09-11 22:55:16 -0400 | [diff] [blame] | 514 | #else |
| 515 | { |
| 516 | struct tm local = buf; |
| 517 | char zone[100]; |
Victor Stinner | 0d659e5 | 2017-04-25 01:22:42 +0200 | [diff] [blame] | 518 | time_t gmtoff; |
Steve Dower | c3c6f71 | 2016-12-14 11:22:05 -0800 | [diff] [blame] | 519 | strftime(zone, sizeof(zone), "%Z", &buf); |
Alexander Belopolsky | 18f3a9b | 2016-09-11 22:55:16 -0400 | [diff] [blame] | 520 | gmtoff = timegm(&buf) - when; |
| 521 | return tmtotuple(&local, zone, gmtoff); |
| 522 | } |
| 523 | #endif |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 526 | PyDoc_STRVAR(localtime_doc, |
Christian Heimes | 9a37159 | 2007-12-28 14:08:13 +0000 | [diff] [blame] | 527 | "localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,\n\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | tm_sec,tm_wday,tm_yday,tm_isdst)\n\ |
Martin v. Löwis | b3cfc1d | 2001-12-02 12:27:43 +0000 | [diff] [blame] | 529 | \n\ |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 530 | Convert seconds since the Epoch to a time tuple expressing local time.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 531 | When 'seconds' is not passed in, convert the current time instead."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 532 | |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 533 | /* Convert 9-item tuple to tm structure. Return 1 on success, set |
| 534 | * an exception and return 0 on error. |
| 535 | */ |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 536 | static int |
Oren Milman | 1d1d3e9 | 2017-08-20 18:35:36 +0300 | [diff] [blame] | 537 | gettmarg(PyObject *args, struct tm *p, const char *format) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 538 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | int y; |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | memset((void *) p, '\0', sizeof(struct tm)); |
Guido van Rossum | b908126 | 2007-08-25 03:14:09 +0000 | [diff] [blame] | 542 | |
Alexander Belopolsky | 610e544 | 2011-01-06 21:57:06 +0000 | [diff] [blame] | 543 | if (!PyTuple_Check(args)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 544 | PyErr_SetString(PyExc_TypeError, |
| 545 | "Tuple or struct_time argument required"); |
| 546 | return 0; |
| 547 | } |
Skip Montanaro | 41cfce9 | 2007-08-24 21:11:00 +0000 | [diff] [blame] | 548 | |
Oren Milman | 1d1d3e9 | 2017-08-20 18:35:36 +0300 | [diff] [blame] | 549 | if (!PyArg_ParseTuple(args, format, |
Alexander Belopolsky | 610e544 | 2011-01-06 21:57:06 +0000 | [diff] [blame] | 550 | &y, &p->tm_mon, &p->tm_mday, |
| 551 | &p->tm_hour, &p->tm_min, &p->tm_sec, |
| 552 | &p->tm_wday, &p->tm_yday, &p->tm_isdst)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | p->tm_year = y - 1900; |
| 555 | p->tm_mon--; |
| 556 | p->tm_wday = (p->tm_wday + 1) % 7; |
| 557 | p->tm_yday--; |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 558 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
| 559 | if (Py_TYPE(args) == &StructTimeType) { |
| 560 | PyObject *item; |
| 561 | item = PyTuple_GET_ITEM(args, 9); |
Victor Stinner | 6e67695 | 2017-04-26 13:51:48 +0200 | [diff] [blame] | 562 | p->tm_zone = item == Py_None ? NULL : (char*)PyUnicode_AsUTF8(item); |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 563 | item = PyTuple_GET_ITEM(args, 10); |
| 564 | p->tm_gmtoff = item == Py_None ? 0 : PyLong_AsLong(item); |
| 565 | if (PyErr_Occurred()) |
| 566 | return 0; |
| 567 | } |
| 568 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | return 1; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 572 | /* Check values of the struct tm fields before it is passed to strftime() and |
| 573 | * asctime(). Return 1 if all values are valid, otherwise set an exception |
| 574 | * and returns 0. |
| 575 | */ |
Victor Stinner | ef12810 | 2010-10-07 01:00:52 +0000 | [diff] [blame] | 576 | static int |
| 577 | checktm(struct tm* buf) |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 578 | { |
Victor Stinner | ef12810 | 2010-10-07 01:00:52 +0000 | [diff] [blame] | 579 | /* Checks added to make sure strftime() and asctime() does not crash Python by |
| 580 | indexing blindly into some array for a textual representation |
| 581 | by some bad index (fixes bug #897625 and #6608). |
| 582 | |
| 583 | Also support values of zero from Python code for arguments in which |
| 584 | that is out of range by forcing that value to the lowest value that |
| 585 | is valid (fixed bug #1520914). |
| 586 | |
| 587 | Valid ranges based on what is allowed in struct tm: |
| 588 | |
| 589 | - tm_year: [0, max(int)] (1) |
| 590 | - tm_mon: [0, 11] (2) |
| 591 | - tm_mday: [1, 31] |
| 592 | - tm_hour: [0, 23] |
| 593 | - tm_min: [0, 59] |
| 594 | - tm_sec: [0, 60] |
| 595 | - tm_wday: [0, 6] (1) |
| 596 | - tm_yday: [0, 365] (2) |
| 597 | - tm_isdst: [-max(int), max(int)] |
| 598 | |
| 599 | (1) gettmarg() handles bounds-checking. |
| 600 | (2) Python's acceptable range is one greater than the range in C, |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 601 | thus need to check against automatic decrement by gettmarg(). |
| 602 | */ |
| 603 | if (buf->tm_mon == -1) |
| 604 | buf->tm_mon = 0; |
| 605 | else if (buf->tm_mon < 0 || buf->tm_mon > 11) { |
| 606 | PyErr_SetString(PyExc_ValueError, "month out of range"); |
| 607 | return 0; |
| 608 | } |
| 609 | if (buf->tm_mday == 0) |
| 610 | buf->tm_mday = 1; |
| 611 | else if (buf->tm_mday < 0 || buf->tm_mday > 31) { |
| 612 | PyErr_SetString(PyExc_ValueError, "day of month out of range"); |
| 613 | return 0; |
| 614 | } |
| 615 | if (buf->tm_hour < 0 || buf->tm_hour > 23) { |
| 616 | PyErr_SetString(PyExc_ValueError, "hour out of range"); |
| 617 | return 0; |
| 618 | } |
| 619 | if (buf->tm_min < 0 || buf->tm_min > 59) { |
| 620 | PyErr_SetString(PyExc_ValueError, "minute out of range"); |
| 621 | return 0; |
| 622 | } |
| 623 | if (buf->tm_sec < 0 || buf->tm_sec > 61) { |
| 624 | PyErr_SetString(PyExc_ValueError, "seconds out of range"); |
| 625 | return 0; |
| 626 | } |
| 627 | /* tm_wday does not need checking of its upper-bound since taking |
| 628 | ``% 7`` in gettmarg() automatically restricts the range. */ |
| 629 | if (buf->tm_wday < 0) { |
| 630 | PyErr_SetString(PyExc_ValueError, "day of week out of range"); |
| 631 | return 0; |
| 632 | } |
| 633 | if (buf->tm_yday == -1) |
| 634 | buf->tm_yday = 0; |
| 635 | else if (buf->tm_yday < 0 || buf->tm_yday > 365) { |
| 636 | PyErr_SetString(PyExc_ValueError, "day of year out of range"); |
| 637 | return 0; |
| 638 | } |
| 639 | return 1; |
| 640 | } |
| 641 | |
Victor Stinner | c1f32ca | 2011-10-14 02:36:13 +0200 | [diff] [blame] | 642 | #ifdef MS_WINDOWS |
| 643 | /* wcsftime() doesn't format correctly time zones, see issue #10653 */ |
| 644 | # undef HAVE_WCSFTIME |
| 645 | #endif |
Alexander Belopolsky | cf77454 | 2012-10-02 18:39:16 -0400 | [diff] [blame] | 646 | #define STRFTIME_FORMAT_CODES \ |
| 647 | "Commonly used format codes:\n\ |
| 648 | \n\ |
| 649 | %Y Year with century as a decimal number.\n\ |
| 650 | %m Month as a decimal number [01,12].\n\ |
| 651 | %d Day of the month as a decimal number [01,31].\n\ |
| 652 | %H Hour (24-hour clock) as a decimal number [00,23].\n\ |
| 653 | %M Minute as a decimal number [00,59].\n\ |
| 654 | %S Second as a decimal number [00,61].\n\ |
| 655 | %z Time zone offset from UTC.\n\ |
| 656 | %a Locale's abbreviated weekday name.\n\ |
| 657 | %A Locale's full weekday name.\n\ |
| 658 | %b Locale's abbreviated month name.\n\ |
| 659 | %B Locale's full month name.\n\ |
| 660 | %c Locale's appropriate date and time representation.\n\ |
| 661 | %I Hour (12-hour clock) as a decimal number [01,12].\n\ |
| 662 | %p Locale's equivalent of either AM or PM.\n\ |
| 663 | \n\ |
| 664 | Other codes may be available on your platform. See documentation for\n\ |
| 665 | the C library strftime function.\n" |
Victor Stinner | c1f32ca | 2011-10-14 02:36:13 +0200 | [diff] [blame] | 666 | |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 667 | #ifdef HAVE_STRFTIME |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 668 | #ifdef HAVE_WCSFTIME |
| 669 | #define time_char wchar_t |
| 670 | #define format_time wcsftime |
| 671 | #define time_strlen wcslen |
| 672 | #else |
| 673 | #define time_char char |
| 674 | #define format_time strftime |
| 675 | #define time_strlen strlen |
| 676 | #endif |
| 677 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 678 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 679 | time_strftime(PyObject *self, PyObject *args) |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 680 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | PyObject *tup = NULL; |
| 682 | struct tm buf; |
| 683 | const time_char *fmt; |
Victor Stinner | b290478 | 2010-09-29 10:34:19 +0000 | [diff] [blame] | 684 | #ifdef HAVE_WCSFTIME |
| 685 | wchar_t *format; |
| 686 | #else |
| 687 | PyObject *format; |
| 688 | #endif |
Victor Stinner | ef12810 | 2010-10-07 01:00:52 +0000 | [diff] [blame] | 689 | PyObject *format_arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | size_t fmtlen, buflen; |
Victor Stinner | b290478 | 2010-09-29 10:34:19 +0000 | [diff] [blame] | 691 | time_char *outbuf = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | size_t i; |
Victor Stinner | b290478 | 2010-09-29 10:34:19 +0000 | [diff] [blame] | 693 | PyObject *ret = NULL; |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 695 | memset((void *) &buf, '\0', sizeof(buf)); |
Guido van Rossum | 1f41f84 | 1998-04-27 19:04:26 +0000 | [diff] [blame] | 696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | /* Will always expect a unicode string to be passed as format. |
| 698 | Given that there's no str type anymore in py3k this seems safe. |
| 699 | */ |
Victor Stinner | ef12810 | 2010-10-07 01:00:52 +0000 | [diff] [blame] | 700 | if (!PyArg_ParseTuple(args, "U|O:strftime", &format_arg, &tup)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | return NULL; |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 703 | if (tup == NULL) { |
| 704 | time_t tt = time(NULL); |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 705 | if (_PyTime_localtime(tt, &buf) != 0) |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 706 | return NULL; |
Alexander Belopolsky | 38e2996 | 2010-10-01 14:18:49 +0000 | [diff] [blame] | 707 | } |
Oren Milman | 1d1d3e9 | 2017-08-20 18:35:36 +0300 | [diff] [blame] | 708 | else if (!gettmarg(tup, &buf, |
| 709 | "iiiiiiiii;strftime(): illegal time tuple argument") || |
| 710 | !checktm(&buf)) |
| 711 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 712 | return NULL; |
Oren Milman | 1d1d3e9 | 2017-08-20 18:35:36 +0300 | [diff] [blame] | 713 | } |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 714 | |
Victor Stinner | 36b82d8 | 2013-06-25 02:33:53 +0200 | [diff] [blame] | 715 | #if defined(_MSC_VER) || defined(sun) || defined(_AIX) |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 716 | if (buf.tm_year + 1900 < 1 || 9999 < buf.tm_year + 1900) { |
Victor Stinner | 6f0e4f9 | 2011-03-21 02:14:53 +0100 | [diff] [blame] | 717 | PyErr_SetString(PyExc_ValueError, |
| 718 | "strftime() requires year in [1; 9999]"); |
Alexander Belopolsky | 0dd06f4 | 2011-01-08 01:23:02 +0000 | [diff] [blame] | 719 | return NULL; |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 720 | } |
Victor Stinner | 73ea29c | 2011-01-08 01:56:31 +0000 | [diff] [blame] | 721 | #endif |
Alexander Belopolsky | c64708a | 2011-01-07 19:59:19 +0000 | [diff] [blame] | 722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | /* Normalize tm_isdst just in case someone foolishly implements %Z |
| 724 | based on the assumption that tm_isdst falls within the range of |
| 725 | [-1, 1] */ |
| 726 | if (buf.tm_isdst < -1) |
| 727 | buf.tm_isdst = -1; |
| 728 | else if (buf.tm_isdst > 1) |
| 729 | buf.tm_isdst = 1; |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 730 | |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 731 | #ifdef HAVE_WCSFTIME |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 732 | format = PyUnicode_AsWideCharString(format_arg, NULL); |
Victor Stinner | b290478 | 2010-09-29 10:34:19 +0000 | [diff] [blame] | 733 | if (format == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 734 | return NULL; |
Victor Stinner | b290478 | 2010-09-29 10:34:19 +0000 | [diff] [blame] | 735 | fmt = format; |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 736 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 737 | /* Convert the unicode string to an ascii one */ |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 738 | format = PyUnicode_EncodeLocale(format_arg, "surrogateescape"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | if (format == NULL) |
| 740 | return NULL; |
| 741 | fmt = PyBytes_AS_STRING(format); |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 742 | #endif |
Amaury Forgeot d'Arc | b5be6d4 | 2009-03-02 23:52:57 +0000 | [diff] [blame] | 743 | |
Stefan Krah | 4aea7d3 | 2012-02-27 16:30:26 +0100 | [diff] [blame] | 744 | #if defined(MS_WINDOWS) && !defined(HAVE_WCSFTIME) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 745 | /* check that the format string contains only valid directives */ |
Steve Dower | e5b5895 | 2015-09-06 19:20:51 -0700 | [diff] [blame] | 746 | for (outbuf = strchr(fmt, '%'); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | outbuf != NULL; |
Victor Stinner | 5a3ff79 | 2011-10-16 19:08:23 +0200 | [diff] [blame] | 748 | outbuf = strchr(outbuf+2, '%')) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | { |
Steve Dower | e5b5895 | 2015-09-06 19:20:51 -0700 | [diff] [blame] | 750 | if (outbuf[1] == '#') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | ++outbuf; /* not documented by python, */ |
Steve Dower | e5b5895 | 2015-09-06 19:20:51 -0700 | [diff] [blame] | 752 | if (outbuf[1] == '\0') |
| 753 | break; |
| 754 | if ((outbuf[1] == 'y') && buf.tm_year < 0) { |
Tim Golden | 6e51b8f | 2013-11-12 12:36:54 +0000 | [diff] [blame] | 755 | PyErr_SetString(PyExc_ValueError, |
| 756 | "format %y requires year >= 1900 on Windows"); |
| 757 | Py_DECREF(format); |
| 758 | return NULL; |
| 759 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 760 | } |
Victor Stinner | 93965f7 | 2013-11-23 14:59:33 +0100 | [diff] [blame] | 761 | #elif (defined(_AIX) || defined(sun)) && defined(HAVE_WCSFTIME) |
Steve Dower | e5b5895 | 2015-09-06 19:20:51 -0700 | [diff] [blame] | 762 | for (outbuf = wcschr(fmt, '%'); |
Victor Stinner | 55329f8 | 2013-11-17 23:39:21 +0100 | [diff] [blame] | 763 | outbuf != NULL; |
| 764 | outbuf = wcschr(outbuf+2, '%')) |
| 765 | { |
Steve Dower | e5b5895 | 2015-09-06 19:20:51 -0700 | [diff] [blame] | 766 | if (outbuf[1] == L'\0') |
| 767 | break; |
Victor Stinner | 55329f8 | 2013-11-17 23:39:21 +0100 | [diff] [blame] | 768 | /* Issue #19634: On AIX, wcsftime("y", (1899, 1, 1, 0, 0, 0, 0, 0, 0)) |
| 769 | returns "0/" instead of "99" */ |
| 770 | if (outbuf[1] == L'y' && buf.tm_year < 0) { |
| 771 | PyErr_SetString(PyExc_ValueError, |
| 772 | "format %y requires year >= 1900 on AIX"); |
Victor Stinner | 55329f8 | 2013-11-17 23:39:21 +0100 | [diff] [blame] | 773 | return NULL; |
| 774 | } |
| 775 | } |
Amaury Forgeot d'Arc | b5be6d4 | 2009-03-02 23:52:57 +0000 | [diff] [blame] | 776 | #endif |
| 777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | fmtlen = time_strlen(fmt); |
Guido van Rossum | c222ec2 | 1999-02-23 00:00:10 +0000 | [diff] [blame] | 779 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 780 | /* I hate these functions that presume you know how big the output |
| 781 | * will be ahead of time... |
| 782 | */ |
| 783 | for (i = 1024; ; i += i) { |
| 784 | outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char)); |
| 785 | if (outbuf == NULL) { |
Victor Stinner | b290478 | 2010-09-29 10:34:19 +0000 | [diff] [blame] | 786 | PyErr_NoMemory(); |
| 787 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | } |
Steve Dower | 57ab1cd | 2015-09-22 14:51:42 -0700 | [diff] [blame] | 789 | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
| 790 | errno = 0; |
| 791 | #endif |
Steve Dower | d9ef74e | 2015-05-22 15:08:34 -0700 | [diff] [blame] | 792 | _Py_BEGIN_SUPPRESS_IPH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | buflen = format_time(outbuf, i, fmt, &buf); |
Steve Dower | d9ef74e | 2015-05-22 15:08:34 -0700 | [diff] [blame] | 794 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 136ea49 | 2011-12-17 22:37:18 +0100 | [diff] [blame] | 795 | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
Steve Dower | 97cded9 | 2015-09-08 19:12:51 -0700 | [diff] [blame] | 796 | /* VisualStudio .NET 2005 does this properly */ |
| 797 | if (buflen == 0 && errno == EINVAL) { |
| 798 | PyErr_SetString(PyExc_ValueError, "Invalid format string"); |
| 799 | PyMem_Free(outbuf); |
| 800 | break; |
| 801 | } |
Victor Stinner | 136ea49 | 2011-12-17 22:37:18 +0100 | [diff] [blame] | 802 | #endif |
Steve Dower | 97cded9 | 2015-09-08 19:12:51 -0700 | [diff] [blame] | 803 | if (buflen > 0 || i >= 256 * fmtlen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 804 | /* If the buffer is 256 times as long as the format, |
| 805 | it's probably not failing for lack of room! |
| 806 | More likely, the format yields an empty result, |
| 807 | e.g. an empty format, or %Z when the timezone |
| 808 | is unknown. */ |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 809 | #ifdef HAVE_WCSFTIME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | ret = PyUnicode_FromWideChar(outbuf, buflen); |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 811 | #else |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 812 | ret = PyUnicode_DecodeLocaleAndSize(outbuf, buflen, "surrogateescape"); |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 813 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | PyMem_Free(outbuf); |
Victor Stinner | b290478 | 2010-09-29 10:34:19 +0000 | [diff] [blame] | 815 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 816 | } |
| 817 | PyMem_Free(outbuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | } |
Victor Stinner | b290478 | 2010-09-29 10:34:19 +0000 | [diff] [blame] | 819 | #ifdef HAVE_WCSFTIME |
| 820 | PyMem_Free(format); |
| 821 | #else |
| 822 | Py_DECREF(format); |
| 823 | #endif |
| 824 | return ret; |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 825 | } |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 826 | |
Martin v. Löwis | 1b01ccd | 2009-05-30 06:13:40 +0000 | [diff] [blame] | 827 | #undef time_char |
| 828 | #undef format_time |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 829 | PyDoc_STRVAR(strftime_doc, |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 830 | "strftime(format[, tuple]) -> string\n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 831 | \n\ |
| 832 | Convert a time tuple to a string according to a format specification.\n\ |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 833 | See the library reference manual for formatting codes. When the time tuple\n\ |
Alexander Belopolsky | cf77454 | 2012-10-02 18:39:16 -0400 | [diff] [blame] | 834 | is not present, current time as returned by localtime() is used.\n\ |
| 835 | \n" STRFTIME_FORMAT_CODES); |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 836 | #endif /* HAVE_STRFTIME */ |
| 837 | |
Guido van Rossum | d3c46d5 | 2002-07-19 17:06:47 +0000 | [diff] [blame] | 838 | static PyObject * |
| 839 | time_strptime(PyObject *self, PyObject *args) |
| 840 | { |
Victor Stinner | dbe28d2 | 2016-12-09 00:38:53 +0100 | [diff] [blame] | 841 | PyObject *module, *func, *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 842 | _Py_IDENTIFIER(_strptime_time); |
Guido van Rossum | d3c46d5 | 2002-07-19 17:06:47 +0000 | [diff] [blame] | 843 | |
Victor Stinner | dbe28d2 | 2016-12-09 00:38:53 +0100 | [diff] [blame] | 844 | module = PyImport_ImportModuleNoBlock("_strptime"); |
| 845 | if (!module) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 846 | return NULL; |
Victor Stinner | dbe28d2 | 2016-12-09 00:38:53 +0100 | [diff] [blame] | 847 | |
| 848 | func = _PyObject_GetAttrId(module, &PyId__strptime_time); |
| 849 | Py_DECREF(module); |
| 850 | if (!func) { |
| 851 | return NULL; |
| 852 | } |
| 853 | |
| 854 | result = PyObject_Call(func, args, NULL); |
| 855 | Py_DECREF(func); |
| 856 | return result; |
Guido van Rossum | d3c46d5 | 2002-07-19 17:06:47 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 859 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 860 | PyDoc_STRVAR(strptime_doc, |
Brett Cannon | 20def8b | 2003-07-01 05:16:08 +0000 | [diff] [blame] | 861 | "strptime(string, format) -> struct_time\n\ |
Martin v. Löwis | b3cfc1d | 2001-12-02 12:27:43 +0000 | [diff] [blame] | 862 | \n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 863 | Parse a string to a time tuple according to a format specification.\n\ |
Alexander Belopolsky | cf77454 | 2012-10-02 18:39:16 -0400 | [diff] [blame] | 864 | See the library reference manual for formatting codes (same as\n\ |
| 865 | strftime()).\n\ |
| 866 | \n" STRFTIME_FORMAT_CODES); |
Guido van Rossum | d3c46d5 | 2002-07-19 17:06:47 +0000 | [diff] [blame] | 867 | |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 868 | static PyObject * |
| 869 | _asctime(struct tm *timeptr) |
| 870 | { |
| 871 | /* Inspired by Open Group reference implementation available at |
| 872 | * http://pubs.opengroup.org/onlinepubs/009695399/functions/asctime.html */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 873 | static const char wday_name[7][4] = { |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 874 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" |
| 875 | }; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 876 | static const char mon_name[12][4] = { |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 877 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 878 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 879 | }; |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 880 | return PyUnicode_FromFormat( |
| 881 | "%s %s%3d %.2d:%.2d:%.2d %d", |
| 882 | wday_name[timeptr->tm_wday], |
| 883 | mon_name[timeptr->tm_mon], |
| 884 | timeptr->tm_mday, timeptr->tm_hour, |
| 885 | timeptr->tm_min, timeptr->tm_sec, |
| 886 | 1900 + timeptr->tm_year); |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 887 | } |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 888 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 889 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 890 | time_asctime(PyObject *self, PyObject *args) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 891 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 892 | PyObject *tup = NULL; |
| 893 | struct tm buf; |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 895 | if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup)) |
| 896 | return NULL; |
| 897 | if (tup == NULL) { |
| 898 | time_t tt = time(NULL); |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 899 | if (_PyTime_localtime(tt, &buf) != 0) |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 900 | return NULL; |
Oren Milman | 1d1d3e9 | 2017-08-20 18:35:36 +0300 | [diff] [blame] | 901 | } |
| 902 | else if (!gettmarg(tup, &buf, |
| 903 | "iiiiiiiii;asctime(): illegal time tuple argument") || |
| 904 | !checktm(&buf)) |
| 905 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | return NULL; |
Oren Milman | 1d1d3e9 | 2017-08-20 18:35:36 +0300 | [diff] [blame] | 907 | } |
Alexander Belopolsky | b9588b5 | 2011-01-04 16:34:30 +0000 | [diff] [blame] | 908 | return _asctime(&buf); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 911 | PyDoc_STRVAR(asctime_doc, |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 912 | "asctime([tuple]) -> string\n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 913 | \n\ |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 914 | Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\ |
| 915 | When the time tuple is not present, current time as returned by localtime()\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 916 | is used."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 917 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 918 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 919 | time_ctime(PyObject *self, PyObject *args) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 920 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | time_t tt; |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 922 | struct tm buf; |
| 923 | if (!parse_time_t_args(args, "|O:ctime", &tt)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | return NULL; |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 925 | if (_PyTime_localtime(tt, &buf) != 0) |
Alexander Belopolsky | 5da468f | 2011-01-04 17:15:52 +0000 | [diff] [blame] | 926 | return NULL; |
Victor Stinner | c1b5d34 | 2012-01-27 00:08:48 +0100 | [diff] [blame] | 927 | return _asctime(&buf); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 930 | PyDoc_STRVAR(ctime_doc, |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 931 | "ctime(seconds) -> string\n\ |
| 932 | \n\ |
| 933 | Convert a time in seconds since the Epoch to a string in local time.\n\ |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 934 | This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 935 | not present, current time as returned by localtime() is used."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 936 | |
Guido van Rossum | 60cd813 | 1998-03-06 17:16:21 +0000 | [diff] [blame] | 937 | #ifdef HAVE_MKTIME |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 938 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 939 | time_mktime(PyObject *self, PyObject *tup) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 940 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | struct tm buf; |
| 942 | time_t tt; |
Oren Milman | 1d1d3e9 | 2017-08-20 18:35:36 +0300 | [diff] [blame] | 943 | if (!gettmarg(tup, &buf, |
| 944 | "iiiiiiiii;mktime(): illegal time tuple argument")) |
| 945 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | return NULL; |
Oren Milman | 1d1d3e9 | 2017-08-20 18:35:36 +0300 | [diff] [blame] | 947 | } |
Victor Stinner | 1ac4261 | 2014-02-21 09:27:17 +0100 | [diff] [blame] | 948 | #ifdef _AIX |
| 949 | /* year < 1902 or year > 2037 */ |
| 950 | if (buf.tm_year < 2 || buf.tm_year > 137) { |
| 951 | /* Issue #19748: On AIX, mktime() doesn't report overflow error for |
| 952 | * timestamp < -2^31 or timestamp > 2**31-1. */ |
| 953 | PyErr_SetString(PyExc_OverflowError, |
| 954 | "mktime argument out of range"); |
| 955 | return NULL; |
| 956 | } |
| 957 | #else |
Alexander Belopolsky | b7d40d1 | 2011-01-11 01:21:25 +0000 | [diff] [blame] | 958 | buf.tm_wday = -1; /* sentinel; original value ignored */ |
Victor Stinner | 1ac4261 | 2014-02-21 09:27:17 +0100 | [diff] [blame] | 959 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | tt = mktime(&buf); |
Alexander Belopolsky | b7d40d1 | 2011-01-11 01:21:25 +0000 | [diff] [blame] | 961 | /* Return value of -1 does not necessarily mean an error, but tm_wday |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 962 | * cannot remain set to -1 if mktime succeeded. */ |
Victor Stinner | 9303749 | 2013-06-25 22:54:35 +0200 | [diff] [blame] | 963 | if (tt == (time_t)(-1) |
| 964 | #ifndef _AIX |
| 965 | /* Return value of -1 does not necessarily mean an error, but |
| 966 | * tm_wday cannot remain set to -1 if mktime succeeded. */ |
| 967 | && buf.tm_wday == -1 |
| 968 | #else |
| 969 | /* on AIX, tm_wday is always sets, even on error */ |
| 970 | #endif |
| 971 | ) |
| 972 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | PyErr_SetString(PyExc_OverflowError, |
| 974 | "mktime argument out of range"); |
| 975 | return NULL; |
| 976 | } |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 977 | return PyFloat_FromDouble((double)tt); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 978 | } |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 979 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 980 | PyDoc_STRVAR(mktime_doc, |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 981 | "mktime(tuple) -> floating point number\n\ |
| 982 | \n\ |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 983 | Convert a time tuple in local time to seconds since the Epoch.\n\ |
| 984 | Note that mktime(gmtime(0)) will not generally return zero for most\n\ |
| 985 | time zones; instead the returned value will either be equal to that\n\ |
| 986 | of the timezone or altzone attributes on the time module."); |
Guido van Rossum | 60cd813 | 1998-03-06 17:16:21 +0000 | [diff] [blame] | 987 | #endif /* HAVE_MKTIME */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 988 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 989 | #ifdef HAVE_WORKING_TZSET |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 990 | static void PyInit_timezone(PyObject *module); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 991 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 992 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 993 | time_tzset(PyObject *self, PyObject *unused) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 994 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | PyObject* m; |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 997 | m = PyImport_ImportModuleNoBlock("time"); |
| 998 | if (m == NULL) { |
| 999 | return NULL; |
| 1000 | } |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | tzset(); |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1004 | /* Reset timezone, altzone, daylight and tzname */ |
| 1005 | PyInit_timezone(m); |
| 1006 | Py_DECREF(m); |
Victor Stinner | 2ff51b8 | 2013-07-17 21:42:45 +0200 | [diff] [blame] | 1007 | if (PyErr_Occurred()) |
| 1008 | return NULL; |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 1009 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1010 | Py_RETURN_NONE; |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | PyDoc_STRVAR(tzset_doc, |
R. David Murray | 4d55bf9 | 2010-12-14 00:55:46 +0000 | [diff] [blame] | 1014 | "tzset()\n\ |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1015 | \n\ |
| 1016 | Initialize, or reinitialize, the local timezone to the value stored in\n\ |
| 1017 | os.environ['TZ']. The TZ environment variable should be specified in\n\ |
Neal Norwitz | dc8e194 | 2004-07-20 22:34:37 +0000 | [diff] [blame] | 1018 | standard Unix timezone format as documented in the tzset man page\n\ |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1019 | (eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\ |
| 1020 | fall back to UTC. If the TZ environment variable is not set, the local\n\ |
| 1021 | timezone is set to the systems best guess of wallclock time.\n\ |
| 1022 | Changing the TZ environment variable without calling tzset *may* change\n\ |
| 1023 | the local timezone used by methods such as localtime, but this behaviour\n\ |
| 1024 | should not be relied on."); |
| 1025 | #endif /* HAVE_WORKING_TZSET */ |
| 1026 | |
Victor Stinner | ae58649 | 2014-09-02 23:18:25 +0200 | [diff] [blame] | 1027 | static PyObject * |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1028 | time_monotonic(PyObject *self, PyObject *unused) |
Victor Stinner | 071eca3 | 2012-03-15 01:17:09 +0100 | [diff] [blame] | 1029 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1030 | _PyTime_t t = _PyTime_GetMonotonicClock(); |
| 1031 | return _PyFloat_FromPyTime(t); |
Victor Stinner | 071eca3 | 2012-03-15 01:17:09 +0100 | [diff] [blame] | 1032 | } |
| 1033 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1034 | PyDoc_STRVAR(monotonic_doc, |
| 1035 | "monotonic() -> float\n\ |
Victor Stinner | 8b30201 | 2012-02-07 23:29:46 +0100 | [diff] [blame] | 1036 | \n\ |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1037 | Monotonic clock, cannot go backward."); |
Victor Stinner | ec919cc | 2012-03-15 00:58:32 +0100 | [diff] [blame] | 1038 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1039 | static PyObject * |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1040 | time_monotonic_ns(PyObject *self, PyObject *unused) |
| 1041 | { |
| 1042 | _PyTime_t t = _PyTime_GetMonotonicClock(); |
| 1043 | return _PyTime_AsNanosecondsObject(t); |
| 1044 | } |
| 1045 | |
| 1046 | PyDoc_STRVAR(monotonic_ns_doc, |
| 1047 | "monotonic_ns() -> int\n\ |
| 1048 | \n\ |
| 1049 | Monotonic clock, cannot go backward, as nanoseconds."); |
| 1050 | |
| 1051 | static PyObject * |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1052 | time_perf_counter(PyObject *self, PyObject *unused) |
| 1053 | { |
| 1054 | return perf_counter(NULL); |
| 1055 | } |
| 1056 | |
| 1057 | PyDoc_STRVAR(perf_counter_doc, |
| 1058 | "perf_counter() -> float\n\ |
| 1059 | \n\ |
| 1060 | Performance counter for benchmarking."); |
| 1061 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1062 | static PyObject * |
| 1063 | time_perf_counter_ns(PyObject *self, PyObject *unused) |
| 1064 | { |
| 1065 | _PyTime_t t = _PyTime_GetPerfCounter(); |
| 1066 | return _PyTime_AsNanosecondsObject(t); |
| 1067 | } |
| 1068 | |
| 1069 | PyDoc_STRVAR(perf_counter_ns_doc, |
| 1070 | "perf_counter_ns() -> int\n\ |
| 1071 | \n\ |
| 1072 | Performance counter for benchmarking as nanoseconds."); |
| 1073 | |
| 1074 | static int |
| 1075 | _PyTime_GetProcessTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info) |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1076 | { |
| 1077 | #if defined(MS_WINDOWS) |
| 1078 | HANDLE process; |
| 1079 | FILETIME creation_time, exit_time, kernel_time, user_time; |
| 1080 | ULARGE_INTEGER large; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1081 | _PyTime_t ktime, utime, t; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1082 | BOOL ok; |
| 1083 | |
| 1084 | process = GetCurrentProcess(); |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1085 | ok = GetProcessTimes(process, &creation_time, &exit_time, |
| 1086 | &kernel_time, &user_time); |
| 1087 | if (!ok) { |
| 1088 | PyErr_SetFromWindowsErr(0); |
| 1089 | return -1; |
| 1090 | } |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1091 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1092 | if (info) { |
| 1093 | info->implementation = "GetProcessTimes()"; |
| 1094 | info->resolution = 1e-7; |
Benjamin Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 1095 | info->monotonic = 1; |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 1096 | info->adjustable = 0; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1097 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1098 | |
| 1099 | large.u.LowPart = kernel_time.dwLowDateTime; |
| 1100 | large.u.HighPart = kernel_time.dwHighDateTime; |
| 1101 | ktime = large.QuadPart; |
| 1102 | |
| 1103 | large.u.LowPart = user_time.dwLowDateTime; |
| 1104 | large.u.HighPart = user_time.dwHighDateTime; |
| 1105 | utime = large.QuadPart; |
| 1106 | |
| 1107 | /* ktime and utime have a resolution of 100 nanoseconds */ |
| 1108 | t = _PyTime_FromNanoseconds((ktime + utime) * 100); |
| 1109 | *tp = t; |
| 1110 | return 0; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1111 | #else |
| 1112 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1113 | /* clock_gettime */ |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1114 | #if defined(HAVE_CLOCK_GETTIME) \ |
| 1115 | && (defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_PROF)) |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1116 | struct timespec ts; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1117 | #ifdef CLOCK_PROF |
| 1118 | const clockid_t clk_id = CLOCK_PROF; |
| 1119 | const char *function = "clock_gettime(CLOCK_PROF)"; |
| 1120 | #else |
| 1121 | const clockid_t clk_id = CLOCK_PROCESS_CPUTIME_ID; |
| 1122 | const char *function = "clock_gettime(CLOCK_PROCESS_CPUTIME_ID)"; |
| 1123 | #endif |
| 1124 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1125 | if (clock_gettime(clk_id, &ts) == 0) { |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1126 | if (info) { |
| 1127 | struct timespec res; |
| 1128 | info->implementation = function; |
Benjamin Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 1129 | info->monotonic = 1; |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 1130 | info->adjustable = 0; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1131 | if (clock_getres(clk_id, &res)) { |
| 1132 | PyErr_SetFromErrno(PyExc_OSError); |
| 1133 | return -1; |
| 1134 | } |
| 1135 | info->resolution = res.tv_sec + res.tv_nsec * 1e-9; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1136 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1137 | |
| 1138 | if (_PyTime_FromTimespec(tp, &ts) < 0) { |
| 1139 | return -1; |
| 1140 | } |
| 1141 | return 0; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1142 | } |
| 1143 | #endif |
| 1144 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1145 | /* getrusage(RUSAGE_SELF) */ |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1146 | #if defined(HAVE_SYS_RESOURCE_H) |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1147 | struct rusage ru; |
| 1148 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1149 | if (getrusage(RUSAGE_SELF, &ru) == 0) { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1150 | _PyTime_t utime, stime; |
| 1151 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1152 | if (info) { |
| 1153 | info->implementation = "getrusage(RUSAGE_SELF)"; |
Benjamin Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 1154 | info->monotonic = 1; |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 1155 | info->adjustable = 0; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1156 | info->resolution = 1e-6; |
| 1157 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1158 | |
| 1159 | if (_PyTime_FromTimeval(&utime, &ru.ru_utime) < 0) { |
| 1160 | return -1; |
| 1161 | } |
| 1162 | if (_PyTime_FromTimeval(&stime, &ru.ru_stime) < 0) { |
| 1163 | return -1; |
| 1164 | } |
| 1165 | |
| 1166 | _PyTime_t total = utime + utime; |
| 1167 | *tp = total; |
| 1168 | return 0; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1169 | } |
| 1170 | #endif |
| 1171 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1172 | /* times() */ |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1173 | #ifdef HAVE_TIMES |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1174 | struct tms t; |
| 1175 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1176 | if (times(&t) != (clock_t)-1) { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1177 | static long ticks_per_second = -1; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1178 | |
| 1179 | if (ticks_per_second == -1) { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1180 | long freq; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1181 | #if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1182 | freq = sysconf(_SC_CLK_TCK); |
| 1183 | if (freq < 1) { |
| 1184 | freq = -1; |
| 1185 | } |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1186 | #elif defined(HZ) |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1187 | freq = HZ; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1188 | #else |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1189 | freq = 60; /* magic fallback value; may be bogus */ |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1190 | #endif |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1191 | |
| 1192 | if (freq != -1) { |
| 1193 | /* check that _PyTime_MulDiv(t, SEC_TO_NS, ticks_per_second) |
| 1194 | cannot overflow below */ |
Serhiy Storchaka | bfe4fd5 | 2018-02-09 17:31:26 +0200 | [diff] [blame] | 1195 | #if LONG_MAX > _PyTime_MAX / SEC_TO_NS |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1196 | if ((_PyTime_t)freq > _PyTime_MAX / SEC_TO_NS) { |
| 1197 | PyErr_SetString(PyExc_OverflowError, |
| 1198 | "_SC_CLK_TCK is too large"); |
| 1199 | return -1; |
| 1200 | } |
Serhiy Storchaka | bfe4fd5 | 2018-02-09 17:31:26 +0200 | [diff] [blame] | 1201 | #endif |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1202 | |
| 1203 | ticks_per_second = freq; |
| 1204 | } |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | if (ticks_per_second != -1) { |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1208 | if (info) { |
| 1209 | info->implementation = "times()"; |
Benjamin Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 1210 | info->monotonic = 1; |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 1211 | info->adjustable = 0; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1212 | info->resolution = 1.0 / (double)ticks_per_second; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1213 | } |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1214 | |
| 1215 | _PyTime_t total; |
| 1216 | total = _PyTime_MulDiv(t.tms_utime, SEC_TO_NS, ticks_per_second); |
| 1217 | total += _PyTime_MulDiv(t.tms_stime, SEC_TO_NS, ticks_per_second); |
| 1218 | *tp = total; |
| 1219 | return 0; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | #endif |
| 1223 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1224 | /* clock */ |
Victor Stinner | 53e22bf | 2016-07-08 17:55:01 +0200 | [diff] [blame] | 1225 | /* Currently, Python 3 requires clock() to build: see issue #22624 */ |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1226 | return _PyTime_GetClockWithInfo(tp, info); |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1227 | #endif |
| 1228 | } |
| 1229 | |
| 1230 | static PyObject * |
| 1231 | time_process_time(PyObject *self, PyObject *unused) |
| 1232 | { |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1233 | _PyTime_t t; |
| 1234 | if (_PyTime_GetProcessTimeWithInfo(&t, NULL) < 0) { |
| 1235 | return NULL; |
| 1236 | } |
| 1237 | return _PyFloat_FromPyTime(t); |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | PyDoc_STRVAR(process_time_doc, |
| 1241 | "process_time() -> float\n\ |
| 1242 | \n\ |
| 1243 | Process time for profiling: sum of the kernel and user-space CPU time."); |
| 1244 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1245 | static PyObject * |
| 1246 | time_process_time_ns(PyObject *self, PyObject *unused) |
| 1247 | { |
| 1248 | _PyTime_t t; |
| 1249 | if (_PyTime_GetProcessTimeWithInfo(&t, NULL) < 0) { |
| 1250 | return NULL; |
| 1251 | } |
| 1252 | return _PyTime_AsNanosecondsObject(t); |
| 1253 | } |
| 1254 | |
| 1255 | PyDoc_STRVAR(process_time_ns_doc, |
| 1256 | "process_time() -> int\n\ |
| 1257 | \n\ |
| 1258 | Process time for profiling as nanoseconds:\n\ |
| 1259 | sum of the kernel and user-space CPU time."); |
| 1260 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1261 | |
Antoine Pitrou | 4bd41c9 | 2017-11-15 22:52:21 +0100 | [diff] [blame] | 1262 | #if defined(MS_WINDOWS) |
| 1263 | #define HAVE_THREAD_TIME |
| 1264 | static int |
| 1265 | _PyTime_GetThreadTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info) |
| 1266 | { |
| 1267 | HANDLE thread; |
| 1268 | FILETIME creation_time, exit_time, kernel_time, user_time; |
| 1269 | ULARGE_INTEGER large; |
| 1270 | _PyTime_t ktime, utime, t; |
| 1271 | BOOL ok; |
| 1272 | |
| 1273 | thread = GetCurrentThread(); |
| 1274 | ok = GetThreadTimes(thread, &creation_time, &exit_time, |
| 1275 | &kernel_time, &user_time); |
| 1276 | if (!ok) { |
| 1277 | PyErr_SetFromWindowsErr(0); |
| 1278 | return -1; |
| 1279 | } |
| 1280 | |
| 1281 | if (info) { |
| 1282 | info->implementation = "GetThreadTimes()"; |
| 1283 | info->resolution = 1e-7; |
| 1284 | info->monotonic = 1; |
| 1285 | info->adjustable = 0; |
| 1286 | } |
| 1287 | |
| 1288 | large.u.LowPart = kernel_time.dwLowDateTime; |
| 1289 | large.u.HighPart = kernel_time.dwHighDateTime; |
| 1290 | ktime = large.QuadPart; |
| 1291 | |
| 1292 | large.u.LowPart = user_time.dwLowDateTime; |
| 1293 | large.u.HighPart = user_time.dwHighDateTime; |
| 1294 | utime = large.QuadPart; |
| 1295 | |
| 1296 | /* ktime and utime have a resolution of 100 nanoseconds */ |
| 1297 | t = _PyTime_FromNanoseconds((ktime + utime) * 100); |
| 1298 | *tp = t; |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
| 1302 | #elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID) |
| 1303 | #define HAVE_THREAD_TIME |
| 1304 | static int |
| 1305 | _PyTime_GetThreadTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info) |
| 1306 | { |
| 1307 | struct timespec ts; |
| 1308 | const clockid_t clk_id = CLOCK_THREAD_CPUTIME_ID; |
| 1309 | const char *function = "clock_gettime(CLOCK_THREAD_CPUTIME_ID)"; |
| 1310 | |
| 1311 | if (clock_gettime(clk_id, &ts)) { |
| 1312 | PyErr_SetFromErrno(PyExc_OSError); |
| 1313 | return -1; |
| 1314 | } |
| 1315 | if (info) { |
| 1316 | struct timespec res; |
| 1317 | info->implementation = function; |
| 1318 | info->monotonic = 1; |
| 1319 | info->adjustable = 0; |
| 1320 | if (clock_getres(clk_id, &res)) { |
| 1321 | PyErr_SetFromErrno(PyExc_OSError); |
| 1322 | return -1; |
| 1323 | } |
| 1324 | info->resolution = res.tv_sec + res.tv_nsec * 1e-9; |
| 1325 | } |
| 1326 | |
| 1327 | if (_PyTime_FromTimespec(tp, &ts) < 0) { |
| 1328 | return -1; |
| 1329 | } |
| 1330 | return 0; |
| 1331 | } |
| 1332 | #endif |
| 1333 | |
| 1334 | #ifdef HAVE_THREAD_TIME |
| 1335 | static PyObject * |
| 1336 | time_thread_time(PyObject *self, PyObject *unused) |
| 1337 | { |
| 1338 | _PyTime_t t; |
| 1339 | if (_PyTime_GetThreadTimeWithInfo(&t, NULL) < 0) { |
| 1340 | return NULL; |
| 1341 | } |
| 1342 | return _PyFloat_FromPyTime(t); |
| 1343 | } |
| 1344 | |
| 1345 | PyDoc_STRVAR(thread_time_doc, |
| 1346 | "thread_time() -> float\n\ |
| 1347 | \n\ |
| 1348 | Thread time for profiling: sum of the kernel and user-space CPU time."); |
| 1349 | |
| 1350 | static PyObject * |
| 1351 | time_thread_time_ns(PyObject *self, PyObject *unused) |
| 1352 | { |
| 1353 | _PyTime_t t; |
| 1354 | if (_PyTime_GetThreadTimeWithInfo(&t, NULL) < 0) { |
| 1355 | return NULL; |
| 1356 | } |
| 1357 | return _PyTime_AsNanosecondsObject(t); |
| 1358 | } |
| 1359 | |
| 1360 | PyDoc_STRVAR(thread_time_ns_doc, |
| 1361 | "thread_time() -> int\n\ |
| 1362 | \n\ |
| 1363 | Thread time for profiling as nanoseconds:\n\ |
| 1364 | sum of the kernel and user-space CPU time."); |
| 1365 | #endif |
| 1366 | |
| 1367 | |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1368 | static PyObject * |
| 1369 | time_get_clock_info(PyObject *self, PyObject *args) |
| 1370 | { |
| 1371 | char *name; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1372 | _Py_clock_info_t info; |
Victor Stinner | bda4b88 | 2012-06-12 22:11:44 +0200 | [diff] [blame] | 1373 | PyObject *obj = NULL, *dict, *ns; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1374 | _PyTime_t t; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1375 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1376 | if (!PyArg_ParseTuple(args, "s:get_clock_info", &name)) { |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1377 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1378 | } |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1379 | |
| 1380 | #ifdef Py_DEBUG |
| 1381 | info.implementation = NULL; |
Benjamin Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 1382 | info.monotonic = -1; |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 1383 | info.adjustable = -1; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1384 | info.resolution = -1.0; |
| 1385 | #else |
| 1386 | info.implementation = ""; |
Benjamin Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 1387 | info.monotonic = 0; |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 1388 | info.adjustable = 0; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1389 | info.resolution = 1.0; |
| 1390 | #endif |
| 1391 | |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1392 | if (strcmp(name, "time") == 0) { |
| 1393 | if (_PyTime_GetSystemClockWithInfo(&t, &info) < 0) { |
| 1394 | return NULL; |
| 1395 | } |
| 1396 | } |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1397 | #ifdef PYCLOCK |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1398 | else if (strcmp(name, "clock") == 0) { |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1399 | obj = pyclock(&info); |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1400 | if (obj == NULL) { |
| 1401 | return NULL; |
| 1402 | } |
| 1403 | Py_DECREF(obj); |
| 1404 | } |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1405 | #endif |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1406 | else if (strcmp(name, "monotonic") == 0) { |
| 1407 | if (_PyTime_GetMonotonicClockWithInfo(&t, &info) < 0) { |
| 1408 | return NULL; |
| 1409 | } |
| 1410 | } |
| 1411 | else if (strcmp(name, "perf_counter") == 0) { |
| 1412 | if (_PyTime_GetPerfCounterWithInfo(&t, &info) < 0) { |
| 1413 | return NULL; |
| 1414 | } |
| 1415 | } |
| 1416 | else if (strcmp(name, "process_time") == 0) { |
| 1417 | if (_PyTime_GetProcessTimeWithInfo(&t, &info) < 0) { |
| 1418 | return NULL; |
| 1419 | } |
| 1420 | } |
Antoine Pitrou | 4bd41c9 | 2017-11-15 22:52:21 +0100 | [diff] [blame] | 1421 | #ifdef HAVE_THREAD_TIME |
| 1422 | else if (strcmp(name, "thread_time") == 0) { |
| 1423 | if (_PyTime_GetThreadTimeWithInfo(&t, &info) < 0) { |
| 1424 | return NULL; |
| 1425 | } |
| 1426 | } |
| 1427 | #endif |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1428 | else { |
| 1429 | PyErr_SetString(PyExc_ValueError, "unknown clock"); |
| 1430 | return NULL; |
| 1431 | } |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1432 | |
Victor Stinner | bda4b88 | 2012-06-12 22:11:44 +0200 | [diff] [blame] | 1433 | dict = PyDict_New(); |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1434 | if (dict == NULL) { |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1435 | return NULL; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1436 | } |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1437 | |
| 1438 | assert(info.implementation != NULL); |
| 1439 | obj = PyUnicode_FromString(info.implementation); |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1440 | if (obj == NULL) { |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1441 | goto error; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1442 | } |
| 1443 | if (PyDict_SetItemString(dict, "implementation", obj) == -1) { |
Victor Stinner | bda4b88 | 2012-06-12 22:11:44 +0200 | [diff] [blame] | 1444 | goto error; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1445 | } |
Victor Stinner | bda4b88 | 2012-06-12 22:11:44 +0200 | [diff] [blame] | 1446 | Py_CLEAR(obj); |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1447 | |
Benjamin Peterson | 49a69e4 | 2012-05-01 09:38:34 -0400 | [diff] [blame] | 1448 | assert(info.monotonic != -1); |
| 1449 | obj = PyBool_FromLong(info.monotonic); |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1450 | if (obj == NULL) { |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1451 | goto error; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1452 | } |
| 1453 | if (PyDict_SetItemString(dict, "monotonic", obj) == -1) { |
Victor Stinner | bda4b88 | 2012-06-12 22:11:44 +0200 | [diff] [blame] | 1454 | goto error; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1455 | } |
Victor Stinner | bda4b88 | 2012-06-12 22:11:44 +0200 | [diff] [blame] | 1456 | Py_CLEAR(obj); |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1457 | |
Victor Stinner | 2b89fdf | 2012-06-12 22:46:37 +0200 | [diff] [blame] | 1458 | assert(info.adjustable != -1); |
| 1459 | obj = PyBool_FromLong(info.adjustable); |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1460 | if (obj == NULL) { |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1461 | goto error; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1462 | } |
| 1463 | if (PyDict_SetItemString(dict, "adjustable", obj) == -1) { |
Victor Stinner | bda4b88 | 2012-06-12 22:11:44 +0200 | [diff] [blame] | 1464 | goto error; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1465 | } |
Victor Stinner | bda4b88 | 2012-06-12 22:11:44 +0200 | [diff] [blame] | 1466 | Py_CLEAR(obj); |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1467 | |
| 1468 | assert(info.resolution > 0.0); |
| 1469 | assert(info.resolution <= 1.0); |
| 1470 | obj = PyFloat_FromDouble(info.resolution); |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1471 | if (obj == NULL) { |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1472 | goto error; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1473 | } |
| 1474 | if (PyDict_SetItemString(dict, "resolution", obj) == -1) { |
Victor Stinner | bda4b88 | 2012-06-12 22:11:44 +0200 | [diff] [blame] | 1475 | goto error; |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1476 | } |
Victor Stinner | bda4b88 | 2012-06-12 22:11:44 +0200 | [diff] [blame] | 1477 | Py_CLEAR(obj); |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1478 | |
Victor Stinner | bda4b88 | 2012-06-12 22:11:44 +0200 | [diff] [blame] | 1479 | ns = _PyNamespace_New(dict); |
| 1480 | Py_DECREF(dict); |
| 1481 | return ns; |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1482 | |
| 1483 | error: |
Victor Stinner | bda4b88 | 2012-06-12 22:11:44 +0200 | [diff] [blame] | 1484 | Py_DECREF(dict); |
| 1485 | Py_XDECREF(obj); |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1486 | return NULL; |
| 1487 | } |
| 1488 | |
| 1489 | PyDoc_STRVAR(get_clock_info_doc, |
| 1490 | "get_clock_info(name: str) -> dict\n\ |
| 1491 | \n\ |
| 1492 | Get information of the specified clock."); |
Victor Stinner | 8b30201 | 2012-02-07 23:29:46 +0100 | [diff] [blame] | 1493 | |
Victor Stinner | 8f5cdfa | 2017-04-20 13:41:09 +0200 | [diff] [blame] | 1494 | #if !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__) |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1495 | static void |
Alexander Belopolsky | 18f3a9b | 2016-09-11 22:55:16 -0400 | [diff] [blame] | 1496 | get_zone(char *zone, int n, struct tm *p) |
| 1497 | { |
| 1498 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
| 1499 | strncpy(zone, p->tm_zone ? p->tm_zone : " ", n); |
| 1500 | #else |
| 1501 | tzset(); |
| 1502 | strftime(zone, n, "%Z", p); |
| 1503 | #endif |
| 1504 | } |
| 1505 | |
| 1506 | static int |
| 1507 | get_gmtoff(time_t t, struct tm *p) |
| 1508 | { |
| 1509 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
| 1510 | return p->tm_gmtoff; |
| 1511 | #else |
| 1512 | return timegm(p) - t; |
| 1513 | #endif |
| 1514 | } |
Victor Stinner | 8f5cdfa | 2017-04-20 13:41:09 +0200 | [diff] [blame] | 1515 | #endif /* !defined(HAVE_TZNAME) || defined(__GLIBC__) || defined(__CYGWIN__) */ |
Alexander Belopolsky | 18f3a9b | 2016-09-11 22:55:16 -0400 | [diff] [blame] | 1516 | |
| 1517 | static void |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1518 | PyInit_timezone(PyObject *m) { |
| 1519 | /* This code moved from PyInit_time wholesale to allow calling it from |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1520 | time_tzset. In the future, some parts of it can be moved back |
| 1521 | (for platforms that don't HAVE_WORKING_TZSET, when we know what they |
| 1522 | are), and the extraneous calls to tzset(3) should be removed. |
| 1523 | I haven't done this yet, as I don't want to change this code as |
| 1524 | little as possible when introducing the time.tzset and time.tzsetwall |
| 1525 | methods. This should simply be a method of doing the following once, |
| 1526 | at the top of this function and removing the call to tzset() from |
| 1527 | time_tzset(): |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1528 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1529 | #ifdef HAVE_TZSET |
| 1530 | tzset() |
| 1531 | #endif |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | And I'm lazy and hate C so nyer. |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1534 | */ |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 1535 | #if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1536 | PyObject *otz0, *otz1; |
| 1537 | tzset(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | PyModule_AddIntConstant(m, "timezone", timezone); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1539 | #ifdef HAVE_ALTZONE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 | PyModule_AddIntConstant(m, "altzone", altzone); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1541 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | PyModule_AddIntConstant(m, "altzone", timezone-3600); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1543 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | PyModule_AddIntConstant(m, "daylight", daylight); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 1545 | otz0 = PyUnicode_DecodeLocale(tzname[0], "surrogateescape"); |
| 1546 | otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1547 | PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1)); |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 1548 | #else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | { |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1550 | #define YEAR ((time_t)((365 * 24 + 6) * 3600)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1551 | time_t t; |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 1552 | struct tm p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1553 | long janzone, julyzone; |
| 1554 | char janname[10], julyname[10]; |
| 1555 | t = (time((time_t *)0) / YEAR) * YEAR; |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 1556 | _PyTime_localtime(t, &p); |
| 1557 | get_zone(janname, 9, &p); |
| 1558 | janzone = -get_gmtoff(t, &p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1559 | janname[9] = '\0'; |
| 1560 | t += YEAR/2; |
Alexander Belopolsky | 3e7a3cb | 2016-09-28 17:31:35 -0400 | [diff] [blame] | 1561 | _PyTime_localtime(t, &p); |
| 1562 | get_zone(julyname, 9, &p); |
| 1563 | julyzone = -get_gmtoff(t, &p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | julyname[9] = '\0'; |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 1565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | if( janzone < julyzone ) { |
| 1567 | /* DST is reversed in the southern hemisphere */ |
| 1568 | PyModule_AddIntConstant(m, "timezone", julyzone); |
| 1569 | PyModule_AddIntConstant(m, "altzone", janzone); |
| 1570 | PyModule_AddIntConstant(m, "daylight", |
| 1571 | janzone != julyzone); |
| 1572 | PyModule_AddObject(m, "tzname", |
| 1573 | Py_BuildValue("(zz)", |
| 1574 | julyname, janname)); |
| 1575 | } else { |
| 1576 | PyModule_AddIntConstant(m, "timezone", janzone); |
| 1577 | PyModule_AddIntConstant(m, "altzone", julyzone); |
| 1578 | PyModule_AddIntConstant(m, "daylight", |
| 1579 | janzone != julyzone); |
| 1580 | PyModule_AddObject(m, "tzname", |
| 1581 | Py_BuildValue("(zz)", |
| 1582 | janname, julyname)); |
| 1583 | } |
| 1584 | } |
Tim Peters | 26ae7cd | 2001-03-20 03:26:49 +0000 | [diff] [blame] | 1585 | #ifdef __CYGWIN__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1586 | tzset(); |
| 1587 | PyModule_AddIntConstant(m, "timezone", _timezone); |
| 1588 | PyModule_AddIntConstant(m, "altzone", _timezone-3600); |
| 1589 | PyModule_AddIntConstant(m, "daylight", _daylight); |
| 1590 | PyModule_AddObject(m, "tzname", |
| 1591 | Py_BuildValue("(zz)", _tzname[0], _tzname[1])); |
Tim Peters | 26ae7cd | 2001-03-20 03:26:49 +0000 | [diff] [blame] | 1592 | #endif /* __CYGWIN__ */ |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 1593 | #endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | |
| 1597 | static PyMethodDef time_methods[] = { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1598 | {"time", time_time, METH_NOARGS, time_doc}, |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1599 | {"time_ns", time_time_ns, METH_NOARGS, time_ns_doc}, |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1600 | #ifdef PYCLOCK |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1601 | {"clock", time_clock, METH_NOARGS, clock_doc}, |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1602 | #endif |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 1603 | #ifdef HAVE_CLOCK_GETTIME |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1604 | {"clock_gettime", time_clock_gettime, METH_VARARGS, clock_gettime_doc}, |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1605 | {"clock_gettime_ns",time_clock_gettime_ns, METH_VARARGS, clock_gettime_ns_doc}, |
Benjamin Peterson | 37098cd | 2016-09-13 22:55:09 -0700 | [diff] [blame] | 1606 | #endif |
| 1607 | #ifdef HAVE_CLOCK_SETTIME |
Victor Stinner | 30d7947 | 2012-04-03 00:45:07 +0200 | [diff] [blame] | 1608 | {"clock_settime", time_clock_settime, METH_VARARGS, clock_settime_doc}, |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1609 | {"clock_settime_ns",time_clock_settime_ns, METH_VARARGS, clock_settime_ns_doc}, |
Benjamin Peterson | 37098cd | 2016-09-13 22:55:09 -0700 | [diff] [blame] | 1610 | #endif |
| 1611 | #ifdef HAVE_CLOCK_GETRES |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1612 | {"clock_getres", time_clock_getres, METH_VARARGS, clock_getres_doc}, |
Victor Stinner | e0be423 | 2011-10-25 13:06:09 +0200 | [diff] [blame] | 1613 | #endif |
pdox | e14679c | 2017-10-05 00:01:56 -0700 | [diff] [blame] | 1614 | #ifdef HAVE_PTHREAD_GETCPUCLOCKID |
| 1615 | {"pthread_getcpuclockid", time_pthread_getcpuclockid, METH_VARARGS, pthread_getcpuclockid_doc}, |
| 1616 | #endif |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 1617 | {"sleep", time_sleep, METH_O, sleep_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1618 | {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc}, |
| 1619 | {"localtime", time_localtime, METH_VARARGS, localtime_doc}, |
| 1620 | {"asctime", time_asctime, METH_VARARGS, asctime_doc}, |
| 1621 | {"ctime", time_ctime, METH_VARARGS, ctime_doc}, |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1622 | #ifdef HAVE_MKTIME |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 1623 | {"mktime", time_mktime, METH_O, mktime_doc}, |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1624 | #endif |
| 1625 | #ifdef HAVE_STRFTIME |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1626 | {"strftime", time_strftime, METH_VARARGS, strftime_doc}, |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1627 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1628 | {"strptime", time_strptime, METH_VARARGS, strptime_doc}, |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1629 | #ifdef HAVE_WORKING_TZSET |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1630 | {"tzset", time_tzset, METH_NOARGS, tzset_doc}, |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1631 | #endif |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1632 | {"monotonic", time_monotonic, METH_NOARGS, monotonic_doc}, |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1633 | {"monotonic_ns", time_monotonic_ns, METH_NOARGS, monotonic_ns_doc}, |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1634 | {"process_time", time_process_time, METH_NOARGS, process_time_doc}, |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1635 | {"process_time_ns", time_process_time_ns, METH_NOARGS, process_time_ns_doc}, |
Antoine Pitrou | 4bd41c9 | 2017-11-15 22:52:21 +0100 | [diff] [blame] | 1636 | #ifdef HAVE_THREAD_TIME |
| 1637 | {"thread_time", time_thread_time, METH_NOARGS, thread_time_doc}, |
| 1638 | {"thread_time_ns", time_thread_time_ns, METH_NOARGS, thread_time_ns_doc}, |
| 1639 | #endif |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1640 | {"perf_counter", time_perf_counter, METH_NOARGS, perf_counter_doc}, |
Victor Stinner | c29b585 | 2017-11-02 07:28:27 -0700 | [diff] [blame] | 1641 | {"perf_counter_ns", time_perf_counter_ns, METH_NOARGS, perf_counter_ns_doc}, |
Victor Stinner | ec89539 | 2012-04-29 02:41:27 +0200 | [diff] [blame] | 1642 | {"get_clock_info", time_get_clock_info, METH_VARARGS, get_clock_info_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1643 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1644 | }; |
| 1645 | |
| 1646 | |
| 1647 | PyDoc_STRVAR(module_doc, |
| 1648 | "This module provides various functions to manipulate time values.\n\ |
| 1649 | \n\ |
| 1650 | There are two standard representations of time. One is the number\n\ |
| 1651 | of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\ |
| 1652 | or a floating point number (to represent fractions of seconds).\n\ |
| 1653 | The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\ |
| 1654 | The actual value can be retrieved by calling gmtime(0).\n\ |
| 1655 | \n\ |
| 1656 | The other representation is a tuple of 9 integers giving local time.\n\ |
| 1657 | The tuple items are:\n\ |
Alexander Belopolsky | 03163ac | 2011-05-02 12:20:52 -0400 | [diff] [blame] | 1658 | year (including century, e.g. 1998)\n\ |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1659 | month (1-12)\n\ |
| 1660 | day (1-31)\n\ |
| 1661 | hours (0-23)\n\ |
| 1662 | minutes (0-59)\n\ |
| 1663 | seconds (0-59)\n\ |
| 1664 | weekday (0-6, Monday is 0)\n\ |
| 1665 | Julian day (day in the year, 1-366)\n\ |
| 1666 | DST (Daylight Savings Time) flag (-1, 0 or 1)\n\ |
| 1667 | If the DST flag is 0, the time is given in the regular time zone;\n\ |
| 1668 | if it is 1, the time is given in the DST time zone;\n\ |
Cheryl Sabella | 703ff38 | 2017-10-11 09:29:14 -0400 | [diff] [blame] | 1669 | if it is -1, mktime() should guess based on the date and time.\n"); |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1670 | |
| 1671 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1672 | |
| 1673 | static struct PyModuleDef timemodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1674 | PyModuleDef_HEAD_INIT, |
| 1675 | "time", |
| 1676 | module_doc, |
| 1677 | -1, |
| 1678 | time_methods, |
| 1679 | NULL, |
| 1680 | NULL, |
| 1681 | NULL, |
| 1682 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1683 | }; |
| 1684 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1685 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1686 | PyInit_time(void) |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1687 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1688 | PyObject *m; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | m = PyModule_Create(&timemodule); |
| 1690 | if (m == NULL) |
| 1691 | return NULL; |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1693 | /* Set, or reset, module variables like time.timezone */ |
| 1694 | PyInit_timezone(m); |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 1695 | |
Benjamin Peterson | 37098cd | 2016-09-13 22:55:09 -0700 | [diff] [blame] | 1696 | #ifdef CLOCK_REALTIME |
Alexander Belopolsky | 18f3a9b | 2016-09-11 22:55:16 -0400 | [diff] [blame] | 1697 | PyModule_AddIntMacro(m, CLOCK_REALTIME); |
Benjamin Peterson | 37098cd | 2016-09-13 22:55:09 -0700 | [diff] [blame] | 1698 | #endif |
Alexander Belopolsky | 18f3a9b | 2016-09-11 22:55:16 -0400 | [diff] [blame] | 1699 | #ifdef CLOCK_MONOTONIC |
| 1700 | PyModule_AddIntMacro(m, CLOCK_MONOTONIC); |
| 1701 | #endif |
| 1702 | #ifdef CLOCK_MONOTONIC_RAW |
| 1703 | PyModule_AddIntMacro(m, CLOCK_MONOTONIC_RAW); |
| 1704 | #endif |
| 1705 | #ifdef CLOCK_HIGHRES |
| 1706 | PyModule_AddIntMacro(m, CLOCK_HIGHRES); |
| 1707 | #endif |
| 1708 | #ifdef CLOCK_PROCESS_CPUTIME_ID |
| 1709 | PyModule_AddIntMacro(m, CLOCK_PROCESS_CPUTIME_ID); |
| 1710 | #endif |
| 1711 | #ifdef CLOCK_THREAD_CPUTIME_ID |
| 1712 | PyModule_AddIntMacro(m, CLOCK_THREAD_CPUTIME_ID); |
| 1713 | #endif |
Victor Stinner | a64ce97 | 2017-11-02 04:19:19 -0700 | [diff] [blame] | 1714 | #ifdef CLOCK_PROF |
| 1715 | PyModule_AddIntMacro(m, CLOCK_PROF); |
| 1716 | #endif |
| 1717 | #ifdef CLOCK_BOOTTIME |
| 1718 | PyModule_AddIntMacro(m, CLOCK_BOOTTIME); |
| 1719 | #endif |
| 1720 | #ifdef CLOCK_UPTIME |
| 1721 | PyModule_AddIntMacro(m, CLOCK_UPTIME); |
| 1722 | #endif |
Alexander Belopolsky | 18f3a9b | 2016-09-11 22:55:16 -0400 | [diff] [blame] | 1723 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1724 | if (!initialized) { |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 1725 | if (PyStructSequence_InitType2(&StructTimeType, |
| 1726 | &struct_time_type_desc) < 0) |
| 1727 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1728 | } |
| 1729 | Py_INCREF(&StructTimeType); |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 1730 | PyModule_AddIntConstant(m, "_STRUCT_TM_ITEMS", 11); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1731 | PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType); |
| 1732 | initialized = 1; |
| 1733 | return m; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 1736 | /* Implement pysleep() for various platforms. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1737 | When interrupted (or when another error occurs), return -1 and |
| 1738 | set an exception; else return 0. */ |
| 1739 | |
| 1740 | static int |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 1741 | pysleep(_PyTime_t secs) |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 1742 | { |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 1743 | _PyTime_t deadline, monotonic; |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 1744 | #ifndef MS_WINDOWS |
| 1745 | struct timeval timeout; |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 1746 | int err = 0; |
| 1747 | #else |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 1748 | _PyTime_t millisecs; |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 1749 | unsigned long ul_millis; |
| 1750 | DWORD rc; |
| 1751 | HANDLE hInterruptEvent; |
Victor Stinner | 0c2fd89 | 2015-03-17 10:49:17 +0100 | [diff] [blame] | 1752 | #endif |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 1753 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 1754 | deadline = _PyTime_GetMonotonicClock() + secs; |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 1755 | |
| 1756 | do { |
| 1757 | #ifndef MS_WINDOWS |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 1758 | if (_PyTime_AsTimeval(secs, &timeout, _PyTime_ROUND_CEILING) < 0) |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 1759 | return -1; |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 1760 | |
| 1761 | Py_BEGIN_ALLOW_THREADS |
| 1762 | err = select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout); |
| 1763 | Py_END_ALLOW_THREADS |
| 1764 | |
| 1765 | if (err == 0) |
| 1766 | break; |
| 1767 | |
| 1768 | if (errno != EINTR) { |
Victor Stinner | 0c2fd89 | 2015-03-17 10:49:17 +0100 | [diff] [blame] | 1769 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1770 | return -1; |
| 1771 | } |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 1772 | #else |
Victor Stinner | 869e177 | 2015-03-30 03:49:14 +0200 | [diff] [blame] | 1773 | millisecs = _PyTime_AsMilliseconds(secs, _PyTime_ROUND_CEILING); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1774 | if (millisecs > (double)ULONG_MAX) { |
| 1775 | PyErr_SetString(PyExc_OverflowError, |
| 1776 | "sleep length is too large"); |
| 1777 | return -1; |
| 1778 | } |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 1779 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1780 | /* Allow sleep(0) to maintain win32 semantics, and as decreed |
| 1781 | * by Guido, only the main thread can be interrupted. |
| 1782 | */ |
| 1783 | ul_millis = (unsigned long)millisecs; |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 1784 | if (ul_millis == 0 || !_PyOS_IsMainThread()) { |
| 1785 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 0eac130 | 2015-03-20 03:06:12 +0100 | [diff] [blame] | 1786 | Sleep(ul_millis); |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 1787 | Py_END_ALLOW_THREADS |
| 1788 | break; |
Victor Stinner | 945c82e | 2015-03-12 16:19:01 +0100 | [diff] [blame] | 1789 | } |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 1790 | |
| 1791 | hInterruptEvent = _PyOS_SigintEvent(); |
| 1792 | ResetEvent(hInterruptEvent); |
| 1793 | |
| 1794 | Py_BEGIN_ALLOW_THREADS |
| 1795 | rc = WaitForSingleObjectEx(hInterruptEvent, ul_millis, FALSE); |
Victor Stinner | 0c2fd89 | 2015-03-17 10:49:17 +0100 | [diff] [blame] | 1796 | Py_END_ALLOW_THREADS |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 1797 | |
| 1798 | if (rc != WAIT_OBJECT_0) |
| 1799 | break; |
Victor Stinner | 945c82e | 2015-03-12 16:19:01 +0100 | [diff] [blame] | 1800 | #endif |
Victor Stinner | 0c2fd89 | 2015-03-17 10:49:17 +0100 | [diff] [blame] | 1801 | |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 1802 | /* sleep was interrupted by SIGINT */ |
| 1803 | if (PyErr_CheckSignals()) |
| 1804 | return -1; |
| 1805 | |
Victor Stinner | cb29f01 | 2015-03-27 13:31:18 +0100 | [diff] [blame] | 1806 | monotonic = _PyTime_GetMonotonicClock(); |
| 1807 | secs = deadline - monotonic; |
Victor Stinner | 6aa446c | 2015-03-30 21:33:51 +0200 | [diff] [blame] | 1808 | if (secs < 0) |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 1809 | break; |
| 1810 | /* retry with the recomputed delay */ |
| 1811 | } while (1); |
| 1812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1813 | return 0; |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 1814 | } |