blob: cfce06bca569fb77f4481e2b2aaba5341867ee99 [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
Guido van Rossum48a680c2001-03-02 06:34:14 +000019#ifndef RISCOS
Guido van Rossumb6775db1994-08-01 11:34:53 +000020#include <sys/types.h>
Guido van Rossum48a680c2001-03-02 06:34:14 +000021#endif /* RISCOS */
Guido van Rossum6d946f91992-08-14 13:49:30 +000022#endif
23
Guido van Rossumb6775db1994-08-01 11:34:53 +000024#ifdef QUICKWIN
25#include <io.h>
26#endif
27
28#ifdef HAVE_UNISTD_H
Guido van Rossum2762f251992-03-27 17:22:13 +000029#include <unistd.h>
30#endif
31
Guido van Rossumb6775db1994-08-01 11:34:53 +000032#ifdef HAVE_FTIME
33#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000034#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000035extern int ftime(struct timeb *);
Guido van Rossum52174571996-12-09 18:38:52 +000036#endif /* MS_WINDOWS */
37#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038
Guido van Rossum7bf22de1997-12-02 20:34:19 +000039#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000040#include <i86.h>
41#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000042#ifdef MS_WINDOWS
Guido van Rossum258ccd42001-03-02 06:53:29 +000043#include <windows.h>
Guido van Rossumb2fb3641996-09-07 00:47:35 +000044#ifdef MS_WIN16
45/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000046#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000047#define tzname _tzname
48#define daylight _daylight
49#define altzone _altzone
Guido van Rossumb2fb3641996-09-07 00:47:35 +000050#endif /* MS_WIN16 */
Guido van Rossumcac6c721996-09-06 13:34:02 +000051#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000052#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000053
Fred Drakedfb4ebd2000-06-29 20:56:28 +000054#if defined(MS_WIN32) && !defined(MS_WIN64)
55/* Win32 has better clock replacement
56 XXX Win64 does not yet, but might when the platform matures. */
Guido van Rossum3917c221997-04-02 05:35:28 +000057#include <largeint.h>
58#undef HAVE_CLOCK /* We have our own version down below */
Fred Drakedfb4ebd2000-06-29 20:56:28 +000059#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +000060
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000061#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000062#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000063#endif
64
Guido van Rossumbcc20741998-08-04 22:53:56 +000065#ifdef __BEOS__
Fred Drake56221a72000-08-15 18:52:33 +000066#include <time.h>
Guido van Rossumbcc20741998-08-04 22:53:56 +000067/* For bigtime_t, snooze(). - [cjh] */
68#include <support/SupportDefs.h>
69#include <kernel/OS.h>
70#endif
71
Guido van Rossum234f9421993-06-17 12:35:49 +000072/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000073static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000074static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000076/* For Y2K check */
77static PyObject *moddict;
78
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000079#ifdef macintosh
80/* Our own timezone. We have enough information to deduce whether
81** DST is on currently, but unfortunately we cannot put it to good
82** use because we don't know the rules (and that is needed to have
83** localtime() return correct tm_isdst values for times other than
84** the current time. So, we cop out and only tell the user the current
85** timezone.
86*/
87static long timezone;
88
89static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000090initmactimezone(void)
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000091{
92 MachineLocation loc;
93 long delta;
94
95 ReadLocation(&loc);
96
97 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
98 return;
99
100 delta = loc.u.gmtDelta & 0x00FFFFFF;
101
102 if (delta & 0x00800000)
103 delta |= 0xFF000000;
104
105 timezone = -delta;
106}
107#endif /* macintosh */
108
109
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000110static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000111time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000113 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000114 if (!PyArg_ParseTuple(args, ":time"))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000115 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000116 secs = floattime();
117 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000118 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000119 return NULL;
120 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000121 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000122}
123
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000124static char time_doc[] =
125"time() -> floating point number\n\
126\n\
127Return the current time in seconds since the Epoch.\n\
128Fractions of a second may be present if the system clock provides them.";
129
Guido van Rossumb6775db1994-08-01 11:34:53 +0000130#ifdef HAVE_CLOCK
131
132#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000133#ifdef CLK_TCK
134#define CLOCKS_PER_SEC CLK_TCK
135#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000136#define CLOCKS_PER_SEC 1000000
137#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000138#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000139
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000140static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000141time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000142{
Thomas Woutersfe385252001-01-19 23:16:56 +0000143 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000145 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000147#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000149#if defined(MS_WIN32) && !defined(MS_WIN64)
Guido van Rossum3917c221997-04-02 05:35:28 +0000150/* Due to Mark Hammond */
151static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000152time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000153{
154 static LARGE_INTEGER ctrStart;
155 static LARGE_INTEGER divisor = {0,0};
156 LARGE_INTEGER now, diff, rem;
157
Thomas Woutersfe385252001-01-19 23:16:56 +0000158 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum3917c221997-04-02 05:35:28 +0000159 return NULL;
160
161 if (LargeIntegerEqualToZero(divisor)) {
162 QueryPerformanceCounter(&ctrStart);
163 if (!QueryPerformanceFrequency(&divisor) ||
164 LargeIntegerEqualToZero(divisor)) {
165 /* Unlikely to happen -
166 this works on all intel machines at least!
167 Revert to clock() */
168 return PyFloat_FromDouble(clock());
169 }
170 }
171 QueryPerformanceCounter(&now);
172 diff = LargeIntegerSubtract(now, ctrStart);
173 diff = LargeIntegerDivide(diff, divisor, &rem);
174 /* XXX - we assume both divide results fit in 32 bits. This is
175 true on Intels. First person who can afford a machine that
176 doesnt deserves to fix it :-)
177 */
178 return PyFloat_FromDouble((double)diff.LowPart +
179 ((double)rem.LowPart / (double)divisor.LowPart));
180}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000181
Guido van Rossum3917c221997-04-02 05:35:28 +0000182#define HAVE_CLOCK /* So it gets included in the methods */
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000183#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000184
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000185#ifdef HAVE_CLOCK
186static char clock_doc[] =
187"clock() -> floating point number\n\
188\n\
189Return the CPU time or real time since the start of the process or since\n\
190the first call to clock(). This has as much precision as the system records.";
191#endif
192
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000193static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000194time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000196 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000197 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000199 if (floatsleep(secs) != 0)
200 return NULL;
201 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000202 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203}
204
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000205static char sleep_doc[] =
206"sleep(seconds)\n\
207\n\
208Delay execution for a given number of seconds. The argument may be\n\
209a floating point number for subsecond precision.";
210
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000211static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000212tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000213{
214 return Py_BuildValue("(iiiiiiiii)",
215 p->tm_year + 1900,
216 p->tm_mon + 1, /* Want January == 1 */
217 p->tm_mday,
218 p->tm_hour,
219 p->tm_min,
220 p->tm_sec,
221 (p->tm_wday + 6) % 7, /* Want Monday == 0 */
222 p->tm_yday + 1, /* Want January, 1 == 1 */
223 p->tm_isdst);
224}
225
226static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000227time_convert(time_t when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000228{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000229 struct tm *p;
230 errno = 0;
Jack Jansenee398fa2000-07-03 21:37:27 +0000231#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000232 when = when + GUSI_TO_MSL_EPOCH;
233#endif
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000234 p = function(&when);
235 if (p == NULL) {
236#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000237 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000238 errno = EINVAL;
239#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000240 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000241 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000242 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000243}
244
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000245static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000246time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000247{
248 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000249 if (PyTuple_Size(args) == 0)
250 when = floattime();
251 if (!PyArg_ParseTuple(args, "|d:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000252 return NULL;
253 return time_convert((time_t)when, gmtime);
254}
255
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000256static char gmtime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000257"gmtime([seconds]) -> tuple\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000258\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000259Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
260GMT). When 'seconds' is not passed in, convert the current time instead.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000261
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000262static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000263time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000264{
265 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000266 if (PyTuple_Size(args) == 0)
267 when = floattime();
268 if (!PyArg_ParseTuple(args, "|d:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000269 return NULL;
270 return time_convert((time_t)when, localtime);
271}
272
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000273static char localtime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000274"localtime([seconds]) -> tuple\n\
275Convert seconds since the Epoch to a time tuple expressing local time.\n\
276When 'seconds' is not passed in, convert the current time instead.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000277
Guido van Rossum9e90a671993-06-24 11:10:19 +0000278static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000279gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000280{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000281 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000282 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000283
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000284 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000285 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000286 &p->tm_mon,
287 &p->tm_mday,
288 &p->tm_hour,
289 &p->tm_min,
290 &p->tm_sec,
291 &p->tm_wday,
292 &p->tm_yday,
293 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000294 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000295 if (y < 1900) {
296 PyObject *accept = PyDict_GetItemString(moddict,
297 "accept2dyear");
298 if (accept == NULL || !PyInt_Check(accept) ||
299 PyInt_AsLong(accept) == 0) {
300 PyErr_SetString(PyExc_ValueError,
301 "year >= 1900 required");
302 return 0;
303 }
304 if (69 <= y && y <= 99)
305 y += 1900;
306 else if (0 <= y && y <= 68)
307 y += 2000;
308 else {
309 PyErr_SetString(PyExc_ValueError,
310 "year out of range (00-99, 1900-*)");
311 return 0;
312 }
313 }
314 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000315 p->tm_mon--;
316 p->tm_wday = (p->tm_wday + 1) % 7;
317 p->tm_yday--;
318 return 1;
319}
320
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000321#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000322static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000323time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000324{
Thomas Woutersfe385252001-01-19 23:16:56 +0000325 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000326 struct tm buf;
327 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000328 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000329 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000330 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000331
Thomas Wouters334fb892000-07-25 12:56:38 +0000332 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000333
Thomas Woutersfe385252001-01-19 23:16:56 +0000334 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000335 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000336
337 if (tup == NULL) {
338 time_t tt = time(NULL);
339 buf = *localtime(&tt);
340 } else if (!gettmarg(tup, &buf))
341 return NULL;
342
Guido van Rossumc222ec21999-02-23 00:00:10 +0000343 fmtlen = strlen(fmt);
344
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000345 /* I hate these functions that presume you know how big the output
346 * will be ahead of time...
347 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000348 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000349 outbuf = malloc(i);
350 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000351 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000352 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000353 buflen = strftime(outbuf, i, fmt, &buf);
354 if (buflen > 0 || i >= 256 * fmtlen) {
355 /* If the buffer is 256 times as long as the format,
356 it's probably not failing for lack of room!
357 More likely, the format yields an empty result,
358 e.g. an empty format, or %Z when the timezone
359 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000360 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000361 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000362 free(outbuf);
363 return ret;
364 }
365 free(outbuf);
366 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000367}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000368
369static char strftime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000370"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000371\n\
372Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000373See the library reference manual for formatting codes. When the time tuple\n\
374is not present, current time as returned by localtime() is used.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000375#endif /* HAVE_STRFTIME */
376
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000377#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000378
379#if 0
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000380/* Enable this if it's not declared in <time.h> */
381extern char *strptime(const char *, const char *, struct tm *);
Fred Drakeaff60182000-05-09 19:52:40 +0000382#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000383
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000384static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000385time_strptime(PyObject *self, PyObject *args)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000386{
387 struct tm tm;
388 char *fmt = "%a %b %d %H:%M:%S %Y";
389 char *buf;
390 char *s;
391
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000392 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
393 return NULL;
Thomas Wouters334fb892000-07-25 12:56:38 +0000394 memset((void *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000395 s = strptime(buf, fmt, &tm);
396 if (s == NULL) {
397 PyErr_SetString(PyExc_ValueError, "format mismatch");
398 return NULL;
399 }
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000400 while (*s && isspace(Py_CHARMASK(*s)))
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000401 s++;
402 if (*s) {
403 PyErr_Format(PyExc_ValueError,
404 "unconverted data remains: '%.400s'", s);
405 return NULL;
406 }
407 return tmtotuple(&tm);
408}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000409
410static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000411"strptime(string, format) -> tuple\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000412Parse a string to a time tuple according to a format specification.\n\
413See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000414#endif /* HAVE_STRPTIME */
415
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000416static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000417time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000418{
Thomas Woutersfe385252001-01-19 23:16:56 +0000419 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000420 struct tm buf;
421 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000422 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000423 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000424 if (tup == NULL) {
425 time_t tt = time(NULL);
426 buf = *localtime(&tt);
427 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000428 return NULL;
429 p = asctime(&buf);
430 if (p[24] == '\n')
431 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000432 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000433}
434
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000435static char asctime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000436"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000437\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000438Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
439When the time tuple is not present, current time as returned by localtime()\n\
440is used.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000441
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000442static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000443time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000444{
445 double dt;
446 time_t tt;
447 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000448
449 if (PyTuple_Size(args) == 0)
450 tt = time(NULL);
451 else {
452 if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
453 return NULL;
454 tt = (time_t)dt;
455 }
Jack Jansenee398fa2000-07-03 21:37:27 +0000456#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000457 tt = tt + GUSI_TO_MSL_EPOCH;
458#endif
Guido van Rossum9e90a671993-06-24 11:10:19 +0000459 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000460 if (p == NULL) {
461 PyErr_SetString(PyExc_ValueError, "unconvertible time");
462 return NULL;
463 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000464 if (p[24] == '\n')
465 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000466 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000467}
468
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000469static char ctime_doc[] =
470"ctime(seconds) -> string\n\
471\n\
472Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000473This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
474not present, current time as returned by localtime() is used.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000475
Guido van Rossum60cd8131998-03-06 17:16:21 +0000476#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000477static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000478time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000479{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000480 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000481 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000482 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000483 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000484 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000485 tt = time(&tt);
486 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000487 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000488 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000489 tt = mktime(&buf);
490 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000491 PyErr_SetString(PyExc_OverflowError,
492 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000493 return NULL;
494 }
Jack Jansen63596ae2000-12-12 22:42:30 +0000495#if defined(macintosh) && defined(USE_GUSI211)
Guido van Rossumc410e922000-04-26 20:40:13 +0000496 tt = tt - GUSI_TO_MSL_EPOCH;
497#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000498 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000499}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000500
501static char mktime_doc[] =
502"mktime(tuple) -> floating point number\n\
503\n\
504Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000505#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000506
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000507static PyMethodDef time_methods[] = {
Thomas Woutersfe385252001-01-19 23:16:56 +0000508 {"time", time_time, METH_VARARGS, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000509#ifdef HAVE_CLOCK
Thomas Woutersfe385252001-01-19 23:16:56 +0000510 {"clock", time_clock, METH_VARARGS, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000511#endif
Thomas Woutersfe385252001-01-19 23:16:56 +0000512 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
513 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
514 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000515 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
Thomas Woutersfe385252001-01-19 23:16:56 +0000516 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000517#ifdef HAVE_MKTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000518 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000519#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000520#ifdef HAVE_STRFTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000521 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000522#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000523#ifdef HAVE_STRPTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000524 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000525#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000526 {NULL, NULL} /* sentinel */
527};
528
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000529static void
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000530ins(PyObject *d, char *name, PyObject *v)
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000531{
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000532 /* Don't worry too much about errors, they'll be caught by the
533 * caller of inittime().
534 */
535 if (v)
536 PyDict_SetItemString(d, name, v);
537 Py_XDECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000538}
539
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000540
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000541static char module_doc[] =
542"This module provides various functions to manipulate time values.\n\
543\n\
544There are two standard representations of time. One is the number\n\
545of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
546or a floating point number (to represent fractions of seconds).\n\
547The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
548The actual value can be retrieved by calling gmtime(0).\n\
549\n\
550The other representation is a tuple of 9 integers giving local time.\n\
551The tuple items are:\n\
552 year (four digits, e.g. 1998)\n\
553 month (1-12)\n\
554 day (1-31)\n\
555 hours (0-23)\n\
556 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000557 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000558 weekday (0-6, Monday is 0)\n\
559 Julian day (day in the year, 1-366)\n\
560 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
561If the DST flag is 0, the time is given in the regular time zone;\n\
562if it is 1, the time is given in the DST time zone;\n\
563if it is -1, mktime() should guess based on the date and time.\n\
564\n\
565Variables:\n\
566\n\
567timezone -- difference in seconds between UTC and local standard time\n\
568altzone -- difference in seconds between UTC and local DST time\n\
569daylight -- whether local time should reflect DST\n\
570tzname -- tuple of (standard time zone name, DST time zone name)\n\
571\n\
572Functions:\n\
573\n\
574time() -- return current time in seconds since the Epoch as a float\n\
575clock() -- return CPU time since process start as a float\n\
576sleep() -- delay for a number of seconds given as a float\n\
577gmtime() -- convert seconds since Epoch to UTC tuple\n\
578localtime() -- convert seconds since Epoch to local time tuple\n\
579asctime() -- convert time tuple to string\n\
580ctime() -- convert time in seconds to string\n\
581mktime() -- convert local time tuple to seconds since Epoch\n\
582strftime() -- convert time tuple to string according to format specification\n\
583strptime() -- parse string to time tuple according to format specification\n\
584";
585
586
Guido van Rossum3886bb61998-12-04 18:50:17 +0000587DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000588inittime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000590 PyObject *m, *d;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000591 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000592 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000593 d = PyModule_GetDict(m);
Guido van Rossumc2068731998-10-07 16:35:25 +0000594 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
595 p = getenv("PYTHONY2K");
596 ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
597 /* Squirrel away the module's dictionary for the y2k check */
598 Py_INCREF(d);
599 moddict = d;
Guido van Rossumea424e11999-04-23 20:59:05 +0000600#if defined(HAVE_TZNAME) && !defined(__GLIBC__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000601 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000602#ifdef PYOS_OS2
603 ins(d, "timezone", PyInt_FromLong((long)_timezone));
604#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000605 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossum26452411998-09-28 22:07:11 +0000606#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000607#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000608 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000609#else
Guido van Rossum26452411998-09-28 22:07:11 +0000610#ifdef PYOS_OS2
611 ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
612#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000613 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossum26452411998-09-28 22:07:11 +0000614#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000615#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000616 ins(d, "daylight", PyInt_FromLong((long)daylight));
617 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossumea424e11999-04-23 20:59:05 +0000618#else /* !HAVE_TZNAME || __GLIBC__ */
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000619#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000620 {
621#define YEAR ((time_t)((365 * 24 + 6) * 3600))
622 time_t t;
623 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000624 long janzone, julyzone;
625 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000626 t = (time((time_t *)0) / YEAR) * YEAR;
627 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000628 janzone = -p->tm_gmtoff;
629 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
630 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000631 t += YEAR/2;
632 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000633 julyzone = -p->tm_gmtoff;
634 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
635 julyname[9] = '\0';
636
637 if( janzone < julyzone ) {
638 /* DST is reversed in the southern hemisphere */
639 ins(d, "timezone", PyInt_FromLong(julyzone));
640 ins(d, "altzone", PyInt_FromLong(janzone));
641 ins(d, "daylight",
642 PyInt_FromLong((long)(janzone != julyzone)));
643 ins(d, "tzname",
644 Py_BuildValue("(zz)", julyname, janname));
645 } else {
646 ins(d, "timezone", PyInt_FromLong(janzone));
647 ins(d, "altzone", PyInt_FromLong(julyzone));
648 ins(d, "daylight",
649 PyInt_FromLong((long)(janzone != julyzone)));
650 ins(d, "tzname",
651 Py_BuildValue("(zz)", janname, julyname));
652 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000653 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000654#else
655#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000656 /* The only thing we can obtain is the current timezone
657 ** (and whether dst is currently _active_, but that is not what
658 ** we're looking for:-( )
659 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000660 initmactimezone();
661 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000662 ins(d, "altzone", PyInt_FromLong(timezone));
663 ins(d, "daylight", PyInt_FromLong((long)0));
664 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000665#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000666#endif /* HAVE_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000667#ifdef __CYGWIN__
668 tzset();
669 ins(d, "timezone", PyInt_FromLong(_timezone));
670 ins(d, "altzone", PyInt_FromLong(_timezone));
671 ins(d, "daylight", PyInt_FromLong(_daylight));
672 ins(d, "tzname", Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
673#endif /* __CYGWIN__ */
Guido van Rossumea424e11999-04-23 20:59:05 +0000674#endif /* !HAVE_TZNAME || __GLIBC__ */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000675}
676
677
Guido van Rossumb6775db1994-08-01 11:34:53 +0000678/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000679
Guido van Rossumb6775db1994-08-01 11:34:53 +0000680static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000681floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000682{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000683 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000684 (1) gettimeofday() -- resolution in microseconds
685 (2) ftime() -- resolution in milliseconds
686 (3) time() -- resolution in seconds
687 In all cases the return value is a float in seconds.
688 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
689 fail, so we fall back on ftime() or time().
690 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000691#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000692 {
693 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000694#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000695 if (gettimeofday(&t) == 0)
696 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000697#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000698 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
699 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000700#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000701 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000702#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000703 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000704#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000705 struct timeb t;
706 ftime(&t);
707 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000708#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000709 time_t secs;
710 time(&secs);
711 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000712#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000713 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000714}
715
Guido van Rossumb6775db1994-08-01 11:34:53 +0000716
717/* Implement floatsleep() for various platforms.
718 When interrupted (or when another error occurs), return -1 and
719 set an exception; else return 0. */
720
721static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000722floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000723{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000724/* XXX Should test for MS_WIN32 first! */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000725#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000726 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000727 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000728 frac = fmod(secs, 1.0);
729 secs = floor(secs);
730 t.tv_sec = (long)secs;
731 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000732 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000733 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000734#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000735 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000736#else
737 if (1) {
738#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000739 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000740 PyErr_SetFromErrno(PyExc_IOError);
741 return -1;
742 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000743 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000744 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000745#else /* !HAVE_SELECT || __BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000746#ifdef macintosh
747#define MacTicks (* (long *)0x16A)
748 long deadline;
749 deadline = MacTicks + (long)(secs * 60.0);
750 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000751 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000752 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000753 return -1;
754 }
755#else /* !macintosh */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000756#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000757 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000758 Py_BEGIN_ALLOW_THREADS
Guido van Rossum48a680c2001-03-02 06:34:14 +0000759#ifndef RISCOS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000760 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum48a680c2001-03-02 06:34:14 +0000761#endif
Guido van Rossum8607ae21997-11-03 22:04:46 +0000762 Py_END_ALLOW_THREADS
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000763#else /* !__WATCOMC__ || __QNX__ */
Guido van Rossume22e6441993-07-09 10:51:31 +0000764#ifdef MSDOS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000765 struct timeb t1, t2;
766 double frac;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000767 extern double fmod(double, double);
768 extern double floor(double);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000769 if (secs <= 0.0)
770 return;
771 frac = fmod(secs, 1.0);
772 secs = floor(secs);
773 ftime(&t1);
774 t2.time = t1.time + (int)secs;
775 t2.millitm = t1.millitm + (int)(frac*1000.0);
776 while (t2.millitm >= 1000) {
777 t2.time++;
778 t2.millitm -= 1000;
779 }
780 for (;;) {
781#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000782 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000783 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000784 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000785#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000786 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000787 return -1;
788 ftime(&t1);
789 if (t1.time > t2.time ||
790 t1.time == t2.time && t1.millitm >= t2.millitm)
791 break;
792 }
793#else /* !MSDOS */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000794#ifdef MS_WIN32
Fred Drake0e123952000-06-29 21:31:02 +0000795 {
796 double millisecs = secs * 1000.0;
797 if (millisecs > (double)ULONG_MAX) {
798 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
799 return -1;
800 }
801 /* XXX Can't interrupt this sleep */
802 Py_BEGIN_ALLOW_THREADS
803 Sleep((unsigned long)millisecs);
804 Py_END_ALLOW_THREADS
805 }
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000806#else /* !MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000807#ifdef PYOS_OS2
808 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000809 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000810 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000811 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000812 PyErr_SetFromErrno(PyExc_IOError);
813 return -1;
814 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000815 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000816#else /* !PYOS_OS2 */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000817#ifdef __BEOS__
818 /* This sleep *CAN BE* interrupted. */
819 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000820 if( secs <= 0.0 ) {
821 return;
822 }
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000823
Guido van Rossumbcc20741998-08-04 22:53:56 +0000824 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000825 /* BeOS snooze() is in microseconds... */
826 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000827 Py_BLOCK_THREADS
828 PyErr_SetFromErrno( PyExc_IOError );
829 return -1;
830 }
831 Py_END_ALLOW_THREADS
832 }
833#else /* !__BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000834 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000835 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000836 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000837 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000838#endif /* !__BEOS__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000839#endif /* !PYOS_OS2 */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000840#endif /* !MS_WIN32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000841#endif /* !MSDOS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000842#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000843#endif /* !macintosh */
844#endif /* !HAVE_SELECT */
845 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000846}