blob: faadf1e0fc3daa1eb78e8da7995086b30421c9d3 [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
Guido van Rossum3917c221997-04-02 05:35:28 +000092#ifdef MS_WIN32
93/* Win32 has better clock replacement */
94#include <largeint.h>
95#undef HAVE_CLOCK /* We have our own version down below */
96#endif /* MS_WIN32 */
97
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000098#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000099#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000100#endif
101
Guido van Rossumbcc20741998-08-04 22:53:56 +0000102#ifdef __BEOS__
103/* For bigtime_t, snooze(). - [cjh] */
104#include <support/SupportDefs.h>
105#include <kernel/OS.h>
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000106#ifndef CLOCKS_PER_SEC
107/* C'mon, fix the bloody headers... - [cjh] */
108#define CLOCKS_PER_SEC 1000
109#endif
Guido van Rossumbcc20741998-08-04 22:53:56 +0000110#endif
111
Guido van Rossum234f9421993-06-17 12:35:49 +0000112/* Forward declarations */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000113static int floatsleep Py_PROTO((double));
114static double floattime Py_PROTO(());
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000116/* For Y2K check */
117static PyObject *moddict;
118
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000119#ifdef macintosh
120/* Our own timezone. We have enough information to deduce whether
121** DST is on currently, but unfortunately we cannot put it to good
122** use because we don't know the rules (and that is needed to have
123** localtime() return correct tm_isdst values for times other than
124** the current time. So, we cop out and only tell the user the current
125** timezone.
126*/
127static long timezone;
128
129static void
130initmactimezone()
131{
132 MachineLocation loc;
133 long delta;
134
135 ReadLocation(&loc);
136
137 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
138 return;
139
140 delta = loc.u.gmtDelta & 0x00FFFFFF;
141
142 if (delta & 0x00800000)
143 delta |= 0xFF000000;
144
145 timezone = -delta;
146}
147#endif /* macintosh */
148
149
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000150static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151time_time(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000152 PyObject *self;
153 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000155 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000156 if (!PyArg_NoArgs(args))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000157 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000158 secs = floattime();
159 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000160 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000161 return NULL;
162 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000163 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000164}
165
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000166static char time_doc[] =
167"time() -> floating point number\n\
168\n\
169Return the current time in seconds since the Epoch.\n\
170Fractions of a second may be present if the system clock provides them.";
171
Guido van Rossumb6775db1994-08-01 11:34:53 +0000172#ifdef HAVE_CLOCK
173
174#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000175#ifdef CLK_TCK
176#define CLOCKS_PER_SEC CLK_TCK
177#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000178#define CLOCKS_PER_SEC 1000000
179#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000180#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000181
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000182static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183time_clock(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000184 PyObject *self;
185 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000186{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000187 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000189 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000191#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192
Guido van Rossum3917c221997-04-02 05:35:28 +0000193#ifdef MS_WIN32
194/* Due to Mark Hammond */
195static PyObject *
196time_clock(self, args)
197 PyObject *self;
198 PyObject *args;
199{
200 static LARGE_INTEGER ctrStart;
201 static LARGE_INTEGER divisor = {0,0};
202 LARGE_INTEGER now, diff, rem;
203
204 if (!PyArg_NoArgs(args))
205 return NULL;
206
207 if (LargeIntegerEqualToZero(divisor)) {
208 QueryPerformanceCounter(&ctrStart);
209 if (!QueryPerformanceFrequency(&divisor) ||
210 LargeIntegerEqualToZero(divisor)) {
211 /* Unlikely to happen -
212 this works on all intel machines at least!
213 Revert to clock() */
214 return PyFloat_FromDouble(clock());
215 }
216 }
217 QueryPerformanceCounter(&now);
218 diff = LargeIntegerSubtract(now, ctrStart);
219 diff = LargeIntegerDivide(diff, divisor, &rem);
220 /* XXX - we assume both divide results fit in 32 bits. This is
221 true on Intels. First person who can afford a machine that
222 doesnt deserves to fix it :-)
223 */
224 return PyFloat_FromDouble((double)diff.LowPart +
225 ((double)rem.LowPart / (double)divisor.LowPart));
226}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000227
Guido van Rossum3917c221997-04-02 05:35:28 +0000228#define HAVE_CLOCK /* So it gets included in the methods */
229#endif /* MS_WIN32 */
230
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000231#ifdef HAVE_CLOCK
232static char clock_doc[] =
233"clock() -> floating point number\n\
234\n\
235Return the CPU time or real time since the start of the process or since\n\
236the first call to clock(). This has as much precision as the system records.";
237#endif
238
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000239static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240time_sleep(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000241 PyObject *self;
242 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000244 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000245 if (!PyArg_Parse(args, "d", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000247 if (floatsleep(secs) != 0)
248 return NULL;
249 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000250 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251}
252
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000253static char sleep_doc[] =
254"sleep(seconds)\n\
255\n\
256Delay execution for a given number of seconds. The argument may be\n\
257a floating point number for subsecond precision.";
258
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000259static PyObject *
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000260tmtotuple(p)
261 struct tm *p;
262{
263 return Py_BuildValue("(iiiiiiiii)",
264 p->tm_year + 1900,
265 p->tm_mon + 1, /* Want January == 1 */
266 p->tm_mday,
267 p->tm_hour,
268 p->tm_min,
269 p->tm_sec,
270 (p->tm_wday + 6) % 7, /* Want Monday == 0 */
271 p->tm_yday + 1, /* Want January, 1 == 1 */
272 p->tm_isdst);
273}
274
275static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000276time_convert(when, function)
277 time_t when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000278 struct tm * (*function) Py_PROTO((const time_t *));
Guido van Rossum234f9421993-06-17 12:35:49 +0000279{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000280 struct tm *p;
281 errno = 0;
Guido van Rossumc410e922000-04-26 20:40:13 +0000282#if defined(macintosh) && defined(USE_GUSI2)
283 when = when + GUSI_TO_MSL_EPOCH;
284#endif
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000285 p = function(&when);
286 if (p == NULL) {
287#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000288 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000289 errno = EINVAL;
290#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000291 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000292 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000293 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000294}
295
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000296static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000297time_gmtime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000298 PyObject *self;
299 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000300{
301 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000302 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000303 return NULL;
304 return time_convert((time_t)when, gmtime);
305}
306
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000307static char gmtime_doc[] =
308"gmtime(seconds) -> tuple\n\
309\n\
310Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT).";
311
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000312static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000313time_localtime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000314 PyObject *self;
315 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000316{
317 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000318 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000319 return NULL;
320 return time_convert((time_t)when, localtime);
321}
322
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000323static char localtime_doc[] =
324"localtime(seconds) -> tuple\n\
325Convert seconds since the Epoch to a time tuple expressing local time.";
326
Guido van Rossum9e90a671993-06-24 11:10:19 +0000327static int
328gettmarg(args, p)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000329 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000330 struct tm *p;
331{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000332 int y;
333 memset((ANY *) p, '\0', sizeof(struct tm));
334
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000335 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000336 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000337 &p->tm_mon,
338 &p->tm_mday,
339 &p->tm_hour,
340 &p->tm_min,
341 &p->tm_sec,
342 &p->tm_wday,
343 &p->tm_yday,
344 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000345 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000346 if (y < 1900) {
347 PyObject *accept = PyDict_GetItemString(moddict,
348 "accept2dyear");
349 if (accept == NULL || !PyInt_Check(accept) ||
350 PyInt_AsLong(accept) == 0) {
351 PyErr_SetString(PyExc_ValueError,
352 "year >= 1900 required");
353 return 0;
354 }
355 if (69 <= y && y <= 99)
356 y += 1900;
357 else if (0 <= y && y <= 68)
358 y += 2000;
359 else {
360 PyErr_SetString(PyExc_ValueError,
361 "year out of range (00-99, 1900-*)");
362 return 0;
363 }
364 }
365 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000366 p->tm_mon--;
367 p->tm_wday = (p->tm_wday + 1) % 7;
368 p->tm_yday--;
369 return 1;
370}
371
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000372#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000373static PyObject *
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000374time_strftime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000375 PyObject *self;
376 PyObject *args;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000377{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000378 PyObject *tup;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000379 struct tm buf;
380 const char *fmt;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000381 int fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000382 char *outbuf = 0;
383 int i;
384
Guido van Rossum1f41f841998-04-27 19:04:26 +0000385 memset((ANY *) &buf, '\0', sizeof(buf));
386
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000387 if (!PyArg_ParseTuple(args, "sO:strftime", &fmt, &tup)
388 || !gettmarg(tup, &buf))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000389 return NULL;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000390 fmtlen = strlen(fmt);
391
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000392 /* I hate these functions that presume you know how big the output
393 * will be ahead of time...
394 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000395 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000396 outbuf = malloc(i);
397 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000398 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000399 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000400 buflen = strftime(outbuf, i, fmt, &buf);
401 if (buflen > 0 || i >= 256 * fmtlen) {
402 /* If the buffer is 256 times as long as the format,
403 it's probably not failing for lack of room!
404 More likely, the format yields an empty result,
405 e.g. an empty format, or %Z when the timezone
406 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000407 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000408 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000409 free(outbuf);
410 return ret;
411 }
412 free(outbuf);
413 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000414}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000415
416static char strftime_doc[] =
417"strftime(format, tuple) -> string\n\
418\n\
419Convert a time tuple to a string according to a format specification.\n\
420See the library reference manual for formatting codes.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000421#endif /* HAVE_STRFTIME */
422
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000423#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000424
425#if 0
426extern char *strptime(); /* Enable this if it's not declared in <time.h> */
427#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000428
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000429static PyObject *
430time_strptime(self, args)
431 PyObject *self;
432 PyObject *args;
433{
434 struct tm tm;
435 char *fmt = "%a %b %d %H:%M:%S %Y";
436 char *buf;
437 char *s;
438
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000439 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
440 return NULL;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000441 memset((ANY *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000442 s = strptime(buf, fmt, &tm);
443 if (s == NULL) {
444 PyErr_SetString(PyExc_ValueError, "format mismatch");
445 return NULL;
446 }
447 while (*s && isspace(*s))
448 s++;
449 if (*s) {
450 PyErr_Format(PyExc_ValueError,
451 "unconverted data remains: '%.400s'", s);
452 return NULL;
453 }
454 return tmtotuple(&tm);
455}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000456
457static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000458"strptime(string, format) -> tuple\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000459Parse a string to a time tuple according to a format specification.\n\
460See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000461#endif /* HAVE_STRPTIME */
462
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000463static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000464time_asctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000465 PyObject *self;
466 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000467{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000468 PyObject *tup;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000469 struct tm buf;
470 char *p;
Guido van Rossum43713e52000-02-29 13:59:29 +0000471 if (!PyArg_ParseTuple(args, "O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000472 return NULL;
473 if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000474 return NULL;
475 p = asctime(&buf);
476 if (p[24] == '\n')
477 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000478 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000479}
480
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000481static char asctime_doc[] =
482"asctime(tuple) -> string\n\
483\n\
484Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.";
485
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000486static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000487time_ctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000488 PyObject *self;
489 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000490{
491 double dt;
492 time_t tt;
493 char *p;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000494 if (!PyArg_Parse(args, "d", &dt))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000495 return NULL;
Guido van Rossumcac6c721996-09-06 13:34:02 +0000496 tt = (time_t)dt;
Guido van Rossumc410e922000-04-26 20:40:13 +0000497#if defined(macintosh) && defined(USE_GUSI2)
498 tt = tt + GUSI_TO_MSL_EPOCH;
499#endif
Guido van Rossum9e90a671993-06-24 11:10:19 +0000500 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000501 if (p == NULL) {
502 PyErr_SetString(PyExc_ValueError, "unconvertible time");
503 return NULL;
504 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000505 if (p[24] == '\n')
506 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000507 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000508}
509
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000510static char ctime_doc[] =
511"ctime(seconds) -> string\n\
512\n\
513Convert a time in seconds since the Epoch to a string in local time.\n\
514This is equivalent to asctime(localtime(seconds)).";
515
Guido van Rossum60cd8131998-03-06 17:16:21 +0000516#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000517static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000518time_mktime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000519 PyObject *self;
520 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000521{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000522 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000523 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000524 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000525 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000526 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000527 tt = time(&tt);
528 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000529 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000530 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000531 tt = mktime(&buf);
532 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000533 PyErr_SetString(PyExc_OverflowError,
534 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000535 return NULL;
536 }
Guido van Rossumc410e922000-04-26 20:40:13 +0000537#if defined(macintosh) && defined(USE_GUSI2)
538 tt = tt - GUSI_TO_MSL_EPOCH;
539#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000540 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000541}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000542
543static char mktime_doc[] =
544"mktime(tuple) -> floating point number\n\
545\n\
546Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000547#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000548
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000549static PyMethodDef time_methods[] = {
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000550 {"time", time_time, 0, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000551#ifdef HAVE_CLOCK
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000552 {"clock", time_clock, 0, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000553#endif
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000554 {"sleep", time_sleep, 0, sleep_doc},
555 {"gmtime", time_gmtime, 0, gmtime_doc},
556 {"localtime", time_localtime, 0, localtime_doc},
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000557 {"asctime", time_asctime, 1, asctime_doc},
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000558 {"ctime", time_ctime, 0, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000559#ifdef HAVE_MKTIME
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000560 {"mktime", time_mktime, 1, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000561#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000562#ifdef HAVE_STRFTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000563 {"strftime", time_strftime, 1, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000564#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000565#ifdef HAVE_STRPTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000566 {"strptime", time_strptime, 1, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000567#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568 {NULL, NULL} /* sentinel */
569};
570
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000571static void
572ins(d, name, v)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000573 PyObject *d;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000574 char *name;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000575 PyObject *v;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000576{
577 if (v == NULL)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000578 Py_FatalError("Can't initialize time module -- NULL value");
579 if (PyDict_SetItemString(d, name, v) != 0)
580 Py_FatalError(
Guido van Rossum52174571996-12-09 18:38:52 +0000581 "Can't initialize time module -- PyDict_SetItemString failed");
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000582 Py_DECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000583}
584
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000585static char module_doc[] =
586"This module provides various functions to manipulate time values.\n\
587\n\
588There are two standard representations of time. One is the number\n\
589of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
590or a floating point number (to represent fractions of seconds).\n\
591The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
592The actual value can be retrieved by calling gmtime(0).\n\
593\n\
594The other representation is a tuple of 9 integers giving local time.\n\
595The tuple items are:\n\
596 year (four digits, e.g. 1998)\n\
597 month (1-12)\n\
598 day (1-31)\n\
599 hours (0-23)\n\
600 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000601 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000602 weekday (0-6, Monday is 0)\n\
603 Julian day (day in the year, 1-366)\n\
604 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
605If the DST flag is 0, the time is given in the regular time zone;\n\
606if it is 1, the time is given in the DST time zone;\n\
607if it is -1, mktime() should guess based on the date and time.\n\
608\n\
609Variables:\n\
610\n\
611timezone -- difference in seconds between UTC and local standard time\n\
612altzone -- difference in seconds between UTC and local DST time\n\
613daylight -- whether local time should reflect DST\n\
614tzname -- tuple of (standard time zone name, DST time zone name)\n\
615\n\
616Functions:\n\
617\n\
618time() -- return current time in seconds since the Epoch as a float\n\
619clock() -- return CPU time since process start as a float\n\
620sleep() -- delay for a number of seconds given as a float\n\
621gmtime() -- convert seconds since Epoch to UTC tuple\n\
622localtime() -- convert seconds since Epoch to local time tuple\n\
623asctime() -- convert time tuple to string\n\
624ctime() -- convert time in seconds to string\n\
625mktime() -- convert local time tuple to seconds since Epoch\n\
626strftime() -- convert time tuple to string according to format specification\n\
627strptime() -- parse string to time tuple according to format specification\n\
628";
629
630
Guido van Rossum3886bb61998-12-04 18:50:17 +0000631DL_EXPORT(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000632inittime()
633{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000634 PyObject *m, *d;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000635 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000636 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000637 d = PyModule_GetDict(m);
Guido van Rossumc2068731998-10-07 16:35:25 +0000638 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
639 p = getenv("PYTHONY2K");
640 ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
641 /* Squirrel away the module's dictionary for the y2k check */
642 Py_INCREF(d);
643 moddict = d;
Guido van Rossumea424e11999-04-23 20:59:05 +0000644#if defined(HAVE_TZNAME) && !defined(__GLIBC__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000645 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000646#ifdef PYOS_OS2
647 ins(d, "timezone", PyInt_FromLong((long)_timezone));
648#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000649 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossum26452411998-09-28 22:07:11 +0000650#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000651#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000652 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000653#else
Guido van Rossum26452411998-09-28 22:07:11 +0000654#ifdef PYOS_OS2
655 ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
656#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000657 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossum26452411998-09-28 22:07:11 +0000658#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000659#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000660 ins(d, "daylight", PyInt_FromLong((long)daylight));
661 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossumea424e11999-04-23 20:59:05 +0000662#else /* !HAVE_TZNAME || __GLIBC__ */
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000663#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000664 {
665#define YEAR ((time_t)((365 * 24 + 6) * 3600))
666 time_t t;
667 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000668 long janzone, julyzone;
669 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000670 t = (time((time_t *)0) / YEAR) * YEAR;
671 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000672 janzone = -p->tm_gmtoff;
673 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
674 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000675 t += YEAR/2;
676 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000677 julyzone = -p->tm_gmtoff;
678 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
679 julyname[9] = '\0';
680
681 if( janzone < julyzone ) {
682 /* DST is reversed in the southern hemisphere */
683 ins(d, "timezone", PyInt_FromLong(julyzone));
684 ins(d, "altzone", PyInt_FromLong(janzone));
685 ins(d, "daylight",
686 PyInt_FromLong((long)(janzone != julyzone)));
687 ins(d, "tzname",
688 Py_BuildValue("(zz)", julyname, janname));
689 } else {
690 ins(d, "timezone", PyInt_FromLong(janzone));
691 ins(d, "altzone", PyInt_FromLong(julyzone));
692 ins(d, "daylight",
693 PyInt_FromLong((long)(janzone != julyzone)));
694 ins(d, "tzname",
695 Py_BuildValue("(zz)", janname, julyname));
696 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000697 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000698#else
699#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000700 /* The only thing we can obtain is the current timezone
701 ** (and whether dst is currently _active_, but that is not what
702 ** we're looking for:-( )
703 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000704 initmactimezone();
705 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000706 ins(d, "altzone", PyInt_FromLong(timezone));
707 ins(d, "daylight", PyInt_FromLong((long)0));
708 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000709#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000710#endif /* HAVE_TM_ZONE */
Guido van Rossumea424e11999-04-23 20:59:05 +0000711#endif /* !HAVE_TZNAME || __GLIBC__ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000712 if (PyErr_Occurred())
713 Py_FatalError("Can't initialize time module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000714}
715
716
Guido van Rossumb6775db1994-08-01 11:34:53 +0000717/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000718
Guido van Rossumb6775db1994-08-01 11:34:53 +0000719static double
720floattime()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000721{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000722 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000723 (1) gettimeofday() -- resolution in microseconds
724 (2) ftime() -- resolution in milliseconds
725 (3) time() -- resolution in seconds
726 In all cases the return value is a float in seconds.
727 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
728 fail, so we fall back on ftime() or time().
729 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000730#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000731 {
732 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000733#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000734 if (gettimeofday(&t) == 0)
735 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000736#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000737 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
738 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000739#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000740 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000741#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000742 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000743#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000744 struct timeb t;
745 ftime(&t);
746 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000747#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000748 time_t secs;
749 time(&secs);
750 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000751#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000752 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000753}
754
Guido van Rossumb6775db1994-08-01 11:34:53 +0000755
756/* Implement floatsleep() for various platforms.
757 When interrupted (or when another error occurs), return -1 and
758 set an exception; else return 0. */
759
760static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000761#ifdef MPW
762floatsleep(double secs)
763#else
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000764 floatsleep(secs)
Guido van Rossum775f4da1993-01-09 17:18:52 +0000765 double secs;
Guido van Rossuma320fd31995-03-09 12:14:15 +0000766#endif /* MPW */
Guido van Rossum426035c1991-02-19 12:27:35 +0000767{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000768/* XXX Should test for MS_WIN32 first! */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000769#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000770 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000771 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000772 frac = fmod(secs, 1.0);
773 secs = floor(secs);
774 t.tv_sec = (long)secs;
775 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000776 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000777 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000778#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000779 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000780#else
781 if (1) {
782#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000783 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000784 PyErr_SetFromErrno(PyExc_IOError);
785 return -1;
786 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000787 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000788 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000789#else /* !HAVE_SELECT || __BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000790#ifdef macintosh
791#define MacTicks (* (long *)0x16A)
792 long deadline;
793 deadline = MacTicks + (long)(secs * 60.0);
794 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000795 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000796 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000797 return -1;
798 }
799#else /* !macintosh */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000800#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000801 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000802 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000803 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000804 Py_END_ALLOW_THREADS
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000805#else /* !__WATCOMC__ || __QNX__ */
Guido van Rossume22e6441993-07-09 10:51:31 +0000806#ifdef MSDOS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000807 struct timeb t1, t2;
808 double frac;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000809 extern double fmod Py_PROTO((double, double));
810 extern double floor Py_PROTO((double));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000811 if (secs <= 0.0)
812 return;
813 frac = fmod(secs, 1.0);
814 secs = floor(secs);
815 ftime(&t1);
816 t2.time = t1.time + (int)secs;
817 t2.millitm = t1.millitm + (int)(frac*1000.0);
818 while (t2.millitm >= 1000) {
819 t2.time++;
820 t2.millitm -= 1000;
821 }
822 for (;;) {
823#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000824 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000825 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000826 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000827#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000828 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000829 return -1;
830 ftime(&t1);
831 if (t1.time > t2.time ||
832 t1.time == t2.time && t1.millitm >= t2.millitm)
833 break;
834 }
835#else /* !MSDOS */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000836#ifdef MS_WIN32
Guido van Rossumb6775db1994-08-01 11:34:53 +0000837 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000838 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000839 Sleep((int)(secs*1000));
Guido van Rossum8607ae21997-11-03 22:04:46 +0000840 Py_END_ALLOW_THREADS
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000841#else /* !MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000842#ifdef PYOS_OS2
843 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000844 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000845 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000846 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000847 PyErr_SetFromErrno(PyExc_IOError);
848 return -1;
849 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000850 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000851#else /* !PYOS_OS2 */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000852#ifdef __BEOS__
853 /* This sleep *CAN BE* interrupted. */
854 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000855 if( secs <= 0.0 ) {
856 return;
857 }
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000858
Guido van Rossumbcc20741998-08-04 22:53:56 +0000859 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000860 /* BeOS snooze() is in microseconds... */
861 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000862 Py_BLOCK_THREADS
863 PyErr_SetFromErrno( PyExc_IOError );
864 return -1;
865 }
866 Py_END_ALLOW_THREADS
867 }
868#else /* !__BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000869 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000870 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000871 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000872 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000873#endif /* !__BEOS__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000874#endif /* !PYOS_OS2 */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000875#endif /* !MS_WIN32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000876#endif /* !MSDOS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000877#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000878#endif /* !macintosh */
879#endif /* !HAVE_SELECT */
880 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000881}