blob: cebcc68fc86df9d76e6750740a861514878a6712 [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>
Guido van Rossumc410e922000-04-26 20:40:13 +000044#include <OSUtils.h>
45#ifdef USE_GUSI2
46/* GUSI, the I/O library which has the time() function and such uses the
47** Mac epoch of 1904. MSL, the C library which has localtime() and so uses
48** the ANSI epoch of 1900.
49*/
50#define GUSI_TO_MSL_EPOCH (4*365*24*60*60)
51#endif /* USE_GUSI2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000052#else
53#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000054#endif
55
Guido van Rossumb6775db1994-08-01 11:34:53 +000056#ifdef QUICKWIN
57#include <io.h>
58#endif
59
60#ifdef HAVE_UNISTD_H
Guido van Rossum2762f251992-03-27 17:22:13 +000061#include <unistd.h>
62#endif
63
Guido van Rossumbcc20741998-08-04 22:53:56 +000064#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossumb6775db1994-08-01 11:34:53 +000065#include "myselect.h"
66#else
67#include "mytime.h"
68#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069
Guido van Rossumb6775db1994-08-01 11:34:53 +000070#ifdef HAVE_FTIME
71#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000072#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Guido van Rossum1bb126f1996-12-06 20:17:44 +000073extern int ftime();
Guido van Rossum52174571996-12-09 18:38:52 +000074#endif /* MS_WINDOWS */
75#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076
Guido van Rossum7bf22de1997-12-02 20:34:19 +000077#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000078#include <i86.h>
79#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000080#ifdef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +000081#include <windows.h>
Guido van Rossumb2fb3641996-09-07 00:47:35 +000082#ifdef MS_WIN16
83/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000084#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000085#define tzname _tzname
86#define daylight _daylight
87#define altzone _altzone
Guido van Rossumb2fb3641996-09-07 00:47:35 +000088#endif /* MS_WIN16 */
Guido van Rossumcac6c721996-09-06 13:34:02 +000089#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000090#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000091
Fred Drakedfb4ebd2000-06-29 20:56:28 +000092#if defined(MS_WIN32) && !defined(MS_WIN64)
93/* Win32 has better clock replacement
94 XXX Win64 does not yet, but might when the platform matures. */
Guido van Rossum3917c221997-04-02 05:35:28 +000095#include <largeint.h>
96#undef HAVE_CLOCK /* We have our own version down below */
Fred Drakedfb4ebd2000-06-29 20:56:28 +000097#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +000098
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000099#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +0000100#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000101#endif
102
Guido van Rossumbcc20741998-08-04 22:53:56 +0000103#ifdef __BEOS__
104/* For bigtime_t, snooze(). - [cjh] */
105#include <support/SupportDefs.h>
106#include <kernel/OS.h>
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000107#ifndef CLOCKS_PER_SEC
108/* C'mon, fix the bloody headers... - [cjh] */
109#define CLOCKS_PER_SEC 1000
110#endif
Guido van Rossumbcc20741998-08-04 22:53:56 +0000111#endif
112
Guido van Rossum234f9421993-06-17 12:35:49 +0000113/* Forward declarations */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000114static int floatsleep Py_PROTO((double));
115static double floattime Py_PROTO(());
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000117/* For Y2K check */
118static PyObject *moddict;
119
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000120#ifdef macintosh
121/* Our own timezone. We have enough information to deduce whether
122** DST is on currently, but unfortunately we cannot put it to good
123** use because we don't know the rules (and that is needed to have
124** localtime() return correct tm_isdst values for times other than
125** the current time. So, we cop out and only tell the user the current
126** timezone.
127*/
128static long timezone;
129
130static void
131initmactimezone()
132{
133 MachineLocation loc;
134 long delta;
135
136 ReadLocation(&loc);
137
138 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
139 return;
140
141 delta = loc.u.gmtDelta & 0x00FFFFFF;
142
143 if (delta & 0x00800000)
144 delta |= 0xFF000000;
145
146 timezone = -delta;
147}
148#endif /* macintosh */
149
150
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000151static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152time_time(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000153 PyObject *self;
154 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000156 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000157 if (!PyArg_NoArgs(args))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000158 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000159 secs = floattime();
160 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000161 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000162 return NULL;
163 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000164 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000165}
166
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000167static char time_doc[] =
168"time() -> floating point number\n\
169\n\
170Return the current time in seconds since the Epoch.\n\
171Fractions of a second may be present if the system clock provides them.";
172
Guido van Rossumb6775db1994-08-01 11:34:53 +0000173#ifdef HAVE_CLOCK
174
175#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000176#ifdef CLK_TCK
177#define CLOCKS_PER_SEC CLK_TCK
178#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000179#define CLOCKS_PER_SEC 1000000
180#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000181#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000182
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000183static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000184time_clock(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000185 PyObject *self;
186 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000187{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000188 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000190 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000192#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000194#if defined(MS_WIN32) && !defined(MS_WIN64)
Guido van Rossum3917c221997-04-02 05:35:28 +0000195/* Due to Mark Hammond */
196static PyObject *
197time_clock(self, args)
198 PyObject *self;
199 PyObject *args;
200{
201 static LARGE_INTEGER ctrStart;
202 static LARGE_INTEGER divisor = {0,0};
203 LARGE_INTEGER now, diff, rem;
204
205 if (!PyArg_NoArgs(args))
206 return NULL;
207
208 if (LargeIntegerEqualToZero(divisor)) {
209 QueryPerformanceCounter(&ctrStart);
210 if (!QueryPerformanceFrequency(&divisor) ||
211 LargeIntegerEqualToZero(divisor)) {
212 /* Unlikely to happen -
213 this works on all intel machines at least!
214 Revert to clock() */
215 return PyFloat_FromDouble(clock());
216 }
217 }
218 QueryPerformanceCounter(&now);
219 diff = LargeIntegerSubtract(now, ctrStart);
220 diff = LargeIntegerDivide(diff, divisor, &rem);
221 /* XXX - we assume both divide results fit in 32 bits. This is
222 true on Intels. First person who can afford a machine that
223 doesnt deserves to fix it :-)
224 */
225 return PyFloat_FromDouble((double)diff.LowPart +
226 ((double)rem.LowPart / (double)divisor.LowPart));
227}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000228
Guido van Rossum3917c221997-04-02 05:35:28 +0000229#define HAVE_CLOCK /* So it gets included in the methods */
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000230#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000231
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000232#ifdef HAVE_CLOCK
233static char clock_doc[] =
234"clock() -> floating point number\n\
235\n\
236Return the CPU time or real time since the start of the process or since\n\
237the first call to clock(). This has as much precision as the system records.";
238#endif
239
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000240static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241time_sleep(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000242 PyObject *self;
243 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000245 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000246 if (!PyArg_Parse(args, "d", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000248 if (floatsleep(secs) != 0)
249 return NULL;
250 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000251 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252}
253
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000254static char sleep_doc[] =
255"sleep(seconds)\n\
256\n\
257Delay execution for a given number of seconds. The argument may be\n\
258a floating point number for subsecond precision.";
259
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000260static PyObject *
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000261tmtotuple(p)
262 struct tm *p;
263{
264 return Py_BuildValue("(iiiiiiiii)",
265 p->tm_year + 1900,
266 p->tm_mon + 1, /* Want January == 1 */
267 p->tm_mday,
268 p->tm_hour,
269 p->tm_min,
270 p->tm_sec,
271 (p->tm_wday + 6) % 7, /* Want Monday == 0 */
272 p->tm_yday + 1, /* Want January, 1 == 1 */
273 p->tm_isdst);
274}
275
276static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000277time_convert(when, function)
278 time_t when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000279 struct tm * (*function) Py_PROTO((const time_t *));
Guido van Rossum234f9421993-06-17 12:35:49 +0000280{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000281 struct tm *p;
282 errno = 0;
Guido van Rossumc410e922000-04-26 20:40:13 +0000283#if defined(macintosh) && defined(USE_GUSI2)
284 when = when + GUSI_TO_MSL_EPOCH;
285#endif
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000286 p = function(&when);
287 if (p == NULL) {
288#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000289 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000290 errno = EINVAL;
291#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000292 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000293 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000294 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000295}
296
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000297static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000298time_gmtime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000299 PyObject *self;
300 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000301{
302 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000303 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000304 return NULL;
305 return time_convert((time_t)when, gmtime);
306}
307
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000308static char gmtime_doc[] =
309"gmtime(seconds) -> tuple\n\
310\n\
311Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT).";
312
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000313static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000314time_localtime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000315 PyObject *self;
316 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000317{
318 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000319 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000320 return NULL;
321 return time_convert((time_t)when, localtime);
322}
323
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000324static char localtime_doc[] =
325"localtime(seconds) -> tuple\n\
326Convert seconds since the Epoch to a time tuple expressing local time.";
327
Guido van Rossum9e90a671993-06-24 11:10:19 +0000328static int
329gettmarg(args, p)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000330 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000331 struct tm *p;
332{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000333 int y;
334 memset((ANY *) p, '\0', sizeof(struct tm));
335
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000336 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000337 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000338 &p->tm_mon,
339 &p->tm_mday,
340 &p->tm_hour,
341 &p->tm_min,
342 &p->tm_sec,
343 &p->tm_wday,
344 &p->tm_yday,
345 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000346 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000347 if (y < 1900) {
348 PyObject *accept = PyDict_GetItemString(moddict,
349 "accept2dyear");
350 if (accept == NULL || !PyInt_Check(accept) ||
351 PyInt_AsLong(accept) == 0) {
352 PyErr_SetString(PyExc_ValueError,
353 "year >= 1900 required");
354 return 0;
355 }
356 if (69 <= y && y <= 99)
357 y += 1900;
358 else if (0 <= y && y <= 68)
359 y += 2000;
360 else {
361 PyErr_SetString(PyExc_ValueError,
362 "year out of range (00-99, 1900-*)");
363 return 0;
364 }
365 }
366 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000367 p->tm_mon--;
368 p->tm_wday = (p->tm_wday + 1) % 7;
369 p->tm_yday--;
370 return 1;
371}
372
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000373#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000374static PyObject *
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000375time_strftime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000376 PyObject *self;
377 PyObject *args;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000378{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000379 PyObject *tup;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000380 struct tm buf;
381 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000382 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000383 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000384 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000385
Guido van Rossum1f41f841998-04-27 19:04:26 +0000386 memset((ANY *) &buf, '\0', sizeof(buf));
387
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000388 if (!PyArg_ParseTuple(args, "sO:strftime", &fmt, &tup)
389 || !gettmarg(tup, &buf))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000390 return NULL;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000391 fmtlen = strlen(fmt);
392
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000393 /* I hate these functions that presume you know how big the output
394 * will be ahead of time...
395 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000396 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000397 outbuf = malloc(i);
398 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000399 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000400 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000401 buflen = strftime(outbuf, i, fmt, &buf);
402 if (buflen > 0 || i >= 256 * fmtlen) {
403 /* If the buffer is 256 times as long as the format,
404 it's probably not failing for lack of room!
405 More likely, the format yields an empty result,
406 e.g. an empty format, or %Z when the timezone
407 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000408 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000409 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000410 free(outbuf);
411 return ret;
412 }
413 free(outbuf);
414 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000415}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000416
417static char strftime_doc[] =
418"strftime(format, tuple) -> string\n\
419\n\
420Convert a time tuple to a string according to a format specification.\n\
421See the library reference manual for formatting codes.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000422#endif /* HAVE_STRFTIME */
423
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000424#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000425
426#if 0
427extern char *strptime(); /* Enable this if it's not declared in <time.h> */
428#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000429
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000430static PyObject *
431time_strptime(self, args)
432 PyObject *self;
433 PyObject *args;
434{
435 struct tm tm;
436 char *fmt = "%a %b %d %H:%M:%S %Y";
437 char *buf;
438 char *s;
439
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000440 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
441 return NULL;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000442 memset((ANY *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000443 s = strptime(buf, fmt, &tm);
444 if (s == NULL) {
445 PyErr_SetString(PyExc_ValueError, "format mismatch");
446 return NULL;
447 }
448 while (*s && isspace(*s))
449 s++;
450 if (*s) {
451 PyErr_Format(PyExc_ValueError,
452 "unconverted data remains: '%.400s'", s);
453 return NULL;
454 }
455 return tmtotuple(&tm);
456}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000457
458static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000459"strptime(string, format) -> tuple\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000460Parse a string to a time tuple according to a format specification.\n\
461See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000462#endif /* HAVE_STRPTIME */
463
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000464static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000465time_asctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000466 PyObject *self;
467 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000468{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000469 PyObject *tup;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000470 struct tm buf;
471 char *p;
Guido van Rossum43713e52000-02-29 13:59:29 +0000472 if (!PyArg_ParseTuple(args, "O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000473 return NULL;
474 if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000475 return NULL;
476 p = asctime(&buf);
477 if (p[24] == '\n')
478 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000479 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000480}
481
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000482static char asctime_doc[] =
483"asctime(tuple) -> string\n\
484\n\
485Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.";
486
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000487static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000488time_ctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000489 PyObject *self;
490 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000491{
492 double dt;
493 time_t tt;
494 char *p;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000495 if (!PyArg_Parse(args, "d", &dt))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000496 return NULL;
Guido van Rossumcac6c721996-09-06 13:34:02 +0000497 tt = (time_t)dt;
Guido van Rossumc410e922000-04-26 20:40:13 +0000498#if defined(macintosh) && defined(USE_GUSI2)
499 tt = tt + GUSI_TO_MSL_EPOCH;
500#endif
Guido van Rossum9e90a671993-06-24 11:10:19 +0000501 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000502 if (p == NULL) {
503 PyErr_SetString(PyExc_ValueError, "unconvertible time");
504 return NULL;
505 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000506 if (p[24] == '\n')
507 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000508 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000509}
510
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000511static char ctime_doc[] =
512"ctime(seconds) -> string\n\
513\n\
514Convert a time in seconds since the Epoch to a string in local time.\n\
515This is equivalent to asctime(localtime(seconds)).";
516
Guido van Rossum60cd8131998-03-06 17:16:21 +0000517#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000518static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000519time_mktime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000520 PyObject *self;
521 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000522{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000523 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000524 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000525 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000526 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000527 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000528 tt = time(&tt);
529 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000530 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000531 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000532 tt = mktime(&buf);
533 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000534 PyErr_SetString(PyExc_OverflowError,
535 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000536 return NULL;
537 }
Guido van Rossumc410e922000-04-26 20:40:13 +0000538#if defined(macintosh) && defined(USE_GUSI2)
539 tt = tt - GUSI_TO_MSL_EPOCH;
540#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000541 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000542}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000543
544static char mktime_doc[] =
545"mktime(tuple) -> floating point number\n\
546\n\
547Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000548#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000549
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000550static PyMethodDef time_methods[] = {
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000551 {"time", time_time, 0, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000552#ifdef HAVE_CLOCK
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000553 {"clock", time_clock, 0, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000554#endif
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000555 {"sleep", time_sleep, 0, sleep_doc},
556 {"gmtime", time_gmtime, 0, gmtime_doc},
557 {"localtime", time_localtime, 0, localtime_doc},
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000558 {"asctime", time_asctime, 1, asctime_doc},
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000559 {"ctime", time_ctime, 0, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000560#ifdef HAVE_MKTIME
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000561 {"mktime", time_mktime, 1, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000562#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000563#ifdef HAVE_STRFTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000564 {"strftime", time_strftime, 1, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000565#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000566#ifdef HAVE_STRPTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000567 {"strptime", time_strptime, 1, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000568#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569 {NULL, NULL} /* sentinel */
570};
571
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000572static void
573ins(d, name, v)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000574 PyObject *d;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000575 char *name;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000576 PyObject *v;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000577{
578 if (v == NULL)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000579 Py_FatalError("Can't initialize time module -- NULL value");
580 if (PyDict_SetItemString(d, name, v) != 0)
581 Py_FatalError(
Guido van Rossum52174571996-12-09 18:38:52 +0000582 "Can't initialize time module -- PyDict_SetItemString failed");
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000583 Py_DECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000584}
585
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000586static char module_doc[] =
587"This module provides various functions to manipulate time values.\n\
588\n\
589There are two standard representations of time. One is the number\n\
590of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
591or a floating point number (to represent fractions of seconds).\n\
592The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
593The actual value can be retrieved by calling gmtime(0).\n\
594\n\
595The other representation is a tuple of 9 integers giving local time.\n\
596The tuple items are:\n\
597 year (four digits, e.g. 1998)\n\
598 month (1-12)\n\
599 day (1-31)\n\
600 hours (0-23)\n\
601 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000602 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000603 weekday (0-6, Monday is 0)\n\
604 Julian day (day in the year, 1-366)\n\
605 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
606If the DST flag is 0, the time is given in the regular time zone;\n\
607if it is 1, the time is given in the DST time zone;\n\
608if it is -1, mktime() should guess based on the date and time.\n\
609\n\
610Variables:\n\
611\n\
612timezone -- difference in seconds between UTC and local standard time\n\
613altzone -- difference in seconds between UTC and local DST time\n\
614daylight -- whether local time should reflect DST\n\
615tzname -- tuple of (standard time zone name, DST time zone name)\n\
616\n\
617Functions:\n\
618\n\
619time() -- return current time in seconds since the Epoch as a float\n\
620clock() -- return CPU time since process start as a float\n\
621sleep() -- delay for a number of seconds given as a float\n\
622gmtime() -- convert seconds since Epoch to UTC tuple\n\
623localtime() -- convert seconds since Epoch to local time tuple\n\
624asctime() -- convert time tuple to string\n\
625ctime() -- convert time in seconds to string\n\
626mktime() -- convert local time tuple to seconds since Epoch\n\
627strftime() -- convert time tuple to string according to format specification\n\
628strptime() -- parse string to time tuple according to format specification\n\
629";
630
631
Guido van Rossum3886bb61998-12-04 18:50:17 +0000632DL_EXPORT(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000633inittime()
634{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000635 PyObject *m, *d;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000636 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000637 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000638 d = PyModule_GetDict(m);
Guido van Rossumc2068731998-10-07 16:35:25 +0000639 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
640 p = getenv("PYTHONY2K");
641 ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
642 /* Squirrel away the module's dictionary for the y2k check */
643 Py_INCREF(d);
644 moddict = d;
Guido van Rossumea424e11999-04-23 20:59:05 +0000645#if defined(HAVE_TZNAME) && !defined(__GLIBC__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000646 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000647#ifdef PYOS_OS2
648 ins(d, "timezone", PyInt_FromLong((long)_timezone));
649#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000650 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossum26452411998-09-28 22:07:11 +0000651#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000652#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000653 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000654#else
Guido van Rossum26452411998-09-28 22:07:11 +0000655#ifdef PYOS_OS2
656 ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
657#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000658 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossum26452411998-09-28 22:07:11 +0000659#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000660#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000661 ins(d, "daylight", PyInt_FromLong((long)daylight));
662 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossumea424e11999-04-23 20:59:05 +0000663#else /* !HAVE_TZNAME || __GLIBC__ */
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000664#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000665 {
666#define YEAR ((time_t)((365 * 24 + 6) * 3600))
667 time_t t;
668 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000669 long janzone, julyzone;
670 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000671 t = (time((time_t *)0) / YEAR) * YEAR;
672 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000673 janzone = -p->tm_gmtoff;
674 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
675 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000676 t += YEAR/2;
677 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000678 julyzone = -p->tm_gmtoff;
679 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
680 julyname[9] = '\0';
681
682 if( janzone < julyzone ) {
683 /* DST is reversed in the southern hemisphere */
684 ins(d, "timezone", PyInt_FromLong(julyzone));
685 ins(d, "altzone", PyInt_FromLong(janzone));
686 ins(d, "daylight",
687 PyInt_FromLong((long)(janzone != julyzone)));
688 ins(d, "tzname",
689 Py_BuildValue("(zz)", julyname, janname));
690 } else {
691 ins(d, "timezone", PyInt_FromLong(janzone));
692 ins(d, "altzone", PyInt_FromLong(julyzone));
693 ins(d, "daylight",
694 PyInt_FromLong((long)(janzone != julyzone)));
695 ins(d, "tzname",
696 Py_BuildValue("(zz)", janname, julyname));
697 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000698 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000699#else
700#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000701 /* The only thing we can obtain is the current timezone
702 ** (and whether dst is currently _active_, but that is not what
703 ** we're looking for:-( )
704 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000705 initmactimezone();
706 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000707 ins(d, "altzone", PyInt_FromLong(timezone));
708 ins(d, "daylight", PyInt_FromLong((long)0));
709 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000710#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000711#endif /* HAVE_TM_ZONE */
Guido van Rossumea424e11999-04-23 20:59:05 +0000712#endif /* !HAVE_TZNAME || __GLIBC__ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000713 if (PyErr_Occurred())
714 Py_FatalError("Can't initialize time module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715}
716
717
Guido van Rossumb6775db1994-08-01 11:34:53 +0000718/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719
Guido van Rossumb6775db1994-08-01 11:34:53 +0000720static double
721floattime()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000722{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000723 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000724 (1) gettimeofday() -- resolution in microseconds
725 (2) ftime() -- resolution in milliseconds
726 (3) time() -- resolution in seconds
727 In all cases the return value is a float in seconds.
728 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
729 fail, so we fall back on ftime() or time().
730 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000731#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000732 {
733 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000734#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000735 if (gettimeofday(&t) == 0)
736 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000737#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000738 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
739 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000740#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000741 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000742#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000743 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000744#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000745 struct timeb t;
746 ftime(&t);
747 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000748#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000749 time_t secs;
750 time(&secs);
751 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000752#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000753 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000754}
755
Guido van Rossumb6775db1994-08-01 11:34:53 +0000756
757/* Implement floatsleep() for various platforms.
758 When interrupted (or when another error occurs), return -1 and
759 set an exception; else return 0. */
760
761static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000762#ifdef MPW
763floatsleep(double secs)
764#else
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000765 floatsleep(secs)
Guido van Rossum775f4da1993-01-09 17:18:52 +0000766 double secs;
Guido van Rossuma320fd31995-03-09 12:14:15 +0000767#endif /* MPW */
Guido van Rossum426035c1991-02-19 12:27:35 +0000768{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000769/* XXX Should test for MS_WIN32 first! */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000770#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000771 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000772 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000773 frac = fmod(secs, 1.0);
774 secs = floor(secs);
775 t.tv_sec = (long)secs;
776 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000777 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000778 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000779#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000780 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000781#else
782 if (1) {
783#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000784 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000785 PyErr_SetFromErrno(PyExc_IOError);
786 return -1;
787 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000788 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000789 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000790#else /* !HAVE_SELECT || __BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000791#ifdef macintosh
792#define MacTicks (* (long *)0x16A)
793 long deadline;
794 deadline = MacTicks + (long)(secs * 60.0);
795 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000796 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000797 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000798 return -1;
799 }
800#else /* !macintosh */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000801#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000802 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000803 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000804 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000805 Py_END_ALLOW_THREADS
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000806#else /* !__WATCOMC__ || __QNX__ */
Guido van Rossume22e6441993-07-09 10:51:31 +0000807#ifdef MSDOS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000808 struct timeb t1, t2;
809 double frac;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000810 extern double fmod Py_PROTO((double, double));
811 extern double floor Py_PROTO((double));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000812 if (secs <= 0.0)
813 return;
814 frac = fmod(secs, 1.0);
815 secs = floor(secs);
816 ftime(&t1);
817 t2.time = t1.time + (int)secs;
818 t2.millitm = t1.millitm + (int)(frac*1000.0);
819 while (t2.millitm >= 1000) {
820 t2.time++;
821 t2.millitm -= 1000;
822 }
823 for (;;) {
824#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000825 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000826 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000827 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000828#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000829 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000830 return -1;
831 ftime(&t1);
832 if (t1.time > t2.time ||
833 t1.time == t2.time && t1.millitm >= t2.millitm)
834 break;
835 }
836#else /* !MSDOS */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000837#ifdef MS_WIN32
Fred Drake0e123952000-06-29 21:31:02 +0000838 {
839 double millisecs = secs * 1000.0;
840 if (millisecs > (double)ULONG_MAX) {
841 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
842 return -1;
843 }
844 /* XXX Can't interrupt this sleep */
845 Py_BEGIN_ALLOW_THREADS
846 Sleep((unsigned long)millisecs);
847 Py_END_ALLOW_THREADS
848 }
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000849#else /* !MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000850#ifdef PYOS_OS2
851 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000852 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000853 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000854 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000855 PyErr_SetFromErrno(PyExc_IOError);
856 return -1;
857 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000858 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000859#else /* !PYOS_OS2 */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000860#ifdef __BEOS__
861 /* This sleep *CAN BE* interrupted. */
862 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000863 if( secs <= 0.0 ) {
864 return;
865 }
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000866
Guido van Rossumbcc20741998-08-04 22:53:56 +0000867 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000868 /* BeOS snooze() is in microseconds... */
869 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000870 Py_BLOCK_THREADS
871 PyErr_SetFromErrno( PyExc_IOError );
872 return -1;
873 }
874 Py_END_ALLOW_THREADS
875 }
876#else /* !__BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000877 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000878 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000879 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000880 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000881#endif /* !__BEOS__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000882#endif /* !PYOS_OS2 */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000883#endif /* !MS_WIN32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000884#endif /* !MSDOS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000885#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000886#endif /* !macintosh */
887#endif /* !HAVE_SELECT */
888 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000889}