Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Time module */ |
| 33 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 99d90c0 | 1996-08-08 19:17:45 +0000 | [diff] [blame] | 36 | #ifdef HAVE_SELECT |
| 37 | #include "mymath.h" |
| 38 | #endif |
| 39 | |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 40 | #ifdef macintosh |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 41 | #include <time.h> |
| 42 | #else |
| 43 | #include <sys/types.h> |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 46 | #ifdef QUICKWIN |
| 47 | #include <io.h> |
| 48 | #endif |
| 49 | |
| 50 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 51 | #include <unistd.h> |
| 52 | #endif |
| 53 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 54 | #ifdef HAVE_SELECT |
| 55 | #include "myselect.h" |
| 56 | #else |
| 57 | #include "mytime.h" |
| 58 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 60 | #ifdef HAVE_FTIME |
| 61 | #include <sys/timeb.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 62 | #if !defined(MS_WINDOWS) && !defined(PYOS_OS2) |
Guido van Rossum | 1bb126f | 1996-12-06 20:17:44 +0000 | [diff] [blame] | 63 | extern int ftime(); |
Guido van Rossum | 5217457 | 1996-12-09 18:38:52 +0000 | [diff] [blame] | 64 | #endif /* MS_WINDOWS */ |
| 65 | #endif /* HAVE_FTIME */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 67 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 68 | #include <i86.h> |
| 69 | #else |
Guido van Rossum | cac6c72 | 1996-09-06 13:34:02 +0000 | [diff] [blame] | 70 | #ifdef MS_WINDOWS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 71 | #include <windows.h> |
Guido van Rossum | b2fb364 | 1996-09-07 00:47:35 +0000 | [diff] [blame] | 72 | #ifdef MS_WIN16 |
| 73 | /* These overrides not needed for Win32 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 74 | #define timezone _timezone |
Guido van Rossum | cc08112 | 1995-03-14 15:05:41 +0000 | [diff] [blame] | 75 | #define tzname _tzname |
| 76 | #define daylight _daylight |
| 77 | #define altzone _altzone |
Guido van Rossum | b2fb364 | 1996-09-07 00:47:35 +0000 | [diff] [blame] | 78 | #endif /* MS_WIN16 */ |
Guido van Rossum | cac6c72 | 1996-09-06 13:34:02 +0000 | [diff] [blame] | 79 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 80 | #endif /* !__WATCOMC__ || __QNX__ */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 82 | #ifdef MS_WIN32 |
| 83 | /* Win32 has better clock replacement */ |
| 84 | #include <largeint.h> |
| 85 | #undef HAVE_CLOCK /* We have our own version down below */ |
| 86 | #endif /* MS_WIN32 */ |
| 87 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 88 | #if defined(PYOS_OS2) |
| 89 | #define INCL_DOS |
| 90 | #define INCL_DOSERRORS |
| 91 | #define INCL_NOPMAPI |
| 92 | #include <os2.h> |
| 93 | #endif |
| 94 | |
| 95 | #if defined(PYCC_VACPP) |
| 96 | #include <time.h> |
| 97 | #define timezone _timezone |
| 98 | #endif |
| 99 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 100 | /* Forward declarations */ |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 101 | static int floatsleep Py_PROTO((double)); |
| 102 | static double floattime Py_PROTO(()); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | |
Guido van Rossum | e6a4b7b | 1997-10-08 15:27:56 +0000 | [diff] [blame] | 104 | #ifdef macintosh |
| 105 | /* Our own timezone. We have enough information to deduce whether |
| 106 | ** DST is on currently, but unfortunately we cannot put it to good |
| 107 | ** use because we don't know the rules (and that is needed to have |
| 108 | ** localtime() return correct tm_isdst values for times other than |
| 109 | ** the current time. So, we cop out and only tell the user the current |
| 110 | ** timezone. |
| 111 | */ |
| 112 | static long timezone; |
| 113 | |
| 114 | static void |
| 115 | initmactimezone() |
| 116 | { |
| 117 | MachineLocation loc; |
| 118 | long delta; |
| 119 | |
| 120 | ReadLocation(&loc); |
| 121 | |
| 122 | if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0) |
| 123 | return; |
| 124 | |
| 125 | delta = loc.u.gmtDelta & 0x00FFFFFF; |
| 126 | |
| 127 | if (delta & 0x00800000) |
| 128 | delta |= 0xFF000000; |
| 129 | |
| 130 | timezone = -delta; |
| 131 | } |
| 132 | #endif /* macintosh */ |
| 133 | |
| 134 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 135 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | time_time(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 137 | PyObject *self; |
| 138 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 139 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 140 | double secs; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 141 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 142 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 143 | secs = floattime(); |
| 144 | if (secs == 0.0) { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 145 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 146 | return NULL; |
| 147 | } |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 148 | return PyFloat_FromDouble(secs); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | #ifdef HAVE_CLOCK |
| 152 | |
| 153 | #ifndef CLOCKS_PER_SEC |
Guido van Rossum | 1b66a4f | 1996-02-25 04:50:33 +0000 | [diff] [blame] | 154 | #ifdef CLK_TCK |
| 155 | #define CLOCKS_PER_SEC CLK_TCK |
| 156 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 157 | #define CLOCKS_PER_SEC 1000000 |
| 158 | #endif |
Guido van Rossum | 1b66a4f | 1996-02-25 04:50:33 +0000 | [diff] [blame] | 159 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 160 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 161 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 162 | time_clock(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 163 | PyObject *self; |
| 164 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 165 | { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 166 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 167 | return NULL; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 168 | return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 169 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 170 | #endif /* HAVE_CLOCK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 172 | #ifdef MS_WIN32 |
| 173 | /* Due to Mark Hammond */ |
| 174 | static PyObject * |
| 175 | time_clock(self, args) |
| 176 | PyObject *self; |
| 177 | PyObject *args; |
| 178 | { |
| 179 | static LARGE_INTEGER ctrStart; |
| 180 | static LARGE_INTEGER divisor = {0,0}; |
| 181 | LARGE_INTEGER now, diff, rem; |
| 182 | |
| 183 | if (!PyArg_NoArgs(args)) |
| 184 | return NULL; |
| 185 | |
| 186 | if (LargeIntegerEqualToZero(divisor)) { |
| 187 | QueryPerformanceCounter(&ctrStart); |
| 188 | if (!QueryPerformanceFrequency(&divisor) || |
| 189 | LargeIntegerEqualToZero(divisor)) { |
| 190 | /* Unlikely to happen - |
| 191 | this works on all intel machines at least! |
| 192 | Revert to clock() */ |
| 193 | return PyFloat_FromDouble(clock()); |
| 194 | } |
| 195 | } |
| 196 | QueryPerformanceCounter(&now); |
| 197 | diff = LargeIntegerSubtract(now, ctrStart); |
| 198 | diff = LargeIntegerDivide(diff, divisor, &rem); |
| 199 | /* XXX - we assume both divide results fit in 32 bits. This is |
| 200 | true on Intels. First person who can afford a machine that |
| 201 | doesnt deserves to fix it :-) |
| 202 | */ |
| 203 | return PyFloat_FromDouble((double)diff.LowPart + |
| 204 | ((double)rem.LowPart / (double)divisor.LowPart)); |
| 205 | } |
| 206 | #define HAVE_CLOCK /* So it gets included in the methods */ |
| 207 | #endif /* MS_WIN32 */ |
| 208 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 209 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 210 | time_sleep(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 211 | PyObject *self; |
| 212 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 213 | { |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 214 | double secs; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 215 | if (!PyArg_Parse(args, "d", &secs)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 216 | return NULL; |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 217 | if (floatsleep(secs) != 0) |
| 218 | return NULL; |
| 219 | Py_INCREF(Py_None); |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 220 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 223 | static PyObject * |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 224 | time_convert(when, function) |
| 225 | time_t when; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 226 | struct tm * (*function) Py_PROTO((const time_t *)); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 227 | { |
Guido van Rossum | 6e8583d | 1996-10-08 14:19:52 +0000 | [diff] [blame] | 228 | struct tm *p; |
| 229 | errno = 0; |
| 230 | p = function(&when); |
| 231 | if (p == NULL) { |
| 232 | #ifdef EINVAL |
Guido van Rossum | 0b1ff66 | 1996-11-02 17:31:22 +0000 | [diff] [blame] | 233 | if (errno == 0) |
Guido van Rossum | 6e8583d | 1996-10-08 14:19:52 +0000 | [diff] [blame] | 234 | errno = EINVAL; |
| 235 | #endif |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 236 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 6e8583d | 1996-10-08 14:19:52 +0000 | [diff] [blame] | 237 | } |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 238 | return Py_BuildValue("(iiiiiiiii)", |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 239 | p->tm_year + 1900, |
| 240 | p->tm_mon + 1, /* Want January == 1 */ |
| 241 | p->tm_mday, |
| 242 | p->tm_hour, |
| 243 | p->tm_min, |
| 244 | p->tm_sec, |
| 245 | (p->tm_wday + 6) % 7, /* Want Monday == 0 */ |
| 246 | p->tm_yday + 1, /* Want January, 1 == 1 */ |
| 247 | p->tm_isdst); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 250 | static PyObject * |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 251 | time_gmtime(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 252 | PyObject *self; |
| 253 | PyObject *args; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 254 | { |
| 255 | double when; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 256 | if (!PyArg_Parse(args, "d", &when)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 257 | return NULL; |
| 258 | return time_convert((time_t)when, gmtime); |
| 259 | } |
| 260 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 261 | static PyObject * |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 262 | time_localtime(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 263 | PyObject *self; |
| 264 | PyObject *args; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 265 | { |
| 266 | double when; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 267 | if (!PyArg_Parse(args, "d", &when)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 268 | return NULL; |
| 269 | return time_convert((time_t)when, localtime); |
| 270 | } |
| 271 | |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 272 | static int |
| 273 | gettmarg(args, p) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 274 | PyObject *args; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 275 | struct tm *p; |
| 276 | { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 277 | if (!PyArg_Parse(args, "(iiiiiiiii)", |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 278 | &p->tm_year, |
| 279 | &p->tm_mon, |
| 280 | &p->tm_mday, |
| 281 | &p->tm_hour, |
| 282 | &p->tm_min, |
| 283 | &p->tm_sec, |
| 284 | &p->tm_wday, |
| 285 | &p->tm_yday, |
| 286 | &p->tm_isdst)) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 287 | return 0; |
| 288 | if (p->tm_year >= 1900) |
| 289 | p->tm_year -= 1900; |
| 290 | p->tm_mon--; |
| 291 | p->tm_wday = (p->tm_wday + 1) % 7; |
| 292 | p->tm_yday--; |
| 293 | return 1; |
| 294 | } |
| 295 | |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 296 | #ifdef HAVE_STRFTIME |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 297 | static PyObject * |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 298 | time_strftime(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 299 | PyObject *self; |
| 300 | PyObject *args; |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 301 | { |
| 302 | struct tm buf; |
| 303 | const char *fmt; |
| 304 | char *outbuf = 0; |
| 305 | int i; |
| 306 | |
| 307 | if (!PyArg_ParseTuple(args, "s(iiiiiiiii)", |
| 308 | &fmt, |
| 309 | &(buf.tm_year), |
| 310 | &(buf.tm_mon), |
| 311 | &(buf.tm_mday), |
| 312 | &(buf.tm_hour), |
| 313 | &(buf.tm_min), |
| 314 | &(buf.tm_sec), |
| 315 | &(buf.tm_wday), |
| 316 | &(buf.tm_yday), |
| 317 | &(buf.tm_isdst))) |
| 318 | return NULL; |
| 319 | if (buf.tm_year >= 1900) |
| 320 | buf.tm_year -= 1900; |
| 321 | buf.tm_mon--; |
| 322 | buf.tm_wday = (buf.tm_wday + 1) % 7; |
| 323 | buf.tm_yday--; |
Guido van Rossum | be1eb0d | 1997-12-08 21:56:43 +0000 | [diff] [blame] | 324 | (void) mktime(&buf); |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 325 | /* I hate these functions that presume you know how big the output |
| 326 | * will be ahead of time... |
| 327 | */ |
Guido van Rossum | a78bfe1 | 1997-02-14 16:35:10 +0000 | [diff] [blame] | 328 | for (i = 1024 ; i <= 8192 ; i += 1024) { |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 329 | outbuf = malloc(i); |
| 330 | if (outbuf == NULL) { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 331 | return PyErr_NoMemory(); |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 332 | } |
| 333 | if (strftime(outbuf, i-1, fmt, &buf) != 0) { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 334 | PyObject *ret; |
| 335 | ret = PyString_FromString(outbuf); |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 336 | free(outbuf); |
| 337 | return ret; |
| 338 | } |
| 339 | free(outbuf); |
| 340 | } |
Guido van Rossum | a78bfe1 | 1997-02-14 16:35:10 +0000 | [diff] [blame] | 341 | PyErr_SetString(PyExc_ValueError, |
| 342 | "bad strftime format or result too big"); |
| 343 | return NULL; |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 344 | } |
| 345 | #endif /* HAVE_STRFTIME */ |
| 346 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 347 | static PyObject * |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 348 | time_asctime(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 349 | PyObject *self; |
| 350 | PyObject *args; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 351 | { |
| 352 | struct tm buf; |
| 353 | char *p; |
| 354 | if (!gettmarg(args, &buf)) |
| 355 | return NULL; |
| 356 | p = asctime(&buf); |
| 357 | if (p[24] == '\n') |
| 358 | p[24] = '\0'; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 359 | return PyString_FromString(p); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 362 | static PyObject * |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 363 | time_ctime(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 364 | PyObject *self; |
| 365 | PyObject *args; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 366 | { |
| 367 | double dt; |
| 368 | time_t tt; |
| 369 | char *p; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 370 | if (!PyArg_Parse(args, "d", &dt)) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 371 | return NULL; |
Guido van Rossum | cac6c72 | 1996-09-06 13:34:02 +0000 | [diff] [blame] | 372 | tt = (time_t)dt; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 373 | p = ctime(&tt); |
| 374 | if (p[24] == '\n') |
| 375 | p[24] = '\0'; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 376 | return PyString_FromString(p); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 379 | static PyObject * |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 380 | time_mktime(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 381 | PyObject *self; |
| 382 | PyObject *args; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 383 | { |
| 384 | struct tm buf; |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 385 | time_t tt; |
| 386 | tt = time(&tt); |
| 387 | buf = *localtime(&tt); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 388 | if (!gettmarg(args, &buf)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 389 | return NULL; |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 390 | tt = mktime(&buf); |
| 391 | if (tt == (time_t)(-1)) { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 392 | PyErr_SetString(PyExc_OverflowError, |
| 393 | "mktime argument out of range"); |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 394 | return NULL; |
| 395 | } |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 396 | return PyFloat_FromDouble((double)tt); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 399 | static PyMethodDef time_methods[] = { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 400 | {"time", time_time}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 401 | #ifdef HAVE_CLOCK |
| 402 | {"clock", time_clock}, |
| 403 | #endif |
| 404 | {"sleep", time_sleep}, |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 405 | {"gmtime", time_gmtime}, |
| 406 | {"localtime", time_localtime}, |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 407 | {"asctime", time_asctime}, |
| 408 | {"ctime", time_ctime}, |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 409 | {"mktime", time_mktime}, |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 410 | #ifdef HAVE_STRFTIME |
Guido van Rossum | 5416e20 | 1996-02-13 00:14:09 +0000 | [diff] [blame] | 411 | {"strftime", time_strftime, 1}, |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 412 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 413 | {NULL, NULL} /* sentinel */ |
| 414 | }; |
| 415 | |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame] | 416 | static void |
| 417 | ins(d, name, v) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 418 | PyObject *d; |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame] | 419 | char *name; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 420 | PyObject *v; |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame] | 421 | { |
| 422 | if (v == NULL) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 423 | Py_FatalError("Can't initialize time module -- NULL value"); |
| 424 | if (PyDict_SetItemString(d, name, v) != 0) |
| 425 | Py_FatalError( |
Guido van Rossum | 5217457 | 1996-12-09 18:38:52 +0000 | [diff] [blame] | 426 | "Can't initialize time module -- PyDict_SetItemString failed"); |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 427 | Py_DECREF(v); |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 430 | void |
| 431 | inittime() |
| 432 | { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 433 | PyObject *m, *d; |
| 434 | m = Py_InitModule("time", time_methods); |
| 435 | d = PyModule_GetDict(m); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 436 | #ifdef HAVE_TZNAME |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 437 | tzset(); |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 438 | ins(d, "timezone", PyInt_FromLong((long)timezone)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 439 | #ifdef HAVE_ALTZONE |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 440 | ins(d, "altzone", PyInt_FromLong((long)altzone)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 441 | #else |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 442 | ins(d, "altzone", PyInt_FromLong((long)timezone-3600)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 443 | #endif |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 444 | ins(d, "daylight", PyInt_FromLong((long)daylight)); |
| 445 | ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1])); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 446 | #else /* !HAVE_TZNAME */ |
| 447 | #if HAVE_TM_ZONE |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 448 | { |
| 449 | #define YEAR ((time_t)((365 * 24 + 6) * 3600)) |
| 450 | time_t t; |
| 451 | struct tm *p; |
| 452 | long winterzone, summerzone; |
| 453 | char wintername[10], summername[10]; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 454 | /* XXX This won't work on the southern hemisphere. |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 455 | XXX Anybody got a better idea? */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 456 | t = (time((time_t *)0) / YEAR) * YEAR; |
| 457 | p = localtime(&t); |
| 458 | winterzone = -p->tm_gmtoff; |
| 459 | strncpy(wintername, p->tm_zone ? p->tm_zone : " ", 9); |
| 460 | wintername[9] = '\0'; |
| 461 | t += YEAR/2; |
| 462 | p = localtime(&t); |
| 463 | summerzone = -p->tm_gmtoff; |
| 464 | strncpy(summername, p->tm_zone ? p->tm_zone : " ", 9); |
| 465 | summername[9] = '\0'; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 466 | ins(d, "timezone", PyInt_FromLong(winterzone)); |
| 467 | ins(d, "altzone", PyInt_FromLong(summerzone)); |
| 468 | ins(d, "daylight", |
| 469 | PyInt_FromLong((long)(winterzone != summerzone))); |
| 470 | ins(d, "tzname", |
| 471 | Py_BuildValue("(zz)", wintername, summername)); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 472 | } |
Guido van Rossum | e6a4b7b | 1997-10-08 15:27:56 +0000 | [diff] [blame] | 473 | #else |
| 474 | #ifdef macintosh |
Guido van Rossum | be1eb0d | 1997-12-08 21:56:43 +0000 | [diff] [blame] | 475 | /* The only thing we can obtain is the current timezone |
| 476 | ** (and whether dst is currently _active_, but that is not what |
| 477 | ** we're looking for:-( ) |
| 478 | */ |
Guido van Rossum | e6a4b7b | 1997-10-08 15:27:56 +0000 | [diff] [blame] | 479 | initmactimezone(); |
| 480 | ins(d, "timezone", PyInt_FromLong(timezone)); |
Guido van Rossum | be1eb0d | 1997-12-08 21:56:43 +0000 | [diff] [blame] | 481 | ins(d, "altzone", PyInt_FromLong(timezone)); |
| 482 | ins(d, "daylight", PyInt_FromLong((long)0)); |
| 483 | ins(d, "tzname", Py_BuildValue("(zz)", "", "")); |
Guido van Rossum | e6a4b7b | 1997-10-08 15:27:56 +0000 | [diff] [blame] | 484 | #endif /* macintosh */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 485 | #endif /* HAVE_TM_ZONE */ |
| 486 | #endif /* !HAVE_TZNAME */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 487 | if (PyErr_Occurred()) |
| 488 | Py_FatalError("Can't initialize time module"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 492 | /* Implement floattime() for various platforms */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 493 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 494 | static double |
| 495 | floattime() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 496 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 497 | /* There are three ways to get the time: |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 498 | (1) gettimeofday() -- resolution in microseconds |
| 499 | (2) ftime() -- resolution in milliseconds |
| 500 | (3) time() -- resolution in seconds |
| 501 | In all cases the return value is a float in seconds. |
| 502 | Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may |
| 503 | fail, so we fall back on ftime() or time(). |
| 504 | Note: clock resolution does not imply clock accuracy! */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 505 | #ifdef HAVE_GETTIMEOFDAY |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 506 | { |
| 507 | struct timeval t; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 508 | #ifdef GETTIMEOFDAY_NO_TZ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 509 | if (gettimeofday(&t) == 0) |
| 510 | return (double)t.tv_sec + t.tv_usec*0.000001; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 511 | #else /* !GETTIMEOFDAY_NO_TZ */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 512 | if (gettimeofday(&t, (struct timezone *)NULL) == 0) |
| 513 | return (double)t.tv_sec + t.tv_usec*0.000001; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 514 | #endif /* !GETTIMEOFDAY_NO_TZ */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 515 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 516 | #endif /* !HAVE_GETTIMEOFDAY */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 517 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 518 | #ifdef HAVE_FTIME |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 519 | struct timeb t; |
| 520 | ftime(&t); |
| 521 | return (double)t.time + (double)t.millitm * (double)0.001; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 522 | #else /* !HAVE_FTIME */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 523 | time_t secs; |
| 524 | time(&secs); |
| 525 | return (double)secs; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 526 | #endif /* !HAVE_FTIME */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 527 | } |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 530 | |
| 531 | /* Implement floatsleep() for various platforms. |
| 532 | When interrupted (or when another error occurs), return -1 and |
| 533 | set an exception; else return 0. */ |
| 534 | |
| 535 | static int |
Guido van Rossum | a320fd3 | 1995-03-09 12:14:15 +0000 | [diff] [blame] | 536 | #ifdef MPW |
| 537 | floatsleep(double secs) |
| 538 | #else |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 539 | floatsleep(secs) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 540 | double secs; |
Guido van Rossum | a320fd3 | 1995-03-09 12:14:15 +0000 | [diff] [blame] | 541 | #endif /* MPW */ |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 542 | { |
Guido van Rossum | a78bfe1 | 1997-02-14 16:35:10 +0000 | [diff] [blame] | 543 | /* XXX Should test for MS_WIN32 first! */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 544 | #ifdef HAVE_SELECT |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 545 | struct timeval t; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 546 | double frac; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 547 | frac = fmod(secs, 1.0); |
| 548 | secs = floor(secs); |
| 549 | t.tv_sec = (long)secs; |
| 550 | t.tv_usec = (long)(frac*1000000.0); |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 551 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 552 | if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) { |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 553 | Py_BLOCK_THREADS |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 554 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 555 | return -1; |
| 556 | } |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 557 | Py_END_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 558 | #else /* !HAVE_SELECT */ |
| 559 | #ifdef macintosh |
| 560 | #define MacTicks (* (long *)0x16A) |
| 561 | long deadline; |
| 562 | deadline = MacTicks + (long)(secs * 60.0); |
| 563 | while (MacTicks < deadline) { |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 564 | /* XXX Should call some yielding function here */ |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 565 | if (PyErr_CheckSignals()) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 566 | return -1; |
| 567 | } |
| 568 | #else /* !macintosh */ |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 569 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 570 | /* XXX Can't interrupt this sleep */ |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 571 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 572 | delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */ |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 573 | Py_END_ALLOW_THREADS |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 574 | #else /* !__WATCOMC__ || __QNX__ */ |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 575 | #ifdef MSDOS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 576 | struct timeb t1, t2; |
| 577 | double frac; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 578 | extern double fmod Py_PROTO((double, double)); |
| 579 | extern double floor Py_PROTO((double)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 580 | if (secs <= 0.0) |
| 581 | return; |
| 582 | frac = fmod(secs, 1.0); |
| 583 | secs = floor(secs); |
| 584 | ftime(&t1); |
| 585 | t2.time = t1.time + (int)secs; |
| 586 | t2.millitm = t1.millitm + (int)(frac*1000.0); |
| 587 | while (t2.millitm >= 1000) { |
| 588 | t2.time++; |
| 589 | t2.millitm -= 1000; |
| 590 | } |
| 591 | for (;;) { |
| 592 | #ifdef QUICKWIN |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 593 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 594 | _wyield(); |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 595 | Py_END_ALLOW_THREADS |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 596 | #endif |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 597 | if (PyErr_CheckSignals()) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 598 | return -1; |
| 599 | ftime(&t1); |
| 600 | if (t1.time > t2.time || |
| 601 | t1.time == t2.time && t1.millitm >= t2.millitm) |
| 602 | break; |
| 603 | } |
| 604 | #else /* !MSDOS */ |
Guido van Rossum | b2fb364 | 1996-09-07 00:47:35 +0000 | [diff] [blame] | 605 | #ifdef MS_WIN32 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 606 | /* XXX Can't interrupt this sleep */ |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 607 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 608 | Sleep((int)(secs*1000)); |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 609 | Py_END_ALLOW_THREADS |
Guido van Rossum | b2fb364 | 1996-09-07 00:47:35 +0000 | [diff] [blame] | 610 | #else /* !MS_WIN32 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 611 | #ifdef PYOS_OS2 |
| 612 | /* This Sleep *IS* Interruptable by Exceptions */ |
Guido van Rossum | 1d0d7e4 | 1997-12-29 20:03:10 +0000 | [diff] [blame^] | 613 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 614 | if (DosSleep(secs * 1000) != NO_ERROR) { |
Guido van Rossum | 1d0d7e4 | 1997-12-29 20:03:10 +0000 | [diff] [blame^] | 615 | Py_BLOCK_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 616 | PyErr_SetFromErrno(PyExc_IOError); |
| 617 | return -1; |
| 618 | } |
Guido van Rossum | 1d0d7e4 | 1997-12-29 20:03:10 +0000 | [diff] [blame^] | 619 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 620 | #else /* !PYOS_OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 621 | /* XXX Can't interrupt this sleep */ |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 622 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 623 | sleep((int)secs); |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 624 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 625 | #endif /* !PYOS_OS2 */ |
Guido van Rossum | b2fb364 | 1996-09-07 00:47:35 +0000 | [diff] [blame] | 626 | #endif /* !MS_WIN32 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 627 | #endif /* !MSDOS */ |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 628 | #endif /* !__WATCOMC__ || __QNX__ */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 629 | #endif /* !macintosh */ |
| 630 | #endif /* !HAVE_SELECT */ |
| 631 | return 0; |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 632 | } |