blob: d4b2f3728544f7dee5db6e8dcb766fc386deda59 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Time module */
3
Barry Warsaw9a2a8a81996-12-06 23:32:14 +00004#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005
Guido van Rossum87ce7bb1998-06-09 16:30:31 +00006#include <ctype.h>
7
Guido van Rossum6d946f91992-08-14 13:49:30 +00008#ifdef macintosh
Guido van Rossumb6775db1994-08-01 11:34:53 +00009#include <time.h>
Guido van Rossumc410e922000-04-26 20:40:13 +000010#include <OSUtils.h>
Jack Jansen63596ae2000-12-12 22:42:30 +000011#ifdef USE_GUSI211
Guido van Rossumc410e922000-04-26 20:40:13 +000012/* GUSI, the I/O library which has the time() function and such uses the
13** Mac epoch of 1904. MSL, the C library which has localtime() and so uses
14** the ANSI epoch of 1900.
15*/
16#define GUSI_TO_MSL_EPOCH (4*365*24*60*60)
17#endif /* USE_GUSI2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000018#else
19#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000020#endif
21
Guido van Rossumb6775db1994-08-01 11:34:53 +000022#ifdef QUICKWIN
23#include <io.h>
24#endif
25
26#ifdef HAVE_UNISTD_H
Guido van Rossum2762f251992-03-27 17:22:13 +000027#include <unistd.h>
28#endif
29
Guido van Rossumb6775db1994-08-01 11:34:53 +000030#ifdef HAVE_FTIME
31#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000032#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000033extern int ftime(struct timeb *);
Guido van Rossum52174571996-12-09 18:38:52 +000034#endif /* MS_WINDOWS */
35#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036
Guido van Rossum7bf22de1997-12-02 20:34:19 +000037#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000038#include <i86.h>
39#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000040#ifdef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +000041#include <windows.h>
Guido van Rossumb2fb3641996-09-07 00:47:35 +000042#ifdef MS_WIN16
43/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000044#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000045#define tzname _tzname
46#define daylight _daylight
47#define altzone _altzone
Guido van Rossumb2fb3641996-09-07 00:47:35 +000048#endif /* MS_WIN16 */
Guido van Rossumcac6c721996-09-06 13:34:02 +000049#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000050#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000051
Fred Drakedfb4ebd2000-06-29 20:56:28 +000052#if defined(MS_WIN32) && !defined(MS_WIN64)
53/* Win32 has better clock replacement
54 XXX Win64 does not yet, but might when the platform matures. */
Guido van Rossum3917c221997-04-02 05:35:28 +000055#include <largeint.h>
56#undef HAVE_CLOCK /* We have our own version down below */
Fred Drakedfb4ebd2000-06-29 20:56:28 +000057#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +000058
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000059#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000060#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000061#endif
62
Guido van Rossumbcc20741998-08-04 22:53:56 +000063#ifdef __BEOS__
Fred Drake56221a72000-08-15 18:52:33 +000064#include <time.h>
Guido van Rossumbcc20741998-08-04 22:53:56 +000065/* For bigtime_t, snooze(). - [cjh] */
66#include <support/SupportDefs.h>
67#include <kernel/OS.h>
68#endif
69
Guido van Rossum234f9421993-06-17 12:35:49 +000070/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000071static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000072static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000074/* For Y2K check */
75static PyObject *moddict;
76
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000077#ifdef macintosh
78/* Our own timezone. We have enough information to deduce whether
79** DST is on currently, but unfortunately we cannot put it to good
80** use because we don't know the rules (and that is needed to have
81** localtime() return correct tm_isdst values for times other than
82** the current time. So, we cop out and only tell the user the current
83** timezone.
84*/
85static long timezone;
86
87static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000088initmactimezone(void)
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000089{
90 MachineLocation loc;
91 long delta;
92
93 ReadLocation(&loc);
94
95 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
96 return;
97
98 delta = loc.u.gmtDelta & 0x00FFFFFF;
99
100 if (delta & 0x00800000)
101 delta |= 0xFF000000;
102
103 timezone = -delta;
104}
105#endif /* macintosh */
106
107
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000108static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000109time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000111 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000112 if (!PyArg_ParseTuple(args, ":time"))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000113 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000114 secs = floattime();
115 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000116 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000117 return NULL;
118 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000119 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000120}
121
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000122static char time_doc[] =
123"time() -> floating point number\n\
124\n\
125Return the current time in seconds since the Epoch.\n\
126Fractions of a second may be present if the system clock provides them.";
127
Guido van Rossumb6775db1994-08-01 11:34:53 +0000128#ifdef HAVE_CLOCK
129
130#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000131#ifdef CLK_TCK
132#define CLOCKS_PER_SEC CLK_TCK
133#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000134#define CLOCKS_PER_SEC 1000000
135#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000136#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000137
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000138static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000139time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000140{
Thomas Woutersfe385252001-01-19 23:16:56 +0000141 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000143 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000147#if defined(MS_WIN32) && !defined(MS_WIN64)
Guido van Rossum3917c221997-04-02 05:35:28 +0000148/* Due to Mark Hammond */
149static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000150time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000151{
152 static LARGE_INTEGER ctrStart;
153 static LARGE_INTEGER divisor = {0,0};
154 LARGE_INTEGER now, diff, rem;
155
Thomas Woutersfe385252001-01-19 23:16:56 +0000156 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum3917c221997-04-02 05:35:28 +0000157 return NULL;
158
159 if (LargeIntegerEqualToZero(divisor)) {
160 QueryPerformanceCounter(&ctrStart);
161 if (!QueryPerformanceFrequency(&divisor) ||
162 LargeIntegerEqualToZero(divisor)) {
163 /* Unlikely to happen -
164 this works on all intel machines at least!
165 Revert to clock() */
166 return PyFloat_FromDouble(clock());
167 }
168 }
169 QueryPerformanceCounter(&now);
170 diff = LargeIntegerSubtract(now, ctrStart);
171 diff = LargeIntegerDivide(diff, divisor, &rem);
172 /* XXX - we assume both divide results fit in 32 bits. This is
173 true on Intels. First person who can afford a machine that
174 doesnt deserves to fix it :-)
175 */
176 return PyFloat_FromDouble((double)diff.LowPart +
177 ((double)rem.LowPart / (double)divisor.LowPart));
178}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000179
Guido van Rossum3917c221997-04-02 05:35:28 +0000180#define HAVE_CLOCK /* So it gets included in the methods */
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000181#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000182
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000183#ifdef HAVE_CLOCK
184static char clock_doc[] =
185"clock() -> floating point number\n\
186\n\
187Return the CPU time or real time since the start of the process or since\n\
188the first call to clock(). This has as much precision as the system records.";
189#endif
190
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000191static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000192time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000194 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000195 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000196 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000197 if (floatsleep(secs) != 0)
198 return NULL;
199 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000200 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000201}
202
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000203static char sleep_doc[] =
204"sleep(seconds)\n\
205\n\
206Delay execution for a given number of seconds. The argument may be\n\
207a floating point number for subsecond precision.";
208
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000209static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000210tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000211{
212 return Py_BuildValue("(iiiiiiiii)",
213 p->tm_year + 1900,
214 p->tm_mon + 1, /* Want January == 1 */
215 p->tm_mday,
216 p->tm_hour,
217 p->tm_min,
218 p->tm_sec,
219 (p->tm_wday + 6) % 7, /* Want Monday == 0 */
220 p->tm_yday + 1, /* Want January, 1 == 1 */
221 p->tm_isdst);
222}
223
224static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000225time_convert(time_t when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000226{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000227 struct tm *p;
228 errno = 0;
Jack Jansenee398fa2000-07-03 21:37:27 +0000229#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000230 when = when + GUSI_TO_MSL_EPOCH;
231#endif
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000232 p = function(&when);
233 if (p == NULL) {
234#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000235 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000236 errno = EINVAL;
237#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000238 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000239 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000240 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000241}
242
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000243static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000244time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000245{
246 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000247 if (PyTuple_Size(args) == 0)
248 when = floattime();
249 if (!PyArg_ParseTuple(args, "|d:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000250 return NULL;
251 return time_convert((time_t)when, gmtime);
252}
253
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000254static char gmtime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000255"gmtime([seconds]) -> tuple\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000256\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000257Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
258GMT). When 'seconds' is not passed in, convert the current time instead.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000259
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000260static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000261time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000262{
263 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000264 if (PyTuple_Size(args) == 0)
265 when = floattime();
266 if (!PyArg_ParseTuple(args, "|d:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000267 return NULL;
268 return time_convert((time_t)when, localtime);
269}
270
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000271static char localtime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000272"localtime([seconds]) -> tuple\n\
273Convert seconds since the Epoch to a time tuple expressing local time.\n\
274When 'seconds' is not passed in, convert the current time instead.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000275
Guido van Rossum9e90a671993-06-24 11:10:19 +0000276static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000277gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000278{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000279 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000280 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000281
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000282 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000283 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000284 &p->tm_mon,
285 &p->tm_mday,
286 &p->tm_hour,
287 &p->tm_min,
288 &p->tm_sec,
289 &p->tm_wday,
290 &p->tm_yday,
291 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000292 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000293 if (y < 1900) {
294 PyObject *accept = PyDict_GetItemString(moddict,
295 "accept2dyear");
296 if (accept == NULL || !PyInt_Check(accept) ||
297 PyInt_AsLong(accept) == 0) {
298 PyErr_SetString(PyExc_ValueError,
299 "year >= 1900 required");
300 return 0;
301 }
302 if (69 <= y && y <= 99)
303 y += 1900;
304 else if (0 <= y && y <= 68)
305 y += 2000;
306 else {
307 PyErr_SetString(PyExc_ValueError,
308 "year out of range (00-99, 1900-*)");
309 return 0;
310 }
311 }
312 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000313 p->tm_mon--;
314 p->tm_wday = (p->tm_wday + 1) % 7;
315 p->tm_yday--;
316 return 1;
317}
318
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000319#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000320static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000321time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000322{
Thomas Woutersfe385252001-01-19 23:16:56 +0000323 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000324 struct tm buf;
325 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000326 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000327 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000328 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000329
Thomas Wouters334fb892000-07-25 12:56:38 +0000330 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000331
Thomas Woutersfe385252001-01-19 23:16:56 +0000332 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000333 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000334
335 if (tup == NULL) {
336 time_t tt = time(NULL);
337 buf = *localtime(&tt);
338 } else if (!gettmarg(tup, &buf))
339 return NULL;
340
Guido van Rossumc222ec21999-02-23 00:00:10 +0000341 fmtlen = strlen(fmt);
342
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000343 /* I hate these functions that presume you know how big the output
344 * will be ahead of time...
345 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000346 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000347 outbuf = malloc(i);
348 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000349 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000350 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000351 buflen = strftime(outbuf, i, fmt, &buf);
352 if (buflen > 0 || i >= 256 * fmtlen) {
353 /* If the buffer is 256 times as long as the format,
354 it's probably not failing for lack of room!
355 More likely, the format yields an empty result,
356 e.g. an empty format, or %Z when the timezone
357 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000358 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000359 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000360 free(outbuf);
361 return ret;
362 }
363 free(outbuf);
364 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000365}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000366
367static char strftime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000368"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000369\n\
370Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000371See the library reference manual for formatting codes. When the time tuple\n\
372is not present, current time as returned by localtime() is used.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000373#endif /* HAVE_STRFTIME */
374
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000375#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000376
377#if 0
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000378/* Enable this if it's not declared in <time.h> */
379extern char *strptime(const char *, const char *, struct tm *);
Fred Drakeaff60182000-05-09 19:52:40 +0000380#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000381
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000382static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000383time_strptime(PyObject *self, PyObject *args)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000384{
385 struct tm tm;
386 char *fmt = "%a %b %d %H:%M:%S %Y";
387 char *buf;
388 char *s;
389
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000390 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
391 return NULL;
Thomas Wouters334fb892000-07-25 12:56:38 +0000392 memset((void *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000393 s = strptime(buf, fmt, &tm);
394 if (s == NULL) {
395 PyErr_SetString(PyExc_ValueError, "format mismatch");
396 return NULL;
397 }
398 while (*s && isspace(*s))
399 s++;
400 if (*s) {
401 PyErr_Format(PyExc_ValueError,
402 "unconverted data remains: '%.400s'", s);
403 return NULL;
404 }
405 return tmtotuple(&tm);
406}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000407
408static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000409"strptime(string, format) -> tuple\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000410Parse a string to a time tuple according to a format specification.\n\
411See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000412#endif /* HAVE_STRPTIME */
413
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000414static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000415time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000416{
Thomas Woutersfe385252001-01-19 23:16:56 +0000417 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000418 struct tm buf;
419 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000420 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000421 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000422 if (tup == NULL) {
423 time_t tt = time(NULL);
424 buf = *localtime(&tt);
425 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000426 return NULL;
427 p = asctime(&buf);
428 if (p[24] == '\n')
429 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000430 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000431}
432
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000433static char asctime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000434"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000435\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000436Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
437When the time tuple is not present, current time as returned by localtime()\n\
438is used.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000439
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000440static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000441time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000442{
443 double dt;
444 time_t tt;
445 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000446
447 if (PyTuple_Size(args) == 0)
448 tt = time(NULL);
449 else {
450 if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
451 return NULL;
452 tt = (time_t)dt;
453 }
Jack Jansenee398fa2000-07-03 21:37:27 +0000454#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000455 tt = tt + GUSI_TO_MSL_EPOCH;
456#endif
Guido van Rossum9e90a671993-06-24 11:10:19 +0000457 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000458 if (p == NULL) {
459 PyErr_SetString(PyExc_ValueError, "unconvertible time");
460 return NULL;
461 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000462 if (p[24] == '\n')
463 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000464 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000465}
466
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000467static char ctime_doc[] =
468"ctime(seconds) -> string\n\
469\n\
470Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000471This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
472not present, current time as returned by localtime() is used.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000473
Guido van Rossum60cd8131998-03-06 17:16:21 +0000474#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000475static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000476time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000477{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000478 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000479 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000480 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000481 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000482 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000483 tt = time(&tt);
484 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000485 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000486 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000487 tt = mktime(&buf);
488 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000489 PyErr_SetString(PyExc_OverflowError,
490 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000491 return NULL;
492 }
Jack Jansen63596ae2000-12-12 22:42:30 +0000493#if defined(macintosh) && defined(USE_GUSI211)
Guido van Rossumc410e922000-04-26 20:40:13 +0000494 tt = tt - GUSI_TO_MSL_EPOCH;
495#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000496 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000497}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000498
499static char mktime_doc[] =
500"mktime(tuple) -> floating point number\n\
501\n\
502Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000503#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000504
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000505static PyMethodDef time_methods[] = {
Thomas Woutersfe385252001-01-19 23:16:56 +0000506 {"time", time_time, METH_VARARGS, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000507#ifdef HAVE_CLOCK
Thomas Woutersfe385252001-01-19 23:16:56 +0000508 {"clock", time_clock, METH_VARARGS, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000509#endif
Thomas Woutersfe385252001-01-19 23:16:56 +0000510 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
511 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
512 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000513 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
Thomas Woutersfe385252001-01-19 23:16:56 +0000514 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000515#ifdef HAVE_MKTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000516 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000517#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000518#ifdef HAVE_STRFTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000519 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000520#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000521#ifdef HAVE_STRPTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000522 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000523#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000524 {NULL, NULL} /* sentinel */
525};
526
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000527static void
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000528ins(PyObject *d, char *name, PyObject *v)
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000529{
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000530 /* Don't worry too much about errors, they'll be caught by the
531 * caller of inittime().
532 */
533 if (v)
534 PyDict_SetItemString(d, name, v);
535 Py_XDECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000536}
537
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000538
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000539static char module_doc[] =
540"This module provides various functions to manipulate time values.\n\
541\n\
542There are two standard representations of time. One is the number\n\
543of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
544or a floating point number (to represent fractions of seconds).\n\
545The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
546The actual value can be retrieved by calling gmtime(0).\n\
547\n\
548The other representation is a tuple of 9 integers giving local time.\n\
549The tuple items are:\n\
550 year (four digits, e.g. 1998)\n\
551 month (1-12)\n\
552 day (1-31)\n\
553 hours (0-23)\n\
554 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000555 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000556 weekday (0-6, Monday is 0)\n\
557 Julian day (day in the year, 1-366)\n\
558 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
559If the DST flag is 0, the time is given in the regular time zone;\n\
560if it is 1, the time is given in the DST time zone;\n\
561if it is -1, mktime() should guess based on the date and time.\n\
562\n\
563Variables:\n\
564\n\
565timezone -- difference in seconds between UTC and local standard time\n\
566altzone -- difference in seconds between UTC and local DST time\n\
567daylight -- whether local time should reflect DST\n\
568tzname -- tuple of (standard time zone name, DST time zone name)\n\
569\n\
570Functions:\n\
571\n\
572time() -- return current time in seconds since the Epoch as a float\n\
573clock() -- return CPU time since process start as a float\n\
574sleep() -- delay for a number of seconds given as a float\n\
575gmtime() -- convert seconds since Epoch to UTC tuple\n\
576localtime() -- convert seconds since Epoch to local time tuple\n\
577asctime() -- convert time tuple to string\n\
578ctime() -- convert time in seconds to string\n\
579mktime() -- convert local time tuple to seconds since Epoch\n\
580strftime() -- convert time tuple to string according to format specification\n\
581strptime() -- parse string to time tuple according to format specification\n\
582";
583
584
Guido van Rossum3886bb61998-12-04 18:50:17 +0000585DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000586inittime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000588 PyObject *m, *d;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000589 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000590 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000591 d = PyModule_GetDict(m);
Guido van Rossumc2068731998-10-07 16:35:25 +0000592 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
593 p = getenv("PYTHONY2K");
594 ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
595 /* Squirrel away the module's dictionary for the y2k check */
596 Py_INCREF(d);
597 moddict = d;
Guido van Rossumea424e11999-04-23 20:59:05 +0000598#if defined(HAVE_TZNAME) && !defined(__GLIBC__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000599 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000600#ifdef PYOS_OS2
601 ins(d, "timezone", PyInt_FromLong((long)_timezone));
602#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000603 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossum26452411998-09-28 22:07:11 +0000604#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000605#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000606 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000607#else
Guido van Rossum26452411998-09-28 22:07:11 +0000608#ifdef PYOS_OS2
609 ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
610#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000611 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossum26452411998-09-28 22:07:11 +0000612#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000613#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000614 ins(d, "daylight", PyInt_FromLong((long)daylight));
615 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossumea424e11999-04-23 20:59:05 +0000616#else /* !HAVE_TZNAME || __GLIBC__ */
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000617#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000618 {
619#define YEAR ((time_t)((365 * 24 + 6) * 3600))
620 time_t t;
621 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000622 long janzone, julyzone;
623 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000624 t = (time((time_t *)0) / YEAR) * YEAR;
625 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000626 janzone = -p->tm_gmtoff;
627 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
628 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000629 t += YEAR/2;
630 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000631 julyzone = -p->tm_gmtoff;
632 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
633 julyname[9] = '\0';
634
635 if( janzone < julyzone ) {
636 /* DST is reversed in the southern hemisphere */
637 ins(d, "timezone", PyInt_FromLong(julyzone));
638 ins(d, "altzone", PyInt_FromLong(janzone));
639 ins(d, "daylight",
640 PyInt_FromLong((long)(janzone != julyzone)));
641 ins(d, "tzname",
642 Py_BuildValue("(zz)", julyname, janname));
643 } else {
644 ins(d, "timezone", PyInt_FromLong(janzone));
645 ins(d, "altzone", PyInt_FromLong(julyzone));
646 ins(d, "daylight",
647 PyInt_FromLong((long)(janzone != julyzone)));
648 ins(d, "tzname",
649 Py_BuildValue("(zz)", janname, julyname));
650 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000651 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000652#else
653#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000654 /* The only thing we can obtain is the current timezone
655 ** (and whether dst is currently _active_, but that is not what
656 ** we're looking for:-( )
657 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000658 initmactimezone();
659 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000660 ins(d, "altzone", PyInt_FromLong(timezone));
661 ins(d, "daylight", PyInt_FromLong((long)0));
662 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000663#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000664#endif /* HAVE_TM_ZONE */
Guido van Rossumea424e11999-04-23 20:59:05 +0000665#endif /* !HAVE_TZNAME || __GLIBC__ */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000666}
667
668
Guido van Rossumb6775db1994-08-01 11:34:53 +0000669/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000670
Guido van Rossumb6775db1994-08-01 11:34:53 +0000671static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000672floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000673{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000674 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000675 (1) gettimeofday() -- resolution in microseconds
676 (2) ftime() -- resolution in milliseconds
677 (3) time() -- resolution in seconds
678 In all cases the return value is a float in seconds.
679 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
680 fail, so we fall back on ftime() or time().
681 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000682#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000683 {
684 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000685#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000686 if (gettimeofday(&t) == 0)
687 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000688#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000689 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
690 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000691#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000692 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000693#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000694 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000695#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000696 struct timeb t;
697 ftime(&t);
698 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000699#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000700 time_t secs;
701 time(&secs);
702 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000703#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000704 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000705}
706
Guido van Rossumb6775db1994-08-01 11:34:53 +0000707
708/* Implement floatsleep() for various platforms.
709 When interrupted (or when another error occurs), return -1 and
710 set an exception; else return 0. */
711
712static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000713floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000714{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000715/* XXX Should test for MS_WIN32 first! */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000716#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000717 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000718 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000719 frac = fmod(secs, 1.0);
720 secs = floor(secs);
721 t.tv_sec = (long)secs;
722 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000723 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000724 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000725#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000726 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000727#else
728 if (1) {
729#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000730 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000731 PyErr_SetFromErrno(PyExc_IOError);
732 return -1;
733 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000734 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000735 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000736#else /* !HAVE_SELECT || __BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000737#ifdef macintosh
738#define MacTicks (* (long *)0x16A)
739 long deadline;
740 deadline = MacTicks + (long)(secs * 60.0);
741 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000742 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000743 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000744 return -1;
745 }
746#else /* !macintosh */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000747#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000748 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000749 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000750 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000751 Py_END_ALLOW_THREADS
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000752#else /* !__WATCOMC__ || __QNX__ */
Guido van Rossume22e6441993-07-09 10:51:31 +0000753#ifdef MSDOS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000754 struct timeb t1, t2;
755 double frac;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000756 extern double fmod(double, double);
757 extern double floor(double);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000758 if (secs <= 0.0)
759 return;
760 frac = fmod(secs, 1.0);
761 secs = floor(secs);
762 ftime(&t1);
763 t2.time = t1.time + (int)secs;
764 t2.millitm = t1.millitm + (int)(frac*1000.0);
765 while (t2.millitm >= 1000) {
766 t2.time++;
767 t2.millitm -= 1000;
768 }
769 for (;;) {
770#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000771 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000772 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000773 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000774#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000775 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000776 return -1;
777 ftime(&t1);
778 if (t1.time > t2.time ||
779 t1.time == t2.time && t1.millitm >= t2.millitm)
780 break;
781 }
782#else /* !MSDOS */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000783#ifdef MS_WIN32
Fred Drake0e123952000-06-29 21:31:02 +0000784 {
785 double millisecs = secs * 1000.0;
786 if (millisecs > (double)ULONG_MAX) {
787 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
788 return -1;
789 }
790 /* XXX Can't interrupt this sleep */
791 Py_BEGIN_ALLOW_THREADS
792 Sleep((unsigned long)millisecs);
793 Py_END_ALLOW_THREADS
794 }
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000795#else /* !MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000796#ifdef PYOS_OS2
797 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000798 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000799 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000800 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000801 PyErr_SetFromErrno(PyExc_IOError);
802 return -1;
803 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000804 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000805#else /* !PYOS_OS2 */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000806#ifdef __BEOS__
807 /* This sleep *CAN BE* interrupted. */
808 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000809 if( secs <= 0.0 ) {
810 return;
811 }
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000812
Guido van Rossumbcc20741998-08-04 22:53:56 +0000813 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000814 /* BeOS snooze() is in microseconds... */
815 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000816 Py_BLOCK_THREADS
817 PyErr_SetFromErrno( PyExc_IOError );
818 return -1;
819 }
820 Py_END_ALLOW_THREADS
821 }
822#else /* !__BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000823 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000824 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000825 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000826 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000827#endif /* !__BEOS__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000828#endif /* !PYOS_OS2 */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000829#endif /* !MS_WIN32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000830#endif /* !MSDOS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000831#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000832#endif /* !macintosh */
833#endif /* !HAVE_SELECT */
834 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000835}