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