blob: 005189a507dcd02692fe48adda6cfafb255c3510 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* Time module */
12
Barry Warsaw9a2a8a81996-12-06 23:32:14 +000013#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000014
Guido van Rossum87ce7bb1998-06-09 16:30:31 +000015#include <ctype.h>
16
Guido van Rossum99d90c01996-08-08 19:17:45 +000017#ifdef HAVE_SELECT
18#include "mymath.h"
19#endif
20
Guido van Rossum6d946f91992-08-14 13:49:30 +000021#ifdef macintosh
Guido van Rossumb6775db1994-08-01 11:34:53 +000022#include <time.h>
Guido van Rossumc410e922000-04-26 20:40:13 +000023#include <OSUtils.h>
24#ifdef USE_GUSI2
25/* GUSI, the I/O library which has the time() function and such uses the
26** Mac epoch of 1904. MSL, the C library which has localtime() and so uses
27** the ANSI epoch of 1900.
28*/
29#define GUSI_TO_MSL_EPOCH (4*365*24*60*60)
30#endif /* USE_GUSI2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000031#else
32#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000033#endif
34
Guido van Rossumb6775db1994-08-01 11:34:53 +000035#ifdef QUICKWIN
36#include <io.h>
37#endif
38
39#ifdef HAVE_UNISTD_H
Guido van Rossum2762f251992-03-27 17:22:13 +000040#include <unistd.h>
41#endif
42
Guido van Rossumbcc20741998-08-04 22:53:56 +000043#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossumb6775db1994-08-01 11:34:53 +000044#include "myselect.h"
45#else
46#include "mytime.h"
47#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048
Guido van Rossumb6775db1994-08-01 11:34:53 +000049#ifdef HAVE_FTIME
50#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000051#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000052extern int ftime(struct timeb *);
Guido van Rossum52174571996-12-09 18:38:52 +000053#endif /* MS_WINDOWS */
54#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055
Guido van Rossum7bf22de1997-12-02 20:34:19 +000056#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000057#include <i86.h>
58#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000059#ifdef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +000060#include <windows.h>
Guido van Rossumb2fb3641996-09-07 00:47:35 +000061#ifdef MS_WIN16
62/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000063#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000064#define tzname _tzname
65#define daylight _daylight
66#define altzone _altzone
Guido van Rossumb2fb3641996-09-07 00:47:35 +000067#endif /* MS_WIN16 */
Guido van Rossumcac6c721996-09-06 13:34:02 +000068#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000069#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000070
Fred Drakedfb4ebd2000-06-29 20:56:28 +000071#if defined(MS_WIN32) && !defined(MS_WIN64)
72/* Win32 has better clock replacement
73 XXX Win64 does not yet, but might when the platform matures. */
Guido van Rossum3917c221997-04-02 05:35:28 +000074#include <largeint.h>
75#undef HAVE_CLOCK /* We have our own version down below */
Fred Drakedfb4ebd2000-06-29 20:56:28 +000076#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +000077
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000078#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000079#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000080#endif
81
Guido van Rossumbcc20741998-08-04 22:53:56 +000082#ifdef __BEOS__
83/* For bigtime_t, snooze(). - [cjh] */
84#include <support/SupportDefs.h>
85#include <kernel/OS.h>
Guido van Rossumd3eb5771999-03-09 16:07:23 +000086#ifndef CLOCKS_PER_SEC
87/* C'mon, fix the bloody headers... - [cjh] */
88#define CLOCKS_PER_SEC 1000
89#endif
Guido van Rossumbcc20741998-08-04 22:53:56 +000090#endif
91
Guido van Rossum234f9421993-06-17 12:35:49 +000092/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000093static int floatsleep(double);
94static double floattime();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000096/* For Y2K check */
97static PyObject *moddict;
98
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000099#ifdef macintosh
100/* Our own timezone. We have enough information to deduce whether
101** DST is on currently, but unfortunately we cannot put it to good
102** use because we don't know the rules (and that is needed to have
103** localtime() return correct tm_isdst values for times other than
104** the current time. So, we cop out and only tell the user the current
105** timezone.
106*/
107static long timezone;
108
109static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000110initmactimezone(void)
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000111{
112 MachineLocation loc;
113 long delta;
114
115 ReadLocation(&loc);
116
117 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
118 return;
119
120 delta = loc.u.gmtDelta & 0x00FFFFFF;
121
122 if (delta & 0x00800000)
123 delta |= 0xFF000000;
124
125 timezone = -delta;
126}
127#endif /* macintosh */
128
129
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000130static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000131time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000133 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000134 if (!PyArg_NoArgs(args))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000135 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000136 secs = floattime();
137 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000138 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000139 return NULL;
140 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000141 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000142}
143
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000144static char time_doc[] =
145"time() -> floating point number\n\
146\n\
147Return the current time in seconds since the Epoch.\n\
148Fractions of a second may be present if the system clock provides them.";
149
Guido van Rossumb6775db1994-08-01 11:34:53 +0000150#ifdef HAVE_CLOCK
151
152#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000153#ifdef CLK_TCK
154#define CLOCKS_PER_SEC CLK_TCK
155#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000156#define CLOCKS_PER_SEC 1000000
157#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000158#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000159
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000160static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000161time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000162{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000163 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000165 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000167#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000169#if defined(MS_WIN32) && !defined(MS_WIN64)
Guido van Rossum3917c221997-04-02 05:35:28 +0000170/* Due to Mark Hammond */
171static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000172time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000173{
174 static LARGE_INTEGER ctrStart;
175 static LARGE_INTEGER divisor = {0,0};
176 LARGE_INTEGER now, diff, rem;
177
178 if (!PyArg_NoArgs(args))
179 return NULL;
180
181 if (LargeIntegerEqualToZero(divisor)) {
182 QueryPerformanceCounter(&ctrStart);
183 if (!QueryPerformanceFrequency(&divisor) ||
184 LargeIntegerEqualToZero(divisor)) {
185 /* Unlikely to happen -
186 this works on all intel machines at least!
187 Revert to clock() */
188 return PyFloat_FromDouble(clock());
189 }
190 }
191 QueryPerformanceCounter(&now);
192 diff = LargeIntegerSubtract(now, ctrStart);
193 diff = LargeIntegerDivide(diff, divisor, &rem);
194 /* XXX - we assume both divide results fit in 32 bits. This is
195 true on Intels. First person who can afford a machine that
196 doesnt deserves to fix it :-)
197 */
198 return PyFloat_FromDouble((double)diff.LowPart +
199 ((double)rem.LowPart / (double)divisor.LowPart));
200}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000201
Guido van Rossum3917c221997-04-02 05:35:28 +0000202#define HAVE_CLOCK /* So it gets included in the methods */
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000203#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000204
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000205#ifdef HAVE_CLOCK
206static char clock_doc[] =
207"clock() -> floating point number\n\
208\n\
209Return the CPU time or real time since the start of the process or since\n\
210the first call to clock(). This has as much precision as the system records.";
211#endif
212
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000213static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000214time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000215{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000216 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000217 if (!PyArg_Parse(args, "d", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000219 if (floatsleep(secs) != 0)
220 return NULL;
221 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000222 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223}
224
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000225static char sleep_doc[] =
226"sleep(seconds)\n\
227\n\
228Delay execution for a given number of seconds. The argument may be\n\
229a floating point number for subsecond precision.";
230
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000231static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000232tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000233{
234 return Py_BuildValue("(iiiiiiiii)",
235 p->tm_year + 1900,
236 p->tm_mon + 1, /* Want January == 1 */
237 p->tm_mday,
238 p->tm_hour,
239 p->tm_min,
240 p->tm_sec,
241 (p->tm_wday + 6) % 7, /* Want Monday == 0 */
242 p->tm_yday + 1, /* Want January, 1 == 1 */
243 p->tm_isdst);
244}
245
246static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000247time_convert(time_t when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000248{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000249 struct tm *p;
250 errno = 0;
Jack Jansenee398fa2000-07-03 21:37:27 +0000251#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000252 when = when + GUSI_TO_MSL_EPOCH;
253#endif
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000254 p = function(&when);
255 if (p == NULL) {
256#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000257 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000258 errno = EINVAL;
259#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000260 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000261 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000262 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000263}
264
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000265static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000266time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000267{
268 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000269 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000270 return NULL;
271 return time_convert((time_t)when, gmtime);
272}
273
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000274static char gmtime_doc[] =
275"gmtime(seconds) -> tuple\n\
276\n\
277Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT).";
278
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000279static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000280time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000281{
282 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000283 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000284 return NULL;
285 return time_convert((time_t)when, localtime);
286}
287
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000288static char localtime_doc[] =
289"localtime(seconds) -> tuple\n\
290Convert seconds since the Epoch to a time tuple expressing local time.";
291
Guido van Rossum9e90a671993-06-24 11:10:19 +0000292static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000293gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000294{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000295 int y;
296 memset((ANY *) p, '\0', sizeof(struct tm));
297
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000298 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000299 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000300 &p->tm_mon,
301 &p->tm_mday,
302 &p->tm_hour,
303 &p->tm_min,
304 &p->tm_sec,
305 &p->tm_wday,
306 &p->tm_yday,
307 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000308 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000309 if (y < 1900) {
310 PyObject *accept = PyDict_GetItemString(moddict,
311 "accept2dyear");
312 if (accept == NULL || !PyInt_Check(accept) ||
313 PyInt_AsLong(accept) == 0) {
314 PyErr_SetString(PyExc_ValueError,
315 "year >= 1900 required");
316 return 0;
317 }
318 if (69 <= y && y <= 99)
319 y += 1900;
320 else if (0 <= y && y <= 68)
321 y += 2000;
322 else {
323 PyErr_SetString(PyExc_ValueError,
324 "year out of range (00-99, 1900-*)");
325 return 0;
326 }
327 }
328 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000329 p->tm_mon--;
330 p->tm_wday = (p->tm_wday + 1) % 7;
331 p->tm_yday--;
332 return 1;
333}
334
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000335#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000336static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000337time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000338{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000339 PyObject *tup;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000340 struct tm buf;
341 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000342 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000343 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000344 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000345
Guido van Rossum1f41f841998-04-27 19:04:26 +0000346 memset((ANY *) &buf, '\0', sizeof(buf));
347
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000348 if (!PyArg_ParseTuple(args, "sO:strftime", &fmt, &tup)
349 || !gettmarg(tup, &buf))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000350 return NULL;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000351 fmtlen = strlen(fmt);
352
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000353 /* I hate these functions that presume you know how big the output
354 * will be ahead of time...
355 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000356 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000357 outbuf = malloc(i);
358 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000359 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000360 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000361 buflen = strftime(outbuf, i, fmt, &buf);
362 if (buflen > 0 || i >= 256 * fmtlen) {
363 /* If the buffer is 256 times as long as the format,
364 it's probably not failing for lack of room!
365 More likely, the format yields an empty result,
366 e.g. an empty format, or %Z when the timezone
367 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000368 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000369 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000370 free(outbuf);
371 return ret;
372 }
373 free(outbuf);
374 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000375}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000376
377static char strftime_doc[] =
378"strftime(format, tuple) -> string\n\
379\n\
380Convert a time tuple to a string according to a format specification.\n\
381See the library reference manual for formatting codes.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000382#endif /* HAVE_STRFTIME */
383
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000384#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000385
386#if 0
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000387/* Enable this if it's not declared in <time.h> */
388extern char *strptime(const char *, const char *, struct tm *);
Fred Drakeaff60182000-05-09 19:52:40 +0000389#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000390
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000391static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000392time_strptime(PyObject *self, PyObject *args)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000393{
394 struct tm tm;
395 char *fmt = "%a %b %d %H:%M:%S %Y";
396 char *buf;
397 char *s;
398
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000399 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
400 return NULL;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000401 memset((ANY *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000402 s = strptime(buf, fmt, &tm);
403 if (s == NULL) {
404 PyErr_SetString(PyExc_ValueError, "format mismatch");
405 return NULL;
406 }
407 while (*s && isspace(*s))
408 s++;
409 if (*s) {
410 PyErr_Format(PyExc_ValueError,
411 "unconverted data remains: '%.400s'", s);
412 return NULL;
413 }
414 return tmtotuple(&tm);
415}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000416
417static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000418"strptime(string, format) -> tuple\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000419Parse a string to a time tuple according to a format specification.\n\
420See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000421#endif /* HAVE_STRPTIME */
422
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000423static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000424time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000425{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000426 PyObject *tup;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000427 struct tm buf;
428 char *p;
Guido van Rossum43713e52000-02-29 13:59:29 +0000429 if (!PyArg_ParseTuple(args, "O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000430 return NULL;
431 if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000432 return NULL;
433 p = asctime(&buf);
434 if (p[24] == '\n')
435 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000436 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000437}
438
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000439static char asctime_doc[] =
440"asctime(tuple) -> string\n\
441\n\
442Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.";
443
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000444static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000445time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000446{
447 double dt;
448 time_t tt;
449 char *p;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000450 if (!PyArg_Parse(args, "d", &dt))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000451 return NULL;
Guido van Rossumcac6c721996-09-06 13:34:02 +0000452 tt = (time_t)dt;
Jack Jansenee398fa2000-07-03 21:37:27 +0000453#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000454 tt = tt + GUSI_TO_MSL_EPOCH;
455#endif
Guido van Rossum9e90a671993-06-24 11:10:19 +0000456 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000457 if (p == NULL) {
458 PyErr_SetString(PyExc_ValueError, "unconvertible time");
459 return NULL;
460 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000461 if (p[24] == '\n')
462 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000463 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000464}
465
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000466static char ctime_doc[] =
467"ctime(seconds) -> string\n\
468\n\
469Convert a time in seconds since the Epoch to a string in local time.\n\
470This is equivalent to asctime(localtime(seconds)).";
471
Guido van Rossum60cd8131998-03-06 17:16:21 +0000472#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000473static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000474time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000475{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000476 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000477 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000478 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000479 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000480 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000481 tt = time(&tt);
482 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000483 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000484 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000485 tt = mktime(&buf);
486 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000487 PyErr_SetString(PyExc_OverflowError,
488 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000489 return NULL;
490 }
Guido van Rossumc410e922000-04-26 20:40:13 +0000491#if defined(macintosh) && defined(USE_GUSI2)
492 tt = tt - GUSI_TO_MSL_EPOCH;
493#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000494 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000495}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000496
497static char mktime_doc[] =
498"mktime(tuple) -> floating point number\n\
499\n\
500Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000501#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000502
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000503static PyMethodDef time_methods[] = {
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000504 {"time", time_time, 0, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000505#ifdef HAVE_CLOCK
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000506 {"clock", time_clock, 0, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000507#endif
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000508 {"sleep", time_sleep, 0, sleep_doc},
509 {"gmtime", time_gmtime, 0, gmtime_doc},
510 {"localtime", time_localtime, 0, localtime_doc},
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000511 {"asctime", time_asctime, 1, asctime_doc},
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000512 {"ctime", time_ctime, 0, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000513#ifdef HAVE_MKTIME
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000514 {"mktime", time_mktime, 1, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000515#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000516#ifdef HAVE_STRFTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000517 {"strftime", time_strftime, 1, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000518#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000519#ifdef HAVE_STRPTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000520 {"strptime", time_strptime, 1, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000521#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000522 {NULL, NULL} /* sentinel */
523};
524
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000525static void
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000526ins(PyObject *d, char *name, PyObject *v)
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000527{
528 if (v == NULL)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000529 Py_FatalError("Can't initialize time module -- NULL value");
530 if (PyDict_SetItemString(d, name, v) != 0)
531 Py_FatalError(
Guido van Rossum52174571996-12-09 18:38:52 +0000532 "Can't initialize time module -- PyDict_SetItemString failed");
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000533 Py_DECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000534}
535
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000536static char module_doc[] =
537"This module provides various functions to manipulate time values.\n\
538\n\
539There are two standard representations of time. One is the number\n\
540of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
541or a floating point number (to represent fractions of seconds).\n\
542The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
543The actual value can be retrieved by calling gmtime(0).\n\
544\n\
545The other representation is a tuple of 9 integers giving local time.\n\
546The tuple items are:\n\
547 year (four digits, e.g. 1998)\n\
548 month (1-12)\n\
549 day (1-31)\n\
550 hours (0-23)\n\
551 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000552 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000553 weekday (0-6, Monday is 0)\n\
554 Julian day (day in the year, 1-366)\n\
555 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
556If the DST flag is 0, the time is given in the regular time zone;\n\
557if it is 1, the time is given in the DST time zone;\n\
558if it is -1, mktime() should guess based on the date and time.\n\
559\n\
560Variables:\n\
561\n\
562timezone -- difference in seconds between UTC and local standard time\n\
563altzone -- difference in seconds between UTC and local DST time\n\
564daylight -- whether local time should reflect DST\n\
565tzname -- tuple of (standard time zone name, DST time zone name)\n\
566\n\
567Functions:\n\
568\n\
569time() -- return current time in seconds since the Epoch as a float\n\
570clock() -- return CPU time since process start as a float\n\
571sleep() -- delay for a number of seconds given as a float\n\
572gmtime() -- convert seconds since Epoch to UTC tuple\n\
573localtime() -- convert seconds since Epoch to local time tuple\n\
574asctime() -- convert time tuple to string\n\
575ctime() -- convert time in seconds to string\n\
576mktime() -- convert local time tuple to seconds since Epoch\n\
577strftime() -- convert time tuple to string according to format specification\n\
578strptime() -- parse string to time tuple according to format specification\n\
579";
580
581
Guido van Rossum3886bb61998-12-04 18:50:17 +0000582DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000583inittime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000585 PyObject *m, *d;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000586 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000587 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000588 d = PyModule_GetDict(m);
Guido van Rossumc2068731998-10-07 16:35:25 +0000589 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
590 p = getenv("PYTHONY2K");
591 ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
592 /* Squirrel away the module's dictionary for the y2k check */
593 Py_INCREF(d);
594 moddict = d;
Guido van Rossumea424e11999-04-23 20:59:05 +0000595#if defined(HAVE_TZNAME) && !defined(__GLIBC__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000596 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000597#ifdef PYOS_OS2
598 ins(d, "timezone", PyInt_FromLong((long)_timezone));
599#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000600 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossum26452411998-09-28 22:07:11 +0000601#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000602#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000603 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000604#else
Guido van Rossum26452411998-09-28 22:07:11 +0000605#ifdef PYOS_OS2
606 ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
607#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000608 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossum26452411998-09-28 22:07:11 +0000609#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000610#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000611 ins(d, "daylight", PyInt_FromLong((long)daylight));
612 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossumea424e11999-04-23 20:59:05 +0000613#else /* !HAVE_TZNAME || __GLIBC__ */
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000614#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000615 {
616#define YEAR ((time_t)((365 * 24 + 6) * 3600))
617 time_t t;
618 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000619 long janzone, julyzone;
620 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000621 t = (time((time_t *)0) / YEAR) * YEAR;
622 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000623 janzone = -p->tm_gmtoff;
624 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
625 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000626 t += YEAR/2;
627 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000628 julyzone = -p->tm_gmtoff;
629 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
630 julyname[9] = '\0';
631
632 if( janzone < julyzone ) {
633 /* DST is reversed in the southern hemisphere */
634 ins(d, "timezone", PyInt_FromLong(julyzone));
635 ins(d, "altzone", PyInt_FromLong(janzone));
636 ins(d, "daylight",
637 PyInt_FromLong((long)(janzone != julyzone)));
638 ins(d, "tzname",
639 Py_BuildValue("(zz)", julyname, janname));
640 } else {
641 ins(d, "timezone", PyInt_FromLong(janzone));
642 ins(d, "altzone", PyInt_FromLong(julyzone));
643 ins(d, "daylight",
644 PyInt_FromLong((long)(janzone != julyzone)));
645 ins(d, "tzname",
646 Py_BuildValue("(zz)", janname, julyname));
647 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000648 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000649#else
650#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000651 /* The only thing we can obtain is the current timezone
652 ** (and whether dst is currently _active_, but that is not what
653 ** we're looking for:-( )
654 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000655 initmactimezone();
656 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000657 ins(d, "altzone", PyInt_FromLong(timezone));
658 ins(d, "daylight", PyInt_FromLong((long)0));
659 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000660#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000661#endif /* HAVE_TM_ZONE */
Guido van Rossumea424e11999-04-23 20:59:05 +0000662#endif /* !HAVE_TZNAME || __GLIBC__ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000663 if (PyErr_Occurred())
664 Py_FatalError("Can't initialize time module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000665}
666
667
Guido van Rossumb6775db1994-08-01 11:34:53 +0000668/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000669
Guido van Rossumb6775db1994-08-01 11:34:53 +0000670static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000671floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000672{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000673 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000674 (1) gettimeofday() -- resolution in microseconds
675 (2) ftime() -- resolution in milliseconds
676 (3) time() -- resolution in seconds
677 In all cases the return value is a float in seconds.
678 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
679 fail, so we fall back on ftime() or time().
680 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000681#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000682 {
683 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000684#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000685 if (gettimeofday(&t) == 0)
686 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000687#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000688 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
689 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000690#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000691 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000692#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000693 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000694#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000695 struct timeb t;
696 ftime(&t);
697 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000698#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000699 time_t secs;
700 time(&secs);
701 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000702#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000703 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000704}
705
Guido van Rossumb6775db1994-08-01 11:34:53 +0000706
707/* Implement floatsleep() for various platforms.
708 When interrupted (or when another error occurs), return -1 and
709 set an exception; else return 0. */
710
711static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000712#ifdef MPW
713floatsleep(double secs)
714#else
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000715 floatsleep(secs)
Guido van Rossum775f4da1993-01-09 17:18:52 +0000716 double secs;
Guido van Rossuma320fd31995-03-09 12:14:15 +0000717#endif /* MPW */
Guido van Rossum426035c1991-02-19 12:27:35 +0000718{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000719/* XXX Should test for MS_WIN32 first! */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000720#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000721 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000722 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000723 frac = fmod(secs, 1.0);
724 secs = floor(secs);
725 t.tv_sec = (long)secs;
726 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000727 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000728 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000729#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000730 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000731#else
732 if (1) {
733#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000734 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000735 PyErr_SetFromErrno(PyExc_IOError);
736 return -1;
737 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000738 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000739 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000740#else /* !HAVE_SELECT || __BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000741#ifdef macintosh
742#define MacTicks (* (long *)0x16A)
743 long deadline;
744 deadline = MacTicks + (long)(secs * 60.0);
745 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000746 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000747 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000748 return -1;
749 }
750#else /* !macintosh */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000751#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000752 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000753 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000754 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000755 Py_END_ALLOW_THREADS
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000756#else /* !__WATCOMC__ || __QNX__ */
Guido van Rossume22e6441993-07-09 10:51:31 +0000757#ifdef MSDOS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000758 struct timeb t1, t2;
759 double frac;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000760 extern double fmod(double, double);
761 extern double floor(double);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000762 if (secs <= 0.0)
763 return;
764 frac = fmod(secs, 1.0);
765 secs = floor(secs);
766 ftime(&t1);
767 t2.time = t1.time + (int)secs;
768 t2.millitm = t1.millitm + (int)(frac*1000.0);
769 while (t2.millitm >= 1000) {
770 t2.time++;
771 t2.millitm -= 1000;
772 }
773 for (;;) {
774#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000775 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000776 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000777 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000778#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000779 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000780 return -1;
781 ftime(&t1);
782 if (t1.time > t2.time ||
783 t1.time == t2.time && t1.millitm >= t2.millitm)
784 break;
785 }
786#else /* !MSDOS */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000787#ifdef MS_WIN32
Fred Drake0e123952000-06-29 21:31:02 +0000788 {
789 double millisecs = secs * 1000.0;
790 if (millisecs > (double)ULONG_MAX) {
791 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
792 return -1;
793 }
794 /* XXX Can't interrupt this sleep */
795 Py_BEGIN_ALLOW_THREADS
796 Sleep((unsigned long)millisecs);
797 Py_END_ALLOW_THREADS
798 }
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000799#else /* !MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000800#ifdef PYOS_OS2
801 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000802 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000803 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000804 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000805 PyErr_SetFromErrno(PyExc_IOError);
806 return -1;
807 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000808 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000809#else /* !PYOS_OS2 */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000810#ifdef __BEOS__
811 /* This sleep *CAN BE* interrupted. */
812 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000813 if( secs <= 0.0 ) {
814 return;
815 }
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000816
Guido van Rossumbcc20741998-08-04 22:53:56 +0000817 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000818 /* BeOS snooze() is in microseconds... */
819 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000820 Py_BLOCK_THREADS
821 PyErr_SetFromErrno( PyExc_IOError );
822 return -1;
823 }
824 Py_END_ALLOW_THREADS
825 }
826#else /* !__BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000827 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000828 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000829 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000830 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000831#endif /* !__BEOS__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000832#endif /* !PYOS_OS2 */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000833#endif /* !MS_WIN32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000834#endif /* !MSDOS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000835#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000836#endif /* !macintosh */
837#endif /* !HAVE_SELECT */
838 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000839}