blob: fa40762582bb3c0062747f0a60b0b2ee98ee20a3 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* Time module */
12
Barry Warsaw9a2a8a81996-12-06 23:32:14 +000013#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000014
Guido van Rossum87ce7bb1998-06-09 16:30:31 +000015#include <ctype.h>
16
Guido van Rossum6d946f91992-08-14 13:49:30 +000017#ifdef macintosh
Guido van Rossumb6775db1994-08-01 11:34:53 +000018#include <time.h>
Guido van Rossumc410e922000-04-26 20:40:13 +000019#include <OSUtils.h>
20#ifdef USE_GUSI2
21/* GUSI, the I/O library which has the time() function and such uses the
22** Mac epoch of 1904. MSL, the C library which has localtime() and so uses
23** the ANSI epoch of 1900.
24*/
25#define GUSI_TO_MSL_EPOCH (4*365*24*60*60)
26#endif /* USE_GUSI2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000027#else
28#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000029#endif
30
Guido van Rossumb6775db1994-08-01 11:34:53 +000031#ifdef QUICKWIN
32#include <io.h>
33#endif
34
35#ifdef HAVE_UNISTD_H
Guido van Rossum2762f251992-03-27 17:22:13 +000036#include <unistd.h>
37#endif
38
Guido van Rossumb6775db1994-08-01 11:34:53 +000039#ifdef HAVE_FTIME
40#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000041#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000042extern int ftime(struct timeb *);
Guido van Rossum52174571996-12-09 18:38:52 +000043#endif /* MS_WINDOWS */
44#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045
Guido van Rossum7bf22de1997-12-02 20:34:19 +000046#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000047#include <i86.h>
48#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000049#ifdef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +000050#include <windows.h>
Guido van Rossumb2fb3641996-09-07 00:47:35 +000051#ifdef MS_WIN16
52/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000053#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000054#define tzname _tzname
55#define daylight _daylight
56#define altzone _altzone
Guido van Rossumb2fb3641996-09-07 00:47:35 +000057#endif /* MS_WIN16 */
Guido van Rossumcac6c721996-09-06 13:34:02 +000058#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000059#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000060
Fred Drakedfb4ebd2000-06-29 20:56:28 +000061#if defined(MS_WIN32) && !defined(MS_WIN64)
62/* Win32 has better clock replacement
63 XXX Win64 does not yet, but might when the platform matures. */
Guido van Rossum3917c221997-04-02 05:35:28 +000064#include <largeint.h>
65#undef HAVE_CLOCK /* We have our own version down below */
Fred Drakedfb4ebd2000-06-29 20:56:28 +000066#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +000067
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000068#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000069#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000070#endif
71
Guido van Rossumbcc20741998-08-04 22:53:56 +000072#ifdef __BEOS__
73/* For bigtime_t, snooze(). - [cjh] */
74#include <support/SupportDefs.h>
75#include <kernel/OS.h>
Guido van Rossumd3eb5771999-03-09 16:07:23 +000076#ifndef CLOCKS_PER_SEC
77/* C'mon, fix the bloody headers... - [cjh] */
78#define CLOCKS_PER_SEC 1000
79#endif
Guido van Rossumbcc20741998-08-04 22:53:56 +000080#endif
81
Guido van Rossum234f9421993-06-17 12:35:49 +000082/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000083static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000084static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000086/* For Y2K check */
87static PyObject *moddict;
88
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000089#ifdef macintosh
90/* Our own timezone. We have enough information to deduce whether
91** DST is on currently, but unfortunately we cannot put it to good
92** use because we don't know the rules (and that is needed to have
93** localtime() return correct tm_isdst values for times other than
94** the current time. So, we cop out and only tell the user the current
95** timezone.
96*/
97static long timezone;
98
99static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000100initmactimezone(void)
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000101{
102 MachineLocation loc;
103 long delta;
104
105 ReadLocation(&loc);
106
107 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
108 return;
109
110 delta = loc.u.gmtDelta & 0x00FFFFFF;
111
112 if (delta & 0x00800000)
113 delta |= 0xFF000000;
114
115 timezone = -delta;
116}
117#endif /* macintosh */
118
119
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000120static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000121time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000123 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000124 if (!PyArg_NoArgs(args))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000125 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000126 secs = floattime();
127 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000128 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000129 return NULL;
130 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000131 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000132}
133
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000134static char time_doc[] =
135"time() -> floating point number\n\
136\n\
137Return the current time in seconds since the Epoch.\n\
138Fractions of a second may be present if the system clock provides them.";
139
Guido van Rossumb6775db1994-08-01 11:34:53 +0000140#ifdef HAVE_CLOCK
141
142#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000143#ifdef CLK_TCK
144#define CLOCKS_PER_SEC CLK_TCK
145#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000146#define CLOCKS_PER_SEC 1000000
147#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000148#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000149
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000150static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000151time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000153 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000155 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000157#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000159#if defined(MS_WIN32) && !defined(MS_WIN64)
Guido van Rossum3917c221997-04-02 05:35:28 +0000160/* Due to Mark Hammond */
161static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000162time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000163{
164 static LARGE_INTEGER ctrStart;
165 static LARGE_INTEGER divisor = {0,0};
166 LARGE_INTEGER now, diff, rem;
167
168 if (!PyArg_NoArgs(args))
169 return NULL;
170
171 if (LargeIntegerEqualToZero(divisor)) {
172 QueryPerformanceCounter(&ctrStart);
173 if (!QueryPerformanceFrequency(&divisor) ||
174 LargeIntegerEqualToZero(divisor)) {
175 /* Unlikely to happen -
176 this works on all intel machines at least!
177 Revert to clock() */
178 return PyFloat_FromDouble(clock());
179 }
180 }
181 QueryPerformanceCounter(&now);
182 diff = LargeIntegerSubtract(now, ctrStart);
183 diff = LargeIntegerDivide(diff, divisor, &rem);
184 /* XXX - we assume both divide results fit in 32 bits. This is
185 true on Intels. First person who can afford a machine that
186 doesnt deserves to fix it :-)
187 */
188 return PyFloat_FromDouble((double)diff.LowPart +
189 ((double)rem.LowPart / (double)divisor.LowPart));
190}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000191
Guido van Rossum3917c221997-04-02 05:35:28 +0000192#define HAVE_CLOCK /* So it gets included in the methods */
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000193#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000194
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000195#ifdef HAVE_CLOCK
196static char clock_doc[] =
197"clock() -> floating point number\n\
198\n\
199Return the CPU time or real time since the start of the process or since\n\
200the first call to clock(). This has as much precision as the system records.";
201#endif
202
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000203static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000204time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000206 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000207 if (!PyArg_Parse(args, "d", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000209 if (floatsleep(secs) != 0)
210 return NULL;
211 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000212 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213}
214
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000215static char sleep_doc[] =
216"sleep(seconds)\n\
217\n\
218Delay execution for a given number of seconds. The argument may be\n\
219a floating point number for subsecond precision.";
220
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000221static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000222tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000223{
224 return Py_BuildValue("(iiiiiiiii)",
225 p->tm_year + 1900,
226 p->tm_mon + 1, /* Want January == 1 */
227 p->tm_mday,
228 p->tm_hour,
229 p->tm_min,
230 p->tm_sec,
231 (p->tm_wday + 6) % 7, /* Want Monday == 0 */
232 p->tm_yday + 1, /* Want January, 1 == 1 */
233 p->tm_isdst);
234}
235
236static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000237time_convert(time_t when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000238{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000239 struct tm *p;
240 errno = 0;
Jack Jansenee398fa2000-07-03 21:37:27 +0000241#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000242 when = when + GUSI_TO_MSL_EPOCH;
243#endif
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000244 p = function(&when);
245 if (p == NULL) {
246#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000247 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000248 errno = EINVAL;
249#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000250 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000251 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000252 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000253}
254
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000255static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000256time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000257{
258 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000259 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000260 return NULL;
261 return time_convert((time_t)when, gmtime);
262}
263
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000264static char gmtime_doc[] =
265"gmtime(seconds) -> tuple\n\
266\n\
267Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT).";
268
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000269static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000270time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000271{
272 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000273 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000274 return NULL;
275 return time_convert((time_t)when, localtime);
276}
277
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000278static char localtime_doc[] =
279"localtime(seconds) -> tuple\n\
280Convert seconds since the Epoch to a time tuple expressing local time.";
281
Guido van Rossum9e90a671993-06-24 11:10:19 +0000282static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000283gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000284{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000285 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000286 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000287
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000288 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000289 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000290 &p->tm_mon,
291 &p->tm_mday,
292 &p->tm_hour,
293 &p->tm_min,
294 &p->tm_sec,
295 &p->tm_wday,
296 &p->tm_yday,
297 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000298 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000299 if (y < 1900) {
300 PyObject *accept = PyDict_GetItemString(moddict,
301 "accept2dyear");
302 if (accept == NULL || !PyInt_Check(accept) ||
303 PyInt_AsLong(accept) == 0) {
304 PyErr_SetString(PyExc_ValueError,
305 "year >= 1900 required");
306 return 0;
307 }
308 if (69 <= y && y <= 99)
309 y += 1900;
310 else if (0 <= y && y <= 68)
311 y += 2000;
312 else {
313 PyErr_SetString(PyExc_ValueError,
314 "year out of range (00-99, 1900-*)");
315 return 0;
316 }
317 }
318 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000319 p->tm_mon--;
320 p->tm_wday = (p->tm_wday + 1) % 7;
321 p->tm_yday--;
322 return 1;
323}
324
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000325#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000326static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000327time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000328{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000329 PyObject *tup;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000330 struct tm buf;
331 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000332 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000333 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000334 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000335
Thomas Wouters334fb892000-07-25 12:56:38 +0000336 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000337
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000338 if (!PyArg_ParseTuple(args, "sO:strftime", &fmt, &tup)
339 || !gettmarg(tup, &buf))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000340 return NULL;
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[] =
368"strftime(format, tuple) -> string\n\
369\n\
370Convert a time tuple to a string according to a format specification.\n\
371See the library reference manual for formatting codes.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000372#endif /* HAVE_STRFTIME */
373
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000374#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000375
376#if 0
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000377/* Enable this if it's not declared in <time.h> */
378extern char *strptime(const char *, const char *, struct tm *);
Fred Drakeaff60182000-05-09 19:52:40 +0000379#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000380
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000381static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000382time_strptime(PyObject *self, PyObject *args)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000383{
384 struct tm tm;
385 char *fmt = "%a %b %d %H:%M:%S %Y";
386 char *buf;
387 char *s;
388
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000389 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
390 return NULL;
Thomas Wouters334fb892000-07-25 12:56:38 +0000391 memset((void *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000392 s = strptime(buf, fmt, &tm);
393 if (s == NULL) {
394 PyErr_SetString(PyExc_ValueError, "format mismatch");
395 return NULL;
396 }
397 while (*s && isspace(*s))
398 s++;
399 if (*s) {
400 PyErr_Format(PyExc_ValueError,
401 "unconverted data remains: '%.400s'", s);
402 return NULL;
403 }
404 return tmtotuple(&tm);
405}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000406
407static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000408"strptime(string, format) -> tuple\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000409Parse a string to a time tuple according to a format specification.\n\
410See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000411#endif /* HAVE_STRPTIME */
412
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000413static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000414time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000415{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000416 PyObject *tup;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000417 struct tm buf;
418 char *p;
Guido van Rossum43713e52000-02-29 13:59:29 +0000419 if (!PyArg_ParseTuple(args, "O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000420 return NULL;
421 if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000422 return NULL;
423 p = asctime(&buf);
424 if (p[24] == '\n')
425 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000426 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000427}
428
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000429static char asctime_doc[] =
430"asctime(tuple) -> string\n\
431\n\
432Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.";
433
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000434static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000435time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000436{
437 double dt;
438 time_t tt;
439 char *p;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000440 if (!PyArg_Parse(args, "d", &dt))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000441 return NULL;
Guido van Rossumcac6c721996-09-06 13:34:02 +0000442 tt = (time_t)dt;
Jack Jansenee398fa2000-07-03 21:37:27 +0000443#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000444 tt = tt + GUSI_TO_MSL_EPOCH;
445#endif
Guido van Rossum9e90a671993-06-24 11:10:19 +0000446 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000447 if (p == NULL) {
448 PyErr_SetString(PyExc_ValueError, "unconvertible time");
449 return NULL;
450 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000451 if (p[24] == '\n')
452 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000453 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000454}
455
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000456static char ctime_doc[] =
457"ctime(seconds) -> string\n\
458\n\
459Convert a time in seconds since the Epoch to a string in local time.\n\
460This is equivalent to asctime(localtime(seconds)).";
461
Guido van Rossum60cd8131998-03-06 17:16:21 +0000462#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000463static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000464time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000465{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000466 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000467 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000468 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000469 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000470 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000471 tt = time(&tt);
472 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000473 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000474 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000475 tt = mktime(&buf);
476 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000477 PyErr_SetString(PyExc_OverflowError,
478 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000479 return NULL;
480 }
Guido van Rossumc410e922000-04-26 20:40:13 +0000481#if defined(macintosh) && defined(USE_GUSI2)
482 tt = tt - GUSI_TO_MSL_EPOCH;
483#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000484 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000485}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000486
487static char mktime_doc[] =
488"mktime(tuple) -> floating point number\n\
489\n\
490Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000491#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000492
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000493static PyMethodDef time_methods[] = {
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000494 {"time", time_time, METH_OLDARGS, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000495#ifdef HAVE_CLOCK
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000496 {"clock", time_clock, METH_OLDARGS, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000497#endif
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000498 {"sleep", time_sleep, METH_OLDARGS, sleep_doc},
499 {"gmtime", time_gmtime, METH_OLDARGS, gmtime_doc},
500 {"localtime", time_localtime, METH_OLDARGS, localtime_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000501 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
Andrew M. Kuchlinga1abb722000-08-03 02:34:44 +0000502 {"ctime", time_ctime, METH_OLDARGS, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000503#ifdef HAVE_MKTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000504 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000505#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000506#ifdef HAVE_STRFTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000507 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000508#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000509#ifdef HAVE_STRPTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000510 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000511#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512 {NULL, NULL} /* sentinel */
513};
514
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000515static void
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000516ins(PyObject *d, char *name, PyObject *v)
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000517{
518 if (v == NULL)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000519 Py_FatalError("Can't initialize time module -- NULL value");
520 if (PyDict_SetItemString(d, name, v) != 0)
521 Py_FatalError(
Guido van Rossum52174571996-12-09 18:38:52 +0000522 "Can't initialize time module -- PyDict_SetItemString failed");
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000523 Py_DECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000524}
525
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000526static char module_doc[] =
527"This module provides various functions to manipulate time values.\n\
528\n\
529There are two standard representations of time. One is the number\n\
530of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
531or a floating point number (to represent fractions of seconds).\n\
532The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
533The actual value can be retrieved by calling gmtime(0).\n\
534\n\
535The other representation is a tuple of 9 integers giving local time.\n\
536The tuple items are:\n\
537 year (four digits, e.g. 1998)\n\
538 month (1-12)\n\
539 day (1-31)\n\
540 hours (0-23)\n\
541 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000542 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000543 weekday (0-6, Monday is 0)\n\
544 Julian day (day in the year, 1-366)\n\
545 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
546If the DST flag is 0, the time is given in the regular time zone;\n\
547if it is 1, the time is given in the DST time zone;\n\
548if it is -1, mktime() should guess based on the date and time.\n\
549\n\
550Variables:\n\
551\n\
552timezone -- difference in seconds between UTC and local standard time\n\
553altzone -- difference in seconds between UTC and local DST time\n\
554daylight -- whether local time should reflect DST\n\
555tzname -- tuple of (standard time zone name, DST time zone name)\n\
556\n\
557Functions:\n\
558\n\
559time() -- return current time in seconds since the Epoch as a float\n\
560clock() -- return CPU time since process start as a float\n\
561sleep() -- delay for a number of seconds given as a float\n\
562gmtime() -- convert seconds since Epoch to UTC tuple\n\
563localtime() -- convert seconds since Epoch to local time tuple\n\
564asctime() -- convert time tuple to string\n\
565ctime() -- convert time in seconds to string\n\
566mktime() -- convert local time tuple to seconds since Epoch\n\
567strftime() -- convert time tuple to string according to format specification\n\
568strptime() -- parse string to time tuple according to format specification\n\
569";
570
571
Guido van Rossum3886bb61998-12-04 18:50:17 +0000572DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000573inittime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000575 PyObject *m, *d;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000576 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000577 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000578 d = PyModule_GetDict(m);
Guido van Rossumc2068731998-10-07 16:35:25 +0000579 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
580 p = getenv("PYTHONY2K");
581 ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
582 /* Squirrel away the module's dictionary for the y2k check */
583 Py_INCREF(d);
584 moddict = d;
Guido van Rossumea424e11999-04-23 20:59:05 +0000585#if defined(HAVE_TZNAME) && !defined(__GLIBC__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000586 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000587#ifdef PYOS_OS2
588 ins(d, "timezone", PyInt_FromLong((long)_timezone));
589#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000590 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossum26452411998-09-28 22:07:11 +0000591#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000592#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000593 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000594#else
Guido van Rossum26452411998-09-28 22:07:11 +0000595#ifdef PYOS_OS2
596 ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
597#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000598 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossum26452411998-09-28 22:07:11 +0000599#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000600#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000601 ins(d, "daylight", PyInt_FromLong((long)daylight));
602 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossumea424e11999-04-23 20:59:05 +0000603#else /* !HAVE_TZNAME || __GLIBC__ */
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000604#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000605 {
606#define YEAR ((time_t)((365 * 24 + 6) * 3600))
607 time_t t;
608 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000609 long janzone, julyzone;
610 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000611 t = (time((time_t *)0) / YEAR) * YEAR;
612 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000613 janzone = -p->tm_gmtoff;
614 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
615 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000616 t += YEAR/2;
617 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000618 julyzone = -p->tm_gmtoff;
619 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
620 julyname[9] = '\0';
621
622 if( janzone < julyzone ) {
623 /* DST is reversed in the southern hemisphere */
624 ins(d, "timezone", PyInt_FromLong(julyzone));
625 ins(d, "altzone", PyInt_FromLong(janzone));
626 ins(d, "daylight",
627 PyInt_FromLong((long)(janzone != julyzone)));
628 ins(d, "tzname",
629 Py_BuildValue("(zz)", julyname, janname));
630 } else {
631 ins(d, "timezone", PyInt_FromLong(janzone));
632 ins(d, "altzone", PyInt_FromLong(julyzone));
633 ins(d, "daylight",
634 PyInt_FromLong((long)(janzone != julyzone)));
635 ins(d, "tzname",
636 Py_BuildValue("(zz)", janname, julyname));
637 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000638 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000639#else
640#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000641 /* The only thing we can obtain is the current timezone
642 ** (and whether dst is currently _active_, but that is not what
643 ** we're looking for:-( )
644 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000645 initmactimezone();
646 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000647 ins(d, "altzone", PyInt_FromLong(timezone));
648 ins(d, "daylight", PyInt_FromLong((long)0));
649 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000650#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000651#endif /* HAVE_TM_ZONE */
Guido van Rossumea424e11999-04-23 20:59:05 +0000652#endif /* !HAVE_TZNAME || __GLIBC__ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000653 if (PyErr_Occurred())
654 Py_FatalError("Can't initialize time module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000655}
656
657
Guido van Rossumb6775db1994-08-01 11:34:53 +0000658/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000659
Guido van Rossumb6775db1994-08-01 11:34:53 +0000660static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000661floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000662{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000663 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000664 (1) gettimeofday() -- resolution in microseconds
665 (2) ftime() -- resolution in milliseconds
666 (3) time() -- resolution in seconds
667 In all cases the return value is a float in seconds.
668 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
669 fail, so we fall back on ftime() or time().
670 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000671#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000672 {
673 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000674#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000675 if (gettimeofday(&t) == 0)
676 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000677#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000678 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
679 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000680#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000681 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000682#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000683 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000684#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000685 struct timeb t;
686 ftime(&t);
687 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000688#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000689 time_t secs;
690 time(&secs);
691 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000692#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000693 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000694}
695
Guido van Rossumb6775db1994-08-01 11:34:53 +0000696
697/* Implement floatsleep() for various platforms.
698 When interrupted (or when another error occurs), return -1 and
699 set an exception; else return 0. */
700
701static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000702floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000703{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000704/* XXX Should test for MS_WIN32 first! */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000705#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000706 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000707 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000708 frac = fmod(secs, 1.0);
709 secs = floor(secs);
710 t.tv_sec = (long)secs;
711 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000712 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000713 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000714#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000715 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000716#else
717 if (1) {
718#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000719 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000720 PyErr_SetFromErrno(PyExc_IOError);
721 return -1;
722 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000723 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000724 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000725#else /* !HAVE_SELECT || __BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000726#ifdef macintosh
727#define MacTicks (* (long *)0x16A)
728 long deadline;
729 deadline = MacTicks + (long)(secs * 60.0);
730 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000731 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000732 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000733 return -1;
734 }
735#else /* !macintosh */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000736#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000737 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000738 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000739 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000740 Py_END_ALLOW_THREADS
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000741#else /* !__WATCOMC__ || __QNX__ */
Guido van Rossume22e6441993-07-09 10:51:31 +0000742#ifdef MSDOS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000743 struct timeb t1, t2;
744 double frac;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000745 extern double fmod(double, double);
746 extern double floor(double);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000747 if (secs <= 0.0)
748 return;
749 frac = fmod(secs, 1.0);
750 secs = floor(secs);
751 ftime(&t1);
752 t2.time = t1.time + (int)secs;
753 t2.millitm = t1.millitm + (int)(frac*1000.0);
754 while (t2.millitm >= 1000) {
755 t2.time++;
756 t2.millitm -= 1000;
757 }
758 for (;;) {
759#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000760 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000761 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000762 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000763#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000764 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000765 return -1;
766 ftime(&t1);
767 if (t1.time > t2.time ||
768 t1.time == t2.time && t1.millitm >= t2.millitm)
769 break;
770 }
771#else /* !MSDOS */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000772#ifdef MS_WIN32
Fred Drake0e123952000-06-29 21:31:02 +0000773 {
774 double millisecs = secs * 1000.0;
775 if (millisecs > (double)ULONG_MAX) {
776 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
777 return -1;
778 }
779 /* XXX Can't interrupt this sleep */
780 Py_BEGIN_ALLOW_THREADS
781 Sleep((unsigned long)millisecs);
782 Py_END_ALLOW_THREADS
783 }
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000784#else /* !MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000785#ifdef PYOS_OS2
786 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000787 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000788 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000789 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000790 PyErr_SetFromErrno(PyExc_IOError);
791 return -1;
792 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000793 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000794#else /* !PYOS_OS2 */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000795#ifdef __BEOS__
796 /* This sleep *CAN BE* interrupted. */
797 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000798 if( secs <= 0.0 ) {
799 return;
800 }
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000801
Guido van Rossumbcc20741998-08-04 22:53:56 +0000802 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000803 /* BeOS snooze() is in microseconds... */
804 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000805 Py_BLOCK_THREADS
806 PyErr_SetFromErrno( PyExc_IOError );
807 return -1;
808 }
809 Py_END_ALLOW_THREADS
810 }
811#else /* !__BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000812 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000813 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000814 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000815 Py_END_ALLOW_THREADS
Guido van Rossumbcc20741998-08-04 22:53:56 +0000816#endif /* !__BEOS__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000817#endif /* !PYOS_OS2 */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000818#endif /* !MS_WIN32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000819#endif /* !MSDOS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000820#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000821#endif /* !macintosh */
822#endif /* !HAVE_SELECT */
823 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000824}