| 1 | n/a | /* Time module */ |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | #include "Python.h" |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | #include <ctype.h> |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | #ifdef HAVE_SYS_TIMES_H |
|---|
| 8 | n/a | #include <sys/times.h> |
|---|
| 9 | n/a | #endif |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | #ifdef HAVE_SYS_TYPES_H |
|---|
| 12 | n/a | #include <sys/types.h> |
|---|
| 13 | n/a | #endif |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | #if defined(HAVE_SYS_RESOURCE_H) |
|---|
| 16 | n/a | #include <sys/resource.h> |
|---|
| 17 | n/a | #endif |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | #ifdef QUICKWIN |
|---|
| 20 | n/a | #include <io.h> |
|---|
| 21 | n/a | #endif |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | #if defined(__WATCOMC__) && !defined(__QNX__) |
|---|
| 24 | n/a | #include <i86.h> |
|---|
| 25 | n/a | #else |
|---|
| 26 | n/a | #ifdef MS_WINDOWS |
|---|
| 27 | n/a | #define WIN32_LEAN_AND_MEAN |
|---|
| 28 | n/a | #include <windows.h> |
|---|
| 29 | n/a | #include "pythread.h" |
|---|
| 30 | n/a | #endif /* MS_WINDOWS */ |
|---|
| 31 | n/a | #endif /* !__WATCOMC__ || __QNX__ */ |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | /* Forward declarations */ |
|---|
| 34 | n/a | static int pysleep(_PyTime_t); |
|---|
| 35 | n/a | static PyObject* floattime(_Py_clock_info_t *info); |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | static PyObject * |
|---|
| 38 | n/a | time_time(PyObject *self, PyObject *unused) |
|---|
| 39 | n/a | { |
|---|
| 40 | n/a | return floattime(NULL); |
|---|
| 41 | n/a | } |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | PyDoc_STRVAR(time_doc, |
|---|
| 44 | n/a | "time() -> floating point number\n\ |
|---|
| 45 | n/a | \n\ |
|---|
| 46 | n/a | Return the current time in seconds since the Epoch.\n\ |
|---|
| 47 | n/a | Fractions of a second may be present if the system clock provides them."); |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | #if defined(HAVE_CLOCK) |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | #ifndef CLOCKS_PER_SEC |
|---|
| 52 | n/a | #ifdef CLK_TCK |
|---|
| 53 | n/a | #define CLOCKS_PER_SEC CLK_TCK |
|---|
| 54 | n/a | #else |
|---|
| 55 | n/a | #define CLOCKS_PER_SEC 1000000 |
|---|
| 56 | n/a | #endif |
|---|
| 57 | n/a | #endif |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | static PyObject * |
|---|
| 60 | n/a | floatclock(_Py_clock_info_t *info) |
|---|
| 61 | n/a | { |
|---|
| 62 | n/a | clock_t value; |
|---|
| 63 | n/a | value = clock(); |
|---|
| 64 | n/a | if (value == (clock_t)-1) { |
|---|
| 65 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 66 | n/a | "the processor time used is not available " |
|---|
| 67 | n/a | "or its value cannot be represented"); |
|---|
| 68 | n/a | return NULL; |
|---|
| 69 | n/a | } |
|---|
| 70 | n/a | if (info) { |
|---|
| 71 | n/a | info->implementation = "clock()"; |
|---|
| 72 | n/a | info->resolution = 1.0 / (double)CLOCKS_PER_SEC; |
|---|
| 73 | n/a | info->monotonic = 1; |
|---|
| 74 | n/a | info->adjustable = 0; |
|---|
| 75 | n/a | } |
|---|
| 76 | n/a | return PyFloat_FromDouble((double)value / CLOCKS_PER_SEC); |
|---|
| 77 | n/a | } |
|---|
| 78 | n/a | #endif /* HAVE_CLOCK */ |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | #ifdef MS_WINDOWS |
|---|
| 81 | n/a | #define WIN32_PERF_COUNTER |
|---|
| 82 | n/a | /* Win32 has better clock replacement; we have our own version, due to Mark |
|---|
| 83 | n/a | Hammond and Tim Peters */ |
|---|
| 84 | n/a | static PyObject* |
|---|
| 85 | n/a | win_perf_counter(_Py_clock_info_t *info) |
|---|
| 86 | n/a | { |
|---|
| 87 | n/a | static LONGLONG cpu_frequency = 0; |
|---|
| 88 | n/a | static LONGLONG ctrStart; |
|---|
| 89 | n/a | LARGE_INTEGER now; |
|---|
| 90 | n/a | double diff; |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | if (cpu_frequency == 0) { |
|---|
| 93 | n/a | LARGE_INTEGER freq; |
|---|
| 94 | n/a | QueryPerformanceCounter(&now); |
|---|
| 95 | n/a | ctrStart = now.QuadPart; |
|---|
| 96 | n/a | if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) { |
|---|
| 97 | n/a | PyErr_SetFromWindowsErr(0); |
|---|
| 98 | n/a | return NULL; |
|---|
| 99 | n/a | } |
|---|
| 100 | n/a | cpu_frequency = freq.QuadPart; |
|---|
| 101 | n/a | } |
|---|
| 102 | n/a | QueryPerformanceCounter(&now); |
|---|
| 103 | n/a | diff = (double)(now.QuadPart - ctrStart); |
|---|
| 104 | n/a | if (info) { |
|---|
| 105 | n/a | info->implementation = "QueryPerformanceCounter()"; |
|---|
| 106 | n/a | info->resolution = 1.0 / (double)cpu_frequency; |
|---|
| 107 | n/a | info->monotonic = 1; |
|---|
| 108 | n/a | info->adjustable = 0; |
|---|
| 109 | n/a | } |
|---|
| 110 | n/a | return PyFloat_FromDouble(diff / (double)cpu_frequency); |
|---|
| 111 | n/a | } |
|---|
| 112 | n/a | #endif /* MS_WINDOWS */ |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | #if defined(WIN32_PERF_COUNTER) || defined(HAVE_CLOCK) |
|---|
| 115 | n/a | #define PYCLOCK |
|---|
| 116 | n/a | static PyObject* |
|---|
| 117 | n/a | pyclock(_Py_clock_info_t *info) |
|---|
| 118 | n/a | { |
|---|
| 119 | n/a | #ifdef WIN32_PERF_COUNTER |
|---|
| 120 | n/a | return win_perf_counter(info); |
|---|
| 121 | n/a | #else |
|---|
| 122 | n/a | return floatclock(info); |
|---|
| 123 | n/a | #endif |
|---|
| 124 | n/a | } |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | static PyObject * |
|---|
| 127 | n/a | time_clock(PyObject *self, PyObject *unused) |
|---|
| 128 | n/a | { |
|---|
| 129 | n/a | return pyclock(NULL); |
|---|
| 130 | n/a | } |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | PyDoc_STRVAR(clock_doc, |
|---|
| 133 | n/a | "clock() -> floating point number\n\ |
|---|
| 134 | n/a | \n\ |
|---|
| 135 | n/a | Return the CPU time or real time since the start of the process or since\n\ |
|---|
| 136 | n/a | the first call to clock(). This has as much precision as the system\n\ |
|---|
| 137 | n/a | records."); |
|---|
| 138 | n/a | #endif |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | #ifdef HAVE_CLOCK_GETTIME |
|---|
| 141 | n/a | static PyObject * |
|---|
| 142 | n/a | time_clock_gettime(PyObject *self, PyObject *args) |
|---|
| 143 | n/a | { |
|---|
| 144 | n/a | int ret; |
|---|
| 145 | n/a | int clk_id; |
|---|
| 146 | n/a | struct timespec tp; |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | if (!PyArg_ParseTuple(args, "i:clock_gettime", &clk_id)) |
|---|
| 149 | n/a | return NULL; |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | ret = clock_gettime((clockid_t)clk_id, &tp); |
|---|
| 152 | n/a | if (ret != 0) { |
|---|
| 153 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
|---|
| 154 | n/a | return NULL; |
|---|
| 155 | n/a | } |
|---|
| 156 | n/a | return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); |
|---|
| 157 | n/a | } |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | PyDoc_STRVAR(clock_gettime_doc, |
|---|
| 160 | n/a | "clock_gettime(clk_id) -> floating point number\n\ |
|---|
| 161 | n/a | \n\ |
|---|
| 162 | n/a | Return the time of the specified clock clk_id."); |
|---|
| 163 | n/a | #endif /* HAVE_CLOCK_GETTIME */ |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | #ifdef HAVE_CLOCK_SETTIME |
|---|
| 166 | n/a | static PyObject * |
|---|
| 167 | n/a | time_clock_settime(PyObject *self, PyObject *args) |
|---|
| 168 | n/a | { |
|---|
| 169 | n/a | int clk_id; |
|---|
| 170 | n/a | PyObject *obj; |
|---|
| 171 | n/a | _PyTime_t t; |
|---|
| 172 | n/a | struct timespec tp; |
|---|
| 173 | n/a | int ret; |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | if (!PyArg_ParseTuple(args, "iO:clock_settime", &clk_id, &obj)) |
|---|
| 176 | n/a | return NULL; |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | if (_PyTime_FromSecondsObject(&t, obj, _PyTime_ROUND_FLOOR) < 0) |
|---|
| 179 | n/a | return NULL; |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | if (_PyTime_AsTimespec(t, &tp) == -1) |
|---|
| 182 | n/a | return NULL; |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | ret = clock_settime((clockid_t)clk_id, &tp); |
|---|
| 185 | n/a | if (ret != 0) { |
|---|
| 186 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
|---|
| 187 | n/a | return NULL; |
|---|
| 188 | n/a | } |
|---|
| 189 | n/a | Py_RETURN_NONE; |
|---|
| 190 | n/a | } |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | PyDoc_STRVAR(clock_settime_doc, |
|---|
| 193 | n/a | "clock_settime(clk_id, time)\n\ |
|---|
| 194 | n/a | \n\ |
|---|
| 195 | n/a | Set the time of the specified clock clk_id."); |
|---|
| 196 | n/a | #endif /* HAVE_CLOCK_SETTIME */ |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | #ifdef HAVE_CLOCK_GETRES |
|---|
| 199 | n/a | static PyObject * |
|---|
| 200 | n/a | time_clock_getres(PyObject *self, PyObject *args) |
|---|
| 201 | n/a | { |
|---|
| 202 | n/a | int ret; |
|---|
| 203 | n/a | int clk_id; |
|---|
| 204 | n/a | struct timespec tp; |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | if (!PyArg_ParseTuple(args, "i:clock_getres", &clk_id)) |
|---|
| 207 | n/a | return NULL; |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | ret = clock_getres((clockid_t)clk_id, &tp); |
|---|
| 210 | n/a | if (ret != 0) { |
|---|
| 211 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
|---|
| 212 | n/a | return NULL; |
|---|
| 213 | n/a | } |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); |
|---|
| 216 | n/a | } |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | PyDoc_STRVAR(clock_getres_doc, |
|---|
| 219 | n/a | "clock_getres(clk_id) -> floating point number\n\ |
|---|
| 220 | n/a | \n\ |
|---|
| 221 | n/a | Return the resolution (precision) of the specified clock clk_id."); |
|---|
| 222 | n/a | #endif /* HAVE_CLOCK_GETRES */ |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | static PyObject * |
|---|
| 225 | n/a | time_sleep(PyObject *self, PyObject *obj) |
|---|
| 226 | n/a | { |
|---|
| 227 | n/a | _PyTime_t secs; |
|---|
| 228 | n/a | if (_PyTime_FromSecondsObject(&secs, obj, _PyTime_ROUND_CEILING)) |
|---|
| 229 | n/a | return NULL; |
|---|
| 230 | n/a | if (secs < 0) { |
|---|
| 231 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 232 | n/a | "sleep length must be non-negative"); |
|---|
| 233 | n/a | return NULL; |
|---|
| 234 | n/a | } |
|---|
| 235 | n/a | if (pysleep(secs) != 0) |
|---|
| 236 | n/a | return NULL; |
|---|
| 237 | n/a | Py_RETURN_NONE; |
|---|
| 238 | n/a | } |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | PyDoc_STRVAR(sleep_doc, |
|---|
| 241 | n/a | "sleep(seconds)\n\ |
|---|
| 242 | n/a | \n\ |
|---|
| 243 | n/a | Delay execution for a given number of seconds. The argument may be\n\ |
|---|
| 244 | n/a | a floating point number for subsecond precision."); |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | static PyStructSequence_Field struct_time_type_fields[] = { |
|---|
| 247 | n/a | {"tm_year", "year, for example, 1993"}, |
|---|
| 248 | n/a | {"tm_mon", "month of year, range [1, 12]"}, |
|---|
| 249 | n/a | {"tm_mday", "day of month, range [1, 31]"}, |
|---|
| 250 | n/a | {"tm_hour", "hours, range [0, 23]"}, |
|---|
| 251 | n/a | {"tm_min", "minutes, range [0, 59]"}, |
|---|
| 252 | n/a | {"tm_sec", "seconds, range [0, 61])"}, |
|---|
| 253 | n/a | {"tm_wday", "day of week, range [0, 6], Monday is 0"}, |
|---|
| 254 | n/a | {"tm_yday", "day of year, range [1, 366]"}, |
|---|
| 255 | n/a | {"tm_isdst", "1 if summer time is in effect, 0 if not, and -1 if unknown"}, |
|---|
| 256 | n/a | {"tm_zone", "abbreviation of timezone name"}, |
|---|
| 257 | n/a | {"tm_gmtoff", "offset from UTC in seconds"}, |
|---|
| 258 | n/a | {0} |
|---|
| 259 | n/a | }; |
|---|
| 260 | n/a | |
|---|
| 261 | n/a | static PyStructSequence_Desc struct_time_type_desc = { |
|---|
| 262 | n/a | "time.struct_time", |
|---|
| 263 | n/a | "The time value as returned by gmtime(), localtime(), and strptime(), and\n" |
|---|
| 264 | n/a | " accepted by asctime(), mktime() and strftime(). May be considered as a\n" |
|---|
| 265 | n/a | " sequence of 9 integers.\n\n" |
|---|
| 266 | n/a | " Note that several fields' values are not the same as those defined by\n" |
|---|
| 267 | n/a | " the C language standard for struct tm. For example, the value of the\n" |
|---|
| 268 | n/a | " field tm_year is the actual year, not year - 1900. See individual\n" |
|---|
| 269 | n/a | " fields' descriptions for details.", |
|---|
| 270 | n/a | struct_time_type_fields, |
|---|
| 271 | n/a | 9, |
|---|
| 272 | n/a | }; |
|---|
| 273 | n/a | |
|---|
| 274 | n/a | static int initialized; |
|---|
| 275 | n/a | static PyTypeObject StructTimeType; |
|---|
| 276 | n/a | |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | static PyObject * |
|---|
| 279 | n/a | tmtotuple(struct tm *p |
|---|
| 280 | n/a | #ifndef HAVE_STRUCT_TM_TM_ZONE |
|---|
| 281 | n/a | , const char *zone, int gmtoff |
|---|
| 282 | n/a | #endif |
|---|
| 283 | n/a | ) |
|---|
| 284 | n/a | { |
|---|
| 285 | n/a | PyObject *v = PyStructSequence_New(&StructTimeType); |
|---|
| 286 | n/a | if (v == NULL) |
|---|
| 287 | n/a | return NULL; |
|---|
| 288 | n/a | |
|---|
| 289 | n/a | #define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val)) |
|---|
| 290 | n/a | |
|---|
| 291 | n/a | SET(0, p->tm_year + 1900); |
|---|
| 292 | n/a | SET(1, p->tm_mon + 1); /* Want January == 1 */ |
|---|
| 293 | n/a | SET(2, p->tm_mday); |
|---|
| 294 | n/a | SET(3, p->tm_hour); |
|---|
| 295 | n/a | SET(4, p->tm_min); |
|---|
| 296 | n/a | SET(5, p->tm_sec); |
|---|
| 297 | n/a | SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */ |
|---|
| 298 | n/a | SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */ |
|---|
| 299 | n/a | SET(8, p->tm_isdst); |
|---|
| 300 | n/a | #ifdef HAVE_STRUCT_TM_TM_ZONE |
|---|
| 301 | n/a | PyStructSequence_SET_ITEM(v, 9, |
|---|
| 302 | n/a | PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape")); |
|---|
| 303 | n/a | SET(10, p->tm_gmtoff); |
|---|
| 304 | n/a | #else |
|---|
| 305 | n/a | PyStructSequence_SET_ITEM(v, 9, |
|---|
| 306 | n/a | PyUnicode_DecodeLocale(zone, "surrogateescape")); |
|---|
| 307 | n/a | SET(10, gmtoff); |
|---|
| 308 | n/a | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
|---|
| 309 | n/a | #undef SET |
|---|
| 310 | n/a | if (PyErr_Occurred()) { |
|---|
| 311 | n/a | Py_XDECREF(v); |
|---|
| 312 | n/a | return NULL; |
|---|
| 313 | n/a | } |
|---|
| 314 | n/a | |
|---|
| 315 | n/a | return v; |
|---|
| 316 | n/a | } |
|---|
| 317 | n/a | |
|---|
| 318 | n/a | /* Parse arg tuple that can contain an optional float-or-None value; |
|---|
| 319 | n/a | format needs to be "|O:name". |
|---|
| 320 | n/a | Returns non-zero on success (parallels PyArg_ParseTuple). |
|---|
| 321 | n/a | */ |
|---|
| 322 | n/a | static int |
|---|
| 323 | n/a | parse_time_t_args(PyObject *args, const char *format, time_t *pwhen) |
|---|
| 324 | n/a | { |
|---|
| 325 | n/a | PyObject *ot = NULL; |
|---|
| 326 | n/a | time_t whent; |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | if (!PyArg_ParseTuple(args, format, &ot)) |
|---|
| 329 | n/a | return 0; |
|---|
| 330 | n/a | if (ot == NULL || ot == Py_None) { |
|---|
| 331 | n/a | whent = time(NULL); |
|---|
| 332 | n/a | } |
|---|
| 333 | n/a | else { |
|---|
| 334 | n/a | if (_PyTime_ObjectToTime_t(ot, &whent, _PyTime_ROUND_FLOOR) == -1) |
|---|
| 335 | n/a | return 0; |
|---|
| 336 | n/a | } |
|---|
| 337 | n/a | *pwhen = whent; |
|---|
| 338 | n/a | return 1; |
|---|
| 339 | n/a | } |
|---|
| 340 | n/a | |
|---|
| 341 | n/a | static PyObject * |
|---|
| 342 | n/a | time_gmtime(PyObject *self, PyObject *args) |
|---|
| 343 | n/a | { |
|---|
| 344 | n/a | time_t when; |
|---|
| 345 | n/a | struct tm buf; |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | if (!parse_time_t_args(args, "|O:gmtime", &when)) |
|---|
| 348 | n/a | return NULL; |
|---|
| 349 | n/a | |
|---|
| 350 | n/a | errno = 0; |
|---|
| 351 | n/a | if (_PyTime_gmtime(when, &buf) != 0) |
|---|
| 352 | n/a | return NULL; |
|---|
| 353 | n/a | #ifdef HAVE_STRUCT_TM_TM_ZONE |
|---|
| 354 | n/a | return tmtotuple(&buf); |
|---|
| 355 | n/a | #else |
|---|
| 356 | n/a | return tmtotuple(&buf, "UTC", 0); |
|---|
| 357 | n/a | #endif |
|---|
| 358 | n/a | } |
|---|
| 359 | n/a | |
|---|
| 360 | n/a | #ifndef HAVE_TIMEGM |
|---|
| 361 | n/a | static time_t |
|---|
| 362 | n/a | timegm(struct tm *p) |
|---|
| 363 | n/a | { |
|---|
| 364 | n/a | /* XXX: the following implementation will not work for tm_year < 1970. |
|---|
| 365 | n/a | but it is likely that platforms that don't have timegm do not support |
|---|
| 366 | n/a | negative timestamps anyways. */ |
|---|
| 367 | n/a | return p->tm_sec + p->tm_min*60 + p->tm_hour*3600 + p->tm_yday*86400 + |
|---|
| 368 | n/a | (p->tm_year-70)*31536000 + ((p->tm_year-69)/4)*86400 - |
|---|
| 369 | n/a | ((p->tm_year-1)/100)*86400 + ((p->tm_year+299)/400)*86400; |
|---|
| 370 | n/a | } |
|---|
| 371 | n/a | #endif |
|---|
| 372 | n/a | |
|---|
| 373 | n/a | PyDoc_STRVAR(gmtime_doc, |
|---|
| 374 | n/a | "gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,\n\ |
|---|
| 375 | n/a | tm_sec, tm_wday, tm_yday, tm_isdst)\n\ |
|---|
| 376 | n/a | \n\ |
|---|
| 377 | n/a | Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\ |
|---|
| 378 | n/a | GMT). When 'seconds' is not passed in, convert the current time instead.\n\ |
|---|
| 379 | n/a | \n\ |
|---|
| 380 | n/a | If the platform supports the tm_gmtoff and tm_zone, they are available as\n\ |
|---|
| 381 | n/a | attributes only."); |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | static PyObject * |
|---|
| 384 | n/a | time_localtime(PyObject *self, PyObject *args) |
|---|
| 385 | n/a | { |
|---|
| 386 | n/a | time_t when; |
|---|
| 387 | n/a | struct tm buf; |
|---|
| 388 | n/a | |
|---|
| 389 | n/a | if (!parse_time_t_args(args, "|O:localtime", &when)) |
|---|
| 390 | n/a | return NULL; |
|---|
| 391 | n/a | if (_PyTime_localtime(when, &buf) != 0) |
|---|
| 392 | n/a | return NULL; |
|---|
| 393 | n/a | #ifdef HAVE_STRUCT_TM_TM_ZONE |
|---|
| 394 | n/a | return tmtotuple(&buf); |
|---|
| 395 | n/a | #else |
|---|
| 396 | n/a | { |
|---|
| 397 | n/a | struct tm local = buf; |
|---|
| 398 | n/a | char zone[100]; |
|---|
| 399 | n/a | int gmtoff; |
|---|
| 400 | n/a | strftime(zone, sizeof(zone), "%Z", &buf); |
|---|
| 401 | n/a | gmtoff = timegm(&buf) - when; |
|---|
| 402 | n/a | return tmtotuple(&local, zone, gmtoff); |
|---|
| 403 | n/a | } |
|---|
| 404 | n/a | #endif |
|---|
| 405 | n/a | } |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | PyDoc_STRVAR(localtime_doc, |
|---|
| 408 | n/a | "localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,\n\ |
|---|
| 409 | n/a | tm_sec,tm_wday,tm_yday,tm_isdst)\n\ |
|---|
| 410 | n/a | \n\ |
|---|
| 411 | n/a | Convert seconds since the Epoch to a time tuple expressing local time.\n\ |
|---|
| 412 | n/a | When 'seconds' is not passed in, convert the current time instead."); |
|---|
| 413 | n/a | |
|---|
| 414 | n/a | /* Convert 9-item tuple to tm structure. Return 1 on success, set |
|---|
| 415 | n/a | * an exception and return 0 on error. |
|---|
| 416 | n/a | */ |
|---|
| 417 | n/a | static int |
|---|
| 418 | n/a | gettmarg(PyObject *args, struct tm *p) |
|---|
| 419 | n/a | { |
|---|
| 420 | n/a | int y; |
|---|
| 421 | n/a | |
|---|
| 422 | n/a | memset((void *) p, '\0', sizeof(struct tm)); |
|---|
| 423 | n/a | |
|---|
| 424 | n/a | if (!PyTuple_Check(args)) { |
|---|
| 425 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 426 | n/a | "Tuple or struct_time argument required"); |
|---|
| 427 | n/a | return 0; |
|---|
| 428 | n/a | } |
|---|
| 429 | n/a | |
|---|
| 430 | n/a | if (!PyArg_ParseTuple(args, "iiiiiiiii", |
|---|
| 431 | n/a | &y, &p->tm_mon, &p->tm_mday, |
|---|
| 432 | n/a | &p->tm_hour, &p->tm_min, &p->tm_sec, |
|---|
| 433 | n/a | &p->tm_wday, &p->tm_yday, &p->tm_isdst)) |
|---|
| 434 | n/a | return 0; |
|---|
| 435 | n/a | p->tm_year = y - 1900; |
|---|
| 436 | n/a | p->tm_mon--; |
|---|
| 437 | n/a | p->tm_wday = (p->tm_wday + 1) % 7; |
|---|
| 438 | n/a | p->tm_yday--; |
|---|
| 439 | n/a | #ifdef HAVE_STRUCT_TM_TM_ZONE |
|---|
| 440 | n/a | if (Py_TYPE(args) == &StructTimeType) { |
|---|
| 441 | n/a | PyObject *item; |
|---|
| 442 | n/a | item = PyTuple_GET_ITEM(args, 9); |
|---|
| 443 | n/a | p->tm_zone = item == Py_None ? NULL : PyUnicode_AsUTF8(item); |
|---|
| 444 | n/a | item = PyTuple_GET_ITEM(args, 10); |
|---|
| 445 | n/a | p->tm_gmtoff = item == Py_None ? 0 : PyLong_AsLong(item); |
|---|
| 446 | n/a | if (PyErr_Occurred()) |
|---|
| 447 | n/a | return 0; |
|---|
| 448 | n/a | } |
|---|
| 449 | n/a | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
|---|
| 450 | n/a | return 1; |
|---|
| 451 | n/a | } |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | /* Check values of the struct tm fields before it is passed to strftime() and |
|---|
| 454 | n/a | * asctime(). Return 1 if all values are valid, otherwise set an exception |
|---|
| 455 | n/a | * and returns 0. |
|---|
| 456 | n/a | */ |
|---|
| 457 | n/a | static int |
|---|
| 458 | n/a | checktm(struct tm* buf) |
|---|
| 459 | n/a | { |
|---|
| 460 | n/a | /* Checks added to make sure strftime() and asctime() does not crash Python by |
|---|
| 461 | n/a | indexing blindly into some array for a textual representation |
|---|
| 462 | n/a | by some bad index (fixes bug #897625 and #6608). |
|---|
| 463 | n/a | |
|---|
| 464 | n/a | Also support values of zero from Python code for arguments in which |
|---|
| 465 | n/a | that is out of range by forcing that value to the lowest value that |
|---|
| 466 | n/a | is valid (fixed bug #1520914). |
|---|
| 467 | n/a | |
|---|
| 468 | n/a | Valid ranges based on what is allowed in struct tm: |
|---|
| 469 | n/a | |
|---|
| 470 | n/a | - tm_year: [0, max(int)] (1) |
|---|
| 471 | n/a | - tm_mon: [0, 11] (2) |
|---|
| 472 | n/a | - tm_mday: [1, 31] |
|---|
| 473 | n/a | - tm_hour: [0, 23] |
|---|
| 474 | n/a | - tm_min: [0, 59] |
|---|
| 475 | n/a | - tm_sec: [0, 60] |
|---|
| 476 | n/a | - tm_wday: [0, 6] (1) |
|---|
| 477 | n/a | - tm_yday: [0, 365] (2) |
|---|
| 478 | n/a | - tm_isdst: [-max(int), max(int)] |
|---|
| 479 | n/a | |
|---|
| 480 | n/a | (1) gettmarg() handles bounds-checking. |
|---|
| 481 | n/a | (2) Python's acceptable range is one greater than the range in C, |
|---|
| 482 | n/a | thus need to check against automatic decrement by gettmarg(). |
|---|
| 483 | n/a | */ |
|---|
| 484 | n/a | if (buf->tm_mon == -1) |
|---|
| 485 | n/a | buf->tm_mon = 0; |
|---|
| 486 | n/a | else if (buf->tm_mon < 0 || buf->tm_mon > 11) { |
|---|
| 487 | n/a | PyErr_SetString(PyExc_ValueError, "month out of range"); |
|---|
| 488 | n/a | return 0; |
|---|
| 489 | n/a | } |
|---|
| 490 | n/a | if (buf->tm_mday == 0) |
|---|
| 491 | n/a | buf->tm_mday = 1; |
|---|
| 492 | n/a | else if (buf->tm_mday < 0 || buf->tm_mday > 31) { |
|---|
| 493 | n/a | PyErr_SetString(PyExc_ValueError, "day of month out of range"); |
|---|
| 494 | n/a | return 0; |
|---|
| 495 | n/a | } |
|---|
| 496 | n/a | if (buf->tm_hour < 0 || buf->tm_hour > 23) { |
|---|
| 497 | n/a | PyErr_SetString(PyExc_ValueError, "hour out of range"); |
|---|
| 498 | n/a | return 0; |
|---|
| 499 | n/a | } |
|---|
| 500 | n/a | if (buf->tm_min < 0 || buf->tm_min > 59) { |
|---|
| 501 | n/a | PyErr_SetString(PyExc_ValueError, "minute out of range"); |
|---|
| 502 | n/a | return 0; |
|---|
| 503 | n/a | } |
|---|
| 504 | n/a | if (buf->tm_sec < 0 || buf->tm_sec > 61) { |
|---|
| 505 | n/a | PyErr_SetString(PyExc_ValueError, "seconds out of range"); |
|---|
| 506 | n/a | return 0; |
|---|
| 507 | n/a | } |
|---|
| 508 | n/a | /* tm_wday does not need checking of its upper-bound since taking |
|---|
| 509 | n/a | ``% 7`` in gettmarg() automatically restricts the range. */ |
|---|
| 510 | n/a | if (buf->tm_wday < 0) { |
|---|
| 511 | n/a | PyErr_SetString(PyExc_ValueError, "day of week out of range"); |
|---|
| 512 | n/a | return 0; |
|---|
| 513 | n/a | } |
|---|
| 514 | n/a | if (buf->tm_yday == -1) |
|---|
| 515 | n/a | buf->tm_yday = 0; |
|---|
| 516 | n/a | else if (buf->tm_yday < 0 || buf->tm_yday > 365) { |
|---|
| 517 | n/a | PyErr_SetString(PyExc_ValueError, "day of year out of range"); |
|---|
| 518 | n/a | return 0; |
|---|
| 519 | n/a | } |
|---|
| 520 | n/a | return 1; |
|---|
| 521 | n/a | } |
|---|
| 522 | n/a | |
|---|
| 523 | n/a | #ifdef MS_WINDOWS |
|---|
| 524 | n/a | /* wcsftime() doesn't format correctly time zones, see issue #10653 */ |
|---|
| 525 | n/a | # undef HAVE_WCSFTIME |
|---|
| 526 | n/a | #endif |
|---|
| 527 | n/a | #define STRFTIME_FORMAT_CODES \ |
|---|
| 528 | n/a | "Commonly used format codes:\n\ |
|---|
| 529 | n/a | \n\ |
|---|
| 530 | n/a | %Y Year with century as a decimal number.\n\ |
|---|
| 531 | n/a | %m Month as a decimal number [01,12].\n\ |
|---|
| 532 | n/a | %d Day of the month as a decimal number [01,31].\n\ |
|---|
| 533 | n/a | %H Hour (24-hour clock) as a decimal number [00,23].\n\ |
|---|
| 534 | n/a | %M Minute as a decimal number [00,59].\n\ |
|---|
| 535 | n/a | %S Second as a decimal number [00,61].\n\ |
|---|
| 536 | n/a | %z Time zone offset from UTC.\n\ |
|---|
| 537 | n/a | %a Locale's abbreviated weekday name.\n\ |
|---|
| 538 | n/a | %A Locale's full weekday name.\n\ |
|---|
| 539 | n/a | %b Locale's abbreviated month name.\n\ |
|---|
| 540 | n/a | %B Locale's full month name.\n\ |
|---|
| 541 | n/a | %c Locale's appropriate date and time representation.\n\ |
|---|
| 542 | n/a | %I Hour (12-hour clock) as a decimal number [01,12].\n\ |
|---|
| 543 | n/a | %p Locale's equivalent of either AM or PM.\n\ |
|---|
| 544 | n/a | \n\ |
|---|
| 545 | n/a | Other codes may be available on your platform. See documentation for\n\ |
|---|
| 546 | n/a | the C library strftime function.\n" |
|---|
| 547 | n/a | |
|---|
| 548 | n/a | #ifdef HAVE_STRFTIME |
|---|
| 549 | n/a | #ifdef HAVE_WCSFTIME |
|---|
| 550 | n/a | #define time_char wchar_t |
|---|
| 551 | n/a | #define format_time wcsftime |
|---|
| 552 | n/a | #define time_strlen wcslen |
|---|
| 553 | n/a | #else |
|---|
| 554 | n/a | #define time_char char |
|---|
| 555 | n/a | #define format_time strftime |
|---|
| 556 | n/a | #define time_strlen strlen |
|---|
| 557 | n/a | #endif |
|---|
| 558 | n/a | |
|---|
| 559 | n/a | static PyObject * |
|---|
| 560 | n/a | time_strftime(PyObject *self, PyObject *args) |
|---|
| 561 | n/a | { |
|---|
| 562 | n/a | PyObject *tup = NULL; |
|---|
| 563 | n/a | struct tm buf; |
|---|
| 564 | n/a | const time_char *fmt; |
|---|
| 565 | n/a | #ifdef HAVE_WCSFTIME |
|---|
| 566 | n/a | wchar_t *format; |
|---|
| 567 | n/a | #else |
|---|
| 568 | n/a | PyObject *format; |
|---|
| 569 | n/a | #endif |
|---|
| 570 | n/a | PyObject *format_arg; |
|---|
| 571 | n/a | size_t fmtlen, buflen; |
|---|
| 572 | n/a | time_char *outbuf = NULL; |
|---|
| 573 | n/a | size_t i; |
|---|
| 574 | n/a | PyObject *ret = NULL; |
|---|
| 575 | n/a | |
|---|
| 576 | n/a | memset((void *) &buf, '\0', sizeof(buf)); |
|---|
| 577 | n/a | |
|---|
| 578 | n/a | /* Will always expect a unicode string to be passed as format. |
|---|
| 579 | n/a | Given that there's no str type anymore in py3k this seems safe. |
|---|
| 580 | n/a | */ |
|---|
| 581 | n/a | if (!PyArg_ParseTuple(args, "U|O:strftime", &format_arg, &tup)) |
|---|
| 582 | n/a | return NULL; |
|---|
| 583 | n/a | |
|---|
| 584 | n/a | if (tup == NULL) { |
|---|
| 585 | n/a | time_t tt = time(NULL); |
|---|
| 586 | n/a | if (_PyTime_localtime(tt, &buf) != 0) |
|---|
| 587 | n/a | return NULL; |
|---|
| 588 | n/a | } |
|---|
| 589 | n/a | else if (!gettmarg(tup, &buf) || !checktm(&buf)) |
|---|
| 590 | n/a | return NULL; |
|---|
| 591 | n/a | |
|---|
| 592 | n/a | #if defined(_MSC_VER) || defined(sun) || defined(_AIX) |
|---|
| 593 | n/a | if (buf.tm_year + 1900 < 1 || 9999 < buf.tm_year + 1900) { |
|---|
| 594 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 595 | n/a | "strftime() requires year in [1; 9999]"); |
|---|
| 596 | n/a | return NULL; |
|---|
| 597 | n/a | } |
|---|
| 598 | n/a | #endif |
|---|
| 599 | n/a | |
|---|
| 600 | n/a | /* Normalize tm_isdst just in case someone foolishly implements %Z |
|---|
| 601 | n/a | based on the assumption that tm_isdst falls within the range of |
|---|
| 602 | n/a | [-1, 1] */ |
|---|
| 603 | n/a | if (buf.tm_isdst < -1) |
|---|
| 604 | n/a | buf.tm_isdst = -1; |
|---|
| 605 | n/a | else if (buf.tm_isdst > 1) |
|---|
| 606 | n/a | buf.tm_isdst = 1; |
|---|
| 607 | n/a | |
|---|
| 608 | n/a | #ifdef HAVE_WCSFTIME |
|---|
| 609 | n/a | format = PyUnicode_AsWideCharString(format_arg, NULL); |
|---|
| 610 | n/a | if (format == NULL) |
|---|
| 611 | n/a | return NULL; |
|---|
| 612 | n/a | fmt = format; |
|---|
| 613 | n/a | #else |
|---|
| 614 | n/a | /* Convert the unicode string to an ascii one */ |
|---|
| 615 | n/a | format = PyUnicode_EncodeLocale(format_arg, "surrogateescape"); |
|---|
| 616 | n/a | if (format == NULL) |
|---|
| 617 | n/a | return NULL; |
|---|
| 618 | n/a | fmt = PyBytes_AS_STRING(format); |
|---|
| 619 | n/a | #endif |
|---|
| 620 | n/a | |
|---|
| 621 | n/a | #if defined(MS_WINDOWS) && !defined(HAVE_WCSFTIME) |
|---|
| 622 | n/a | /* check that the format string contains only valid directives */ |
|---|
| 623 | n/a | for (outbuf = strchr(fmt, '%'); |
|---|
| 624 | n/a | outbuf != NULL; |
|---|
| 625 | n/a | outbuf = strchr(outbuf+2, '%')) |
|---|
| 626 | n/a | { |
|---|
| 627 | n/a | if (outbuf[1] == '#') |
|---|
| 628 | n/a | ++outbuf; /* not documented by python, */ |
|---|
| 629 | n/a | if (outbuf[1] == '\0') |
|---|
| 630 | n/a | break; |
|---|
| 631 | n/a | if ((outbuf[1] == 'y') && buf.tm_year < 0) { |
|---|
| 632 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 633 | n/a | "format %y requires year >= 1900 on Windows"); |
|---|
| 634 | n/a | Py_DECREF(format); |
|---|
| 635 | n/a | return NULL; |
|---|
| 636 | n/a | } |
|---|
| 637 | n/a | } |
|---|
| 638 | n/a | #elif (defined(_AIX) || defined(sun)) && defined(HAVE_WCSFTIME) |
|---|
| 639 | n/a | for (outbuf = wcschr(fmt, '%'); |
|---|
| 640 | n/a | outbuf != NULL; |
|---|
| 641 | n/a | outbuf = wcschr(outbuf+2, '%')) |
|---|
| 642 | n/a | { |
|---|
| 643 | n/a | if (outbuf[1] == L'\0') |
|---|
| 644 | n/a | break; |
|---|
| 645 | n/a | /* Issue #19634: On AIX, wcsftime("y", (1899, 1, 1, 0, 0, 0, 0, 0, 0)) |
|---|
| 646 | n/a | returns "0/" instead of "99" */ |
|---|
| 647 | n/a | if (outbuf[1] == L'y' && buf.tm_year < 0) { |
|---|
| 648 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 649 | n/a | "format %y requires year >= 1900 on AIX"); |
|---|
| 650 | n/a | return NULL; |
|---|
| 651 | n/a | } |
|---|
| 652 | n/a | } |
|---|
| 653 | n/a | #endif |
|---|
| 654 | n/a | |
|---|
| 655 | n/a | fmtlen = time_strlen(fmt); |
|---|
| 656 | n/a | |
|---|
| 657 | n/a | /* I hate these functions that presume you know how big the output |
|---|
| 658 | n/a | * will be ahead of time... |
|---|
| 659 | n/a | */ |
|---|
| 660 | n/a | for (i = 1024; ; i += i) { |
|---|
| 661 | n/a | outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char)); |
|---|
| 662 | n/a | if (outbuf == NULL) { |
|---|
| 663 | n/a | PyErr_NoMemory(); |
|---|
| 664 | n/a | break; |
|---|
| 665 | n/a | } |
|---|
| 666 | n/a | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
|---|
| 667 | n/a | errno = 0; |
|---|
| 668 | n/a | #endif |
|---|
| 669 | n/a | _Py_BEGIN_SUPPRESS_IPH |
|---|
| 670 | n/a | buflen = format_time(outbuf, i, fmt, &buf); |
|---|
| 671 | n/a | _Py_END_SUPPRESS_IPH |
|---|
| 672 | n/a | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
|---|
| 673 | n/a | /* VisualStudio .NET 2005 does this properly */ |
|---|
| 674 | n/a | if (buflen == 0 && errno == EINVAL) { |
|---|
| 675 | n/a | PyErr_SetString(PyExc_ValueError, "Invalid format string"); |
|---|
| 676 | n/a | PyMem_Free(outbuf); |
|---|
| 677 | n/a | break; |
|---|
| 678 | n/a | } |
|---|
| 679 | n/a | #endif |
|---|
| 680 | n/a | if (buflen > 0 || i >= 256 * fmtlen) { |
|---|
| 681 | n/a | /* If the buffer is 256 times as long as the format, |
|---|
| 682 | n/a | it's probably not failing for lack of room! |
|---|
| 683 | n/a | More likely, the format yields an empty result, |
|---|
| 684 | n/a | e.g. an empty format, or %Z when the timezone |
|---|
| 685 | n/a | is unknown. */ |
|---|
| 686 | n/a | #ifdef HAVE_WCSFTIME |
|---|
| 687 | n/a | ret = PyUnicode_FromWideChar(outbuf, buflen); |
|---|
| 688 | n/a | #else |
|---|
| 689 | n/a | ret = PyUnicode_DecodeLocaleAndSize(outbuf, buflen, |
|---|
| 690 | n/a | "surrogateescape"); |
|---|
| 691 | n/a | #endif |
|---|
| 692 | n/a | PyMem_Free(outbuf); |
|---|
| 693 | n/a | break; |
|---|
| 694 | n/a | } |
|---|
| 695 | n/a | PyMem_Free(outbuf); |
|---|
| 696 | n/a | } |
|---|
| 697 | n/a | #ifdef HAVE_WCSFTIME |
|---|
| 698 | n/a | PyMem_Free(format); |
|---|
| 699 | n/a | #else |
|---|
| 700 | n/a | Py_DECREF(format); |
|---|
| 701 | n/a | #endif |
|---|
| 702 | n/a | return ret; |
|---|
| 703 | n/a | } |
|---|
| 704 | n/a | |
|---|
| 705 | n/a | #undef time_char |
|---|
| 706 | n/a | #undef format_time |
|---|
| 707 | n/a | PyDoc_STRVAR(strftime_doc, |
|---|
| 708 | n/a | "strftime(format[, tuple]) -> string\n\ |
|---|
| 709 | n/a | \n\ |
|---|
| 710 | n/a | Convert a time tuple to a string according to a format specification.\n\ |
|---|
| 711 | n/a | See the library reference manual for formatting codes. When the time tuple\n\ |
|---|
| 712 | n/a | is not present, current time as returned by localtime() is used.\n\ |
|---|
| 713 | n/a | \n" STRFTIME_FORMAT_CODES); |
|---|
| 714 | n/a | #endif /* HAVE_STRFTIME */ |
|---|
| 715 | n/a | |
|---|
| 716 | n/a | static PyObject * |
|---|
| 717 | n/a | time_strptime(PyObject *self, PyObject *args) |
|---|
| 718 | n/a | { |
|---|
| 719 | n/a | PyObject *module, *func, *result; |
|---|
| 720 | n/a | _Py_IDENTIFIER(_strptime_time); |
|---|
| 721 | n/a | |
|---|
| 722 | n/a | module = PyImport_ImportModuleNoBlock("_strptime"); |
|---|
| 723 | n/a | if (!module) |
|---|
| 724 | n/a | return NULL; |
|---|
| 725 | n/a | |
|---|
| 726 | n/a | func = _PyObject_GetAttrId(module, &PyId__strptime_time); |
|---|
| 727 | n/a | Py_DECREF(module); |
|---|
| 728 | n/a | if (!func) { |
|---|
| 729 | n/a | return NULL; |
|---|
| 730 | n/a | } |
|---|
| 731 | n/a | |
|---|
| 732 | n/a | result = PyObject_Call(func, args, NULL); |
|---|
| 733 | n/a | Py_DECREF(func); |
|---|
| 734 | n/a | return result; |
|---|
| 735 | n/a | } |
|---|
| 736 | n/a | |
|---|
| 737 | n/a | |
|---|
| 738 | n/a | PyDoc_STRVAR(strptime_doc, |
|---|
| 739 | n/a | "strptime(string, format) -> struct_time\n\ |
|---|
| 740 | n/a | \n\ |
|---|
| 741 | n/a | Parse a string to a time tuple according to a format specification.\n\ |
|---|
| 742 | n/a | See the library reference manual for formatting codes (same as\n\ |
|---|
| 743 | n/a | strftime()).\n\ |
|---|
| 744 | n/a | \n" STRFTIME_FORMAT_CODES); |
|---|
| 745 | n/a | |
|---|
| 746 | n/a | static PyObject * |
|---|
| 747 | n/a | _asctime(struct tm *timeptr) |
|---|
| 748 | n/a | { |
|---|
| 749 | n/a | /* Inspired by Open Group reference implementation available at |
|---|
| 750 | n/a | * http://pubs.opengroup.org/onlinepubs/009695399/functions/asctime.html */ |
|---|
| 751 | n/a | static const char wday_name[7][4] = { |
|---|
| 752 | n/a | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" |
|---|
| 753 | n/a | }; |
|---|
| 754 | n/a | static const char mon_name[12][4] = { |
|---|
| 755 | n/a | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
|---|
| 756 | n/a | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
|---|
| 757 | n/a | }; |
|---|
| 758 | n/a | return PyUnicode_FromFormat( |
|---|
| 759 | n/a | "%s %s%3d %.2d:%.2d:%.2d %d", |
|---|
| 760 | n/a | wday_name[timeptr->tm_wday], |
|---|
| 761 | n/a | mon_name[timeptr->tm_mon], |
|---|
| 762 | n/a | timeptr->tm_mday, timeptr->tm_hour, |
|---|
| 763 | n/a | timeptr->tm_min, timeptr->tm_sec, |
|---|
| 764 | n/a | 1900 + timeptr->tm_year); |
|---|
| 765 | n/a | } |
|---|
| 766 | n/a | |
|---|
| 767 | n/a | static PyObject * |
|---|
| 768 | n/a | time_asctime(PyObject *self, PyObject *args) |
|---|
| 769 | n/a | { |
|---|
| 770 | n/a | PyObject *tup = NULL; |
|---|
| 771 | n/a | struct tm buf; |
|---|
| 772 | n/a | |
|---|
| 773 | n/a | if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup)) |
|---|
| 774 | n/a | return NULL; |
|---|
| 775 | n/a | if (tup == NULL) { |
|---|
| 776 | n/a | time_t tt = time(NULL); |
|---|
| 777 | n/a | if (_PyTime_localtime(tt, &buf) != 0) |
|---|
| 778 | n/a | return NULL; |
|---|
| 779 | n/a | |
|---|
| 780 | n/a | } else if (!gettmarg(tup, &buf) || !checktm(&buf)) |
|---|
| 781 | n/a | return NULL; |
|---|
| 782 | n/a | return _asctime(&buf); |
|---|
| 783 | n/a | } |
|---|
| 784 | n/a | |
|---|
| 785 | n/a | PyDoc_STRVAR(asctime_doc, |
|---|
| 786 | n/a | "asctime([tuple]) -> string\n\ |
|---|
| 787 | n/a | \n\ |
|---|
| 788 | n/a | Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\ |
|---|
| 789 | n/a | When the time tuple is not present, current time as returned by localtime()\n\ |
|---|
| 790 | n/a | is used."); |
|---|
| 791 | n/a | |
|---|
| 792 | n/a | static PyObject * |
|---|
| 793 | n/a | time_ctime(PyObject *self, PyObject *args) |
|---|
| 794 | n/a | { |
|---|
| 795 | n/a | time_t tt; |
|---|
| 796 | n/a | struct tm buf; |
|---|
| 797 | n/a | if (!parse_time_t_args(args, "|O:ctime", &tt)) |
|---|
| 798 | n/a | return NULL; |
|---|
| 799 | n/a | if (_PyTime_localtime(tt, &buf) != 0) |
|---|
| 800 | n/a | return NULL; |
|---|
| 801 | n/a | return _asctime(&buf); |
|---|
| 802 | n/a | } |
|---|
| 803 | n/a | |
|---|
| 804 | n/a | PyDoc_STRVAR(ctime_doc, |
|---|
| 805 | n/a | "ctime(seconds) -> string\n\ |
|---|
| 806 | n/a | \n\ |
|---|
| 807 | n/a | Convert a time in seconds since the Epoch to a string in local time.\n\ |
|---|
| 808 | n/a | This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\ |
|---|
| 809 | n/a | not present, current time as returned by localtime() is used."); |
|---|
| 810 | n/a | |
|---|
| 811 | n/a | #ifdef HAVE_MKTIME |
|---|
| 812 | n/a | static PyObject * |
|---|
| 813 | n/a | time_mktime(PyObject *self, PyObject *tup) |
|---|
| 814 | n/a | { |
|---|
| 815 | n/a | struct tm buf; |
|---|
| 816 | n/a | time_t tt; |
|---|
| 817 | n/a | if (!gettmarg(tup, &buf)) |
|---|
| 818 | n/a | return NULL; |
|---|
| 819 | n/a | #ifdef _AIX |
|---|
| 820 | n/a | /* year < 1902 or year > 2037 */ |
|---|
| 821 | n/a | if (buf.tm_year < 2 || buf.tm_year > 137) { |
|---|
| 822 | n/a | /* Issue #19748: On AIX, mktime() doesn't report overflow error for |
|---|
| 823 | n/a | * timestamp < -2^31 or timestamp > 2**31-1. */ |
|---|
| 824 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 825 | n/a | "mktime argument out of range"); |
|---|
| 826 | n/a | return NULL; |
|---|
| 827 | n/a | } |
|---|
| 828 | n/a | #else |
|---|
| 829 | n/a | buf.tm_wday = -1; /* sentinel; original value ignored */ |
|---|
| 830 | n/a | #endif |
|---|
| 831 | n/a | tt = mktime(&buf); |
|---|
| 832 | n/a | /* Return value of -1 does not necessarily mean an error, but tm_wday |
|---|
| 833 | n/a | * cannot remain set to -1 if mktime succeeded. */ |
|---|
| 834 | n/a | if (tt == (time_t)(-1) |
|---|
| 835 | n/a | #ifndef _AIX |
|---|
| 836 | n/a | /* Return value of -1 does not necessarily mean an error, but |
|---|
| 837 | n/a | * tm_wday cannot remain set to -1 if mktime succeeded. */ |
|---|
| 838 | n/a | && buf.tm_wday == -1 |
|---|
| 839 | n/a | #else |
|---|
| 840 | n/a | /* on AIX, tm_wday is always sets, even on error */ |
|---|
| 841 | n/a | #endif |
|---|
| 842 | n/a | ) |
|---|
| 843 | n/a | { |
|---|
| 844 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 845 | n/a | "mktime argument out of range"); |
|---|
| 846 | n/a | return NULL; |
|---|
| 847 | n/a | } |
|---|
| 848 | n/a | return PyFloat_FromDouble((double)tt); |
|---|
| 849 | n/a | } |
|---|
| 850 | n/a | |
|---|
| 851 | n/a | PyDoc_STRVAR(mktime_doc, |
|---|
| 852 | n/a | "mktime(tuple) -> floating point number\n\ |
|---|
| 853 | n/a | \n\ |
|---|
| 854 | n/a | Convert a time tuple in local time to seconds since the Epoch.\n\ |
|---|
| 855 | n/a | Note that mktime(gmtime(0)) will not generally return zero for most\n\ |
|---|
| 856 | n/a | time zones; instead the returned value will either be equal to that\n\ |
|---|
| 857 | n/a | of the timezone or altzone attributes on the time module."); |
|---|
| 858 | n/a | #endif /* HAVE_MKTIME */ |
|---|
| 859 | n/a | |
|---|
| 860 | n/a | #ifdef HAVE_WORKING_TZSET |
|---|
| 861 | n/a | static void PyInit_timezone(PyObject *module); |
|---|
| 862 | n/a | |
|---|
| 863 | n/a | static PyObject * |
|---|
| 864 | n/a | time_tzset(PyObject *self, PyObject *unused) |
|---|
| 865 | n/a | { |
|---|
| 866 | n/a | PyObject* m; |
|---|
| 867 | n/a | |
|---|
| 868 | n/a | m = PyImport_ImportModuleNoBlock("time"); |
|---|
| 869 | n/a | if (m == NULL) { |
|---|
| 870 | n/a | return NULL; |
|---|
| 871 | n/a | } |
|---|
| 872 | n/a | |
|---|
| 873 | n/a | tzset(); |
|---|
| 874 | n/a | |
|---|
| 875 | n/a | /* Reset timezone, altzone, daylight and tzname */ |
|---|
| 876 | n/a | PyInit_timezone(m); |
|---|
| 877 | n/a | Py_DECREF(m); |
|---|
| 878 | n/a | if (PyErr_Occurred()) |
|---|
| 879 | n/a | return NULL; |
|---|
| 880 | n/a | |
|---|
| 881 | n/a | Py_RETURN_NONE; |
|---|
| 882 | n/a | } |
|---|
| 883 | n/a | |
|---|
| 884 | n/a | PyDoc_STRVAR(tzset_doc, |
|---|
| 885 | n/a | "tzset()\n\ |
|---|
| 886 | n/a | \n\ |
|---|
| 887 | n/a | Initialize, or reinitialize, the local timezone to the value stored in\n\ |
|---|
| 888 | n/a | os.environ['TZ']. The TZ environment variable should be specified in\n\ |
|---|
| 889 | n/a | standard Unix timezone format as documented in the tzset man page\n\ |
|---|
| 890 | n/a | (eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\ |
|---|
| 891 | n/a | fall back to UTC. If the TZ environment variable is not set, the local\n\ |
|---|
| 892 | n/a | timezone is set to the systems best guess of wallclock time.\n\ |
|---|
| 893 | n/a | Changing the TZ environment variable without calling tzset *may* change\n\ |
|---|
| 894 | n/a | the local timezone used by methods such as localtime, but this behaviour\n\ |
|---|
| 895 | n/a | should not be relied on."); |
|---|
| 896 | n/a | #endif /* HAVE_WORKING_TZSET */ |
|---|
| 897 | n/a | |
|---|
| 898 | n/a | static PyObject * |
|---|
| 899 | n/a | pymonotonic(_Py_clock_info_t *info) |
|---|
| 900 | n/a | { |
|---|
| 901 | n/a | _PyTime_t t; |
|---|
| 902 | n/a | double d; |
|---|
| 903 | n/a | if (_PyTime_GetMonotonicClockWithInfo(&t, info) < 0) { |
|---|
| 904 | n/a | assert(info != NULL); |
|---|
| 905 | n/a | return NULL; |
|---|
| 906 | n/a | } |
|---|
| 907 | n/a | d = _PyTime_AsSecondsDouble(t); |
|---|
| 908 | n/a | return PyFloat_FromDouble(d); |
|---|
| 909 | n/a | } |
|---|
| 910 | n/a | |
|---|
| 911 | n/a | static PyObject * |
|---|
| 912 | n/a | time_monotonic(PyObject *self, PyObject *unused) |
|---|
| 913 | n/a | { |
|---|
| 914 | n/a | return pymonotonic(NULL); |
|---|
| 915 | n/a | } |
|---|
| 916 | n/a | |
|---|
| 917 | n/a | PyDoc_STRVAR(monotonic_doc, |
|---|
| 918 | n/a | "monotonic() -> float\n\ |
|---|
| 919 | n/a | \n\ |
|---|
| 920 | n/a | Monotonic clock, cannot go backward."); |
|---|
| 921 | n/a | |
|---|
| 922 | n/a | static PyObject* |
|---|
| 923 | n/a | perf_counter(_Py_clock_info_t *info) |
|---|
| 924 | n/a | { |
|---|
| 925 | n/a | #ifdef WIN32_PERF_COUNTER |
|---|
| 926 | n/a | return win_perf_counter(info); |
|---|
| 927 | n/a | #else |
|---|
| 928 | n/a | return pymonotonic(info); |
|---|
| 929 | n/a | #endif |
|---|
| 930 | n/a | } |
|---|
| 931 | n/a | |
|---|
| 932 | n/a | static PyObject * |
|---|
| 933 | n/a | time_perf_counter(PyObject *self, PyObject *unused) |
|---|
| 934 | n/a | { |
|---|
| 935 | n/a | return perf_counter(NULL); |
|---|
| 936 | n/a | } |
|---|
| 937 | n/a | |
|---|
| 938 | n/a | PyDoc_STRVAR(perf_counter_doc, |
|---|
| 939 | n/a | "perf_counter() -> float\n\ |
|---|
| 940 | n/a | \n\ |
|---|
| 941 | n/a | Performance counter for benchmarking."); |
|---|
| 942 | n/a | |
|---|
| 943 | n/a | static PyObject* |
|---|
| 944 | n/a | py_process_time(_Py_clock_info_t *info) |
|---|
| 945 | n/a | { |
|---|
| 946 | n/a | #if defined(MS_WINDOWS) |
|---|
| 947 | n/a | HANDLE process; |
|---|
| 948 | n/a | FILETIME creation_time, exit_time, kernel_time, user_time; |
|---|
| 949 | n/a | ULARGE_INTEGER large; |
|---|
| 950 | n/a | double total; |
|---|
| 951 | n/a | BOOL ok; |
|---|
| 952 | n/a | |
|---|
| 953 | n/a | process = GetCurrentProcess(); |
|---|
| 954 | n/a | ok = GetProcessTimes(process, &creation_time, &exit_time, &kernel_time, &user_time); |
|---|
| 955 | n/a | if (!ok) |
|---|
| 956 | n/a | return PyErr_SetFromWindowsErr(0); |
|---|
| 957 | n/a | |
|---|
| 958 | n/a | large.u.LowPart = kernel_time.dwLowDateTime; |
|---|
| 959 | n/a | large.u.HighPart = kernel_time.dwHighDateTime; |
|---|
| 960 | n/a | total = (double)large.QuadPart; |
|---|
| 961 | n/a | large.u.LowPart = user_time.dwLowDateTime; |
|---|
| 962 | n/a | large.u.HighPart = user_time.dwHighDateTime; |
|---|
| 963 | n/a | total += (double)large.QuadPart; |
|---|
| 964 | n/a | if (info) { |
|---|
| 965 | n/a | info->implementation = "GetProcessTimes()"; |
|---|
| 966 | n/a | info->resolution = 1e-7; |
|---|
| 967 | n/a | info->monotonic = 1; |
|---|
| 968 | n/a | info->adjustable = 0; |
|---|
| 969 | n/a | } |
|---|
| 970 | n/a | return PyFloat_FromDouble(total * 1e-7); |
|---|
| 971 | n/a | #else |
|---|
| 972 | n/a | |
|---|
| 973 | n/a | #if defined(HAVE_SYS_RESOURCE_H) |
|---|
| 974 | n/a | struct rusage ru; |
|---|
| 975 | n/a | #endif |
|---|
| 976 | n/a | #ifdef HAVE_TIMES |
|---|
| 977 | n/a | struct tms t; |
|---|
| 978 | n/a | static long ticks_per_second = -1; |
|---|
| 979 | n/a | #endif |
|---|
| 980 | n/a | |
|---|
| 981 | n/a | #if defined(HAVE_CLOCK_GETTIME) \ |
|---|
| 982 | n/a | && (defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_PROF)) |
|---|
| 983 | n/a | struct timespec tp; |
|---|
| 984 | n/a | #ifdef CLOCK_PROF |
|---|
| 985 | n/a | const clockid_t clk_id = CLOCK_PROF; |
|---|
| 986 | n/a | const char *function = "clock_gettime(CLOCK_PROF)"; |
|---|
| 987 | n/a | #else |
|---|
| 988 | n/a | const clockid_t clk_id = CLOCK_PROCESS_CPUTIME_ID; |
|---|
| 989 | n/a | const char *function = "clock_gettime(CLOCK_PROCESS_CPUTIME_ID)"; |
|---|
| 990 | n/a | #endif |
|---|
| 991 | n/a | |
|---|
| 992 | n/a | if (clock_gettime(clk_id, &tp) == 0) { |
|---|
| 993 | n/a | if (info) { |
|---|
| 994 | n/a | struct timespec res; |
|---|
| 995 | n/a | info->implementation = function; |
|---|
| 996 | n/a | info->monotonic = 1; |
|---|
| 997 | n/a | info->adjustable = 0; |
|---|
| 998 | n/a | if (clock_getres(clk_id, &res) == 0) |
|---|
| 999 | n/a | info->resolution = res.tv_sec + res.tv_nsec * 1e-9; |
|---|
| 1000 | n/a | else |
|---|
| 1001 | n/a | info->resolution = 1e-9; |
|---|
| 1002 | n/a | } |
|---|
| 1003 | n/a | return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9); |
|---|
| 1004 | n/a | } |
|---|
| 1005 | n/a | #endif |
|---|
| 1006 | n/a | |
|---|
| 1007 | n/a | #if defined(HAVE_SYS_RESOURCE_H) |
|---|
| 1008 | n/a | if (getrusage(RUSAGE_SELF, &ru) == 0) { |
|---|
| 1009 | n/a | double total; |
|---|
| 1010 | n/a | total = ru.ru_utime.tv_sec + ru.ru_utime.tv_usec * 1e-6; |
|---|
| 1011 | n/a | total += ru.ru_stime.tv_sec + ru.ru_stime.tv_usec * 1e-6; |
|---|
| 1012 | n/a | if (info) { |
|---|
| 1013 | n/a | info->implementation = "getrusage(RUSAGE_SELF)"; |
|---|
| 1014 | n/a | info->monotonic = 1; |
|---|
| 1015 | n/a | info->adjustable = 0; |
|---|
| 1016 | n/a | info->resolution = 1e-6; |
|---|
| 1017 | n/a | } |
|---|
| 1018 | n/a | return PyFloat_FromDouble(total); |
|---|
| 1019 | n/a | } |
|---|
| 1020 | n/a | #endif |
|---|
| 1021 | n/a | |
|---|
| 1022 | n/a | #ifdef HAVE_TIMES |
|---|
| 1023 | n/a | if (times(&t) != (clock_t)-1) { |
|---|
| 1024 | n/a | double total; |
|---|
| 1025 | n/a | |
|---|
| 1026 | n/a | if (ticks_per_second == -1) { |
|---|
| 1027 | n/a | #if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
|---|
| 1028 | n/a | ticks_per_second = sysconf(_SC_CLK_TCK); |
|---|
| 1029 | n/a | if (ticks_per_second < 1) |
|---|
| 1030 | n/a | ticks_per_second = -1; |
|---|
| 1031 | n/a | #elif defined(HZ) |
|---|
| 1032 | n/a | ticks_per_second = HZ; |
|---|
| 1033 | n/a | #else |
|---|
| 1034 | n/a | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
|---|
| 1035 | n/a | #endif |
|---|
| 1036 | n/a | } |
|---|
| 1037 | n/a | |
|---|
| 1038 | n/a | if (ticks_per_second != -1) { |
|---|
| 1039 | n/a | total = (double)t.tms_utime / ticks_per_second; |
|---|
| 1040 | n/a | total += (double)t.tms_stime / ticks_per_second; |
|---|
| 1041 | n/a | if (info) { |
|---|
| 1042 | n/a | info->implementation = "times()"; |
|---|
| 1043 | n/a | info->monotonic = 1; |
|---|
| 1044 | n/a | info->adjustable = 0; |
|---|
| 1045 | n/a | info->resolution = 1.0 / ticks_per_second; |
|---|
| 1046 | n/a | } |
|---|
| 1047 | n/a | return PyFloat_FromDouble(total); |
|---|
| 1048 | n/a | } |
|---|
| 1049 | n/a | } |
|---|
| 1050 | n/a | #endif |
|---|
| 1051 | n/a | |
|---|
| 1052 | n/a | /* Currently, Python 3 requires clock() to build: see issue #22624 */ |
|---|
| 1053 | n/a | return floatclock(info); |
|---|
| 1054 | n/a | #endif |
|---|
| 1055 | n/a | } |
|---|
| 1056 | n/a | |
|---|
| 1057 | n/a | static PyObject * |
|---|
| 1058 | n/a | time_process_time(PyObject *self, PyObject *unused) |
|---|
| 1059 | n/a | { |
|---|
| 1060 | n/a | return py_process_time(NULL); |
|---|
| 1061 | n/a | } |
|---|
| 1062 | n/a | |
|---|
| 1063 | n/a | PyDoc_STRVAR(process_time_doc, |
|---|
| 1064 | n/a | "process_time() -> float\n\ |
|---|
| 1065 | n/a | \n\ |
|---|
| 1066 | n/a | Process time for profiling: sum of the kernel and user-space CPU time."); |
|---|
| 1067 | n/a | |
|---|
| 1068 | n/a | |
|---|
| 1069 | n/a | static PyObject * |
|---|
| 1070 | n/a | time_get_clock_info(PyObject *self, PyObject *args) |
|---|
| 1071 | n/a | { |
|---|
| 1072 | n/a | char *name; |
|---|
| 1073 | n/a | _Py_clock_info_t info; |
|---|
| 1074 | n/a | PyObject *obj = NULL, *dict, *ns; |
|---|
| 1075 | n/a | |
|---|
| 1076 | n/a | if (!PyArg_ParseTuple(args, "s:get_clock_info", &name)) |
|---|
| 1077 | n/a | return NULL; |
|---|
| 1078 | n/a | |
|---|
| 1079 | n/a | #ifdef Py_DEBUG |
|---|
| 1080 | n/a | info.implementation = NULL; |
|---|
| 1081 | n/a | info.monotonic = -1; |
|---|
| 1082 | n/a | info.adjustable = -1; |
|---|
| 1083 | n/a | info.resolution = -1.0; |
|---|
| 1084 | n/a | #else |
|---|
| 1085 | n/a | info.implementation = ""; |
|---|
| 1086 | n/a | info.monotonic = 0; |
|---|
| 1087 | n/a | info.adjustable = 0; |
|---|
| 1088 | n/a | info.resolution = 1.0; |
|---|
| 1089 | n/a | #endif |
|---|
| 1090 | n/a | |
|---|
| 1091 | n/a | if (strcmp(name, "time") == 0) |
|---|
| 1092 | n/a | obj = floattime(&info); |
|---|
| 1093 | n/a | #ifdef PYCLOCK |
|---|
| 1094 | n/a | else if (strcmp(name, "clock") == 0) |
|---|
| 1095 | n/a | obj = pyclock(&info); |
|---|
| 1096 | n/a | #endif |
|---|
| 1097 | n/a | else if (strcmp(name, "monotonic") == 0) |
|---|
| 1098 | n/a | obj = pymonotonic(&info); |
|---|
| 1099 | n/a | else if (strcmp(name, "perf_counter") == 0) |
|---|
| 1100 | n/a | obj = perf_counter(&info); |
|---|
| 1101 | n/a | else if (strcmp(name, "process_time") == 0) |
|---|
| 1102 | n/a | obj = py_process_time(&info); |
|---|
| 1103 | n/a | else { |
|---|
| 1104 | n/a | PyErr_SetString(PyExc_ValueError, "unknown clock"); |
|---|
| 1105 | n/a | return NULL; |
|---|
| 1106 | n/a | } |
|---|
| 1107 | n/a | if (obj == NULL) |
|---|
| 1108 | n/a | return NULL; |
|---|
| 1109 | n/a | Py_DECREF(obj); |
|---|
| 1110 | n/a | |
|---|
| 1111 | n/a | dict = PyDict_New(); |
|---|
| 1112 | n/a | if (dict == NULL) |
|---|
| 1113 | n/a | return NULL; |
|---|
| 1114 | n/a | |
|---|
| 1115 | n/a | assert(info.implementation != NULL); |
|---|
| 1116 | n/a | obj = PyUnicode_FromString(info.implementation); |
|---|
| 1117 | n/a | if (obj == NULL) |
|---|
| 1118 | n/a | goto error; |
|---|
| 1119 | n/a | if (PyDict_SetItemString(dict, "implementation", obj) == -1) |
|---|
| 1120 | n/a | goto error; |
|---|
| 1121 | n/a | Py_CLEAR(obj); |
|---|
| 1122 | n/a | |
|---|
| 1123 | n/a | assert(info.monotonic != -1); |
|---|
| 1124 | n/a | obj = PyBool_FromLong(info.monotonic); |
|---|
| 1125 | n/a | if (obj == NULL) |
|---|
| 1126 | n/a | goto error; |
|---|
| 1127 | n/a | if (PyDict_SetItemString(dict, "monotonic", obj) == -1) |
|---|
| 1128 | n/a | goto error; |
|---|
| 1129 | n/a | Py_CLEAR(obj); |
|---|
| 1130 | n/a | |
|---|
| 1131 | n/a | assert(info.adjustable != -1); |
|---|
| 1132 | n/a | obj = PyBool_FromLong(info.adjustable); |
|---|
| 1133 | n/a | if (obj == NULL) |
|---|
| 1134 | n/a | goto error; |
|---|
| 1135 | n/a | if (PyDict_SetItemString(dict, "adjustable", obj) == -1) |
|---|
| 1136 | n/a | goto error; |
|---|
| 1137 | n/a | Py_CLEAR(obj); |
|---|
| 1138 | n/a | |
|---|
| 1139 | n/a | assert(info.resolution > 0.0); |
|---|
| 1140 | n/a | assert(info.resolution <= 1.0); |
|---|
| 1141 | n/a | obj = PyFloat_FromDouble(info.resolution); |
|---|
| 1142 | n/a | if (obj == NULL) |
|---|
| 1143 | n/a | goto error; |
|---|
| 1144 | n/a | if (PyDict_SetItemString(dict, "resolution", obj) == -1) |
|---|
| 1145 | n/a | goto error; |
|---|
| 1146 | n/a | Py_CLEAR(obj); |
|---|
| 1147 | n/a | |
|---|
| 1148 | n/a | ns = _PyNamespace_New(dict); |
|---|
| 1149 | n/a | Py_DECREF(dict); |
|---|
| 1150 | n/a | return ns; |
|---|
| 1151 | n/a | |
|---|
| 1152 | n/a | error: |
|---|
| 1153 | n/a | Py_DECREF(dict); |
|---|
| 1154 | n/a | Py_XDECREF(obj); |
|---|
| 1155 | n/a | return NULL; |
|---|
| 1156 | n/a | } |
|---|
| 1157 | n/a | |
|---|
| 1158 | n/a | PyDoc_STRVAR(get_clock_info_doc, |
|---|
| 1159 | n/a | "get_clock_info(name: str) -> dict\n\ |
|---|
| 1160 | n/a | \n\ |
|---|
| 1161 | n/a | Get information of the specified clock."); |
|---|
| 1162 | n/a | |
|---|
| 1163 | n/a | static void |
|---|
| 1164 | n/a | get_zone(char *zone, int n, struct tm *p) |
|---|
| 1165 | n/a | { |
|---|
| 1166 | n/a | #ifdef HAVE_STRUCT_TM_TM_ZONE |
|---|
| 1167 | n/a | strncpy(zone, p->tm_zone ? p->tm_zone : " ", n); |
|---|
| 1168 | n/a | #else |
|---|
| 1169 | n/a | tzset(); |
|---|
| 1170 | n/a | strftime(zone, n, "%Z", p); |
|---|
| 1171 | n/a | #endif |
|---|
| 1172 | n/a | } |
|---|
| 1173 | n/a | |
|---|
| 1174 | n/a | static int |
|---|
| 1175 | n/a | get_gmtoff(time_t t, struct tm *p) |
|---|
| 1176 | n/a | { |
|---|
| 1177 | n/a | #ifdef HAVE_STRUCT_TM_TM_ZONE |
|---|
| 1178 | n/a | return p->tm_gmtoff; |
|---|
| 1179 | n/a | #else |
|---|
| 1180 | n/a | return timegm(p) - t; |
|---|
| 1181 | n/a | #endif |
|---|
| 1182 | n/a | } |
|---|
| 1183 | n/a | |
|---|
| 1184 | n/a | static void |
|---|
| 1185 | n/a | PyInit_timezone(PyObject *m) { |
|---|
| 1186 | n/a | /* This code moved from PyInit_time wholesale to allow calling it from |
|---|
| 1187 | n/a | time_tzset. In the future, some parts of it can be moved back |
|---|
| 1188 | n/a | (for platforms that don't HAVE_WORKING_TZSET, when we know what they |
|---|
| 1189 | n/a | are), and the extraneous calls to tzset(3) should be removed. |
|---|
| 1190 | n/a | I haven't done this yet, as I don't want to change this code as |
|---|
| 1191 | n/a | little as possible when introducing the time.tzset and time.tzsetwall |
|---|
| 1192 | n/a | methods. This should simply be a method of doing the following once, |
|---|
| 1193 | n/a | at the top of this function and removing the call to tzset() from |
|---|
| 1194 | n/a | time_tzset(): |
|---|
| 1195 | n/a | |
|---|
| 1196 | n/a | #ifdef HAVE_TZSET |
|---|
| 1197 | n/a | tzset() |
|---|
| 1198 | n/a | #endif |
|---|
| 1199 | n/a | |
|---|
| 1200 | n/a | And I'm lazy and hate C so nyer. |
|---|
| 1201 | n/a | */ |
|---|
| 1202 | n/a | #if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__) |
|---|
| 1203 | n/a | PyObject *otz0, *otz1; |
|---|
| 1204 | n/a | tzset(); |
|---|
| 1205 | n/a | PyModule_AddIntConstant(m, "timezone", timezone); |
|---|
| 1206 | n/a | #ifdef HAVE_ALTZONE |
|---|
| 1207 | n/a | PyModule_AddIntConstant(m, "altzone", altzone); |
|---|
| 1208 | n/a | #else |
|---|
| 1209 | n/a | PyModule_AddIntConstant(m, "altzone", timezone-3600); |
|---|
| 1210 | n/a | #endif |
|---|
| 1211 | n/a | PyModule_AddIntConstant(m, "daylight", daylight); |
|---|
| 1212 | n/a | otz0 = PyUnicode_DecodeLocale(tzname[0], "surrogateescape"); |
|---|
| 1213 | n/a | otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape"); |
|---|
| 1214 | n/a | PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1)); |
|---|
| 1215 | n/a | #else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ |
|---|
| 1216 | n/a | { |
|---|
| 1217 | n/a | #define YEAR ((time_t)((365 * 24 + 6) * 3600)) |
|---|
| 1218 | n/a | time_t t; |
|---|
| 1219 | n/a | struct tm p; |
|---|
| 1220 | n/a | long janzone, julyzone; |
|---|
| 1221 | n/a | char janname[10], julyname[10]; |
|---|
| 1222 | n/a | t = (time((time_t *)0) / YEAR) * YEAR; |
|---|
| 1223 | n/a | _PyTime_localtime(t, &p); |
|---|
| 1224 | n/a | get_zone(janname, 9, &p); |
|---|
| 1225 | n/a | janzone = -get_gmtoff(t, &p); |
|---|
| 1226 | n/a | janname[9] = '\0'; |
|---|
| 1227 | n/a | t += YEAR/2; |
|---|
| 1228 | n/a | _PyTime_localtime(t, &p); |
|---|
| 1229 | n/a | get_zone(julyname, 9, &p); |
|---|
| 1230 | n/a | julyzone = -get_gmtoff(t, &p); |
|---|
| 1231 | n/a | julyname[9] = '\0'; |
|---|
| 1232 | n/a | |
|---|
| 1233 | n/a | if( janzone < julyzone ) { |
|---|
| 1234 | n/a | /* DST is reversed in the southern hemisphere */ |
|---|
| 1235 | n/a | PyModule_AddIntConstant(m, "timezone", julyzone); |
|---|
| 1236 | n/a | PyModule_AddIntConstant(m, "altzone", janzone); |
|---|
| 1237 | n/a | PyModule_AddIntConstant(m, "daylight", |
|---|
| 1238 | n/a | janzone != julyzone); |
|---|
| 1239 | n/a | PyModule_AddObject(m, "tzname", |
|---|
| 1240 | n/a | Py_BuildValue("(zz)", |
|---|
| 1241 | n/a | julyname, janname)); |
|---|
| 1242 | n/a | } else { |
|---|
| 1243 | n/a | PyModule_AddIntConstant(m, "timezone", janzone); |
|---|
| 1244 | n/a | PyModule_AddIntConstant(m, "altzone", julyzone); |
|---|
| 1245 | n/a | PyModule_AddIntConstant(m, "daylight", |
|---|
| 1246 | n/a | janzone != julyzone); |
|---|
| 1247 | n/a | PyModule_AddObject(m, "tzname", |
|---|
| 1248 | n/a | Py_BuildValue("(zz)", |
|---|
| 1249 | n/a | janname, julyname)); |
|---|
| 1250 | n/a | } |
|---|
| 1251 | n/a | } |
|---|
| 1252 | n/a | #ifdef __CYGWIN__ |
|---|
| 1253 | n/a | tzset(); |
|---|
| 1254 | n/a | PyModule_AddIntConstant(m, "timezone", _timezone); |
|---|
| 1255 | n/a | PyModule_AddIntConstant(m, "altzone", _timezone-3600); |
|---|
| 1256 | n/a | PyModule_AddIntConstant(m, "daylight", _daylight); |
|---|
| 1257 | n/a | PyModule_AddObject(m, "tzname", |
|---|
| 1258 | n/a | Py_BuildValue("(zz)", _tzname[0], _tzname[1])); |
|---|
| 1259 | n/a | #endif /* __CYGWIN__ */ |
|---|
| 1260 | n/a | #endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ |
|---|
| 1261 | n/a | } |
|---|
| 1262 | n/a | |
|---|
| 1263 | n/a | |
|---|
| 1264 | n/a | static PyMethodDef time_methods[] = { |
|---|
| 1265 | n/a | {"time", time_time, METH_NOARGS, time_doc}, |
|---|
| 1266 | n/a | #ifdef PYCLOCK |
|---|
| 1267 | n/a | {"clock", time_clock, METH_NOARGS, clock_doc}, |
|---|
| 1268 | n/a | #endif |
|---|
| 1269 | n/a | #ifdef HAVE_CLOCK_GETTIME |
|---|
| 1270 | n/a | {"clock_gettime", time_clock_gettime, METH_VARARGS, clock_gettime_doc}, |
|---|
| 1271 | n/a | #endif |
|---|
| 1272 | n/a | #ifdef HAVE_CLOCK_SETTIME |
|---|
| 1273 | n/a | {"clock_settime", time_clock_settime, METH_VARARGS, clock_settime_doc}, |
|---|
| 1274 | n/a | #endif |
|---|
| 1275 | n/a | #ifdef HAVE_CLOCK_GETRES |
|---|
| 1276 | n/a | {"clock_getres", time_clock_getres, METH_VARARGS, clock_getres_doc}, |
|---|
| 1277 | n/a | #endif |
|---|
| 1278 | n/a | {"sleep", time_sleep, METH_O, sleep_doc}, |
|---|
| 1279 | n/a | {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc}, |
|---|
| 1280 | n/a | {"localtime", time_localtime, METH_VARARGS, localtime_doc}, |
|---|
| 1281 | n/a | {"asctime", time_asctime, METH_VARARGS, asctime_doc}, |
|---|
| 1282 | n/a | {"ctime", time_ctime, METH_VARARGS, ctime_doc}, |
|---|
| 1283 | n/a | #ifdef HAVE_MKTIME |
|---|
| 1284 | n/a | {"mktime", time_mktime, METH_O, mktime_doc}, |
|---|
| 1285 | n/a | #endif |
|---|
| 1286 | n/a | #ifdef HAVE_STRFTIME |
|---|
| 1287 | n/a | {"strftime", time_strftime, METH_VARARGS, strftime_doc}, |
|---|
| 1288 | n/a | #endif |
|---|
| 1289 | n/a | {"strptime", time_strptime, METH_VARARGS, strptime_doc}, |
|---|
| 1290 | n/a | #ifdef HAVE_WORKING_TZSET |
|---|
| 1291 | n/a | {"tzset", time_tzset, METH_NOARGS, tzset_doc}, |
|---|
| 1292 | n/a | #endif |
|---|
| 1293 | n/a | {"monotonic", time_monotonic, METH_NOARGS, monotonic_doc}, |
|---|
| 1294 | n/a | {"process_time", time_process_time, METH_NOARGS, process_time_doc}, |
|---|
| 1295 | n/a | {"perf_counter", time_perf_counter, METH_NOARGS, perf_counter_doc}, |
|---|
| 1296 | n/a | {"get_clock_info", time_get_clock_info, METH_VARARGS, get_clock_info_doc}, |
|---|
| 1297 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 1298 | n/a | }; |
|---|
| 1299 | n/a | |
|---|
| 1300 | n/a | |
|---|
| 1301 | n/a | PyDoc_STRVAR(module_doc, |
|---|
| 1302 | n/a | "This module provides various functions to manipulate time values.\n\ |
|---|
| 1303 | n/a | \n\ |
|---|
| 1304 | n/a | There are two standard representations of time. One is the number\n\ |
|---|
| 1305 | n/a | of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\ |
|---|
| 1306 | n/a | or a floating point number (to represent fractions of seconds).\n\ |
|---|
| 1307 | n/a | The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\ |
|---|
| 1308 | n/a | The actual value can be retrieved by calling gmtime(0).\n\ |
|---|
| 1309 | n/a | \n\ |
|---|
| 1310 | n/a | The other representation is a tuple of 9 integers giving local time.\n\ |
|---|
| 1311 | n/a | The tuple items are:\n\ |
|---|
| 1312 | n/a | year (including century, e.g. 1998)\n\ |
|---|
| 1313 | n/a | month (1-12)\n\ |
|---|
| 1314 | n/a | day (1-31)\n\ |
|---|
| 1315 | n/a | hours (0-23)\n\ |
|---|
| 1316 | n/a | minutes (0-59)\n\ |
|---|
| 1317 | n/a | seconds (0-59)\n\ |
|---|
| 1318 | n/a | weekday (0-6, Monday is 0)\n\ |
|---|
| 1319 | n/a | Julian day (day in the year, 1-366)\n\ |
|---|
| 1320 | n/a | DST (Daylight Savings Time) flag (-1, 0 or 1)\n\ |
|---|
| 1321 | n/a | If the DST flag is 0, the time is given in the regular time zone;\n\ |
|---|
| 1322 | n/a | if it is 1, the time is given in the DST time zone;\n\ |
|---|
| 1323 | n/a | if it is -1, mktime() should guess based on the date and time.\n\ |
|---|
| 1324 | n/a | \n\ |
|---|
| 1325 | n/a | Variables:\n\ |
|---|
| 1326 | n/a | \n\ |
|---|
| 1327 | n/a | timezone -- difference in seconds between UTC and local standard time\n\ |
|---|
| 1328 | n/a | altzone -- difference in seconds between UTC and local DST time\n\ |
|---|
| 1329 | n/a | daylight -- whether local time should reflect DST\n\ |
|---|
| 1330 | n/a | tzname -- tuple of (standard time zone name, DST time zone name)\n\ |
|---|
| 1331 | n/a | \n\ |
|---|
| 1332 | n/a | Functions:\n\ |
|---|
| 1333 | n/a | \n\ |
|---|
| 1334 | n/a | time() -- return current time in seconds since the Epoch as a float\n\ |
|---|
| 1335 | n/a | clock() -- return CPU time since process start as a float\n\ |
|---|
| 1336 | n/a | sleep() -- delay for a number of seconds given as a float\n\ |
|---|
| 1337 | n/a | gmtime() -- convert seconds since Epoch to UTC tuple\n\ |
|---|
| 1338 | n/a | localtime() -- convert seconds since Epoch to local time tuple\n\ |
|---|
| 1339 | n/a | asctime() -- convert time tuple to string\n\ |
|---|
| 1340 | n/a | ctime() -- convert time in seconds to string\n\ |
|---|
| 1341 | n/a | mktime() -- convert local time tuple to seconds since Epoch\n\ |
|---|
| 1342 | n/a | strftime() -- convert time tuple to string according to format specification\n\ |
|---|
| 1343 | n/a | strptime() -- parse string to time tuple according to format specification\n\ |
|---|
| 1344 | n/a | tzset() -- change the local timezone"); |
|---|
| 1345 | n/a | |
|---|
| 1346 | n/a | |
|---|
| 1347 | n/a | |
|---|
| 1348 | n/a | static struct PyModuleDef timemodule = { |
|---|
| 1349 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 1350 | n/a | "time", |
|---|
| 1351 | n/a | module_doc, |
|---|
| 1352 | n/a | -1, |
|---|
| 1353 | n/a | time_methods, |
|---|
| 1354 | n/a | NULL, |
|---|
| 1355 | n/a | NULL, |
|---|
| 1356 | n/a | NULL, |
|---|
| 1357 | n/a | NULL |
|---|
| 1358 | n/a | }; |
|---|
| 1359 | n/a | |
|---|
| 1360 | n/a | PyMODINIT_FUNC |
|---|
| 1361 | n/a | PyInit_time(void) |
|---|
| 1362 | n/a | { |
|---|
| 1363 | n/a | PyObject *m; |
|---|
| 1364 | n/a | m = PyModule_Create(&timemodule); |
|---|
| 1365 | n/a | if (m == NULL) |
|---|
| 1366 | n/a | return NULL; |
|---|
| 1367 | n/a | |
|---|
| 1368 | n/a | /* Set, or reset, module variables like time.timezone */ |
|---|
| 1369 | n/a | PyInit_timezone(m); |
|---|
| 1370 | n/a | |
|---|
| 1371 | n/a | #ifdef CLOCK_REALTIME |
|---|
| 1372 | n/a | PyModule_AddIntMacro(m, CLOCK_REALTIME); |
|---|
| 1373 | n/a | #endif |
|---|
| 1374 | n/a | #ifdef CLOCK_MONOTONIC |
|---|
| 1375 | n/a | PyModule_AddIntMacro(m, CLOCK_MONOTONIC); |
|---|
| 1376 | n/a | #endif |
|---|
| 1377 | n/a | #ifdef CLOCK_MONOTONIC_RAW |
|---|
| 1378 | n/a | PyModule_AddIntMacro(m, CLOCK_MONOTONIC_RAW); |
|---|
| 1379 | n/a | #endif |
|---|
| 1380 | n/a | #ifdef CLOCK_HIGHRES |
|---|
| 1381 | n/a | PyModule_AddIntMacro(m, CLOCK_HIGHRES); |
|---|
| 1382 | n/a | #endif |
|---|
| 1383 | n/a | #ifdef CLOCK_PROCESS_CPUTIME_ID |
|---|
| 1384 | n/a | PyModule_AddIntMacro(m, CLOCK_PROCESS_CPUTIME_ID); |
|---|
| 1385 | n/a | #endif |
|---|
| 1386 | n/a | #ifdef CLOCK_THREAD_CPUTIME_ID |
|---|
| 1387 | n/a | PyModule_AddIntMacro(m, CLOCK_THREAD_CPUTIME_ID); |
|---|
| 1388 | n/a | #endif |
|---|
| 1389 | n/a | |
|---|
| 1390 | n/a | if (!initialized) { |
|---|
| 1391 | n/a | if (PyStructSequence_InitType2(&StructTimeType, |
|---|
| 1392 | n/a | &struct_time_type_desc) < 0) |
|---|
| 1393 | n/a | return NULL; |
|---|
| 1394 | n/a | } |
|---|
| 1395 | n/a | Py_INCREF(&StructTimeType); |
|---|
| 1396 | n/a | PyModule_AddIntConstant(m, "_STRUCT_TM_ITEMS", 11); |
|---|
| 1397 | n/a | PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType); |
|---|
| 1398 | n/a | initialized = 1; |
|---|
| 1399 | n/a | return m; |
|---|
| 1400 | n/a | } |
|---|
| 1401 | n/a | |
|---|
| 1402 | n/a | static PyObject* |
|---|
| 1403 | n/a | floattime(_Py_clock_info_t *info) |
|---|
| 1404 | n/a | { |
|---|
| 1405 | n/a | _PyTime_t t; |
|---|
| 1406 | n/a | double d; |
|---|
| 1407 | n/a | if (_PyTime_GetSystemClockWithInfo(&t, info) < 0) { |
|---|
| 1408 | n/a | assert(info != NULL); |
|---|
| 1409 | n/a | return NULL; |
|---|
| 1410 | n/a | } |
|---|
| 1411 | n/a | d = _PyTime_AsSecondsDouble(t); |
|---|
| 1412 | n/a | return PyFloat_FromDouble(d); |
|---|
| 1413 | n/a | } |
|---|
| 1414 | n/a | |
|---|
| 1415 | n/a | |
|---|
| 1416 | n/a | /* Implement pysleep() for various platforms. |
|---|
| 1417 | n/a | When interrupted (or when another error occurs), return -1 and |
|---|
| 1418 | n/a | set an exception; else return 0. */ |
|---|
| 1419 | n/a | |
|---|
| 1420 | n/a | static int |
|---|
| 1421 | n/a | pysleep(_PyTime_t secs) |
|---|
| 1422 | n/a | { |
|---|
| 1423 | n/a | _PyTime_t deadline, monotonic; |
|---|
| 1424 | n/a | #ifndef MS_WINDOWS |
|---|
| 1425 | n/a | struct timeval timeout; |
|---|
| 1426 | n/a | int err = 0; |
|---|
| 1427 | n/a | #else |
|---|
| 1428 | n/a | _PyTime_t millisecs; |
|---|
| 1429 | n/a | unsigned long ul_millis; |
|---|
| 1430 | n/a | DWORD rc; |
|---|
| 1431 | n/a | HANDLE hInterruptEvent; |
|---|
| 1432 | n/a | #endif |
|---|
| 1433 | n/a | |
|---|
| 1434 | n/a | deadline = _PyTime_GetMonotonicClock() + secs; |
|---|
| 1435 | n/a | |
|---|
| 1436 | n/a | do { |
|---|
| 1437 | n/a | #ifndef MS_WINDOWS |
|---|
| 1438 | n/a | if (_PyTime_AsTimeval(secs, &timeout, _PyTime_ROUND_CEILING) < 0) |
|---|
| 1439 | n/a | return -1; |
|---|
| 1440 | n/a | |
|---|
| 1441 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 1442 | n/a | err = select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout); |
|---|
| 1443 | n/a | Py_END_ALLOW_THREADS |
|---|
| 1444 | n/a | |
|---|
| 1445 | n/a | if (err == 0) |
|---|
| 1446 | n/a | break; |
|---|
| 1447 | n/a | |
|---|
| 1448 | n/a | if (errno != EINTR) { |
|---|
| 1449 | n/a | PyErr_SetFromErrno(PyExc_OSError); |
|---|
| 1450 | n/a | return -1; |
|---|
| 1451 | n/a | } |
|---|
| 1452 | n/a | #else |
|---|
| 1453 | n/a | millisecs = _PyTime_AsMilliseconds(secs, _PyTime_ROUND_CEILING); |
|---|
| 1454 | n/a | if (millisecs > (double)ULONG_MAX) { |
|---|
| 1455 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 1456 | n/a | "sleep length is too large"); |
|---|
| 1457 | n/a | return -1; |
|---|
| 1458 | n/a | } |
|---|
| 1459 | n/a | |
|---|
| 1460 | n/a | /* Allow sleep(0) to maintain win32 semantics, and as decreed |
|---|
| 1461 | n/a | * by Guido, only the main thread can be interrupted. |
|---|
| 1462 | n/a | */ |
|---|
| 1463 | n/a | ul_millis = (unsigned long)millisecs; |
|---|
| 1464 | n/a | if (ul_millis == 0 || !_PyOS_IsMainThread()) { |
|---|
| 1465 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 1466 | n/a | Sleep(ul_millis); |
|---|
| 1467 | n/a | Py_END_ALLOW_THREADS |
|---|
| 1468 | n/a | break; |
|---|
| 1469 | n/a | } |
|---|
| 1470 | n/a | |
|---|
| 1471 | n/a | hInterruptEvent = _PyOS_SigintEvent(); |
|---|
| 1472 | n/a | ResetEvent(hInterruptEvent); |
|---|
| 1473 | n/a | |
|---|
| 1474 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 1475 | n/a | rc = WaitForSingleObjectEx(hInterruptEvent, ul_millis, FALSE); |
|---|
| 1476 | n/a | Py_END_ALLOW_THREADS |
|---|
| 1477 | n/a | |
|---|
| 1478 | n/a | if (rc != WAIT_OBJECT_0) |
|---|
| 1479 | n/a | break; |
|---|
| 1480 | n/a | #endif |
|---|
| 1481 | n/a | |
|---|
| 1482 | n/a | /* sleep was interrupted by SIGINT */ |
|---|
| 1483 | n/a | if (PyErr_CheckSignals()) |
|---|
| 1484 | n/a | return -1; |
|---|
| 1485 | n/a | |
|---|
| 1486 | n/a | monotonic = _PyTime_GetMonotonicClock(); |
|---|
| 1487 | n/a | secs = deadline - monotonic; |
|---|
| 1488 | n/a | if (secs < 0) |
|---|
| 1489 | n/a | break; |
|---|
| 1490 | n/a | /* retry with the recomputed delay */ |
|---|
| 1491 | n/a | } while (1); |
|---|
| 1492 | n/a | |
|---|
| 1493 | n/a | return 0; |
|---|
| 1494 | n/a | } |
|---|