blob: 392f2b9819d1d8617ee0f6ff0851b40bd6f01dc2 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Time module */
33
Barry Warsaw9a2a8a81996-12-06 23:32:14 +000034#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000035
Guido van Rossum87ce7bb1998-06-09 16:30:31 +000036#include <ctype.h>
37
Guido van Rossum99d90c01996-08-08 19:17:45 +000038#ifdef HAVE_SELECT
39#include "mymath.h"
40#endif
41
Guido van Rossum6d946f91992-08-14 13:49:30 +000042#ifdef macintosh
Guido van Rossumb6775db1994-08-01 11:34:53 +000043#include <time.h>
44#else
45#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000046#endif
47
Guido van Rossumb6775db1994-08-01 11:34:53 +000048#ifdef QUICKWIN
49#include <io.h>
50#endif
51
52#ifdef HAVE_UNISTD_H
Guido van Rossum2762f251992-03-27 17:22:13 +000053#include <unistd.h>
54#endif
55
Guido van Rossumbcc20741998-08-04 22:53:56 +000056#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossumb6775db1994-08-01 11:34:53 +000057#include "myselect.h"
58#else
59#include "mytime.h"
60#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061
Guido van Rossumb6775db1994-08-01 11:34:53 +000062#ifdef HAVE_FTIME
63#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000064#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Guido van Rossum1bb126f1996-12-06 20:17:44 +000065extern int ftime();
Guido van Rossum52174571996-12-09 18:38:52 +000066#endif /* MS_WINDOWS */
67#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068
Guido van Rossum7bf22de1997-12-02 20:34:19 +000069#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000070#include <i86.h>
71#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000072#ifdef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +000073#include <windows.h>
Guido van Rossumb2fb3641996-09-07 00:47:35 +000074#ifdef MS_WIN16
75/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000076#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000077#define tzname _tzname
78#define daylight _daylight
79#define altzone _altzone
Guido van Rossumb2fb3641996-09-07 00:47:35 +000080#endif /* MS_WIN16 */
Guido van Rossumcac6c721996-09-06 13:34:02 +000081#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000082#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000083
Guido van Rossum3917c221997-04-02 05:35:28 +000084#ifdef MS_WIN32
85/* Win32 has better clock replacement */
86#include <largeint.h>
87#undef HAVE_CLOCK /* We have our own version down below */
88#endif /* MS_WIN32 */
89
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000090#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000091#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000092#endif
93
Guido van Rossumbcc20741998-08-04 22:53:56 +000094#ifdef __BEOS__
95/* For bigtime_t, snooze(). - [cjh] */
96#include <support/SupportDefs.h>
97#include <kernel/OS.h>
Guido van Rossumd3eb5771999-03-09 16:07:23 +000098#ifndef CLOCKS_PER_SEC
99/* C'mon, fix the bloody headers... - [cjh] */
100#define CLOCKS_PER_SEC 1000
101#endif
Guido van Rossumbcc20741998-08-04 22:53:56 +0000102#endif
103
Guido van Rossum234f9421993-06-17 12:35:49 +0000104/* Forward declarations */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000105static int floatsleep Py_PROTO((double));
106static double floattime Py_PROTO(());
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000108/* For Y2K check */
109static PyObject *moddict;
110
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000111#ifdef macintosh
112/* Our own timezone. We have enough information to deduce whether
113** DST is on currently, but unfortunately we cannot put it to good
114** use because we don't know the rules (and that is needed to have
115** localtime() return correct tm_isdst values for times other than
116** the current time. So, we cop out and only tell the user the current
117** timezone.
118*/
119static long timezone;
120
121static void
122initmactimezone()
123{
124 MachineLocation loc;
125 long delta;
126
127 ReadLocation(&loc);
128
129 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
130 return;
131
132 delta = loc.u.gmtDelta & 0x00FFFFFF;
133
134 if (delta & 0x00800000)
135 delta |= 0xFF000000;
136
137 timezone = -delta;
138}
139#endif /* macintosh */
140
141
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000142static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143time_time(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000144 PyObject *self;
145 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000147 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000148 if (!PyArg_NoArgs(args))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000149 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000150 secs = floattime();
151 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000152 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000153 return NULL;
154 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000155 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000156}
157
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000158static char time_doc[] =
159"time() -> floating point number\n\
160\n\
161Return the current time in seconds since the Epoch.\n\
162Fractions of a second may be present if the system clock provides them.";
163
Guido van Rossumb6775db1994-08-01 11:34:53 +0000164#ifdef HAVE_CLOCK
165
166#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000167#ifdef CLK_TCK
168#define CLOCKS_PER_SEC CLK_TCK
169#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000170#define CLOCKS_PER_SEC 1000000
171#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000172#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000173
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000174static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000175time_clock(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000176 PyObject *self;
177 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000178{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000179 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000181 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184
Guido van Rossum3917c221997-04-02 05:35:28 +0000185#ifdef MS_WIN32
186/* Due to Mark Hammond */
187static PyObject *
188time_clock(self, args)
189 PyObject *self;
190 PyObject *args;
191{
192 static LARGE_INTEGER ctrStart;
193 static LARGE_INTEGER divisor = {0,0};
194 LARGE_INTEGER now, diff, rem;
195
196 if (!PyArg_NoArgs(args))
197 return NULL;
198
199 if (LargeIntegerEqualToZero(divisor)) {
200 QueryPerformanceCounter(&ctrStart);
201 if (!QueryPerformanceFrequency(&divisor) ||
202 LargeIntegerEqualToZero(divisor)) {
203 /* Unlikely to happen -
204 this works on all intel machines at least!
205 Revert to clock() */
206 return PyFloat_FromDouble(clock());
207 }
208 }
209 QueryPerformanceCounter(&now);
210 diff = LargeIntegerSubtract(now, ctrStart);
211 diff = LargeIntegerDivide(diff, divisor, &rem);
212 /* XXX - we assume both divide results fit in 32 bits. This is
213 true on Intels. First person who can afford a machine that
214 doesnt deserves to fix it :-)
215 */
216 return PyFloat_FromDouble((double)diff.LowPart +
217 ((double)rem.LowPart / (double)divisor.LowPart));
218}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000219
Guido van Rossum3917c221997-04-02 05:35:28 +0000220#define HAVE_CLOCK /* So it gets included in the methods */
221#endif /* MS_WIN32 */
222
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000223#ifdef HAVE_CLOCK
224static char clock_doc[] =
225"clock() -> floating point number\n\
226\n\
227Return the CPU time or real time since the start of the process or since\n\
228the first call to clock(). This has as much precision as the system records.";
229#endif
230
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000231static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232time_sleep(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000233 PyObject *self;
234 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000236 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000237 if (!PyArg_Parse(args, "d", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000239 if (floatsleep(secs) != 0)
240 return NULL;
241 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000242 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243}
244
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000245static char sleep_doc[] =
246"sleep(seconds)\n\
247\n\
248Delay execution for a given number of seconds. The argument may be\n\
249a floating point number for subsecond precision.";
250
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000251static PyObject *
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000252tmtotuple(p)
253 struct tm *p;
254{
255 return Py_BuildValue("(iiiiiiiii)",
256 p->tm_year + 1900,
257 p->tm_mon + 1, /* Want January == 1 */
258 p->tm_mday,
259 p->tm_hour,
260 p->tm_min,
261 p->tm_sec,
262 (p->tm_wday + 6) % 7, /* Want Monday == 0 */
263 p->tm_yday + 1, /* Want January, 1 == 1 */
264 p->tm_isdst);
265}
266
267static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000268time_convert(when, function)
269 time_t when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000270 struct tm * (*function) Py_PROTO((const time_t *));
Guido van Rossum234f9421993-06-17 12:35:49 +0000271{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000272 struct tm *p;
273 errno = 0;
274 p = function(&when);
275 if (p == NULL) {
276#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000277 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000278 errno = EINVAL;
279#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000280 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000281 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000282 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000283}
284
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000285static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000286time_gmtime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000287 PyObject *self;
288 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000289{
290 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000291 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000292 return NULL;
293 return time_convert((time_t)when, gmtime);
294}
295
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000296static char gmtime_doc[] =
297"gmtime(seconds) -> tuple\n\
298\n\
299Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT).";
300
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000301static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000302time_localtime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000303 PyObject *self;
304 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000305{
306 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000307 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000308 return NULL;
309 return time_convert((time_t)when, localtime);
310}
311
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000312static char localtime_doc[] =
313"localtime(seconds) -> tuple\n\
314Convert seconds since the Epoch to a time tuple expressing local time.";
315
Guido van Rossum9e90a671993-06-24 11:10:19 +0000316static int
317gettmarg(args, p)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000318 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000319 struct tm *p;
320{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000321 int y;
322 memset((ANY *) p, '\0', sizeof(struct tm));
323
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000324 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000325 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000326 &p->tm_mon,
327 &p->tm_mday,
328 &p->tm_hour,
329 &p->tm_min,
330 &p->tm_sec,
331 &p->tm_wday,
332 &p->tm_yday,
333 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000334 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000335 if (y < 1900) {
336 PyObject *accept = PyDict_GetItemString(moddict,
337 "accept2dyear");
338 if (accept == NULL || !PyInt_Check(accept) ||
339 PyInt_AsLong(accept) == 0) {
340 PyErr_SetString(PyExc_ValueError,
341 "year >= 1900 required");
342 return 0;
343 }
344 if (69 <= y && y <= 99)
345 y += 1900;
346 else if (0 <= y && y <= 68)
347 y += 2000;
348 else {
349 PyErr_SetString(PyExc_ValueError,
350 "year out of range (00-99, 1900-*)");
351 return 0;
352 }
353 }
354 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000355 p->tm_mon--;
356 p->tm_wday = (p->tm_wday + 1) % 7;
357 p->tm_yday--;
358 return 1;
359}
360
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000361#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000362static PyObject *
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000363time_strftime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000364 PyObject *self;
365 PyObject *args;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000366{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000367 PyObject *tup;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000368 struct tm buf;
369 const char *fmt;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000370 int fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000371 char *outbuf = 0;
372 int i;
373
Guido van Rossum1f41f841998-04-27 19:04:26 +0000374 memset((ANY *) &buf, '\0', sizeof(buf));
375
Guido van Rossum43713e52000-02-29 13:59:29 +0000376 if (!PyArg_ParseTuple(args, "sO:strftime", &fmt, &tup) || !gettmarg(tup, &buf))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000377 return NULL;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000378 fmtlen = strlen(fmt);
379
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000380 /* I hate these functions that presume you know how big the output
381 * will be ahead of time...
382 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000383 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000384 outbuf = malloc(i);
385 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000386 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000387 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000388 buflen = strftime(outbuf, i, fmt, &buf);
389 if (buflen > 0 || i >= 256 * fmtlen) {
390 /* If the buffer is 256 times as long as the format,
391 it's probably not failing for lack of room!
392 More likely, the format yields an empty result,
393 e.g. an empty format, or %Z when the timezone
394 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000395 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000396 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000397 free(outbuf);
398 return ret;
399 }
400 free(outbuf);
401 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000402}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000403
404static char strftime_doc[] =
405"strftime(format, tuple) -> string\n\
406\n\
407Convert a time tuple to a string according to a format specification.\n\
408See the library reference manual for formatting codes.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000409#endif /* HAVE_STRFTIME */
410
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000411#ifdef HAVE_STRPTIME
Guido van Rossum0a6363d1999-01-03 13:00:34 +0000412/* extern char *strptime(); /* Enable this if it's not declared in <time.h> */
Guido van Rossumc2068731998-10-07 16:35:25 +0000413
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000414static PyObject *
415time_strptime(self, args)
416 PyObject *self;
417 PyObject *args;
418{
419 struct tm tm;
420 char *fmt = "%a %b %d %H:%M:%S %Y";
421 char *buf;
422 char *s;
423
Guido van Rossum43713e52000-02-29 13:59:29 +0000424 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt)) {
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000425 PyErr_SetString(PyExc_ValueError, "invalid argument");
426 return NULL;
427 }
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000428 memset((ANY *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000429 s = strptime(buf, fmt, &tm);
430 if (s == NULL) {
431 PyErr_SetString(PyExc_ValueError, "format mismatch");
432 return NULL;
433 }
434 while (*s && isspace(*s))
435 s++;
436 if (*s) {
437 PyErr_Format(PyExc_ValueError,
438 "unconverted data remains: '%.400s'", s);
439 return NULL;
440 }
441 return tmtotuple(&tm);
442}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000443
444static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000445"strptime(string, format) -> tuple\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000446Parse a string to a time tuple according to a format specification.\n\
447See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000448#endif /* HAVE_STRPTIME */
449
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000450static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000451time_asctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000452 PyObject *self;
453 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000454{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000455 PyObject *tup;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000456 struct tm buf;
457 char *p;
Guido van Rossum43713e52000-02-29 13:59:29 +0000458 if (!PyArg_ParseTuple(args, "O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000459 return NULL;
460 if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000461 return NULL;
462 p = asctime(&buf);
463 if (p[24] == '\n')
464 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000465 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000466}
467
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000468static char asctime_doc[] =
469"asctime(tuple) -> string\n\
470\n\
471Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.";
472
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000473static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000474time_ctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000475 PyObject *self;
476 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000477{
478 double dt;
479 time_t tt;
480 char *p;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000481 if (!PyArg_Parse(args, "d", &dt))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000482 return NULL;
Guido van Rossumcac6c721996-09-06 13:34:02 +0000483 tt = (time_t)dt;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000484 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000485 if (p == NULL) {
486 PyErr_SetString(PyExc_ValueError, "unconvertible time");
487 return NULL;
488 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000489 if (p[24] == '\n')
490 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000491 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000492}
493
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000494static char ctime_doc[] =
495"ctime(seconds) -> string\n\
496\n\
497Convert a time in seconds since the Epoch to a string in local time.\n\
498This is equivalent to asctime(localtime(seconds)).";
499
Guido van Rossum60cd8131998-03-06 17:16:21 +0000500#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000501static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000502time_mktime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000503 PyObject *self;
504 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000505{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000506 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000507 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000508 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000509 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000510 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000511 tt = time(&tt);
512 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000513 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000514 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000515 tt = mktime(&buf);
516 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000517 PyErr_SetString(PyExc_OverflowError,
518 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000519 return NULL;
520 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000521 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000522}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000523
524static char mktime_doc[] =
525"mktime(tuple) -> floating point number\n\
526\n\
527Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000528#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000529
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000530static PyMethodDef time_methods[] = {
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000531 {"time", time_time, 0, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000532#ifdef HAVE_CLOCK
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000533 {"clock", time_clock, 0, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000534#endif
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000535 {"sleep", time_sleep, 0, sleep_doc},
536 {"gmtime", time_gmtime, 0, gmtime_doc},
537 {"localtime", time_localtime, 0, localtime_doc},
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000538 {"asctime", time_asctime, 1, asctime_doc},
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000539 {"ctime", time_ctime, 0, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000540#ifdef HAVE_MKTIME
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000541 {"mktime", time_mktime, 1, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000542#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000543#ifdef HAVE_STRFTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000544 {"strftime", time_strftime, 1, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000545#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000546#ifdef HAVE_STRPTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000547 {"strptime", time_strptime, 1, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000548#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000549 {NULL, NULL} /* sentinel */
550};
551
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000552static void
553ins(d, name, v)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000554 PyObject *d;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000555 char *name;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000556 PyObject *v;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000557{
558 if (v == NULL)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000559 Py_FatalError("Can't initialize time module -- NULL value");
560 if (PyDict_SetItemString(d, name, v) != 0)
561 Py_FatalError(
Guido van Rossum52174571996-12-09 18:38:52 +0000562 "Can't initialize time module -- PyDict_SetItemString failed");
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000563 Py_DECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000564}
565
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000566static char module_doc[] =
567"This module provides various functions to manipulate time values.\n\
568\n\
569There are two standard representations of time. One is the number\n\
570of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
571or a floating point number (to represent fractions of seconds).\n\
572The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
573The actual value can be retrieved by calling gmtime(0).\n\
574\n\
575The other representation is a tuple of 9 integers giving local time.\n\
576The tuple items are:\n\
577 year (four digits, e.g. 1998)\n\
578 month (1-12)\n\
579 day (1-31)\n\
580 hours (0-23)\n\
581 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000582 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000583 weekday (0-6, Monday is 0)\n\
584 Julian day (day in the year, 1-366)\n\
585 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
586If the DST flag is 0, the time is given in the regular time zone;\n\
587if it is 1, the time is given in the DST time zone;\n\
588if it is -1, mktime() should guess based on the date and time.\n\
589\n\
590Variables:\n\
591\n\
592timezone -- difference in seconds between UTC and local standard time\n\
593altzone -- difference in seconds between UTC and local DST time\n\
594daylight -- whether local time should reflect DST\n\
595tzname -- tuple of (standard time zone name, DST time zone name)\n\
596\n\
597Functions:\n\
598\n\
599time() -- return current time in seconds since the Epoch as a float\n\
600clock() -- return CPU time since process start as a float\n\
601sleep() -- delay for a number of seconds given as a float\n\
602gmtime() -- convert seconds since Epoch to UTC tuple\n\
603localtime() -- convert seconds since Epoch to local time tuple\n\
604asctime() -- convert time tuple to string\n\
605ctime() -- convert time in seconds to string\n\
606mktime() -- convert local time tuple to seconds since Epoch\n\
607strftime() -- convert time tuple to string according to format specification\n\
608strptime() -- parse string to time tuple according to format specification\n\
609";
610
611
Guido van Rossum3886bb61998-12-04 18:50:17 +0000612DL_EXPORT(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000613inittime()
614{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000615 PyObject *m, *d;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000616 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000617 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000618 d = PyModule_GetDict(m);
Guido van Rossumc2068731998-10-07 16:35:25 +0000619 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
620 p = getenv("PYTHONY2K");
621 ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
622 /* Squirrel away the module's dictionary for the y2k check */
623 Py_INCREF(d);
624 moddict = d;
Guido van Rossumea424e11999-04-23 20:59:05 +0000625#if defined(HAVE_TZNAME) && !defined(__GLIBC__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000626 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000627#ifdef PYOS_OS2
628 ins(d, "timezone", PyInt_FromLong((long)_timezone));
629#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000630 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossum26452411998-09-28 22:07:11 +0000631#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000632#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000633 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000634#else
Guido van Rossum26452411998-09-28 22:07:11 +0000635#ifdef PYOS_OS2
636 ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
637#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000638 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossum26452411998-09-28 22:07:11 +0000639#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000640#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000641 ins(d, "daylight", PyInt_FromLong((long)daylight));
642 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossumea424e11999-04-23 20:59:05 +0000643#else /* !HAVE_TZNAME || __GLIBC__ */
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000644#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000645 {
646#define YEAR ((time_t)((365 * 24 + 6) * 3600))
647 time_t t;
648 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000649 long janzone, julyzone;
650 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000651 t = (time((time_t *)0) / YEAR) * YEAR;
652 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000653 janzone = -p->tm_gmtoff;
654 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
655 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000656 t += YEAR/2;
657 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000658 julyzone = -p->tm_gmtoff;
659 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
660 julyname[9] = '\0';
661
662 if( janzone < julyzone ) {
663 /* DST is reversed in the southern hemisphere */
664 ins(d, "timezone", PyInt_FromLong(julyzone));
665 ins(d, "altzone", PyInt_FromLong(janzone));
666 ins(d, "daylight",
667 PyInt_FromLong((long)(janzone != julyzone)));
668 ins(d, "tzname",
669 Py_BuildValue("(zz)", julyname, janname));
670 } else {
671 ins(d, "timezone", PyInt_FromLong(janzone));
672 ins(d, "altzone", PyInt_FromLong(julyzone));
673 ins(d, "daylight",
674 PyInt_FromLong((long)(janzone != julyzone)));
675 ins(d, "tzname",
676 Py_BuildValue("(zz)", janname, julyname));
677 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000678 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000679#else
680#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000681 /* The only thing we can obtain is the current timezone
682 ** (and whether dst is currently _active_, but that is not what
683 ** we're looking for:-( )
684 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000685 initmactimezone();
686 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000687 ins(d, "altzone", PyInt_FromLong(timezone));
688 ins(d, "daylight", PyInt_FromLong((long)0));
689 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000690#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000691#endif /* HAVE_TM_ZONE */
Guido van Rossumea424e11999-04-23 20:59:05 +0000692#endif /* !HAVE_TZNAME || __GLIBC__ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000693 if (PyErr_Occurred())
694 Py_FatalError("Can't initialize time module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000695}
696
697
Guido van Rossumb6775db1994-08-01 11:34:53 +0000698/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000699
Guido van Rossumb6775db1994-08-01 11:34:53 +0000700static double
701floattime()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000702{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000703 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000704 (1) gettimeofday() -- resolution in microseconds
705 (2) ftime() -- resolution in milliseconds
706 (3) time() -- resolution in seconds
707 In all cases the return value is a float in seconds.
708 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
709 fail, so we fall back on ftime() or time().
710 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000711#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000712 {
713 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000714#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000715 if (gettimeofday(&t) == 0)
716 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000717#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000718 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
719 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000720#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000721 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000722#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000723 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000724#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000725 struct timeb t;
726 ftime(&t);
727 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000728#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000729 time_t secs;
730 time(&secs);
731 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000732#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000733 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000734}
735
Guido van Rossumb6775db1994-08-01 11:34:53 +0000736
737/* Implement floatsleep() for various platforms.
738 When interrupted (or when another error occurs), return -1 and
739 set an exception; else return 0. */
740
741static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000742#ifdef MPW
743floatsleep(double secs)
744#else
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000745 floatsleep(secs)
Guido van Rossum775f4da1993-01-09 17:18:52 +0000746 double secs;
Guido van Rossuma320fd31995-03-09 12:14:15 +0000747#endif /* MPW */
Guido van Rossum426035c1991-02-19 12:27:35 +0000748{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000749/* XXX Should test for MS_WIN32 first! */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000750#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000751 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000752 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000753 frac = fmod(secs, 1.0);
754 secs = floor(secs);
755 t.tv_sec = (long)secs;
756 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000757 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000758 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000759 Py_BLOCK_THREADS
Guido van Rossum09cbb011999-11-08 15:32:27 +0000760#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000761 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000762#else
763 if (1) {
764#endif
Guido van Rossuma5456d51999-08-19 14:40:27 +0000765 PyErr_SetFromErrno(PyExc_IOError);
766 return -1;
767 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000768 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000769 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000770#else /* !HAVE_SELECT || __BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000771#ifdef macintosh
772#define MacTicks (* (long *)0x16A)
773 long deadline;
774 deadline = MacTicks + (long)(secs * 60.0);
775 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000776 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000777 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000778 return -1;
779 }
780#else /* !macintosh */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000781#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000782 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000783 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000784 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000785 Py_END_ALLOW_THREADS
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000786#else /* !__WATCOMC__ || __QNX__ */
Guido van Rossume22e6441993-07-09 10:51:31 +0000787#ifdef MSDOS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000788 struct timeb t1, t2;
789 double frac;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000790 extern double fmod Py_PROTO((double, double));
791 extern double floor Py_PROTO((double));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000792 if (secs <= 0.0)
793 return;
794 frac = fmod(secs, 1.0);
795 secs = floor(secs);
796 ftime(&t1);
797 t2.time = t1.time + (int)secs;
798 t2.millitm = t1.millitm + (int)(frac*1000.0);
799 while (t2.millitm >= 1000) {
800 t2.time++;
801 t2.millitm -= 1000;
802 }
803 for (;;) {
804#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000805 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000806 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000807 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000808#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000809 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000810 return -1;
811 ftime(&t1);
812 if (t1.time > t2.time ||
813 t1.time == t2.time && t1.millitm >= t2.millitm)
814 break;
815 }
816#else /* !MSDOS */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000817#ifdef MS_WIN32
Guido van Rossumb6775db1994-08-01 11:34:53 +0000818 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000819 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000820 Sleep((int)(secs*1000));
Guido van Rossum8607ae21997-11-03 22:04:46 +0000821 Py_END_ALLOW_THREADS
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000822#else /* !MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000823#ifdef PYOS_OS2
824 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000825 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000826 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000827 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000828 PyErr_SetFromErrno(PyExc_IOError);
829 return -1;
830 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000831 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000832#else /* !PYOS_OS2 */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000833#ifdef __BEOS__
834 /* This sleep *CAN BE* interrupted. */
835 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000836 if( secs <= 0.0 ) {
837 return;
838 }
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000839
Guido van Rossumbcc20741998-08-04 22:53:56 +0000840 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000841 /* BeOS snooze() is in microseconds... */
842 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000843 Py_BLOCK_THREADS
844 PyErr_SetFromErrno( PyExc_IOError );
845 return -1;
846 }
847 Py_END_ALLOW_THREADS
848 }
849#else /* !__BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000850 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000851 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000852 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000853 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000854#endif /* !__BEOS__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000855#endif /* !PYOS_OS2 */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000856#endif /* !MS_WIN32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000857#endif /* !MSDOS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000858#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000859#endif /* !macintosh */
860#endif /* !HAVE_SELECT */
861 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000862}