blob: 7d5317972a0baccb433398833726649778f88732 [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
Guido van Rossum0a6363d1999-01-03 13:00:34 +0000424/* extern char *strptime(); /* Enable this if it's not declared in <time.h> */
Guido van Rossumc2068731998-10-07 16:35:25 +0000425
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000426static PyObject *
427time_strptime(self, args)
428 PyObject *self;
429 PyObject *args;
430{
431 struct tm tm;
432 char *fmt = "%a %b %d %H:%M:%S %Y";
433 char *buf;
434 char *s;
435
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000436 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
437 return NULL;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000438 memset((ANY *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000439 s = strptime(buf, fmt, &tm);
440 if (s == NULL) {
441 PyErr_SetString(PyExc_ValueError, "format mismatch");
442 return NULL;
443 }
444 while (*s && isspace(*s))
445 s++;
446 if (*s) {
447 PyErr_Format(PyExc_ValueError,
448 "unconverted data remains: '%.400s'", s);
449 return NULL;
450 }
451 return tmtotuple(&tm);
452}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000453
454static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000455"strptime(string, format) -> tuple\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000456Parse a string to a time tuple according to a format specification.\n\
457See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000458#endif /* HAVE_STRPTIME */
459
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000460static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000461time_asctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000462 PyObject *self;
463 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000464{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000465 PyObject *tup;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000466 struct tm buf;
467 char *p;
Guido van Rossum43713e52000-02-29 13:59:29 +0000468 if (!PyArg_ParseTuple(args, "O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000469 return NULL;
470 if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000471 return NULL;
472 p = asctime(&buf);
473 if (p[24] == '\n')
474 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000475 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000476}
477
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000478static char asctime_doc[] =
479"asctime(tuple) -> string\n\
480\n\
481Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.";
482
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000483static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000484time_ctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000485 PyObject *self;
486 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000487{
488 double dt;
489 time_t tt;
490 char *p;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000491 if (!PyArg_Parse(args, "d", &dt))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000492 return NULL;
Guido van Rossumcac6c721996-09-06 13:34:02 +0000493 tt = (time_t)dt;
Guido van Rossumc410e922000-04-26 20:40:13 +0000494#if defined(macintosh) && defined(USE_GUSI2)
495 tt = tt + GUSI_TO_MSL_EPOCH;
496#endif
Guido van Rossum9e90a671993-06-24 11:10:19 +0000497 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000498 if (p == NULL) {
499 PyErr_SetString(PyExc_ValueError, "unconvertible time");
500 return NULL;
501 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000502 if (p[24] == '\n')
503 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000504 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000505}
506
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000507static char ctime_doc[] =
508"ctime(seconds) -> string\n\
509\n\
510Convert a time in seconds since the Epoch to a string in local time.\n\
511This is equivalent to asctime(localtime(seconds)).";
512
Guido van Rossum60cd8131998-03-06 17:16:21 +0000513#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000514static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000515time_mktime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000516 PyObject *self;
517 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000518{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000519 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000520 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000521 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000522 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000523 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000524 tt = time(&tt);
525 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000526 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000527 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000528 tt = mktime(&buf);
529 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000530 PyErr_SetString(PyExc_OverflowError,
531 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000532 return NULL;
533 }
Guido van Rossumc410e922000-04-26 20:40:13 +0000534#if defined(macintosh) && defined(USE_GUSI2)
535 tt = tt - GUSI_TO_MSL_EPOCH;
536#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000537 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000538}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000539
540static char mktime_doc[] =
541"mktime(tuple) -> floating point number\n\
542\n\
543Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000544#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000545
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000546static PyMethodDef time_methods[] = {
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000547 {"time", time_time, 0, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000548#ifdef HAVE_CLOCK
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000549 {"clock", time_clock, 0, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000550#endif
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000551 {"sleep", time_sleep, 0, sleep_doc},
552 {"gmtime", time_gmtime, 0, gmtime_doc},
553 {"localtime", time_localtime, 0, localtime_doc},
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000554 {"asctime", time_asctime, 1, asctime_doc},
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000555 {"ctime", time_ctime, 0, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000556#ifdef HAVE_MKTIME
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000557 {"mktime", time_mktime, 1, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000558#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000559#ifdef HAVE_STRFTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000560 {"strftime", time_strftime, 1, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000561#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000562#ifdef HAVE_STRPTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000563 {"strptime", time_strptime, 1, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000564#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565 {NULL, NULL} /* sentinel */
566};
567
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000568static void
569ins(d, name, v)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000570 PyObject *d;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000571 char *name;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000572 PyObject *v;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000573{
574 if (v == NULL)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000575 Py_FatalError("Can't initialize time module -- NULL value");
576 if (PyDict_SetItemString(d, name, v) != 0)
577 Py_FatalError(
Guido van Rossum52174571996-12-09 18:38:52 +0000578 "Can't initialize time module -- PyDict_SetItemString failed");
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000579 Py_DECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000580}
581
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000582static char module_doc[] =
583"This module provides various functions to manipulate time values.\n\
584\n\
585There are two standard representations of time. One is the number\n\
586of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
587or a floating point number (to represent fractions of seconds).\n\
588The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
589The actual value can be retrieved by calling gmtime(0).\n\
590\n\
591The other representation is a tuple of 9 integers giving local time.\n\
592The tuple items are:\n\
593 year (four digits, e.g. 1998)\n\
594 month (1-12)\n\
595 day (1-31)\n\
596 hours (0-23)\n\
597 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000598 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000599 weekday (0-6, Monday is 0)\n\
600 Julian day (day in the year, 1-366)\n\
601 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
602If the DST flag is 0, the time is given in the regular time zone;\n\
603if it is 1, the time is given in the DST time zone;\n\
604if it is -1, mktime() should guess based on the date and time.\n\
605\n\
606Variables:\n\
607\n\
608timezone -- difference in seconds between UTC and local standard time\n\
609altzone -- difference in seconds between UTC and local DST time\n\
610daylight -- whether local time should reflect DST\n\
611tzname -- tuple of (standard time zone name, DST time zone name)\n\
612\n\
613Functions:\n\
614\n\
615time() -- return current time in seconds since the Epoch as a float\n\
616clock() -- return CPU time since process start as a float\n\
617sleep() -- delay for a number of seconds given as a float\n\
618gmtime() -- convert seconds since Epoch to UTC tuple\n\
619localtime() -- convert seconds since Epoch to local time tuple\n\
620asctime() -- convert time tuple to string\n\
621ctime() -- convert time in seconds to string\n\
622mktime() -- convert local time tuple to seconds since Epoch\n\
623strftime() -- convert time tuple to string according to format specification\n\
624strptime() -- parse string to time tuple according to format specification\n\
625";
626
627
Guido van Rossum3886bb61998-12-04 18:50:17 +0000628DL_EXPORT(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000629inittime()
630{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000631 PyObject *m, *d;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000632 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000633 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000634 d = PyModule_GetDict(m);
Guido van Rossumc2068731998-10-07 16:35:25 +0000635 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
636 p = getenv("PYTHONY2K");
637 ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
638 /* Squirrel away the module's dictionary for the y2k check */
639 Py_INCREF(d);
640 moddict = d;
Guido van Rossumea424e11999-04-23 20:59:05 +0000641#if defined(HAVE_TZNAME) && !defined(__GLIBC__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000642 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000643#ifdef PYOS_OS2
644 ins(d, "timezone", PyInt_FromLong((long)_timezone));
645#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000646 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossum26452411998-09-28 22:07:11 +0000647#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000648#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000649 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000650#else
Guido van Rossum26452411998-09-28 22:07:11 +0000651#ifdef PYOS_OS2
652 ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
653#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000654 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossum26452411998-09-28 22:07:11 +0000655#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000656#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000657 ins(d, "daylight", PyInt_FromLong((long)daylight));
658 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossumea424e11999-04-23 20:59:05 +0000659#else /* !HAVE_TZNAME || __GLIBC__ */
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000660#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000661 {
662#define YEAR ((time_t)((365 * 24 + 6) * 3600))
663 time_t t;
664 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000665 long janzone, julyzone;
666 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000667 t = (time((time_t *)0) / YEAR) * YEAR;
668 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000669 janzone = -p->tm_gmtoff;
670 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
671 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000672 t += YEAR/2;
673 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000674 julyzone = -p->tm_gmtoff;
675 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
676 julyname[9] = '\0';
677
678 if( janzone < julyzone ) {
679 /* DST is reversed in the southern hemisphere */
680 ins(d, "timezone", PyInt_FromLong(julyzone));
681 ins(d, "altzone", PyInt_FromLong(janzone));
682 ins(d, "daylight",
683 PyInt_FromLong((long)(janzone != julyzone)));
684 ins(d, "tzname",
685 Py_BuildValue("(zz)", julyname, janname));
686 } else {
687 ins(d, "timezone", PyInt_FromLong(janzone));
688 ins(d, "altzone", PyInt_FromLong(julyzone));
689 ins(d, "daylight",
690 PyInt_FromLong((long)(janzone != julyzone)));
691 ins(d, "tzname",
692 Py_BuildValue("(zz)", janname, julyname));
693 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000694 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000695#else
696#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000697 /* The only thing we can obtain is the current timezone
698 ** (and whether dst is currently _active_, but that is not what
699 ** we're looking for:-( )
700 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000701 initmactimezone();
702 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000703 ins(d, "altzone", PyInt_FromLong(timezone));
704 ins(d, "daylight", PyInt_FromLong((long)0));
705 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000706#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000707#endif /* HAVE_TM_ZONE */
Guido van Rossumea424e11999-04-23 20:59:05 +0000708#endif /* !HAVE_TZNAME || __GLIBC__ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000709 if (PyErr_Occurred())
710 Py_FatalError("Can't initialize time module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000711}
712
713
Guido van Rossumb6775db1994-08-01 11:34:53 +0000714/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715
Guido van Rossumb6775db1994-08-01 11:34:53 +0000716static double
717floattime()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000718{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000719 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000720 (1) gettimeofday() -- resolution in microseconds
721 (2) ftime() -- resolution in milliseconds
722 (3) time() -- resolution in seconds
723 In all cases the return value is a float in seconds.
724 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
725 fail, so we fall back on ftime() or time().
726 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000727#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000728 {
729 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000730#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000731 if (gettimeofday(&t) == 0)
732 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000733#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000734 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
735 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000736#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000737 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000738#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000739 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000740#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000741 struct timeb t;
742 ftime(&t);
743 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000744#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000745 time_t secs;
746 time(&secs);
747 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000748#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000749 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000750}
751
Guido van Rossumb6775db1994-08-01 11:34:53 +0000752
753/* Implement floatsleep() for various platforms.
754 When interrupted (or when another error occurs), return -1 and
755 set an exception; else return 0. */
756
757static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000758#ifdef MPW
759floatsleep(double secs)
760#else
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000761 floatsleep(secs)
Guido van Rossum775f4da1993-01-09 17:18:52 +0000762 double secs;
Guido van Rossuma320fd31995-03-09 12:14:15 +0000763#endif /* MPW */
Guido van Rossum426035c1991-02-19 12:27:35 +0000764{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000765/* XXX Should test for MS_WIN32 first! */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000766#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000767 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000768 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000769 frac = fmod(secs, 1.0);
770 secs = floor(secs);
771 t.tv_sec = (long)secs;
772 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000773 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000774 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000775#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000776 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000777#else
778 if (1) {
779#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000780 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000781 PyErr_SetFromErrno(PyExc_IOError);
782 return -1;
783 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000784 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000785 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000786#else /* !HAVE_SELECT || __BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000787#ifdef macintosh
788#define MacTicks (* (long *)0x16A)
789 long deadline;
790 deadline = MacTicks + (long)(secs * 60.0);
791 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000792 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000793 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000794 return -1;
795 }
796#else /* !macintosh */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000797#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000798 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000799 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000800 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000801 Py_END_ALLOW_THREADS
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000802#else /* !__WATCOMC__ || __QNX__ */
Guido van Rossume22e6441993-07-09 10:51:31 +0000803#ifdef MSDOS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000804 struct timeb t1, t2;
805 double frac;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000806 extern double fmod Py_PROTO((double, double));
807 extern double floor Py_PROTO((double));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000808 if (secs <= 0.0)
809 return;
810 frac = fmod(secs, 1.0);
811 secs = floor(secs);
812 ftime(&t1);
813 t2.time = t1.time + (int)secs;
814 t2.millitm = t1.millitm + (int)(frac*1000.0);
815 while (t2.millitm >= 1000) {
816 t2.time++;
817 t2.millitm -= 1000;
818 }
819 for (;;) {
820#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000821 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000822 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000823 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000824#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000825 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000826 return -1;
827 ftime(&t1);
828 if (t1.time > t2.time ||
829 t1.time == t2.time && t1.millitm >= t2.millitm)
830 break;
831 }
832#else /* !MSDOS */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000833#ifdef MS_WIN32
Guido van Rossumb6775db1994-08-01 11:34:53 +0000834 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000835 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000836 Sleep((int)(secs*1000));
Guido van Rossum8607ae21997-11-03 22:04:46 +0000837 Py_END_ALLOW_THREADS
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000838#else /* !MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000839#ifdef PYOS_OS2
840 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000841 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000842 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000843 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000844 PyErr_SetFromErrno(PyExc_IOError);
845 return -1;
846 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000847 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000848#else /* !PYOS_OS2 */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000849#ifdef __BEOS__
850 /* This sleep *CAN BE* interrupted. */
851 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000852 if( secs <= 0.0 ) {
853 return;
854 }
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000855
Guido van Rossumbcc20741998-08-04 22:53:56 +0000856 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000857 /* BeOS snooze() is in microseconds... */
858 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000859 Py_BLOCK_THREADS
860 PyErr_SetFromErrno( PyExc_IOError );
861 return -1;
862 }
863 Py_END_ALLOW_THREADS
864 }
865#else /* !__BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000866 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000867 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000868 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000869 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000870#endif /* !__BEOS__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000871#endif /* !PYOS_OS2 */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000872#endif /* !MS_WIN32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000873#endif /* !MSDOS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000874#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000875#endif /* !macintosh */
876#endif /* !HAVE_SELECT */
877 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000878}