blob: 7ad8a6fe2d9f989767df043f17935ede65be87fb [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
Guido van Rossumbcc20741998-08-04 22:53:56 +000063#ifndef __BEOS__
64/* We have ftime(), but not in the headers (PR2). - [cjh] */
Guido van Rossumb6775db1994-08-01 11:34:53 +000065#include <sys/timeb.h>
Guido van Rossumbcc20741998-08-04 22:53:56 +000066#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000067#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Guido van Rossum1bb126f1996-12-06 20:17:44 +000068extern int ftime();
Guido van Rossum52174571996-12-09 18:38:52 +000069#endif /* MS_WINDOWS */
70#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071
Guido van Rossum7bf22de1997-12-02 20:34:19 +000072#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000073#include <i86.h>
74#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000075#ifdef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +000076#include <windows.h>
Guido van Rossumb2fb3641996-09-07 00:47:35 +000077#ifdef MS_WIN16
78/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000080#define tzname _tzname
81#define daylight _daylight
82#define altzone _altzone
Guido van Rossumb2fb3641996-09-07 00:47:35 +000083#endif /* MS_WIN16 */
Guido van Rossumcac6c721996-09-06 13:34:02 +000084#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000085#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000086
Guido van Rossum3917c221997-04-02 05:35:28 +000087#ifdef MS_WIN32
88/* Win32 has better clock replacement */
89#include <largeint.h>
90#undef HAVE_CLOCK /* We have our own version down below */
91#endif /* MS_WIN32 */
92
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000093#if defined(PYOS_OS2)
94#define INCL_DOS
95#define INCL_DOSERRORS
96#define INCL_NOPMAPI
97#include <os2.h>
98#endif
99
100#if defined(PYCC_VACPP)
101#include <time.h>
102#define timezone _timezone
103#endif
104
Guido van Rossumbcc20741998-08-04 22:53:56 +0000105#ifdef __BEOS__
106/* For bigtime_t, snooze(). - [cjh] */
107#include <support/SupportDefs.h>
108#include <kernel/OS.h>
109#endif
110
Guido van Rossum234f9421993-06-17 12:35:49 +0000111/* Forward declarations */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000112static int floatsleep Py_PROTO((double));
113static double floattime Py_PROTO(());
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000115/* For Y2K check */
116static PyObject *moddict;
117
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000118#ifdef macintosh
119/* Our own timezone. We have enough information to deduce whether
120** DST is on currently, but unfortunately we cannot put it to good
121** use because we don't know the rules (and that is needed to have
122** localtime() return correct tm_isdst values for times other than
123** the current time. So, we cop out and only tell the user the current
124** timezone.
125*/
126static long timezone;
127
128static void
129initmactimezone()
130{
131 MachineLocation loc;
132 long delta;
133
134 ReadLocation(&loc);
135
136 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
137 return;
138
139 delta = loc.u.gmtDelta & 0x00FFFFFF;
140
141 if (delta & 0x00800000)
142 delta |= 0xFF000000;
143
144 timezone = -delta;
145}
146#endif /* macintosh */
147
148
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000149static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150time_time(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000151 PyObject *self;
152 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000154 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000155 if (!PyArg_NoArgs(args))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000156 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000157 secs = floattime();
158 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000159 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000160 return NULL;
161 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000162 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000163}
164
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000165static char time_doc[] =
166"time() -> floating point number\n\
167\n\
168Return the current time in seconds since the Epoch.\n\
169Fractions of a second may be present if the system clock provides them.";
170
Guido van Rossumb6775db1994-08-01 11:34:53 +0000171#ifdef HAVE_CLOCK
172
173#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000174#ifdef CLK_TCK
175#define CLOCKS_PER_SEC CLK_TCK
176#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000177#define CLOCKS_PER_SEC 1000000
178#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000179#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000180
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000181static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000182time_clock(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000183 PyObject *self;
184 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000185{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000186 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000188 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000190#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191
Guido van Rossum3917c221997-04-02 05:35:28 +0000192#ifdef MS_WIN32
193/* Due to Mark Hammond */
194static PyObject *
195time_clock(self, args)
196 PyObject *self;
197 PyObject *args;
198{
199 static LARGE_INTEGER ctrStart;
200 static LARGE_INTEGER divisor = {0,0};
201 LARGE_INTEGER now, diff, rem;
202
203 if (!PyArg_NoArgs(args))
204 return NULL;
205
206 if (LargeIntegerEqualToZero(divisor)) {
207 QueryPerformanceCounter(&ctrStart);
208 if (!QueryPerformanceFrequency(&divisor) ||
209 LargeIntegerEqualToZero(divisor)) {
210 /* Unlikely to happen -
211 this works on all intel machines at least!
212 Revert to clock() */
213 return PyFloat_FromDouble(clock());
214 }
215 }
216 QueryPerformanceCounter(&now);
217 diff = LargeIntegerSubtract(now, ctrStart);
218 diff = LargeIntegerDivide(diff, divisor, &rem);
219 /* XXX - we assume both divide results fit in 32 bits. This is
220 true on Intels. First person who can afford a machine that
221 doesnt deserves to fix it :-)
222 */
223 return PyFloat_FromDouble((double)diff.LowPart +
224 ((double)rem.LowPart / (double)divisor.LowPart));
225}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000226
Guido van Rossum3917c221997-04-02 05:35:28 +0000227#define HAVE_CLOCK /* So it gets included in the methods */
228#endif /* MS_WIN32 */
229
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000230#ifdef HAVE_CLOCK
231static char clock_doc[] =
232"clock() -> floating point number\n\
233\n\
234Return the CPU time or real time since the start of the process or since\n\
235the first call to clock(). This has as much precision as the system records.";
236#endif
237
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000238static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239time_sleep(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000240 PyObject *self;
241 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000243 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000244 if (!PyArg_Parse(args, "d", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000246 if (floatsleep(secs) != 0)
247 return NULL;
248 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000249 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250}
251
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000252static char sleep_doc[] =
253"sleep(seconds)\n\
254\n\
255Delay execution for a given number of seconds. The argument may be\n\
256a floating point number for subsecond precision.";
257
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000258static PyObject *
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000259tmtotuple(p)
260 struct tm *p;
261{
262 return Py_BuildValue("(iiiiiiiii)",
263 p->tm_year + 1900,
264 p->tm_mon + 1, /* Want January == 1 */
265 p->tm_mday,
266 p->tm_hour,
267 p->tm_min,
268 p->tm_sec,
269 (p->tm_wday + 6) % 7, /* Want Monday == 0 */
270 p->tm_yday + 1, /* Want January, 1 == 1 */
271 p->tm_isdst);
272}
273
274static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000275time_convert(when, function)
276 time_t when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000277 struct tm * (*function) Py_PROTO((const time_t *));
Guido van Rossum234f9421993-06-17 12:35:49 +0000278{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000279 struct tm *p;
280 errno = 0;
281 p = function(&when);
282 if (p == NULL) {
283#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000284 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000285 errno = EINVAL;
286#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000287 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000288 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000289 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000290}
291
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000292static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000293time_gmtime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000294 PyObject *self;
295 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000296{
297 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000298 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000299 return NULL;
300 return time_convert((time_t)when, gmtime);
301}
302
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000303static char gmtime_doc[] =
304"gmtime(seconds) -> tuple\n\
305\n\
306Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT).";
307
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000308static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000309time_localtime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000310 PyObject *self;
311 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000312{
313 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000314 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000315 return NULL;
316 return time_convert((time_t)when, localtime);
317}
318
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000319static char localtime_doc[] =
320"localtime(seconds) -> tuple\n\
321Convert seconds since the Epoch to a time tuple expressing local time.";
322
Guido van Rossum9e90a671993-06-24 11:10:19 +0000323static int
324gettmarg(args, p)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000325 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000326 struct tm *p;
327{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000328 int y;
329 memset((ANY *) p, '\0', sizeof(struct tm));
330
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000331 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000332 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000333 &p->tm_mon,
334 &p->tm_mday,
335 &p->tm_hour,
336 &p->tm_min,
337 &p->tm_sec,
338 &p->tm_wday,
339 &p->tm_yday,
340 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000341 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000342 if (y < 1900) {
343 PyObject *accept = PyDict_GetItemString(moddict,
344 "accept2dyear");
345 if (accept == NULL || !PyInt_Check(accept) ||
346 PyInt_AsLong(accept) == 0) {
347 PyErr_SetString(PyExc_ValueError,
348 "year >= 1900 required");
349 return 0;
350 }
351 if (69 <= y && y <= 99)
352 y += 1900;
353 else if (0 <= y && y <= 68)
354 y += 2000;
355 else {
356 PyErr_SetString(PyExc_ValueError,
357 "year out of range (00-99, 1900-*)");
358 return 0;
359 }
360 }
361 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000362 p->tm_mon--;
363 p->tm_wday = (p->tm_wday + 1) % 7;
364 p->tm_yday--;
365 return 1;
366}
367
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000368#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000369static PyObject *
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000370time_strftime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000371 PyObject *self;
372 PyObject *args;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000373{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000374 PyObject *tup;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000375 struct tm buf;
376 const char *fmt;
377 char *outbuf = 0;
378 int i;
379
Guido van Rossum1f41f841998-04-27 19:04:26 +0000380 memset((ANY *) &buf, '\0', sizeof(buf));
381
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000382 if (!PyArg_ParseTuple(args, "sO", &fmt, &tup) || !gettmarg(tup, &buf))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000383 return NULL;
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000384 /* I hate these functions that presume you know how big the output
385 * will be ahead of time...
386 */
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000387 for (i = 1024 ; i <= 8192 ; i += 1024) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000388 outbuf = malloc(i);
389 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000390 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000391 }
392 if (strftime(outbuf, i-1, fmt, &buf) != 0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000393 PyObject *ret;
394 ret = PyString_FromString(outbuf);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000395 free(outbuf);
396 return ret;
397 }
398 free(outbuf);
399 }
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000400 PyErr_SetString(PyExc_ValueError,
401 "bad strftime format or result too big");
402 return NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000403}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000404
405static char strftime_doc[] =
406"strftime(format, tuple) -> string\n\
407\n\
408Convert a time tuple to a string according to a format specification.\n\
409See the library reference manual for formatting codes.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000410#endif /* HAVE_STRFTIME */
411
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000412#ifdef HAVE_STRPTIME
413static PyObject *
414time_strptime(self, args)
415 PyObject *self;
416 PyObject *args;
417{
418 struct tm tm;
419 char *fmt = "%a %b %d %H:%M:%S %Y";
420 char *buf;
421 char *s;
422
423 if (!PyArg_ParseTuple(args, "s|s", &buf, &fmt)) {
424 PyErr_SetString(PyExc_ValueError, "invalid argument");
425 return NULL;
426 }
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000427 memset((ANY *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000428 s = strptime(buf, fmt, &tm);
429 if (s == NULL) {
430 PyErr_SetString(PyExc_ValueError, "format mismatch");
431 return NULL;
432 }
433 while (*s && isspace(*s))
434 s++;
435 if (*s) {
436 PyErr_Format(PyExc_ValueError,
437 "unconverted data remains: '%.400s'", s);
438 return NULL;
439 }
440 return tmtotuple(&tm);
441}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000442
443static char strptime_doc[] =
444"strptime(format, string) -> tuple\n\
445Parse a string to a time tuple according to a format specification.\n\
446See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000447#endif /* HAVE_STRPTIME */
448
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000449static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000450time_asctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000451 PyObject *self;
452 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000453{
454 struct tm buf;
455 char *p;
456 if (!gettmarg(args, &buf))
457 return NULL;
458 p = asctime(&buf);
459 if (p[24] == '\n')
460 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000461 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000462}
463
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000464static char asctime_doc[] =
465"asctime(tuple) -> string\n\
466\n\
467Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.";
468
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000469static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000470time_ctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000471 PyObject *self;
472 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000473{
474 double dt;
475 time_t tt;
476 char *p;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000477 if (!PyArg_Parse(args, "d", &dt))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000478 return NULL;
Guido van Rossumcac6c721996-09-06 13:34:02 +0000479 tt = (time_t)dt;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000480 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000481 if (p == NULL) {
482 PyErr_SetString(PyExc_ValueError, "unconvertible time");
483 return NULL;
484 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000485 if (p[24] == '\n')
486 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000487 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000488}
489
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000490static char ctime_doc[] =
491"ctime(seconds) -> string\n\
492\n\
493Convert a time in seconds since the Epoch to a string in local time.\n\
494This is equivalent to asctime(localtime(seconds)).";
495
Guido van Rossum60cd8131998-03-06 17:16:21 +0000496#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000497static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000498time_mktime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000499 PyObject *self;
500 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000501{
502 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000503 time_t tt;
504 tt = time(&tt);
505 buf = *localtime(&tt);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000506 if (!gettmarg(args, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000507 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000508 tt = mktime(&buf);
509 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000510 PyErr_SetString(PyExc_OverflowError,
511 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000512 return NULL;
513 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000514 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000515}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000516
517static char mktime_doc[] =
518"mktime(tuple) -> floating point number\n\
519\n\
520Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000521#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000522
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000523static PyMethodDef time_methods[] = {
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000524 {"time", time_time, 0, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000525#ifdef HAVE_CLOCK
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000526 {"clock", time_clock, 0, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000527#endif
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000528 {"sleep", time_sleep, 0, sleep_doc},
529 {"gmtime", time_gmtime, 0, gmtime_doc},
530 {"localtime", time_localtime, 0, localtime_doc},
531 {"asctime", time_asctime, 0, asctime_doc},
532 {"ctime", time_ctime, 0, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000533#ifdef HAVE_MKTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000534 {"mktime", time_mktime, 0, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000535#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000536#ifdef HAVE_STRFTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000537 {"strftime", time_strftime, 1, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000538#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000539#ifdef HAVE_STRPTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000540 {"strptime", time_strptime, 1, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000541#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000542 {NULL, NULL} /* sentinel */
543};
544
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000545static void
546ins(d, name, v)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000547 PyObject *d;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000548 char *name;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000549 PyObject *v;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000550{
551 if (v == NULL)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000552 Py_FatalError("Can't initialize time module -- NULL value");
553 if (PyDict_SetItemString(d, name, v) != 0)
554 Py_FatalError(
Guido van Rossum52174571996-12-09 18:38:52 +0000555 "Can't initialize time module -- PyDict_SetItemString failed");
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000556 Py_DECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000557}
558
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000559static char module_doc[] =
560"This module provides various functions to manipulate time values.\n\
561\n\
562There are two standard representations of time. One is the number\n\
563of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
564or a floating point number (to represent fractions of seconds).\n\
565The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
566The actual value can be retrieved by calling gmtime(0).\n\
567\n\
568The other representation is a tuple of 9 integers giving local time.\n\
569The tuple items are:\n\
570 year (four digits, e.g. 1998)\n\
571 month (1-12)\n\
572 day (1-31)\n\
573 hours (0-23)\n\
574 minutes (0-59)\n\
575 seconds (0-61, to allow for leap seconds)\n\
576 weekday (0-6, Monday is 0)\n\
577 Julian day (day in the year, 1-366)\n\
578 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
579If the DST flag is 0, the time is given in the regular time zone;\n\
580if it is 1, the time is given in the DST time zone;\n\
581if it is -1, mktime() should guess based on the date and time.\n\
582\n\
583Variables:\n\
584\n\
585timezone -- difference in seconds between UTC and local standard time\n\
586altzone -- difference in seconds between UTC and local DST time\n\
587daylight -- whether local time should reflect DST\n\
588tzname -- tuple of (standard time zone name, DST time zone name)\n\
589\n\
590Functions:\n\
591\n\
592time() -- return current time in seconds since the Epoch as a float\n\
593clock() -- return CPU time since process start as a float\n\
594sleep() -- delay for a number of seconds given as a float\n\
595gmtime() -- convert seconds since Epoch to UTC tuple\n\
596localtime() -- convert seconds since Epoch to local time tuple\n\
597asctime() -- convert time tuple to string\n\
598ctime() -- convert time in seconds to string\n\
599mktime() -- convert local time tuple to seconds since Epoch\n\
600strftime() -- convert time tuple to string according to format specification\n\
601strptime() -- parse string to time tuple according to format specification\n\
602";
603
604
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000605void
606inittime()
607{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000608 PyObject *m, *d;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000609 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000610 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000611 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000612#ifdef HAVE_TZNAME
Guido van Rossum234f9421993-06-17 12:35:49 +0000613 tzset();
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000614 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000615#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000616 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000617#else
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000618 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000619#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000620 ins(d, "daylight", PyInt_FromLong((long)daylight));
621 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000622 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
623 p = getenv("PYTHONY2K");
624 ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
625 /* Squirrel away the module's dictionary for the y2k check */
626 Py_INCREF(d);
627 moddict = d;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000628#else /* !HAVE_TZNAME */
629#if HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000630 {
631#define YEAR ((time_t)((365 * 24 + 6) * 3600))
632 time_t t;
633 struct tm *p;
634 long winterzone, summerzone;
635 char wintername[10], summername[10];
Guido van Rossumb6775db1994-08-01 11:34:53 +0000636 /* XXX This won't work on the southern hemisphere.
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000637 XXX Anybody got a better idea? */
Guido van Rossum234f9421993-06-17 12:35:49 +0000638 t = (time((time_t *)0) / YEAR) * YEAR;
639 p = localtime(&t);
640 winterzone = -p->tm_gmtoff;
641 strncpy(wintername, p->tm_zone ? p->tm_zone : " ", 9);
642 wintername[9] = '\0';
643 t += YEAR/2;
644 p = localtime(&t);
645 summerzone = -p->tm_gmtoff;
646 strncpy(summername, p->tm_zone ? p->tm_zone : " ", 9);
647 summername[9] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000648 ins(d, "timezone", PyInt_FromLong(winterzone));
649 ins(d, "altzone", PyInt_FromLong(summerzone));
650 ins(d, "daylight",
651 PyInt_FromLong((long)(winterzone != summerzone)));
652 ins(d, "tzname",
653 Py_BuildValue("(zz)", wintername, summername));
Guido van Rossum234f9421993-06-17 12:35:49 +0000654 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000655#else
656#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000657 /* The only thing we can obtain is the current timezone
658 ** (and whether dst is currently _active_, but that is not what
659 ** we're looking for:-( )
660 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000661 initmactimezone();
662 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000663 ins(d, "altzone", PyInt_FromLong(timezone));
664 ins(d, "daylight", PyInt_FromLong((long)0));
665 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000666#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000667#endif /* HAVE_TM_ZONE */
668#endif /* !HAVE_TZNAME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000669 if (PyErr_Occurred())
670 Py_FatalError("Can't initialize time module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000671}
672
673
Guido van Rossumb6775db1994-08-01 11:34:53 +0000674/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000675
Guido van Rossumb6775db1994-08-01 11:34:53 +0000676static double
677floattime()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000678{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000679 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000680 (1) gettimeofday() -- resolution in microseconds
681 (2) ftime() -- resolution in milliseconds
682 (3) time() -- resolution in seconds
683 In all cases the return value is a float in seconds.
684 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
685 fail, so we fall back on ftime() or time().
686 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000687#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000688 {
689 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000690#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000691 if (gettimeofday(&t) == 0)
692 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000693#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000694 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
695 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000696#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000697 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000698#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000699 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000700#if defined(HAVE_FTIME) && !defined(__BEOS__)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000701 struct timeb t;
702 ftime(&t);
703 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000704#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000705 time_t secs;
706 time(&secs);
707 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000708#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000709 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000710}
711
Guido van Rossumb6775db1994-08-01 11:34:53 +0000712
713/* Implement floatsleep() for various platforms.
714 When interrupted (or when another error occurs), return -1 and
715 set an exception; else return 0. */
716
717static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000718#ifdef MPW
719floatsleep(double secs)
720#else
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000721 floatsleep(secs)
Guido van Rossum775f4da1993-01-09 17:18:52 +0000722 double secs;
Guido van Rossuma320fd31995-03-09 12:14:15 +0000723#endif /* MPW */
Guido van Rossum426035c1991-02-19 12:27:35 +0000724{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000725/* XXX Should test for MS_WIN32 first! */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000726#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000727 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000728 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000729 frac = fmod(secs, 1.0);
730 secs = floor(secs);
731 t.tv_sec = (long)secs;
732 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000733 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000734 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000735 Py_BLOCK_THREADS
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000736 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000737 return -1;
738 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000739 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000740#else /* !HAVE_SELECT || __BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000741#ifdef macintosh
742#define MacTicks (* (long *)0x16A)
743 long deadline;
744 deadline = MacTicks + (long)(secs * 60.0);
745 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000746 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000747 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000748 return -1;
749 }
750#else /* !macintosh */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000751#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000752 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000753 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000754 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000755 Py_END_ALLOW_THREADS
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000756#else /* !__WATCOMC__ || __QNX__ */
Guido van Rossume22e6441993-07-09 10:51:31 +0000757#ifdef MSDOS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000758 struct timeb t1, t2;
759 double frac;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000760 extern double fmod Py_PROTO((double, double));
761 extern double floor Py_PROTO((double));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000762 if (secs <= 0.0)
763 return;
764 frac = fmod(secs, 1.0);
765 secs = floor(secs);
766 ftime(&t1);
767 t2.time = t1.time + (int)secs;
768 t2.millitm = t1.millitm + (int)(frac*1000.0);
769 while (t2.millitm >= 1000) {
770 t2.time++;
771 t2.millitm -= 1000;
772 }
773 for (;;) {
774#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000775 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000776 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000777 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000778#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000779 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000780 return -1;
781 ftime(&t1);
782 if (t1.time > t2.time ||
783 t1.time == t2.time && t1.millitm >= t2.millitm)
784 break;
785 }
786#else /* !MSDOS */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000787#ifdef MS_WIN32
Guido van Rossumb6775db1994-08-01 11:34:53 +0000788 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000789 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000790 Sleep((int)(secs*1000));
Guido van Rossum8607ae21997-11-03 22:04:46 +0000791 Py_END_ALLOW_THREADS
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000792#else /* !MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000793#ifdef PYOS_OS2
794 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000795 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000796 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000797 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000798 PyErr_SetFromErrno(PyExc_IOError);
799 return -1;
800 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000801 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000802#else /* !PYOS_OS2 */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000803#ifdef __BEOS__
804 /* This sleep *CAN BE* interrupted. */
805 {
806 bigtime_t frac, seconds;
807
808 extern double fmod Py_PROTO((double,double));
809 extern double floor Py_PROTO((double));
810
811 if( secs <= 0.0 ) {
812 return;
813 }
814
815 frac = (bigtime_t)fmod( secs, 1.0 );
816 seconds = (bigtime_t)floor( secs );
817
818 Py_BEGIN_ALLOW_THREADS
819 if( snooze( seconds * (bigtime_t)1000 + frac ) == B_INTERRUPTED ) {
820 Py_BLOCK_THREADS
821 PyErr_SetFromErrno( PyExc_IOError );
822 return -1;
823 }
824 Py_END_ALLOW_THREADS
825 }
826#else /* !__BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000827 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000828 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000829 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000830 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000831#endif /* !__BEOS__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000832#endif /* !PYOS_OS2 */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000833#endif /* !MS_WIN32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000834#endif /* !MSDOS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000835#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000836#endif /* !macintosh */
837#endif /* !HAVE_SELECT */
838 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000839}