blob: 855863b9de8ce02b94d4896aa70fbaf252e0043c [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)
Guido van Rossum1bb126f1996-12-06 20:17:44 +000052extern int ftime();
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
110initmactimezone()
111{
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 *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131time_time(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000132 PyObject *self;
133 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000135 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000136 if (!PyArg_NoArgs(args))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000137 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000138 secs = floattime();
139 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000140 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000141 return NULL;
142 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000143 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000144}
145
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000146static char time_doc[] =
147"time() -> floating point number\n\
148\n\
149Return the current time in seconds since the Epoch.\n\
150Fractions of a second may be present if the system clock provides them.";
151
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152#ifdef HAVE_CLOCK
153
154#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000155#ifdef CLK_TCK
156#define CLOCKS_PER_SEC CLK_TCK
157#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000158#define CLOCKS_PER_SEC 1000000
159#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000160#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000161
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000162static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000163time_clock(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000164 PyObject *self;
165 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000166{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000167 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000169 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000171#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000173#if defined(MS_WIN32) && !defined(MS_WIN64)
Guido van Rossum3917c221997-04-02 05:35:28 +0000174/* Due to Mark Hammond */
175static PyObject *
176time_clock(self, args)
177 PyObject *self;
178 PyObject *args;
179{
180 static LARGE_INTEGER ctrStart;
181 static LARGE_INTEGER divisor = {0,0};
182 LARGE_INTEGER now, diff, rem;
183
184 if (!PyArg_NoArgs(args))
185 return NULL;
186
187 if (LargeIntegerEqualToZero(divisor)) {
188 QueryPerformanceCounter(&ctrStart);
189 if (!QueryPerformanceFrequency(&divisor) ||
190 LargeIntegerEqualToZero(divisor)) {
191 /* Unlikely to happen -
192 this works on all intel machines at least!
193 Revert to clock() */
194 return PyFloat_FromDouble(clock());
195 }
196 }
197 QueryPerformanceCounter(&now);
198 diff = LargeIntegerSubtract(now, ctrStart);
199 diff = LargeIntegerDivide(diff, divisor, &rem);
200 /* XXX - we assume both divide results fit in 32 bits. This is
201 true on Intels. First person who can afford a machine that
202 doesnt deserves to fix it :-)
203 */
204 return PyFloat_FromDouble((double)diff.LowPart +
205 ((double)rem.LowPart / (double)divisor.LowPart));
206}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000207
Guido van Rossum3917c221997-04-02 05:35:28 +0000208#define HAVE_CLOCK /* So it gets included in the methods */
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000209#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000210
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000211#ifdef HAVE_CLOCK
212static char clock_doc[] =
213"clock() -> floating point number\n\
214\n\
215Return the CPU time or real time since the start of the process or since\n\
216the first call to clock(). This has as much precision as the system records.";
217#endif
218
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000219static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220time_sleep(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000221 PyObject *self;
222 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000224 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000225 if (!PyArg_Parse(args, "d", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000227 if (floatsleep(secs) != 0)
228 return NULL;
229 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000230 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231}
232
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000233static char sleep_doc[] =
234"sleep(seconds)\n\
235\n\
236Delay execution for a given number of seconds. The argument may be\n\
237a floating point number for subsecond precision.";
238
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000239static PyObject *
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000240tmtotuple(p)
241 struct tm *p;
242{
243 return Py_BuildValue("(iiiiiiiii)",
244 p->tm_year + 1900,
245 p->tm_mon + 1, /* Want January == 1 */
246 p->tm_mday,
247 p->tm_hour,
248 p->tm_min,
249 p->tm_sec,
250 (p->tm_wday + 6) % 7, /* Want Monday == 0 */
251 p->tm_yday + 1, /* Want January, 1 == 1 */
252 p->tm_isdst);
253}
254
255static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000256time_convert(when, function)
257 time_t when;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000258 struct tm * (*function)(const time_t *);
Guido van Rossum234f9421993-06-17 12:35:49 +0000259{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000260 struct tm *p;
261 errno = 0;
Jack Jansenee398fa2000-07-03 21:37:27 +0000262#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000263 when = when + GUSI_TO_MSL_EPOCH;
264#endif
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000265 p = function(&when);
266 if (p == NULL) {
267#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000268 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000269 errno = EINVAL;
270#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000271 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000272 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000273 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000274}
275
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000276static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000277time_gmtime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000278 PyObject *self;
279 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000280{
281 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000282 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000283 return NULL;
284 return time_convert((time_t)when, gmtime);
285}
286
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000287static char gmtime_doc[] =
288"gmtime(seconds) -> tuple\n\
289\n\
290Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT).";
291
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000292static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000293time_localtime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000294 PyObject *self;
295 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000296{
297 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000298 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000299 return NULL;
300 return time_convert((time_t)when, localtime);
301}
302
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000303static char localtime_doc[] =
304"localtime(seconds) -> tuple\n\
305Convert seconds since the Epoch to a time tuple expressing local time.";
306
Guido van Rossum9e90a671993-06-24 11:10:19 +0000307static int
308gettmarg(args, p)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000309 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000310 struct tm *p;
311{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000312 int y;
313 memset((ANY *) p, '\0', sizeof(struct tm));
314
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000315 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000316 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000317 &p->tm_mon,
318 &p->tm_mday,
319 &p->tm_hour,
320 &p->tm_min,
321 &p->tm_sec,
322 &p->tm_wday,
323 &p->tm_yday,
324 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000325 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000326 if (y < 1900) {
327 PyObject *accept = PyDict_GetItemString(moddict,
328 "accept2dyear");
329 if (accept == NULL || !PyInt_Check(accept) ||
330 PyInt_AsLong(accept) == 0) {
331 PyErr_SetString(PyExc_ValueError,
332 "year >= 1900 required");
333 return 0;
334 }
335 if (69 <= y && y <= 99)
336 y += 1900;
337 else if (0 <= y && y <= 68)
338 y += 2000;
339 else {
340 PyErr_SetString(PyExc_ValueError,
341 "year out of range (00-99, 1900-*)");
342 return 0;
343 }
344 }
345 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000346 p->tm_mon--;
347 p->tm_wday = (p->tm_wday + 1) % 7;
348 p->tm_yday--;
349 return 1;
350}
351
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000352#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000353static PyObject *
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000354time_strftime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000355 PyObject *self;
356 PyObject *args;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000357{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000358 PyObject *tup;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000359 struct tm buf;
360 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000361 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000362 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000363 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000364
Guido van Rossum1f41f841998-04-27 19:04:26 +0000365 memset((ANY *) &buf, '\0', sizeof(buf));
366
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000367 if (!PyArg_ParseTuple(args, "sO:strftime", &fmt, &tup)
368 || !gettmarg(tup, &buf))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000369 return NULL;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000370 fmtlen = strlen(fmt);
371
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000372 /* I hate these functions that presume you know how big the output
373 * will be ahead of time...
374 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000375 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000376 outbuf = malloc(i);
377 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000378 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000379 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000380 buflen = strftime(outbuf, i, fmt, &buf);
381 if (buflen > 0 || i >= 256 * fmtlen) {
382 /* If the buffer is 256 times as long as the format,
383 it's probably not failing for lack of room!
384 More likely, the format yields an empty result,
385 e.g. an empty format, or %Z when the timezone
386 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000387 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000388 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000389 free(outbuf);
390 return ret;
391 }
392 free(outbuf);
393 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000394}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000395
396static char strftime_doc[] =
397"strftime(format, tuple) -> string\n\
398\n\
399Convert a time tuple to a string according to a format specification.\n\
400See the library reference manual for formatting codes.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000401#endif /* HAVE_STRFTIME */
402
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000403#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000404
405#if 0
406extern char *strptime(); /* Enable this if it's not declared in <time.h> */
407#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000408
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000409static PyObject *
410time_strptime(self, args)
411 PyObject *self;
412 PyObject *args;
413{
414 struct tm tm;
415 char *fmt = "%a %b %d %H:%M:%S %Y";
416 char *buf;
417 char *s;
418
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000419 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
420 return NULL;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000421 memset((ANY *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000422 s = strptime(buf, fmt, &tm);
423 if (s == NULL) {
424 PyErr_SetString(PyExc_ValueError, "format mismatch");
425 return NULL;
426 }
427 while (*s && isspace(*s))
428 s++;
429 if (*s) {
430 PyErr_Format(PyExc_ValueError,
431 "unconverted data remains: '%.400s'", s);
432 return NULL;
433 }
434 return tmtotuple(&tm);
435}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000436
437static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000438"strptime(string, format) -> tuple\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000439Parse a string to a time tuple according to a format specification.\n\
440See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000441#endif /* HAVE_STRPTIME */
442
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000443static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000444time_asctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000445 PyObject *self;
446 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000447{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000448 PyObject *tup;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000449 struct tm buf;
450 char *p;
Guido van Rossum43713e52000-02-29 13:59:29 +0000451 if (!PyArg_ParseTuple(args, "O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000452 return NULL;
453 if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000454 return NULL;
455 p = asctime(&buf);
456 if (p[24] == '\n')
457 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000458 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000459}
460
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000461static char asctime_doc[] =
462"asctime(tuple) -> string\n\
463\n\
464Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.";
465
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000466static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000467time_ctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000468 PyObject *self;
469 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000470{
471 double dt;
472 time_t tt;
473 char *p;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000474 if (!PyArg_Parse(args, "d", &dt))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000475 return NULL;
Guido van Rossumcac6c721996-09-06 13:34:02 +0000476 tt = (time_t)dt;
Jack Jansenee398fa2000-07-03 21:37:27 +0000477#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000478 tt = tt + GUSI_TO_MSL_EPOCH;
479#endif
Guido van Rossum9e90a671993-06-24 11:10:19 +0000480 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000481 if (p == NULL) {
482 PyErr_SetString(PyExc_ValueError, "unconvertible time");
483 return NULL;
484 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000485 if (p[24] == '\n')
486 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000487 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000488}
489
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000490static char ctime_doc[] =
491"ctime(seconds) -> string\n\
492\n\
493Convert a time in seconds since the Epoch to a string in local time.\n\
494This is equivalent to asctime(localtime(seconds)).";
495
Guido van Rossum60cd8131998-03-06 17:16:21 +0000496#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000497static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000498time_mktime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000499 PyObject *self;
500 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000501{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000502 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000503 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000504 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000505 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000506 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000507 tt = time(&tt);
508 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000509 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000510 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000511 tt = mktime(&buf);
512 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000513 PyErr_SetString(PyExc_OverflowError,
514 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000515 return NULL;
516 }
Guido van Rossumc410e922000-04-26 20:40:13 +0000517#if defined(macintosh) && defined(USE_GUSI2)
518 tt = tt - GUSI_TO_MSL_EPOCH;
519#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000520 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000521}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000522
523static char mktime_doc[] =
524"mktime(tuple) -> floating point number\n\
525\n\
526Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000527#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000528
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000529static PyMethodDef time_methods[] = {
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000530 {"time", time_time, 0, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000531#ifdef HAVE_CLOCK
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000532 {"clock", time_clock, 0, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000533#endif
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000534 {"sleep", time_sleep, 0, sleep_doc},
535 {"gmtime", time_gmtime, 0, gmtime_doc},
536 {"localtime", time_localtime, 0, localtime_doc},
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000537 {"asctime", time_asctime, 1, asctime_doc},
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000538 {"ctime", time_ctime, 0, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000539#ifdef HAVE_MKTIME
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000540 {"mktime", time_mktime, 1, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000541#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000542#ifdef HAVE_STRFTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000543 {"strftime", time_strftime, 1, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000544#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000545#ifdef HAVE_STRPTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000546 {"strptime", time_strptime, 1, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000547#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548 {NULL, NULL} /* sentinel */
549};
550
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000551static void
552ins(d, name, v)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000553 PyObject *d;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000554 char *name;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000555 PyObject *v;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000556{
557 if (v == NULL)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000558 Py_FatalError("Can't initialize time module -- NULL value");
559 if (PyDict_SetItemString(d, name, v) != 0)
560 Py_FatalError(
Guido van Rossum52174571996-12-09 18:38:52 +0000561 "Can't initialize time module -- PyDict_SetItemString failed");
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000562 Py_DECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000563}
564
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000565static char module_doc[] =
566"This module provides various functions to manipulate time values.\n\
567\n\
568There are two standard representations of time. One is the number\n\
569of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
570or a floating point number (to represent fractions of seconds).\n\
571The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
572The actual value can be retrieved by calling gmtime(0).\n\
573\n\
574The other representation is a tuple of 9 integers giving local time.\n\
575The tuple items are:\n\
576 year (four digits, e.g. 1998)\n\
577 month (1-12)\n\
578 day (1-31)\n\
579 hours (0-23)\n\
580 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000581 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000582 weekday (0-6, Monday is 0)\n\
583 Julian day (day in the year, 1-366)\n\
584 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
585If the DST flag is 0, the time is given in the regular time zone;\n\
586if it is 1, the time is given in the DST time zone;\n\
587if it is -1, mktime() should guess based on the date and time.\n\
588\n\
589Variables:\n\
590\n\
591timezone -- difference in seconds between UTC and local standard time\n\
592altzone -- difference in seconds between UTC and local DST time\n\
593daylight -- whether local time should reflect DST\n\
594tzname -- tuple of (standard time zone name, DST time zone name)\n\
595\n\
596Functions:\n\
597\n\
598time() -- return current time in seconds since the Epoch as a float\n\
599clock() -- return CPU time since process start as a float\n\
600sleep() -- delay for a number of seconds given as a float\n\
601gmtime() -- convert seconds since Epoch to UTC tuple\n\
602localtime() -- convert seconds since Epoch to local time tuple\n\
603asctime() -- convert time tuple to string\n\
604ctime() -- convert time in seconds to string\n\
605mktime() -- convert local time tuple to seconds since Epoch\n\
606strftime() -- convert time tuple to string according to format specification\n\
607strptime() -- parse string to time tuple according to format specification\n\
608";
609
610
Guido van Rossum3886bb61998-12-04 18:50:17 +0000611DL_EXPORT(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000612inittime()
613{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000614 PyObject *m, *d;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000615 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000616 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000617 d = PyModule_GetDict(m);
Guido van Rossumc2068731998-10-07 16:35:25 +0000618 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
619 p = getenv("PYTHONY2K");
620 ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
621 /* Squirrel away the module's dictionary for the y2k check */
622 Py_INCREF(d);
623 moddict = d;
Guido van Rossumea424e11999-04-23 20:59:05 +0000624#if defined(HAVE_TZNAME) && !defined(__GLIBC__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000625 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000626#ifdef PYOS_OS2
627 ins(d, "timezone", PyInt_FromLong((long)_timezone));
628#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000629 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossum26452411998-09-28 22:07:11 +0000630#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000631#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000632 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000633#else
Guido van Rossum26452411998-09-28 22:07:11 +0000634#ifdef PYOS_OS2
635 ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
636#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000637 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossum26452411998-09-28 22:07:11 +0000638#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000639#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000640 ins(d, "daylight", PyInt_FromLong((long)daylight));
641 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossumea424e11999-04-23 20:59:05 +0000642#else /* !HAVE_TZNAME || __GLIBC__ */
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000643#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000644 {
645#define YEAR ((time_t)((365 * 24 + 6) * 3600))
646 time_t t;
647 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000648 long janzone, julyzone;
649 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000650 t = (time((time_t *)0) / YEAR) * YEAR;
651 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000652 janzone = -p->tm_gmtoff;
653 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
654 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000655 t += YEAR/2;
656 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000657 julyzone = -p->tm_gmtoff;
658 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
659 julyname[9] = '\0';
660
661 if( janzone < julyzone ) {
662 /* DST is reversed in the southern hemisphere */
663 ins(d, "timezone", PyInt_FromLong(julyzone));
664 ins(d, "altzone", PyInt_FromLong(janzone));
665 ins(d, "daylight",
666 PyInt_FromLong((long)(janzone != julyzone)));
667 ins(d, "tzname",
668 Py_BuildValue("(zz)", julyname, janname));
669 } else {
670 ins(d, "timezone", PyInt_FromLong(janzone));
671 ins(d, "altzone", PyInt_FromLong(julyzone));
672 ins(d, "daylight",
673 PyInt_FromLong((long)(janzone != julyzone)));
674 ins(d, "tzname",
675 Py_BuildValue("(zz)", janname, julyname));
676 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000677 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000678#else
679#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000680 /* The only thing we can obtain is the current timezone
681 ** (and whether dst is currently _active_, but that is not what
682 ** we're looking for:-( )
683 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000684 initmactimezone();
685 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000686 ins(d, "altzone", PyInt_FromLong(timezone));
687 ins(d, "daylight", PyInt_FromLong((long)0));
688 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000689#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000690#endif /* HAVE_TM_ZONE */
Guido van Rossumea424e11999-04-23 20:59:05 +0000691#endif /* !HAVE_TZNAME || __GLIBC__ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000692 if (PyErr_Occurred())
693 Py_FatalError("Can't initialize time module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000694}
695
696
Guido van Rossumb6775db1994-08-01 11:34:53 +0000697/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000698
Guido van Rossumb6775db1994-08-01 11:34:53 +0000699static double
700floattime()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000701{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000702 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000703 (1) gettimeofday() -- resolution in microseconds
704 (2) ftime() -- resolution in milliseconds
705 (3) time() -- resolution in seconds
706 In all cases the return value is a float in seconds.
707 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
708 fail, so we fall back on ftime() or time().
709 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000710#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000711 {
712 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000713#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000714 if (gettimeofday(&t) == 0)
715 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000716#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000717 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
718 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000719#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000720 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000721#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000722 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000723#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000724 struct timeb t;
725 ftime(&t);
726 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000727#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000728 time_t secs;
729 time(&secs);
730 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000731#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000732 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000733}
734
Guido van Rossumb6775db1994-08-01 11:34:53 +0000735
736/* Implement floatsleep() for various platforms.
737 When interrupted (or when another error occurs), return -1 and
738 set an exception; else return 0. */
739
740static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000741#ifdef MPW
742floatsleep(double secs)
743#else
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000744 floatsleep(secs)
Guido van Rossum775f4da1993-01-09 17:18:52 +0000745 double secs;
Guido van Rossuma320fd31995-03-09 12:14:15 +0000746#endif /* MPW */
Guido van Rossum426035c1991-02-19 12:27:35 +0000747{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000748/* XXX Should test for MS_WIN32 first! */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000749#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000750 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000751 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000752 frac = fmod(secs, 1.0);
753 secs = floor(secs);
754 t.tv_sec = (long)secs;
755 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000756 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000757 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000758#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000759 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000760#else
761 if (1) {
762#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000763 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000764 PyErr_SetFromErrno(PyExc_IOError);
765 return -1;
766 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000767 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000768 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000769#else /* !HAVE_SELECT || __BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000770#ifdef macintosh
771#define MacTicks (* (long *)0x16A)
772 long deadline;
773 deadline = MacTicks + (long)(secs * 60.0);
774 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000775 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000776 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000777 return -1;
778 }
779#else /* !macintosh */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000780#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000781 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000782 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000783 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000784 Py_END_ALLOW_THREADS
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000785#else /* !__WATCOMC__ || __QNX__ */
Guido van Rossume22e6441993-07-09 10:51:31 +0000786#ifdef MSDOS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000787 struct timeb t1, t2;
788 double frac;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000789 extern double fmod(double, double);
790 extern double floor(double);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000791 if (secs <= 0.0)
792 return;
793 frac = fmod(secs, 1.0);
794 secs = floor(secs);
795 ftime(&t1);
796 t2.time = t1.time + (int)secs;
797 t2.millitm = t1.millitm + (int)(frac*1000.0);
798 while (t2.millitm >= 1000) {
799 t2.time++;
800 t2.millitm -= 1000;
801 }
802 for (;;) {
803#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000804 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000805 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000806 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000807#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000808 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000809 return -1;
810 ftime(&t1);
811 if (t1.time > t2.time ||
812 t1.time == t2.time && t1.millitm >= t2.millitm)
813 break;
814 }
815#else /* !MSDOS */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000816#ifdef MS_WIN32
Fred Drake0e123952000-06-29 21:31:02 +0000817 {
818 double millisecs = secs * 1000.0;
819 if (millisecs > (double)ULONG_MAX) {
820 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
821 return -1;
822 }
823 /* XXX Can't interrupt this sleep */
824 Py_BEGIN_ALLOW_THREADS
825 Sleep((unsigned long)millisecs);
826 Py_END_ALLOW_THREADS
827 }
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000828#else /* !MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000829#ifdef PYOS_OS2
830 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000831 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000832 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000833 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000834 PyErr_SetFromErrno(PyExc_IOError);
835 return -1;
836 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000837 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000838#else /* !PYOS_OS2 */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000839#ifdef __BEOS__
840 /* This sleep *CAN BE* interrupted. */
841 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000842 if( secs <= 0.0 ) {
843 return;
844 }
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000845
Guido van Rossumbcc20741998-08-04 22:53:56 +0000846 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000847 /* BeOS snooze() is in microseconds... */
848 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000849 Py_BLOCK_THREADS
850 PyErr_SetFromErrno( PyExc_IOError );
851 return -1;
852 }
853 Py_END_ALLOW_THREADS
854 }
855#else /* !__BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000856 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000857 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000858 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000859 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000860#endif /* !__BEOS__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000861#endif /* !PYOS_OS2 */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000862#endif /* !MS_WIN32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000863#endif /* !MSDOS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000864#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000865#endif /* !macintosh */
866#endif /* !HAVE_SELECT */
867 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000868}