blob: 116b377e8ef7459d770f1927f029ff6e5a16c7b6 [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>
11#ifdef USE_GUSI2
12/* 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;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000112 if (!PyArg_NoArgs(args))
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{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000141 if (!PyArg_NoArgs(args))
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
156 if (!PyArg_NoArgs(args))
157 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;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000195 if (!PyArg_Parse(args, "d", &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;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000247 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000248 return NULL;
249 return time_convert((time_t)when, gmtime);
250}
251
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000252static char gmtime_doc[] =
253"gmtime(seconds) -> tuple\n\
254\n\
255Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT).";
256
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000257static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000258time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000259{
260 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000261 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000262 return NULL;
263 return time_convert((time_t)when, localtime);
264}
265
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000266static char localtime_doc[] =
267"localtime(seconds) -> tuple\n\
268Convert seconds since the Epoch to a time tuple expressing local time.";
269
Guido van Rossum9e90a671993-06-24 11:10:19 +0000270static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000271gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000272{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000273 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000274 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000275
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000276 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000277 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000278 &p->tm_mon,
279 &p->tm_mday,
280 &p->tm_hour,
281 &p->tm_min,
282 &p->tm_sec,
283 &p->tm_wday,
284 &p->tm_yday,
285 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000286 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000287 if (y < 1900) {
288 PyObject *accept = PyDict_GetItemString(moddict,
289 "accept2dyear");
290 if (accept == NULL || !PyInt_Check(accept) ||
291 PyInt_AsLong(accept) == 0) {
292 PyErr_SetString(PyExc_ValueError,
293 "year >= 1900 required");
294 return 0;
295 }
296 if (69 <= y && y <= 99)
297 y += 1900;
298 else if (0 <= y && y <= 68)
299 y += 2000;
300 else {
301 PyErr_SetString(PyExc_ValueError,
302 "year out of range (00-99, 1900-*)");
303 return 0;
304 }
305 }
306 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000307 p->tm_mon--;
308 p->tm_wday = (p->tm_wday + 1) % 7;
309 p->tm_yday--;
310 return 1;
311}
312
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000313#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000314static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000315time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000316{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000317 PyObject *tup;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000318 struct tm buf;
319 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000320 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000321 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000322 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000323
Thomas Wouters334fb892000-07-25 12:56:38 +0000324 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000325
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000326 if (!PyArg_ParseTuple(args, "sO:strftime", &fmt, &tup)
327 || !gettmarg(tup, &buf))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000328 return NULL;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000329 fmtlen = strlen(fmt);
330
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000331 /* I hate these functions that presume you know how big the output
332 * will be ahead of time...
333 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000334 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000335 outbuf = malloc(i);
336 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000337 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000338 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000339 buflen = strftime(outbuf, i, fmt, &buf);
340 if (buflen > 0 || i >= 256 * fmtlen) {
341 /* If the buffer is 256 times as long as the format,
342 it's probably not failing for lack of room!
343 More likely, the format yields an empty result,
344 e.g. an empty format, or %Z when the timezone
345 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000346 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000347 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000348 free(outbuf);
349 return ret;
350 }
351 free(outbuf);
352 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000353}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000354
355static char strftime_doc[] =
356"strftime(format, tuple) -> string\n\
357\n\
358Convert a time tuple to a string according to a format specification.\n\
359See the library reference manual for formatting codes.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000360#endif /* HAVE_STRFTIME */
361
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000362#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000363
364#if 0
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000365/* Enable this if it's not declared in <time.h> */
366extern char *strptime(const char *, const char *, struct tm *);
Fred Drakeaff60182000-05-09 19:52:40 +0000367#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000368
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000369static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000370time_strptime(PyObject *self, PyObject *args)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000371{
372 struct tm tm;
373 char *fmt = "%a %b %d %H:%M:%S %Y";
374 char *buf;
375 char *s;
376
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000377 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
378 return NULL;
Thomas Wouters334fb892000-07-25 12:56:38 +0000379 memset((void *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000380 s = strptime(buf, fmt, &tm);
381 if (s == NULL) {
382 PyErr_SetString(PyExc_ValueError, "format mismatch");
383 return NULL;
384 }
385 while (*s && isspace(*s))
386 s++;
387 if (*s) {
388 PyErr_Format(PyExc_ValueError,
389 "unconverted data remains: '%.400s'", s);
390 return NULL;
391 }
392 return tmtotuple(&tm);
393}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000394
395static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000396"strptime(string, format) -> tuple\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000397Parse a string to a time tuple according to a format specification.\n\
398See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000399#endif /* HAVE_STRPTIME */
400
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000401static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000402time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000403{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000404 PyObject *tup;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000405 struct tm buf;
406 char *p;
Guido van Rossum43713e52000-02-29 13:59:29 +0000407 if (!PyArg_ParseTuple(args, "O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000408 return NULL;
409 if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000410 return NULL;
411 p = asctime(&buf);
412 if (p[24] == '\n')
413 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000414 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000415}
416
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000417static char asctime_doc[] =
418"asctime(tuple) -> string\n\
419\n\
420Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.";
421
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000422static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000423time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000424{
425 double dt;
426 time_t tt;
427 char *p;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000428 if (!PyArg_Parse(args, "d", &dt))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000429 return NULL;
Guido van Rossumcac6c721996-09-06 13:34:02 +0000430 tt = (time_t)dt;
Jack Jansenee398fa2000-07-03 21:37:27 +0000431#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000432 tt = tt + GUSI_TO_MSL_EPOCH;
433#endif
Guido van Rossum9e90a671993-06-24 11:10:19 +0000434 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000435 if (p == NULL) {
436 PyErr_SetString(PyExc_ValueError, "unconvertible time");
437 return NULL;
438 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000439 if (p[24] == '\n')
440 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000441 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000442}
443
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000444static char ctime_doc[] =
445"ctime(seconds) -> string\n\
446\n\
447Convert a time in seconds since the Epoch to a string in local time.\n\
448This is equivalent to asctime(localtime(seconds)).";
449
Guido van Rossum60cd8131998-03-06 17:16:21 +0000450#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000451static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000452time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000453{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000454 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000455 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000456 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000457 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000458 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000459 tt = time(&tt);
460 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000461 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000462 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000463 tt = mktime(&buf);
464 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000465 PyErr_SetString(PyExc_OverflowError,
466 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000467 return NULL;
468 }
Guido van Rossumc410e922000-04-26 20:40:13 +0000469#if defined(macintosh) && defined(USE_GUSI2)
470 tt = tt - GUSI_TO_MSL_EPOCH;
471#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000472 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000473}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000474
475static char mktime_doc[] =
476"mktime(tuple) -> floating point number\n\
477\n\
478Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000479#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000480
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000481static PyMethodDef time_methods[] = {
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000482 {"time", time_time, METH_OLDARGS, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000483#ifdef HAVE_CLOCK
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000484 {"clock", time_clock, METH_OLDARGS, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000485#endif
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000486 {"sleep", time_sleep, METH_OLDARGS, sleep_doc},
487 {"gmtime", time_gmtime, METH_OLDARGS, gmtime_doc},
488 {"localtime", time_localtime, METH_OLDARGS, localtime_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000489 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000490 {"ctime", time_ctime, METH_OLDARGS, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000491#ifdef HAVE_MKTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000492 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000493#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000494#ifdef HAVE_STRFTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000495 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000496#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000497#ifdef HAVE_STRPTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000498 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000499#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000500 {NULL, NULL} /* sentinel */
501};
502
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000503static void
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000504ins(PyObject *d, char *name, PyObject *v)
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000505{
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000506 /* Don't worry too much about errors, they'll be caught by the
507 * caller of inittime().
508 */
509 if (v)
510 PyDict_SetItemString(d, name, v);
511 Py_XDECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000512}
513
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000514
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000515static char module_doc[] =
516"This module provides various functions to manipulate time values.\n\
517\n\
518There are two standard representations of time. One is the number\n\
519of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
520or a floating point number (to represent fractions of seconds).\n\
521The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
522The actual value can be retrieved by calling gmtime(0).\n\
523\n\
524The other representation is a tuple of 9 integers giving local time.\n\
525The tuple items are:\n\
526 year (four digits, e.g. 1998)\n\
527 month (1-12)\n\
528 day (1-31)\n\
529 hours (0-23)\n\
530 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000531 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000532 weekday (0-6, Monday is 0)\n\
533 Julian day (day in the year, 1-366)\n\
534 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
535If the DST flag is 0, the time is given in the regular time zone;\n\
536if it is 1, the time is given in the DST time zone;\n\
537if it is -1, mktime() should guess based on the date and time.\n\
538\n\
539Variables:\n\
540\n\
541timezone -- difference in seconds between UTC and local standard time\n\
542altzone -- difference in seconds between UTC and local DST time\n\
543daylight -- whether local time should reflect DST\n\
544tzname -- tuple of (standard time zone name, DST time zone name)\n\
545\n\
546Functions:\n\
547\n\
548time() -- return current time in seconds since the Epoch as a float\n\
549clock() -- return CPU time since process start as a float\n\
550sleep() -- delay for a number of seconds given as a float\n\
551gmtime() -- convert seconds since Epoch to UTC tuple\n\
552localtime() -- convert seconds since Epoch to local time tuple\n\
553asctime() -- convert time tuple to string\n\
554ctime() -- convert time in seconds to string\n\
555mktime() -- convert local time tuple to seconds since Epoch\n\
556strftime() -- convert time tuple to string according to format specification\n\
557strptime() -- parse string to time tuple according to format specification\n\
558";
559
560
Guido van Rossum3886bb61998-12-04 18:50:17 +0000561DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000562inittime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000564 PyObject *m, *d;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000565 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000566 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000567 d = PyModule_GetDict(m);
Guido van Rossumc2068731998-10-07 16:35:25 +0000568 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
569 p = getenv("PYTHONY2K");
570 ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
571 /* Squirrel away the module's dictionary for the y2k check */
572 Py_INCREF(d);
573 moddict = d;
Guido van Rossumea424e11999-04-23 20:59:05 +0000574#if defined(HAVE_TZNAME) && !defined(__GLIBC__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000575 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000576#ifdef PYOS_OS2
577 ins(d, "timezone", PyInt_FromLong((long)_timezone));
578#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000579 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossum26452411998-09-28 22:07:11 +0000580#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000581#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000582 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000583#else
Guido van Rossum26452411998-09-28 22:07:11 +0000584#ifdef PYOS_OS2
585 ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
586#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000587 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossum26452411998-09-28 22:07:11 +0000588#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000589#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000590 ins(d, "daylight", PyInt_FromLong((long)daylight));
591 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossumea424e11999-04-23 20:59:05 +0000592#else /* !HAVE_TZNAME || __GLIBC__ */
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000593#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000594 {
595#define YEAR ((time_t)((365 * 24 + 6) * 3600))
596 time_t t;
597 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000598 long janzone, julyzone;
599 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000600 t = (time((time_t *)0) / YEAR) * YEAR;
601 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000602 janzone = -p->tm_gmtoff;
603 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
604 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000605 t += YEAR/2;
606 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000607 julyzone = -p->tm_gmtoff;
608 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
609 julyname[9] = '\0';
610
611 if( janzone < julyzone ) {
612 /* DST is reversed in the southern hemisphere */
613 ins(d, "timezone", PyInt_FromLong(julyzone));
614 ins(d, "altzone", PyInt_FromLong(janzone));
615 ins(d, "daylight",
616 PyInt_FromLong((long)(janzone != julyzone)));
617 ins(d, "tzname",
618 Py_BuildValue("(zz)", julyname, janname));
619 } else {
620 ins(d, "timezone", PyInt_FromLong(janzone));
621 ins(d, "altzone", PyInt_FromLong(julyzone));
622 ins(d, "daylight",
623 PyInt_FromLong((long)(janzone != julyzone)));
624 ins(d, "tzname",
625 Py_BuildValue("(zz)", janname, julyname));
626 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000627 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000628#else
629#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000630 /* The only thing we can obtain is the current timezone
631 ** (and whether dst is currently _active_, but that is not what
632 ** we're looking for:-( )
633 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000634 initmactimezone();
635 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000636 ins(d, "altzone", PyInt_FromLong(timezone));
637 ins(d, "daylight", PyInt_FromLong((long)0));
638 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000639#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000640#endif /* HAVE_TM_ZONE */
Guido van Rossumea424e11999-04-23 20:59:05 +0000641#endif /* !HAVE_TZNAME || __GLIBC__ */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642}
643
644
Guido van Rossumb6775db1994-08-01 11:34:53 +0000645/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000646
Guido van Rossumb6775db1994-08-01 11:34:53 +0000647static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000648floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000650 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000651 (1) gettimeofday() -- resolution in microseconds
652 (2) ftime() -- resolution in milliseconds
653 (3) time() -- resolution in seconds
654 In all cases the return value is a float in seconds.
655 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
656 fail, so we fall back on ftime() or time().
657 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000658#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000659 {
660 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000661#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000662 if (gettimeofday(&t) == 0)
663 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000664#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000665 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
666 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000667#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000668 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000669#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000670 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000671#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000672 struct timeb t;
673 ftime(&t);
674 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000675#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000676 time_t secs;
677 time(&secs);
678 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000679#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000680 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000681}
682
Guido van Rossumb6775db1994-08-01 11:34:53 +0000683
684/* Implement floatsleep() for various platforms.
685 When interrupted (or when another error occurs), return -1 and
686 set an exception; else return 0. */
687
688static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000689floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000690{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000691/* XXX Should test for MS_WIN32 first! */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000692#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000693 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000694 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000695 frac = fmod(secs, 1.0);
696 secs = floor(secs);
697 t.tv_sec = (long)secs;
698 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000699 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000700 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000701#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000702 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000703#else
704 if (1) {
705#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000706 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000707 PyErr_SetFromErrno(PyExc_IOError);
708 return -1;
709 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000710 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000711 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000712#else /* !HAVE_SELECT || __BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000713#ifdef macintosh
714#define MacTicks (* (long *)0x16A)
715 long deadline;
716 deadline = MacTicks + (long)(secs * 60.0);
717 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000718 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000719 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000720 return -1;
721 }
722#else /* !macintosh */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000723#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000724 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000725 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000726 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000727 Py_END_ALLOW_THREADS
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000728#else /* !__WATCOMC__ || __QNX__ */
Guido van Rossume22e6441993-07-09 10:51:31 +0000729#ifdef MSDOS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000730 struct timeb t1, t2;
731 double frac;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000732 extern double fmod(double, double);
733 extern double floor(double);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000734 if (secs <= 0.0)
735 return;
736 frac = fmod(secs, 1.0);
737 secs = floor(secs);
738 ftime(&t1);
739 t2.time = t1.time + (int)secs;
740 t2.millitm = t1.millitm + (int)(frac*1000.0);
741 while (t2.millitm >= 1000) {
742 t2.time++;
743 t2.millitm -= 1000;
744 }
745 for (;;) {
746#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000747 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000748 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000749 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000750#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000751 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000752 return -1;
753 ftime(&t1);
754 if (t1.time > t2.time ||
755 t1.time == t2.time && t1.millitm >= t2.millitm)
756 break;
757 }
758#else /* !MSDOS */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000759#ifdef MS_WIN32
Fred Drake0e123952000-06-29 21:31:02 +0000760 {
761 double millisecs = secs * 1000.0;
762 if (millisecs > (double)ULONG_MAX) {
763 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
764 return -1;
765 }
766 /* XXX Can't interrupt this sleep */
767 Py_BEGIN_ALLOW_THREADS
768 Sleep((unsigned long)millisecs);
769 Py_END_ALLOW_THREADS
770 }
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000771#else /* !MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000772#ifdef PYOS_OS2
773 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000774 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000775 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000776 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000777 PyErr_SetFromErrno(PyExc_IOError);
778 return -1;
779 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000780 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000781#else /* !PYOS_OS2 */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000782#ifdef __BEOS__
783 /* This sleep *CAN BE* interrupted. */
784 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000785 if( secs <= 0.0 ) {
786 return;
787 }
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000788
Guido van Rossumbcc20741998-08-04 22:53:56 +0000789 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000790 /* BeOS snooze() is in microseconds... */
791 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000792 Py_BLOCK_THREADS
793 PyErr_SetFromErrno( PyExc_IOError );
794 return -1;
795 }
796 Py_END_ALLOW_THREADS
797 }
798#else /* !__BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000799 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000800 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000801 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000802 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000803#endif /* !__BEOS__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000804#endif /* !PYOS_OS2 */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000805#endif /* !MS_WIN32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000806#endif /* !MSDOS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000807#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000808#endif /* !macintosh */
809#endif /* !HAVE_SELECT */
810 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000811}