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