Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Time module */ |
| 3 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 5 | #include "structseq.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 7 | #include <ctype.h> |
| 8 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9 | #include <sys/types.h> |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11 | #ifdef QUICKWIN |
| 12 | #include <io.h> |
| 13 | #endif |
| 14 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 15 | #ifdef HAVE_FTIME |
| 16 | #include <sys/timeb.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 17 | #if !defined(MS_WINDOWS) && !defined(PYOS_OS2) |
Thomas Wouters | bd4bc4e | 2000-07-22 23:57:55 +0000 | [diff] [blame] | 18 | extern int ftime(struct timeb *); |
Guido van Rossum | 5217457 | 1996-12-09 18:38:52 +0000 | [diff] [blame] | 19 | #endif /* MS_WINDOWS */ |
| 20 | #endif /* HAVE_FTIME */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 22 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 23 | #include <i86.h> |
| 24 | #else |
Guido van Rossum | cac6c72 | 1996-09-06 13:34:02 +0000 | [diff] [blame] | 25 | #ifdef MS_WINDOWS |
Mark Hammond | 975e392 | 2002-07-16 01:29:19 +0000 | [diff] [blame] | 26 | #define WIN32_LEAN_AND_MEAN |
Guido van Rossum | 258ccd4 | 2001-03-02 06:53:29 +0000 | [diff] [blame] | 27 | #include <windows.h> |
Mark Hammond | 975e392 | 2002-07-16 01:29:19 +0000 | [diff] [blame] | 28 | #include "pythread.h" |
| 29 | |
| 30 | /* helper to allow us to interrupt sleep() on Windows*/ |
| 31 | static HANDLE hInterruptEvent = NULL; |
| 32 | static BOOL WINAPI PyCtrlHandler(DWORD dwCtrlType) |
| 33 | { |
| 34 | SetEvent(hInterruptEvent); |
| 35 | /* allow other default handlers to be called. |
| 36 | Default Python handler will setup the |
| 37 | KeyboardInterrupt exception. |
| 38 | */ |
| 39 | return FALSE; |
| 40 | } |
| 41 | static long main_thread; |
| 42 | |
| 43 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 44 | #if defined(__BORLANDC__) |
Guido van Rossum | b2fb364 | 1996-09-07 00:47:35 +0000 | [diff] [blame] | 45 | /* These overrides not needed for Win32 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 46 | #define timezone _timezone |
Guido van Rossum | cc08112 | 1995-03-14 15:05:41 +0000 | [diff] [blame] | 47 | #define tzname _tzname |
| 48 | #define daylight _daylight |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 49 | #endif /* __BORLANDC__ */ |
Guido van Rossum | cac6c72 | 1996-09-06 13:34:02 +0000 | [diff] [blame] | 50 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 51 | #endif /* !__WATCOMC__ || __QNX__ */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 52 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 53 | #if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__) |
Fred Drake | dfb4ebd | 2000-06-29 20:56:28 +0000 | [diff] [blame] | 54 | /* Win32 has better clock replacement |
| 55 | XXX Win64 does not yet, but might when the platform matures. */ |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 56 | #undef HAVE_CLOCK /* We have our own version down below */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 57 | #endif /* MS_WINDOWS && !MS_WIN64 */ |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 58 | |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 59 | #if defined(PYOS_OS2) |
| 60 | #define INCL_DOS |
| 61 | #define INCL_ERRORS |
| 62 | #include <os2.h> |
| 63 | #endif |
| 64 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 65 | #if defined(PYCC_VACPP) |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 66 | #include <sys/time.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 67 | #endif |
| 68 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 69 | #ifdef __BEOS__ |
Fred Drake | 56221a7 | 2000-08-15 18:52:33 +0000 | [diff] [blame] | 70 | #include <time.h> |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 71 | /* For bigtime_t, snooze(). - [cjh] */ |
| 72 | #include <support/SupportDefs.h> |
| 73 | #include <kernel/OS.h> |
| 74 | #endif |
| 75 | |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 76 | #ifdef RISCOS |
| 77 | extern int riscos_sleep(double); |
| 78 | #endif |
| 79 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 80 | /* Forward declarations */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 81 | static int floatsleep(double); |
Thomas Wouters | ed77bac | 2000-07-24 15:26:39 +0000 | [diff] [blame] | 82 | static double floattime(void); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 84 | /* For Y2K check */ |
| 85 | static PyObject *moddict; |
| 86 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 87 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 88 | time_time(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 89 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 90 | double secs; |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 91 | if (!PyArg_ParseTuple(args, ":time")) |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 92 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 93 | secs = floattime(); |
| 94 | if (secs == 0.0) { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 95 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 96 | return NULL; |
| 97 | } |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 98 | return PyFloat_FromDouble(secs); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 101 | PyDoc_STRVAR(time_doc, |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 102 | "time() -> floating point number\n\ |
| 103 | \n\ |
| 104 | Return the current time in seconds since the Epoch.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 105 | 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] | 106 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 107 | #ifdef HAVE_CLOCK |
| 108 | |
| 109 | #ifndef CLOCKS_PER_SEC |
Guido van Rossum | 1b66a4f | 1996-02-25 04:50:33 +0000 | [diff] [blame] | 110 | #ifdef CLK_TCK |
| 111 | #define CLOCKS_PER_SEC CLK_TCK |
| 112 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 113 | #define CLOCKS_PER_SEC 1000000 |
| 114 | #endif |
Guido van Rossum | 1b66a4f | 1996-02-25 04:50:33 +0000 | [diff] [blame] | 115 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 116 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 117 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 118 | time_clock(PyObject *self, PyObject *args) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 119 | { |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 120 | if (!PyArg_ParseTuple(args, ":clock")) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | return NULL; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 122 | return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 123 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 124 | #endif /* HAVE_CLOCK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 125 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 126 | #if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__) |
Mark Hammond | 7ba5e81 | 2002-02-12 04:02:33 +0000 | [diff] [blame] | 127 | /* Due to Mark Hammond and Tim Peters */ |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 128 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 129 | time_clock(PyObject *self, PyObject *args) |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 130 | { |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 131 | static LARGE_INTEGER ctrStart; |
Mark Hammond | 7ba5e81 | 2002-02-12 04:02:33 +0000 | [diff] [blame] | 132 | static double divisor = 0.0; |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 133 | LARGE_INTEGER now; |
Mark Hammond | 7ba5e81 | 2002-02-12 04:02:33 +0000 | [diff] [blame] | 134 | double diff; |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 135 | |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 136 | if (!PyArg_ParseTuple(args, ":clock")) |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 137 | return NULL; |
| 138 | |
Mark Hammond | 7ba5e81 | 2002-02-12 04:02:33 +0000 | [diff] [blame] | 139 | if (divisor == 0.0) { |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 140 | LARGE_INTEGER freq; |
| 141 | QueryPerformanceCounter(&ctrStart); |
| 142 | if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) { |
Mark Hammond | 7ba5e81 | 2002-02-12 04:02:33 +0000 | [diff] [blame] | 143 | /* Unlikely to happen - this works on all intel |
| 144 | machines at least! Revert to clock() */ |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 145 | return PyFloat_FromDouble(clock()); |
| 146 | } |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 147 | divisor = (double)freq.QuadPart; |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 148 | } |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 149 | QueryPerformanceCounter(&now); |
| 150 | diff = (double)(now.QuadPart - ctrStart.QuadPart); |
Mark Hammond | 7ba5e81 | 2002-02-12 04:02:33 +0000 | [diff] [blame] | 151 | return PyFloat_FromDouble(diff / divisor); |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 152 | } |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 154 | #define HAVE_CLOCK /* So it gets included in the methods */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 155 | #endif /* MS_WINDOWS && !MS_WIN64 */ |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 157 | #ifdef HAVE_CLOCK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 158 | PyDoc_STRVAR(clock_doc, |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 159 | "clock() -> floating point number\n\ |
| 160 | \n\ |
| 161 | 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] | 162 | the first call to clock(). This has as much precision as the system\n\ |
| 163 | records."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 164 | #endif |
| 165 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 166 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 167 | time_sleep(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 168 | { |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 169 | double secs; |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 170 | if (!PyArg_ParseTuple(args, "d:sleep", &secs)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 171 | return NULL; |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 172 | if (floatsleep(secs) != 0) |
| 173 | return NULL; |
| 174 | Py_INCREF(Py_None); |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 175 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 178 | PyDoc_STRVAR(sleep_doc, |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 179 | "sleep(seconds)\n\ |
| 180 | \n\ |
| 181 | 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] | 182 | a floating point number for subsecond precision."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 183 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 184 | static PyStructSequence_Field struct_time_type_fields[] = { |
| 185 | {"tm_year", NULL}, |
| 186 | {"tm_mon", NULL}, |
| 187 | {"tm_mday", NULL}, |
| 188 | {"tm_hour", NULL}, |
| 189 | {"tm_min", NULL}, |
| 190 | {"tm_sec", NULL}, |
| 191 | {"tm_wday", NULL}, |
| 192 | {"tm_yday", NULL}, |
| 193 | {"tm_isdst", NULL}, |
| 194 | {0} |
| 195 | }; |
| 196 | |
| 197 | static PyStructSequence_Desc struct_time_type_desc = { |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 198 | "time.struct_time", |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 199 | NULL, |
| 200 | struct_time_type_fields, |
| 201 | 9, |
| 202 | }; |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 204 | static PyTypeObject StructTimeType; |
| 205 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 206 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 207 | tmtotuple(struct tm *p) |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 208 | { |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 209 | PyObject *v = PyStructSequence_New(&StructTimeType); |
| 210 | if (v == NULL) |
| 211 | return NULL; |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 212 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 213 | #define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val)) |
| 214 | |
| 215 | SET(0, p->tm_year + 1900); |
| 216 | SET(1, p->tm_mon + 1); /* Want January == 1 */ |
| 217 | SET(2, p->tm_mday); |
| 218 | SET(3, p->tm_hour); |
| 219 | SET(4, p->tm_min); |
| 220 | SET(5, p->tm_sec); |
| 221 | SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */ |
| 222 | SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */ |
| 223 | SET(8, p->tm_isdst); |
| 224 | #undef SET |
| 225 | if (PyErr_Occurred()) { |
| 226 | Py_XDECREF(v); |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | return v; |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 234 | time_convert(time_t when, struct tm * (*function)(const time_t *)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 235 | { |
Guido van Rossum | 6e8583d | 1996-10-08 14:19:52 +0000 | [diff] [blame] | 236 | struct tm *p; |
| 237 | errno = 0; |
| 238 | p = function(&when); |
| 239 | if (p == NULL) { |
| 240 | #ifdef EINVAL |
Guido van Rossum | 0b1ff66 | 1996-11-02 17:31:22 +0000 | [diff] [blame] | 241 | if (errno == 0) |
Guido van Rossum | 6e8583d | 1996-10-08 14:19:52 +0000 | [diff] [blame] | 242 | errno = EINVAL; |
| 243 | #endif |
Tim Peters | 8b19a93 | 2003-01-17 20:08:54 +0000 | [diff] [blame] | 244 | return PyErr_SetFromErrno(PyExc_ValueError); |
Guido van Rossum | 6e8583d | 1996-10-08 14:19:52 +0000 | [diff] [blame] | 245 | } |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 246 | return tmtotuple(p); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 249 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 250 | time_gmtime(PyObject *self, PyObject *args) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 251 | { |
| 252 | double when; |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 253 | if (PyTuple_Size(args) == 0) |
| 254 | when = floattime(); |
| 255 | if (!PyArg_ParseTuple(args, "|d:gmtime", &when)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 256 | return NULL; |
| 257 | return time_convert((time_t)when, gmtime); |
| 258 | } |
| 259 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 260 | PyDoc_STRVAR(gmtime_doc, |
Fred Drake | 193a3f6 | 2002-03-12 21:38:49 +0000 | [diff] [blame] | 261 | "gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,\n\ |
| 262 | tm_sec, tm_wday, tm_yday, tm_isdst)\n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 263 | \n\ |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 264 | 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] | 265 | 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] | 266 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 267 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 268 | time_localtime(PyObject *self, PyObject *args) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 269 | { |
| 270 | double when; |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 271 | if (PyTuple_Size(args) == 0) |
| 272 | when = floattime(); |
| 273 | if (!PyArg_ParseTuple(args, "|d:localtime", &when)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 274 | return NULL; |
| 275 | return time_convert((time_t)when, localtime); |
| 276 | } |
| 277 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 278 | PyDoc_STRVAR(localtime_doc, |
Fred Drake | 193a3f6 | 2002-03-12 21:38:49 +0000 | [diff] [blame] | 279 | "localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)\n\ |
Martin v. Löwis | b3cfc1d | 2001-12-02 12:27:43 +0000 | [diff] [blame] | 280 | \n\ |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 281 | 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] | 282 | When 'seconds' is not passed in, convert the current time instead."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 283 | |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 284 | static int |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 285 | gettmarg(PyObject *args, struct tm *p) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 286 | { |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 287 | int y; |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 288 | memset((void *) p, '\0', sizeof(struct tm)); |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 289 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 290 | if (!PyArg_Parse(args, "(iiiiiiiii)", |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 291 | &y, |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 292 | &p->tm_mon, |
| 293 | &p->tm_mday, |
| 294 | &p->tm_hour, |
| 295 | &p->tm_min, |
| 296 | &p->tm_sec, |
| 297 | &p->tm_wday, |
| 298 | &p->tm_yday, |
| 299 | &p->tm_isdst)) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 300 | return 0; |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 301 | if (y < 1900) { |
| 302 | PyObject *accept = PyDict_GetItemString(moddict, |
| 303 | "accept2dyear"); |
| 304 | if (accept == NULL || !PyInt_Check(accept) || |
| 305 | PyInt_AsLong(accept) == 0) { |
| 306 | PyErr_SetString(PyExc_ValueError, |
| 307 | "year >= 1900 required"); |
| 308 | return 0; |
| 309 | } |
| 310 | if (69 <= y && y <= 99) |
| 311 | y += 1900; |
| 312 | else if (0 <= y && y <= 68) |
| 313 | y += 2000; |
| 314 | else { |
| 315 | PyErr_SetString(PyExc_ValueError, |
Skip Montanaro | 1a10aac | 2001-08-22 12:39:16 +0000 | [diff] [blame] | 316 | "year out of range"); |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | } |
| 320 | p->tm_year = y - 1900; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 321 | p->tm_mon--; |
| 322 | p->tm_wday = (p->tm_wday + 1) % 7; |
| 323 | p->tm_yday--; |
| 324 | return 1; |
| 325 | } |
| 326 | |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 327 | #ifdef HAVE_STRFTIME |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 328 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 329 | time_strftime(PyObject *self, PyObject *args) |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 330 | { |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 331 | PyObject *tup = NULL; |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 332 | struct tm buf; |
| 333 | const char *fmt; |
Guido van Rossum | fa48116 | 2000-06-28 21:33:59 +0000 | [diff] [blame] | 334 | size_t fmtlen, buflen; |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 335 | char *outbuf = 0; |
Guido van Rossum | fa48116 | 2000-06-28 21:33:59 +0000 | [diff] [blame] | 336 | size_t i; |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 337 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 338 | memset((void *) &buf, '\0', sizeof(buf)); |
Guido van Rossum | 1f41f84 | 1998-04-27 19:04:26 +0000 | [diff] [blame] | 339 | |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 340 | if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup)) |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 341 | return NULL; |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 342 | |
| 343 | if (tup == NULL) { |
| 344 | time_t tt = time(NULL); |
| 345 | buf = *localtime(&tt); |
| 346 | } else if (!gettmarg(tup, &buf)) |
| 347 | return NULL; |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 348 | |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 349 | /* Checks added to make sure strftime() does not crash Python by |
| 350 | indexing blindly into some array for a textual representation |
| 351 | by some bad index (fixes bug #897625). |
| 352 | |
| 353 | No check for year since handled in gettmarg(). |
| 354 | */ |
| 355 | if (buf.tm_mon < 0 || buf.tm_mon > 11) { |
| 356 | PyErr_SetString(PyExc_ValueError, "month out of range"); |
| 357 | return NULL; |
| 358 | } |
| 359 | if (buf.tm_mday < 1 || buf.tm_mday > 31) { |
| 360 | PyErr_SetString(PyExc_ValueError, "day of month out of range"); |
| 361 | return NULL; |
| 362 | } |
| 363 | if (buf.tm_hour < 0 || buf.tm_hour > 23) { |
| 364 | PyErr_SetString(PyExc_ValueError, "hour out of range"); |
| 365 | return NULL; |
| 366 | } |
| 367 | if (buf.tm_min < 0 || buf.tm_min > 59) { |
| 368 | PyErr_SetString(PyExc_ValueError, "minute out of range"); |
| 369 | return NULL; |
| 370 | } |
| 371 | if (buf.tm_sec < 0 || buf.tm_sec > 61) { |
| 372 | PyErr_SetString(PyExc_ValueError, "seconds out of range"); |
| 373 | return NULL; |
| 374 | } |
| 375 | /* tm_wday does not need checking of its upper-bound since taking |
| 376 | ``% 7`` in gettmarg() automatically restricts the range. */ |
| 377 | if (buf.tm_wday < 0) { |
| 378 | PyErr_SetString(PyExc_ValueError, "day of week out of range"); |
| 379 | return NULL; |
| 380 | } |
| 381 | if (buf.tm_yday < 0 || buf.tm_yday > 365) { |
| 382 | PyErr_SetString(PyExc_ValueError, "day of year out of range"); |
| 383 | return NULL; |
| 384 | } |
| 385 | if (buf.tm_isdst < -1 || buf.tm_isdst > 1) { |
| 386 | PyErr_SetString(PyExc_ValueError, |
| 387 | "daylight savings flag out of range"); |
| 388 | return NULL; |
| 389 | } |
| 390 | |
Guido van Rossum | c222ec2 | 1999-02-23 00:00:10 +0000 | [diff] [blame] | 391 | fmtlen = strlen(fmt); |
| 392 | |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 393 | /* I hate these functions that presume you know how big the output |
| 394 | * will be ahead of time... |
| 395 | */ |
Guido van Rossum | c222ec2 | 1999-02-23 00:00:10 +0000 | [diff] [blame] | 396 | for (i = 1024; ; i += i) { |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 397 | outbuf = malloc(i); |
| 398 | if (outbuf == NULL) { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 399 | return PyErr_NoMemory(); |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 400 | } |
Guido van Rossum | c222ec2 | 1999-02-23 00:00:10 +0000 | [diff] [blame] | 401 | buflen = strftime(outbuf, i, fmt, &buf); |
| 402 | if (buflen > 0 || i >= 256 * fmtlen) { |
| 403 | /* If the buffer is 256 times as long as the format, |
| 404 | it's probably not failing for lack of room! |
| 405 | More likely, the format yields an empty result, |
| 406 | e.g. an empty format, or %Z when the timezone |
| 407 | is unknown. */ |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 408 | PyObject *ret; |
Guido van Rossum | c222ec2 | 1999-02-23 00:00:10 +0000 | [diff] [blame] | 409 | ret = PyString_FromStringAndSize(outbuf, buflen); |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 410 | free(outbuf); |
| 411 | return ret; |
| 412 | } |
| 413 | free(outbuf); |
| 414 | } |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 415 | } |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 416 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 417 | PyDoc_STRVAR(strftime_doc, |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 418 | "strftime(format[, tuple]) -> string\n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 419 | \n\ |
| 420 | 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] | 421 | 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] | 422 | is not present, current time as returned by localtime() is used."); |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 423 | #endif /* HAVE_STRFTIME */ |
| 424 | |
Guido van Rossum | d3c46d5 | 2002-07-19 17:06:47 +0000 | [diff] [blame] | 425 | static PyObject * |
| 426 | time_strptime(PyObject *self, PyObject *args) |
| 427 | { |
| 428 | PyObject *strptime_module = PyImport_ImportModule("_strptime"); |
Raymond Hettinger | 502168a | 2003-04-10 16:03:22 +0000 | [diff] [blame] | 429 | PyObject *strptime_result; |
Guido van Rossum | d3c46d5 | 2002-07-19 17:06:47 +0000 | [diff] [blame] | 430 | |
Tim Peters | 513a1cd | 2003-01-19 04:54:58 +0000 | [diff] [blame] | 431 | if (!strptime_module) |
Guido van Rossum | d3c46d5 | 2002-07-19 17:06:47 +0000 | [diff] [blame] | 432 | return NULL; |
Raymond Hettinger | 502168a | 2003-04-10 16:03:22 +0000 | [diff] [blame] | 433 | strptime_result = PyObject_CallMethod(strptime_module, "strptime", "O", args); |
| 434 | Py_DECREF(strptime_module); |
| 435 | return strptime_result; |
Guido van Rossum | d3c46d5 | 2002-07-19 17:06:47 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 438 | PyDoc_STRVAR(strptime_doc, |
Brett Cannon | 20def8b | 2003-07-01 05:16:08 +0000 | [diff] [blame] | 439 | "strptime(string, format) -> struct_time\n\ |
Martin v. Löwis | b3cfc1d | 2001-12-02 12:27:43 +0000 | [diff] [blame] | 440 | \n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 441 | 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] | 442 | See the library reference manual for formatting codes (same as strftime())."); |
Guido van Rossum | d3c46d5 | 2002-07-19 17:06:47 +0000 | [diff] [blame] | 443 | |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 444 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 445 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 446 | time_asctime(PyObject *self, PyObject *args) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 447 | { |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 448 | PyObject *tup = NULL; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 449 | struct tm buf; |
| 450 | char *p; |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 451 | if (!PyArg_ParseTuple(args, "|O:asctime", &tup)) |
Guido van Rossum | b2b42dd | 2000-01-12 16:38:20 +0000 | [diff] [blame] | 452 | return NULL; |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 453 | if (tup == NULL) { |
| 454 | time_t tt = time(NULL); |
| 455 | buf = *localtime(&tt); |
| 456 | } else if (!gettmarg(tup, &buf)) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 457 | return NULL; |
| 458 | p = asctime(&buf); |
| 459 | if (p[24] == '\n') |
| 460 | p[24] = '\0'; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 461 | return PyString_FromString(p); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 464 | PyDoc_STRVAR(asctime_doc, |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 465 | "asctime([tuple]) -> string\n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 466 | \n\ |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 467 | Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\ |
| 468 | 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] | 469 | is used."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 470 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 471 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 472 | time_ctime(PyObject *self, PyObject *args) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 473 | { |
| 474 | double dt; |
| 475 | time_t tt; |
| 476 | char *p; |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 477 | |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 478 | if (PyTuple_Size(args) == 0) |
| 479 | tt = time(NULL); |
| 480 | else { |
| 481 | if (!PyArg_ParseTuple(args, "|d:ctime", &dt)) |
| 482 | return NULL; |
| 483 | tt = (time_t)dt; |
| 484 | } |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 485 | p = ctime(&tt); |
Guido van Rossum | 7853570 | 1998-03-03 22:19:10 +0000 | [diff] [blame] | 486 | if (p == NULL) { |
| 487 | PyErr_SetString(PyExc_ValueError, "unconvertible time"); |
| 488 | return NULL; |
| 489 | } |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 490 | if (p[24] == '\n') |
| 491 | p[24] = '\0'; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 492 | return PyString_FromString(p); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 495 | PyDoc_STRVAR(ctime_doc, |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 496 | "ctime(seconds) -> string\n\ |
| 497 | \n\ |
| 498 | 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] | 499 | 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] | 500 | not present, current time as returned by localtime() is used."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 501 | |
Guido van Rossum | 60cd813 | 1998-03-06 17:16:21 +0000 | [diff] [blame] | 502 | #ifdef HAVE_MKTIME |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 503 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 504 | time_mktime(PyObject *self, PyObject *args) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 505 | { |
Guido van Rossum | b2b42dd | 2000-01-12 16:38:20 +0000 | [diff] [blame] | 506 | PyObject *tup; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 507 | struct tm buf; |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 508 | time_t tt; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 509 | if (!PyArg_ParseTuple(args, "O:mktime", &tup)) |
Guido van Rossum | b2b42dd | 2000-01-12 16:38:20 +0000 | [diff] [blame] | 510 | return NULL; |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 511 | tt = time(&tt); |
| 512 | buf = *localtime(&tt); |
Guido van Rossum | b2b42dd | 2000-01-12 16:38:20 +0000 | [diff] [blame] | 513 | if (!gettmarg(tup, &buf)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 514 | return NULL; |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 515 | tt = mktime(&buf); |
| 516 | if (tt == (time_t)(-1)) { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 517 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 518 | "mktime argument out of range"); |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 519 | return NULL; |
| 520 | } |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 521 | return PyFloat_FromDouble((double)tt); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 522 | } |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 523 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 524 | PyDoc_STRVAR(mktime_doc, |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 525 | "mktime(tuple) -> floating point number\n\ |
| 526 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 527 | 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] | 528 | #endif /* HAVE_MKTIME */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 529 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 530 | #ifdef HAVE_WORKING_TZSET |
| 531 | void inittimezone(PyObject *module); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 532 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 533 | static PyObject * |
| 534 | time_tzset(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 535 | { |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 536 | PyObject* m; |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 537 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 538 | if (!PyArg_ParseTuple(args, ":tzset")) |
| 539 | return NULL; |
| 540 | |
| 541 | m = PyImport_ImportModule("time"); |
| 542 | if (m == NULL) { |
| 543 | return NULL; |
| 544 | } |
| 545 | |
| 546 | tzset(); |
| 547 | |
| 548 | /* Reset timezone, altzone, daylight and tzname */ |
| 549 | inittimezone(m); |
| 550 | Py_DECREF(m); |
| 551 | |
| 552 | Py_INCREF(Py_None); |
| 553 | return Py_None; |
| 554 | } |
| 555 | |
| 556 | PyDoc_STRVAR(tzset_doc, |
| 557 | "tzset(zone)\n\ |
| 558 | \n\ |
| 559 | Initialize, or reinitialize, the local timezone to the value stored in\n\ |
| 560 | os.environ['TZ']. The TZ environment variable should be specified in\n\ |
| 561 | standard Uniz timezone format as documented in the tzset man page\n\ |
| 562 | (eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\ |
| 563 | fall back to UTC. If the TZ environment variable is not set, the local\n\ |
| 564 | timezone is set to the systems best guess of wallclock time.\n\ |
| 565 | Changing the TZ environment variable without calling tzset *may* change\n\ |
| 566 | the local timezone used by methods such as localtime, but this behaviour\n\ |
| 567 | should not be relied on."); |
| 568 | #endif /* HAVE_WORKING_TZSET */ |
| 569 | |
| 570 | void inittimezone(PyObject *m) { |
| 571 | /* This code moved from inittime wholesale to allow calling it from |
| 572 | time_tzset. In the future, some parts of it can be moved back |
| 573 | (for platforms that don't HAVE_WORKING_TZSET, when we know what they |
| 574 | are), and the extranious calls to tzset(3) should be removed. |
| 575 | I havn't done this yet, as I don't want to change this code as |
| 576 | little as possible when introducing the time.tzset and time.tzsetwall |
| 577 | methods. This should simply be a method of doing the following once, |
| 578 | at the top of this function and removing the call to tzset() from |
| 579 | time_tzset(): |
| 580 | |
| 581 | #ifdef HAVE_TZSET |
| 582 | tzset() |
| 583 | #endif |
| 584 | |
| 585 | And I'm lazy and hate C so nyer. |
| 586 | */ |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 587 | #if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 588 | tzset(); |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 589 | #ifdef PYOS_OS2 |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 590 | PyModule_AddIntConstant(m, "timezone", _timezone); |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 591 | #else /* !PYOS_OS2 */ |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 592 | PyModule_AddIntConstant(m, "timezone", timezone); |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 593 | #endif /* PYOS_OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 594 | #ifdef HAVE_ALTZONE |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 595 | PyModule_AddIntConstant(m, "altzone", altzone); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 596 | #else |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 597 | #ifdef PYOS_OS2 |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 598 | PyModule_AddIntConstant(m, "altzone", _timezone-3600); |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 599 | #else /* !PYOS_OS2 */ |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 600 | PyModule_AddIntConstant(m, "altzone", timezone-3600); |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 601 | #endif /* PYOS_OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 602 | #endif |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 603 | PyModule_AddIntConstant(m, "daylight", daylight); |
| 604 | PyModule_AddObject(m, "tzname", |
| 605 | Py_BuildValue("(zz)", tzname[0], tzname[1])); |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 606 | #else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 607 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 608 | { |
| 609 | #define YEAR ((time_t)((365 * 24 + 6) * 3600)) |
| 610 | time_t t; |
| 611 | struct tm *p; |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 612 | long janzone, julyzone; |
| 613 | char janname[10], julyname[10]; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 614 | t = (time((time_t *)0) / YEAR) * YEAR; |
| 615 | p = localtime(&t); |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 616 | janzone = -p->tm_gmtoff; |
| 617 | strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9); |
| 618 | janname[9] = '\0'; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 619 | t += YEAR/2; |
| 620 | p = localtime(&t); |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 621 | julyzone = -p->tm_gmtoff; |
| 622 | strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9); |
| 623 | julyname[9] = '\0'; |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 624 | |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 625 | if( janzone < julyzone ) { |
| 626 | /* DST is reversed in the southern hemisphere */ |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 627 | PyModule_AddIntConstant(m, "timezone", julyzone); |
| 628 | PyModule_AddIntConstant(m, "altzone", janzone); |
| 629 | PyModule_AddIntConstant(m, "daylight", |
| 630 | janzone != julyzone); |
| 631 | PyModule_AddObject(m, "tzname", |
| 632 | Py_BuildValue("(zz)", |
| 633 | julyname, janname)); |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 634 | } else { |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 635 | PyModule_AddIntConstant(m, "timezone", janzone); |
| 636 | PyModule_AddIntConstant(m, "altzone", julyzone); |
| 637 | PyModule_AddIntConstant(m, "daylight", |
| 638 | janzone != julyzone); |
| 639 | PyModule_AddObject(m, "tzname", |
| 640 | Py_BuildValue("(zz)", |
| 641 | janname, julyname)); |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 642 | } |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 643 | } |
Guido van Rossum | e6a4b7b | 1997-10-08 15:27:56 +0000 | [diff] [blame] | 644 | #else |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 645 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
Tim Peters | 26ae7cd | 2001-03-20 03:26:49 +0000 | [diff] [blame] | 646 | #ifdef __CYGWIN__ |
| 647 | tzset(); |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 648 | PyModule_AddIntConstant(m, "timezone", _timezone); |
| 649 | PyModule_AddIntConstant(m, "altzone", _timezone); |
| 650 | PyModule_AddIntConstant(m, "daylight", _daylight); |
| 651 | PyModule_AddObject(m, "tzname", |
| 652 | Py_BuildValue("(zz)", _tzname[0], _tzname[1])); |
Tim Peters | 26ae7cd | 2001-03-20 03:26:49 +0000 | [diff] [blame] | 653 | #endif /* __CYGWIN__ */ |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 654 | #endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | |
| 658 | static PyMethodDef time_methods[] = { |
| 659 | {"time", time_time, METH_VARARGS, time_doc}, |
| 660 | #ifdef HAVE_CLOCK |
| 661 | {"clock", time_clock, METH_VARARGS, clock_doc}, |
| 662 | #endif |
| 663 | {"sleep", time_sleep, METH_VARARGS, sleep_doc}, |
| 664 | {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc}, |
| 665 | {"localtime", time_localtime, METH_VARARGS, localtime_doc}, |
| 666 | {"asctime", time_asctime, METH_VARARGS, asctime_doc}, |
| 667 | {"ctime", time_ctime, METH_VARARGS, ctime_doc}, |
| 668 | #ifdef HAVE_MKTIME |
| 669 | {"mktime", time_mktime, METH_VARARGS, mktime_doc}, |
| 670 | #endif |
| 671 | #ifdef HAVE_STRFTIME |
| 672 | {"strftime", time_strftime, METH_VARARGS, strftime_doc}, |
| 673 | #endif |
| 674 | {"strptime", time_strptime, METH_VARARGS, strptime_doc}, |
| 675 | #ifdef HAVE_WORKING_TZSET |
| 676 | {"tzset", time_tzset, METH_VARARGS, tzset_doc}, |
| 677 | #endif |
| 678 | {NULL, NULL} /* sentinel */ |
| 679 | }; |
| 680 | |
| 681 | |
| 682 | PyDoc_STRVAR(module_doc, |
| 683 | "This module provides various functions to manipulate time values.\n\ |
| 684 | \n\ |
| 685 | There are two standard representations of time. One is the number\n\ |
| 686 | of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\ |
| 687 | or a floating point number (to represent fractions of seconds).\n\ |
| 688 | The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\ |
| 689 | The actual value can be retrieved by calling gmtime(0).\n\ |
| 690 | \n\ |
| 691 | The other representation is a tuple of 9 integers giving local time.\n\ |
| 692 | The tuple items are:\n\ |
| 693 | year (four digits, e.g. 1998)\n\ |
| 694 | month (1-12)\n\ |
| 695 | day (1-31)\n\ |
| 696 | hours (0-23)\n\ |
| 697 | minutes (0-59)\n\ |
| 698 | seconds (0-59)\n\ |
| 699 | weekday (0-6, Monday is 0)\n\ |
| 700 | Julian day (day in the year, 1-366)\n\ |
| 701 | DST (Daylight Savings Time) flag (-1, 0 or 1)\n\ |
| 702 | If the DST flag is 0, the time is given in the regular time zone;\n\ |
| 703 | if it is 1, the time is given in the DST time zone;\n\ |
| 704 | if it is -1, mktime() should guess based on the date and time.\n\ |
| 705 | \n\ |
| 706 | Variables:\n\ |
| 707 | \n\ |
| 708 | timezone -- difference in seconds between UTC and local standard time\n\ |
| 709 | altzone -- difference in seconds between UTC and local DST time\n\ |
| 710 | daylight -- whether local time should reflect DST\n\ |
| 711 | tzname -- tuple of (standard time zone name, DST time zone name)\n\ |
| 712 | \n\ |
| 713 | Functions:\n\ |
| 714 | \n\ |
| 715 | time() -- return current time in seconds since the Epoch as a float\n\ |
| 716 | clock() -- return CPU time since process start as a float\n\ |
| 717 | sleep() -- delay for a number of seconds given as a float\n\ |
| 718 | gmtime() -- convert seconds since Epoch to UTC tuple\n\ |
| 719 | localtime() -- convert seconds since Epoch to local time tuple\n\ |
| 720 | asctime() -- convert time tuple to string\n\ |
| 721 | ctime() -- convert time in seconds to string\n\ |
| 722 | mktime() -- convert local time tuple to seconds since Epoch\n\ |
| 723 | strftime() -- convert time tuple to string according to format specification\n\ |
| 724 | strptime() -- parse string to time tuple according to format specification\n\ |
| 725 | tzset() -- change the local timezone"); |
| 726 | |
| 727 | |
| 728 | PyMODINIT_FUNC |
| 729 | inittime(void) |
| 730 | { |
| 731 | PyObject *m; |
| 732 | char *p; |
| 733 | m = Py_InitModule3("time", time_methods, module_doc); |
| 734 | |
| 735 | /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */ |
| 736 | p = Py_GETENV("PYTHONY2K"); |
| 737 | PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p)); |
| 738 | /* Squirrel away the module's dictionary for the y2k check */ |
| 739 | moddict = PyModule_GetDict(m); |
| 740 | Py_INCREF(moddict); |
| 741 | |
| 742 | /* Set, or reset, module variables like time.timezone */ |
| 743 | inittimezone(m); |
| 744 | |
Mark Hammond | 975e392 | 2002-07-16 01:29:19 +0000 | [diff] [blame] | 745 | #ifdef MS_WINDOWS |
| 746 | /* Helper to allow interrupts for Windows. |
| 747 | If Ctrl+C event delivered while not sleeping |
| 748 | it will be ignored. |
| 749 | */ |
| 750 | main_thread = PyThread_get_thread_ident(); |
| 751 | hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 752 | SetConsoleCtrlHandler( PyCtrlHandler, TRUE); |
| 753 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 754 | PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc); |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 755 | Py_INCREF(&StructTimeType); |
| 756 | PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 760 | /* Implement floattime() for various platforms */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 761 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 762 | static double |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 763 | floattime(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 764 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 765 | /* There are three ways to get the time: |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 766 | (1) gettimeofday() -- resolution in microseconds |
| 767 | (2) ftime() -- resolution in milliseconds |
| 768 | (3) time() -- resolution in seconds |
| 769 | In all cases the return value is a float in seconds. |
| 770 | Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may |
| 771 | fail, so we fall back on ftime() or time(). |
| 772 | Note: clock resolution does not imply clock accuracy! */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 773 | #ifdef HAVE_GETTIMEOFDAY |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 774 | { |
| 775 | struct timeval t; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 776 | #ifdef GETTIMEOFDAY_NO_TZ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 777 | if (gettimeofday(&t) == 0) |
| 778 | return (double)t.tv_sec + t.tv_usec*0.000001; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 779 | #else /* !GETTIMEOFDAY_NO_TZ */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 780 | if (gettimeofday(&t, (struct timezone *)NULL) == 0) |
| 781 | return (double)t.tv_sec + t.tv_usec*0.000001; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 782 | #endif /* !GETTIMEOFDAY_NO_TZ */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 783 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 784 | #endif /* !HAVE_GETTIMEOFDAY */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 785 | { |
Guido van Rossum | d3eb577 | 1999-03-09 16:07:23 +0000 | [diff] [blame] | 786 | #if defined(HAVE_FTIME) |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 787 | struct timeb t; |
| 788 | ftime(&t); |
| 789 | return (double)t.time + (double)t.millitm * (double)0.001; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 790 | #else /* !HAVE_FTIME */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 791 | time_t secs; |
| 792 | time(&secs); |
| 793 | return (double)secs; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 794 | #endif /* !HAVE_FTIME */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 795 | } |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 798 | |
| 799 | /* Implement floatsleep() for various platforms. |
| 800 | When interrupted (or when another error occurs), return -1 and |
| 801 | set an exception; else return 0. */ |
| 802 | |
| 803 | static int |
Guido van Rossum | a320fd3 | 1995-03-09 12:14:15 +0000 | [diff] [blame] | 804 | floatsleep(double secs) |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 805 | { |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 806 | /* XXX Should test for MS_WINDOWS first! */ |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 807 | #if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__) |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 808 | struct timeval t; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 809 | double frac; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 810 | frac = fmod(secs, 1.0); |
| 811 | secs = floor(secs); |
| 812 | t.tv_sec = (long)secs; |
| 813 | t.tv_usec = (long)(frac*1000000.0); |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 814 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 815 | if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) { |
Guido van Rossum | 09cbb01 | 1999-11-08 15:32:27 +0000 | [diff] [blame] | 816 | #ifdef EINTR |
Guido van Rossum | a5456d5 | 1999-08-19 14:40:27 +0000 | [diff] [blame] | 817 | if (errno != EINTR) { |
Guido van Rossum | 09cbb01 | 1999-11-08 15:32:27 +0000 | [diff] [blame] | 818 | #else |
| 819 | if (1) { |
| 820 | #endif |
Andrew M. Kuchling | c24ca4b | 2000-03-24 20:35:20 +0000 | [diff] [blame] | 821 | Py_BLOCK_THREADS |
Guido van Rossum | a5456d5 | 1999-08-19 14:40:27 +0000 | [diff] [blame] | 822 | PyErr_SetFromErrno(PyExc_IOError); |
| 823 | return -1; |
| 824 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 825 | } |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 826 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 02af964 | 2002-01-16 11:04:06 +0000 | [diff] [blame] | 827 | #elif defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 828 | /* XXX Can't interrupt this sleep */ |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 829 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 830 | delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */ |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 831 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 832 | #elif defined(MS_WINDOWS) |
Fred Drake | 0e12395 | 2000-06-29 21:31:02 +0000 | [diff] [blame] | 833 | { |
| 834 | double millisecs = secs * 1000.0; |
Tim Peters | 513a1cd | 2003-01-19 04:54:58 +0000 | [diff] [blame] | 835 | unsigned long ul_millis; |
| 836 | |
Fred Drake | 0e12395 | 2000-06-29 21:31:02 +0000 | [diff] [blame] | 837 | if (millisecs > (double)ULONG_MAX) { |
Tim Peters | 513a1cd | 2003-01-19 04:54:58 +0000 | [diff] [blame] | 838 | PyErr_SetString(PyExc_OverflowError, |
| 839 | "sleep length is too large"); |
Fred Drake | 0e12395 | 2000-06-29 21:31:02 +0000 | [diff] [blame] | 840 | return -1; |
| 841 | } |
Fred Drake | 0e12395 | 2000-06-29 21:31:02 +0000 | [diff] [blame] | 842 | Py_BEGIN_ALLOW_THREADS |
Tim Peters | 513a1cd | 2003-01-19 04:54:58 +0000 | [diff] [blame] | 843 | /* Allow sleep(0) to maintain win32 semantics, and as decreed |
| 844 | * by Guido, only the main thread can be interrupted. |
| 845 | */ |
| 846 | ul_millis = (unsigned long)millisecs; |
| 847 | if (ul_millis == 0 || |
| 848 | main_thread != PyThread_get_thread_ident()) |
| 849 | Sleep(ul_millis); |
Mark Hammond | 975e392 | 2002-07-16 01:29:19 +0000 | [diff] [blame] | 850 | else { |
| 851 | DWORD rc; |
| 852 | ResetEvent(hInterruptEvent); |
Tim Peters | 513a1cd | 2003-01-19 04:54:58 +0000 | [diff] [blame] | 853 | rc = WaitForSingleObject(hInterruptEvent, ul_millis); |
| 854 | if (rc == WAIT_OBJECT_0) { |
| 855 | /* Yield to make sure real Python signal |
| 856 | * handler called. |
| 857 | */ |
Mark Hammond | 975e392 | 2002-07-16 01:29:19 +0000 | [diff] [blame] | 858 | Sleep(1); |
| 859 | Py_BLOCK_THREADS |
Mark Hammond | 975e392 | 2002-07-16 01:29:19 +0000 | [diff] [blame] | 860 | errno = EINTR; |
| 861 | PyErr_SetFromErrno(PyExc_IOError); |
| 862 | return -1; |
| 863 | } |
| 864 | } |
Fred Drake | 0e12395 | 2000-06-29 21:31:02 +0000 | [diff] [blame] | 865 | Py_END_ALLOW_THREADS |
| 866 | } |
Martin v. Löwis | 02af964 | 2002-01-16 11:04:06 +0000 | [diff] [blame] | 867 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 868 | /* This Sleep *IS* Interruptable by Exceptions */ |
Guido van Rossum | 1d0d7e4 | 1997-12-29 20:03:10 +0000 | [diff] [blame] | 869 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 870 | if (DosSleep(secs * 1000) != NO_ERROR) { |
Guido van Rossum | 1d0d7e4 | 1997-12-29 20:03:10 +0000 | [diff] [blame] | 871 | Py_BLOCK_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 872 | PyErr_SetFromErrno(PyExc_IOError); |
| 873 | return -1; |
| 874 | } |
Guido van Rossum | 1d0d7e4 | 1997-12-29 20:03:10 +0000 | [diff] [blame] | 875 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 02af964 | 2002-01-16 11:04:06 +0000 | [diff] [blame] | 876 | #elif defined(__BEOS__) |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 877 | /* This sleep *CAN BE* interrupted. */ |
| 878 | { |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 879 | if( secs <= 0.0 ) { |
| 880 | return; |
| 881 | } |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 882 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 883 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d3eb577 | 1999-03-09 16:07:23 +0000 | [diff] [blame] | 884 | /* BeOS snooze() is in microseconds... */ |
| 885 | if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) { |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 886 | Py_BLOCK_THREADS |
| 887 | PyErr_SetFromErrno( PyExc_IOError ); |
| 888 | return -1; |
| 889 | } |
| 890 | Py_END_ALLOW_THREADS |
| 891 | } |
Martin v. Löwis | 02af964 | 2002-01-16 11:04:06 +0000 | [diff] [blame] | 892 | #elif defined(RISCOS) |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 893 | if (secs <= 0.0) |
| 894 | return 0; |
| 895 | Py_BEGIN_ALLOW_THREADS |
| 896 | /* This sleep *CAN BE* interrupted. */ |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 897 | if ( riscos_sleep(secs) ) |
Guido van Rossum | bceccf5 | 2001-04-10 22:07:43 +0000 | [diff] [blame] | 898 | return -1; |
| 899 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 02af964 | 2002-01-16 11:04:06 +0000 | [diff] [blame] | 900 | #elif defined(PLAN9) |
| 901 | { |
| 902 | double millisecs = secs * 1000.0; |
| 903 | if (millisecs > (double)LONG_MAX) { |
| 904 | PyErr_SetString(PyExc_OverflowError, "sleep length is too large"); |
| 905 | return -1; |
| 906 | } |
| 907 | /* This sleep *CAN BE* interrupted. */ |
| 908 | Py_BEGIN_ALLOW_THREADS |
| 909 | if(sleep((long)millisecs) < 0){ |
| 910 | Py_BLOCK_THREADS |
| 911 | PyErr_SetFromErrno(PyExc_IOError); |
| 912 | return -1; |
| 913 | } |
| 914 | Py_END_ALLOW_THREADS |
| 915 | } |
| 916 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 917 | /* XXX Can't interrupt this sleep */ |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 918 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 919 | sleep((int)secs); |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 920 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 02af964 | 2002-01-16 11:04:06 +0000 | [diff] [blame] | 921 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 922 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 923 | return 0; |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 924 | } |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 925 | |
| 926 | |