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