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