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