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