blob: ef6ee3e229b826454ef1760320992ce7a8ed6f59 [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 Rossum98bf58f2001-10-18 20:34:25 +00005#include "structseq.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00006
Guido van Rossum87ce7bb1998-06-09 16:30:31 +00007#include <ctype.h>
8
Guido van Rossumb6775db1994-08-01 11:34:53 +00009#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000010
Guido van Rossumb6775db1994-08-01 11:34:53 +000011#ifdef QUICKWIN
12#include <io.h>
13#endif
14
Guido van Rossumb6775db1994-08-01 11:34:53 +000015#ifdef HAVE_FTIME
16#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000017#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000018extern int ftime(struct timeb *);
Guido van Rossum52174571996-12-09 18:38:52 +000019#endif /* MS_WINDOWS */
20#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021
Guido van Rossum7bf22de1997-12-02 20:34:19 +000022#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000023#include <i86.h>
24#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000025#ifdef MS_WINDOWS
Mark Hammond975e3922002-07-16 01:29:19 +000026#define WIN32_LEAN_AND_MEAN
Guido van Rossum258ccd42001-03-02 06:53:29 +000027#include <windows.h>
Mark Hammond975e3922002-07-16 01:29:19 +000028#include "pythread.h"
29
30/* helper to allow us to interrupt sleep() on Windows*/
31static HANDLE hInterruptEvent = NULL;
32static BOOL WINAPI PyCtrlHandler(DWORD dwCtrlType)
33{
34 SetEvent(hInterruptEvent);
35 /* allow other default handlers to be called.
36 Default Python handler will setup the
37 KeyboardInterrupt exception.
38 */
39 return FALSE;
40}
41static long main_thread;
42
43
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000044#if defined(__BORLANDC__)
Guido van Rossumb2fb3641996-09-07 00:47:35 +000045/* 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
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000049#endif /* __BORLANDC__ */
Guido van Rossumcac6c721996-09-06 13:34:02 +000050#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000051#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000052
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000053#if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Fred Drakedfb4ebd2000-06-29 20:56:28 +000054/* Win32 has better clock replacement
55 XXX Win64 does not yet, but might when the platform matures. */
Guido van Rossum3917c221997-04-02 05:35:28 +000056#undef HAVE_CLOCK /* We have our own version down below */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000057#endif /* MS_WINDOWS && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +000058
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000059#if defined(PYOS_OS2)
60#define INCL_DOS
61#define INCL_ERRORS
62#include <os2.h>
63#endif
64
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000065#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000066#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000067#endif
68
Guido van Rossumbcc20741998-08-04 22:53:56 +000069#ifdef __BEOS__
Fred Drake56221a72000-08-15 18:52:33 +000070#include <time.h>
Guido van Rossumbcc20741998-08-04 22:53:56 +000071/* For bigtime_t, snooze(). - [cjh] */
72#include <support/SupportDefs.h>
73#include <kernel/OS.h>
74#endif
75
Martin v. Löwisa94568a2003-05-10 07:36:56 +000076#ifdef RISCOS
77extern int riscos_sleep(double);
78#endif
79
Guido van Rossum234f9421993-06-17 12:35:49 +000080/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000081static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000082static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000084/* For Y2K check */
85static PyObject *moddict;
86
Barry Warsaw9a2a8a81996-12-06 23:32:14 +000087static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +000088time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089{
Guido van Rossumb6775db1994-08-01 11:34:53 +000090 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +000091 if (!PyArg_ParseTuple(args, ":time"))
Guido van Rossuma2b7f401993-01-04 09:09:59 +000092 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +000093 secs = floattime();
94 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +000095 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +000096 return NULL;
97 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +000098 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +000099}
100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000101PyDoc_STRVAR(time_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000102"time() -> floating point number\n\
103\n\
104Return the current time in seconds since the Epoch.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000105Fractions of a second may be present if the system clock provides them.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000106
Guido van Rossumb6775db1994-08-01 11:34:53 +0000107#ifdef HAVE_CLOCK
108
109#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000110#ifdef CLK_TCK
111#define CLOCKS_PER_SEC CLK_TCK
112#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000113#define CLOCKS_PER_SEC 1000000
114#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000115#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000116
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000117static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000118time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000119{
Thomas Woutersfe385252001-01-19 23:16:56 +0000120 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000122 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000124#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000126#if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Mark Hammond7ba5e812002-02-12 04:02:33 +0000127/* Due to Mark Hammond and Tim Peters */
Guido van Rossum3917c221997-04-02 05:35:28 +0000128static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000129time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000130{
Tim Peters9ad4b682002-02-13 05:14:18 +0000131 static LARGE_INTEGER ctrStart;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000132 static double divisor = 0.0;
Tim Peters9ad4b682002-02-13 05:14:18 +0000133 LARGE_INTEGER now;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000134 double diff;
Guido van Rossum3917c221997-04-02 05:35:28 +0000135
Thomas Woutersfe385252001-01-19 23:16:56 +0000136 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum3917c221997-04-02 05:35:28 +0000137 return NULL;
138
Mark Hammond7ba5e812002-02-12 04:02:33 +0000139 if (divisor == 0.0) {
Tim Peters9ad4b682002-02-13 05:14:18 +0000140 LARGE_INTEGER freq;
141 QueryPerformanceCounter(&ctrStart);
142 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
Mark Hammond7ba5e812002-02-12 04:02:33 +0000143 /* Unlikely to happen - this works on all intel
144 machines at least! Revert to clock() */
Guido van Rossum3917c221997-04-02 05:35:28 +0000145 return PyFloat_FromDouble(clock());
146 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000147 divisor = (double)freq.QuadPart;
Guido van Rossum3917c221997-04-02 05:35:28 +0000148 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000149 QueryPerformanceCounter(&now);
150 diff = (double)(now.QuadPart - ctrStart.QuadPart);
Mark Hammond7ba5e812002-02-12 04:02:33 +0000151 return PyFloat_FromDouble(diff / divisor);
Guido van Rossum3917c221997-04-02 05:35:28 +0000152}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000153
Guido van Rossum3917c221997-04-02 05:35:28 +0000154#define HAVE_CLOCK /* So it gets included in the methods */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000155#endif /* MS_WINDOWS && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000156
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000157#ifdef HAVE_CLOCK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000158PyDoc_STRVAR(clock_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000159"clock() -> floating point number\n\
160\n\
161Return the CPU time or real time since the start of the process or since\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000162the first call to clock(). This has as much precision as the system\n\
163records.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000164#endif
165
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000166static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000167time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000169 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000170 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000172 if (floatsleep(secs) != 0)
173 return NULL;
174 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000175 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000176}
177
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000178PyDoc_STRVAR(sleep_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000179"sleep(seconds)\n\
180\n\
181Delay execution for a given number of seconds. The argument may be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000182a floating point number for subsecond precision.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000183
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000184static PyStructSequence_Field struct_time_type_fields[] = {
185 {"tm_year", NULL},
186 {"tm_mon", NULL},
187 {"tm_mday", NULL},
188 {"tm_hour", NULL},
189 {"tm_min", NULL},
190 {"tm_sec", NULL},
191 {"tm_wday", NULL},
192 {"tm_yday", NULL},
193 {"tm_isdst", NULL},
194 {0}
195};
196
197static PyStructSequence_Desc struct_time_type_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000198 "time.struct_time",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000199 NULL,
200 struct_time_type_fields,
201 9,
202};
Tim Peters9ad4b682002-02-13 05:14:18 +0000203
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000204static PyTypeObject StructTimeType;
205
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000206static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000207tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000208{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000209 PyObject *v = PyStructSequence_New(&StructTimeType);
210 if (v == NULL)
211 return NULL;
Tim Peters9ad4b682002-02-13 05:14:18 +0000212
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000213#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
214
215 SET(0, p->tm_year + 1900);
216 SET(1, p->tm_mon + 1); /* Want January == 1 */
217 SET(2, p->tm_mday);
218 SET(3, p->tm_hour);
219 SET(4, p->tm_min);
220 SET(5, p->tm_sec);
221 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
222 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
223 SET(8, p->tm_isdst);
224#undef SET
225 if (PyErr_Occurred()) {
226 Py_XDECREF(v);
227 return NULL;
228 }
229
230 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000231}
232
233static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000234time_convert(time_t when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000235{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000236 struct tm *p;
237 errno = 0;
238 p = function(&when);
239 if (p == NULL) {
240#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000241 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000242 errno = EINVAL;
243#endif
Tim Peters8b19a932003-01-17 20:08:54 +0000244 return PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000245 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000246 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000247}
248
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000249static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000250time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000251{
252 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000253 if (PyTuple_Size(args) == 0)
254 when = floattime();
255 if (!PyArg_ParseTuple(args, "|d:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000256 return NULL;
257 return time_convert((time_t)when, gmtime);
258}
259
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000260PyDoc_STRVAR(gmtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000261"gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,\n\
262 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000263\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000264Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000265GMT). When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000266
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000267static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000268time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000269{
270 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000271 if (PyTuple_Size(args) == 0)
272 when = floattime();
273 if (!PyArg_ParseTuple(args, "|d:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000274 return NULL;
275 return time_convert((time_t)when, localtime);
276}
277
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000278PyDoc_STRVAR(localtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000279"localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000280\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000281Convert seconds since the Epoch to a time tuple expressing local time.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000282When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000283
Guido van Rossum9e90a671993-06-24 11:10:19 +0000284static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000285gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000286{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000287 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000288 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000289
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000290 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000291 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000292 &p->tm_mon,
293 &p->tm_mday,
294 &p->tm_hour,
295 &p->tm_min,
296 &p->tm_sec,
297 &p->tm_wday,
298 &p->tm_yday,
299 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000300 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000301 if (y < 1900) {
302 PyObject *accept = PyDict_GetItemString(moddict,
303 "accept2dyear");
304 if (accept == NULL || !PyInt_Check(accept) ||
305 PyInt_AsLong(accept) == 0) {
306 PyErr_SetString(PyExc_ValueError,
307 "year >= 1900 required");
308 return 0;
309 }
310 if (69 <= y && y <= 99)
311 y += 1900;
312 else if (0 <= y && y <= 68)
313 y += 2000;
314 else {
315 PyErr_SetString(PyExc_ValueError,
Skip Montanaro1a10aac2001-08-22 12:39:16 +0000316 "year out of range");
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000317 return 0;
318 }
319 }
320 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000321 p->tm_mon--;
322 p->tm_wday = (p->tm_wday + 1) % 7;
323 p->tm_yday--;
324 return 1;
325}
326
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000327#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000328static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000329time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000330{
Thomas Woutersfe385252001-01-19 23:16:56 +0000331 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000332 struct tm buf;
333 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000334 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000335 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000336 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000337
Thomas Wouters334fb892000-07-25 12:56:38 +0000338 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000339
Thomas Woutersfe385252001-01-19 23:16:56 +0000340 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000341 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000342
343 if (tup == NULL) {
344 time_t tt = time(NULL);
345 buf = *localtime(&tt);
346 } else if (!gettmarg(tup, &buf))
347 return NULL;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000348
Brett Cannond1080a32004-03-02 04:38:10 +0000349 /* Checks added to make sure strftime() does not crash Python by
350 indexing blindly into some array for a textual representation
351 by some bad index (fixes bug #897625).
352
353 No check for year since handled in gettmarg().
354 */
355 if (buf.tm_mon < 0 || buf.tm_mon > 11) {
356 PyErr_SetString(PyExc_ValueError, "month out of range");
357 return NULL;
358 }
359 if (buf.tm_mday < 1 || buf.tm_mday > 31) {
360 PyErr_SetString(PyExc_ValueError, "day of month out of range");
361 return NULL;
362 }
363 if (buf.tm_hour < 0 || buf.tm_hour > 23) {
364 PyErr_SetString(PyExc_ValueError, "hour out of range");
365 return NULL;
366 }
367 if (buf.tm_min < 0 || buf.tm_min > 59) {
368 PyErr_SetString(PyExc_ValueError, "minute out of range");
369 return NULL;
370 }
371 if (buf.tm_sec < 0 || buf.tm_sec > 61) {
372 PyErr_SetString(PyExc_ValueError, "seconds out of range");
373 return NULL;
374 }
375 /* tm_wday does not need checking of its upper-bound since taking
376 ``% 7`` in gettmarg() automatically restricts the range. */
377 if (buf.tm_wday < 0) {
378 PyErr_SetString(PyExc_ValueError, "day of week out of range");
379 return NULL;
380 }
381 if (buf.tm_yday < 0 || buf.tm_yday > 365) {
382 PyErr_SetString(PyExc_ValueError, "day of year out of range");
383 return NULL;
384 }
385 if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
386 PyErr_SetString(PyExc_ValueError,
387 "daylight savings flag out of range");
388 return NULL;
389 }
390
Guido van Rossumc222ec21999-02-23 00:00:10 +0000391 fmtlen = strlen(fmt);
392
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000393 /* I hate these functions that presume you know how big the output
394 * will be ahead of time...
395 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000396 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000397 outbuf = malloc(i);
398 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000399 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000400 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000401 buflen = strftime(outbuf, i, fmt, &buf);
402 if (buflen > 0 || i >= 256 * fmtlen) {
403 /* If the buffer is 256 times as long as the format,
404 it's probably not failing for lack of room!
405 More likely, the format yields an empty result,
406 e.g. an empty format, or %Z when the timezone
407 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000408 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000409 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000410 free(outbuf);
411 return ret;
412 }
413 free(outbuf);
414 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000415}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000416
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000417PyDoc_STRVAR(strftime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000418"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000419\n\
420Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000421See the library reference manual for formatting codes. When the time tuple\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000422is not present, current time as returned by localtime() is used.");
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000423#endif /* HAVE_STRFTIME */
424
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000425static PyObject *
426time_strptime(PyObject *self, PyObject *args)
427{
428 PyObject *strptime_module = PyImport_ImportModule("_strptime");
Raymond Hettinger502168a2003-04-10 16:03:22 +0000429 PyObject *strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000430
Tim Peters513a1cd2003-01-19 04:54:58 +0000431 if (!strptime_module)
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000432 return NULL;
Raymond Hettinger502168a2003-04-10 16:03:22 +0000433 strptime_result = PyObject_CallMethod(strptime_module, "strptime", "O", args);
434 Py_DECREF(strptime_module);
435 return strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000436}
437
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000438PyDoc_STRVAR(strptime_doc,
Brett Cannon20def8b2003-07-01 05:16:08 +0000439"strptime(string, format) -> struct_time\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000440\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000441Parse a string to a time tuple according to a format specification.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000442See the library reference manual for formatting codes (same as strftime()).");
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000443
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000444
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000445static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000446time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000447{
Thomas Woutersfe385252001-01-19 23:16:56 +0000448 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000449 struct tm buf;
450 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000451 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000452 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000453 if (tup == NULL) {
454 time_t tt = time(NULL);
455 buf = *localtime(&tt);
456 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000457 return NULL;
458 p = asctime(&buf);
459 if (p[24] == '\n')
460 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000461 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000462}
463
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000464PyDoc_STRVAR(asctime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000465"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000466\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000467Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
468When the time tuple is not present, current time as returned by localtime()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000469is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000470
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000471static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000472time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000473{
474 double dt;
475 time_t tt;
476 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000477
Thomas Woutersfe385252001-01-19 23:16:56 +0000478 if (PyTuple_Size(args) == 0)
479 tt = time(NULL);
480 else {
481 if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
482 return NULL;
483 tt = (time_t)dt;
484 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000485 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000486 if (p == NULL) {
487 PyErr_SetString(PyExc_ValueError, "unconvertible time");
488 return NULL;
489 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000490 if (p[24] == '\n')
491 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000492 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000493}
494
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000495PyDoc_STRVAR(ctime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000496"ctime(seconds) -> string\n\
497\n\
498Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000499This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000500not present, current time as returned by localtime() is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000501
Guido van Rossum60cd8131998-03-06 17:16:21 +0000502#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000503static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000504time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000505{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000506 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000507 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000508 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000509 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000510 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000511 tt = time(&tt);
512 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000513 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000514 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000515 tt = mktime(&buf);
516 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000517 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000518 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000519 return NULL;
520 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000521 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000522}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000523
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000524PyDoc_STRVAR(mktime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000525"mktime(tuple) -> floating point number\n\
526\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000527Convert a time tuple in local time to seconds since the Epoch.");
Guido van Rossum60cd8131998-03-06 17:16:21 +0000528#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000529
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000530#ifdef HAVE_WORKING_TZSET
531void inittimezone(PyObject *module);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000533static PyObject *
534time_tzset(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000535{
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000536 PyObject* m;
Fred Drake9bb74322002-04-01 14:49:59 +0000537
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000538 if (!PyArg_ParseTuple(args, ":tzset"))
539 return NULL;
540
541 m = PyImport_ImportModule("time");
542 if (m == NULL) {
543 return NULL;
544 }
545
546 tzset();
547
548 /* Reset timezone, altzone, daylight and tzname */
549 inittimezone(m);
550 Py_DECREF(m);
551
552 Py_INCREF(Py_None);
553 return Py_None;
554}
555
556PyDoc_STRVAR(tzset_doc,
557"tzset(zone)\n\
558\n\
559Initialize, or reinitialize, the local timezone to the value stored in\n\
560os.environ['TZ']. The TZ environment variable should be specified in\n\
561standard Uniz timezone format as documented in the tzset man page\n\
562(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
563fall back to UTC. If the TZ environment variable is not set, the local\n\
564timezone is set to the systems best guess of wallclock time.\n\
565Changing the TZ environment variable without calling tzset *may* change\n\
566the local timezone used by methods such as localtime, but this behaviour\n\
567should not be relied on.");
568#endif /* HAVE_WORKING_TZSET */
569
570void inittimezone(PyObject *m) {
571 /* This code moved from inittime wholesale to allow calling it from
572 time_tzset. In the future, some parts of it can be moved back
573 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
574 are), and the extranious calls to tzset(3) should be removed.
575 I havn't done this yet, as I don't want to change this code as
576 little as possible when introducing the time.tzset and time.tzsetwall
577 methods. This should simply be a method of doing the following once,
578 at the top of this function and removing the call to tzset() from
579 time_tzset():
580
581 #ifdef HAVE_TZSET
582 tzset()
583 #endif
584
585 And I'm lazy and hate C so nyer.
586 */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000587#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000588 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000589#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000590 PyModule_AddIntConstant(m, "timezone", _timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000591#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000592 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000593#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000594#ifdef HAVE_ALTZONE
Fred Drake9bb74322002-04-01 14:49:59 +0000595 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000596#else
Guido van Rossum26452411998-09-28 22:07:11 +0000597#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000598 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000599#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000600 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000601#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000602#endif
Fred Drake9bb74322002-04-01 14:49:59 +0000603 PyModule_AddIntConstant(m, "daylight", daylight);
604 PyModule_AddObject(m, "tzname",
605 Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000606#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000607#ifdef HAVE_STRUCT_TM_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000608 {
609#define YEAR ((time_t)((365 * 24 + 6) * 3600))
610 time_t t;
611 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000612 long janzone, julyzone;
613 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000614 t = (time((time_t *)0) / YEAR) * YEAR;
615 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000616 janzone = -p->tm_gmtoff;
617 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
618 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000619 t += YEAR/2;
620 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000621 julyzone = -p->tm_gmtoff;
622 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
623 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000624
Guido van Rossum57731601999-03-29 19:12:04 +0000625 if( janzone < julyzone ) {
626 /* DST is reversed in the southern hemisphere */
Fred Drake9bb74322002-04-01 14:49:59 +0000627 PyModule_AddIntConstant(m, "timezone", julyzone);
628 PyModule_AddIntConstant(m, "altzone", janzone);
629 PyModule_AddIntConstant(m, "daylight",
630 janzone != julyzone);
631 PyModule_AddObject(m, "tzname",
632 Py_BuildValue("(zz)",
633 julyname, janname));
Guido van Rossum57731601999-03-29 19:12:04 +0000634 } else {
Fred Drake9bb74322002-04-01 14:49:59 +0000635 PyModule_AddIntConstant(m, "timezone", janzone);
636 PyModule_AddIntConstant(m, "altzone", julyzone);
637 PyModule_AddIntConstant(m, "daylight",
638 janzone != julyzone);
639 PyModule_AddObject(m, "tzname",
640 Py_BuildValue("(zz)",
641 janname, julyname));
Guido van Rossum57731601999-03-29 19:12:04 +0000642 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000643 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000644#else
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000645#endif /* HAVE_STRUCT_TM_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000646#ifdef __CYGWIN__
647 tzset();
Fred Drake9bb74322002-04-01 14:49:59 +0000648 PyModule_AddIntConstant(m, "timezone", _timezone);
649 PyModule_AddIntConstant(m, "altzone", _timezone);
650 PyModule_AddIntConstant(m, "daylight", _daylight);
651 PyModule_AddObject(m, "tzname",
652 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +0000653#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000654#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000655}
656
657
658static PyMethodDef time_methods[] = {
659 {"time", time_time, METH_VARARGS, time_doc},
660#ifdef HAVE_CLOCK
661 {"clock", time_clock, METH_VARARGS, clock_doc},
662#endif
663 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
664 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
665 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
666 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
667 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
668#ifdef HAVE_MKTIME
669 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
670#endif
671#ifdef HAVE_STRFTIME
672 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
673#endif
674 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
675#ifdef HAVE_WORKING_TZSET
676 {"tzset", time_tzset, METH_VARARGS, tzset_doc},
677#endif
678 {NULL, NULL} /* sentinel */
679};
680
681
682PyDoc_STRVAR(module_doc,
683"This module provides various functions to manipulate time values.\n\
684\n\
685There are two standard representations of time. One is the number\n\
686of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
687or a floating point number (to represent fractions of seconds).\n\
688The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
689The actual value can be retrieved by calling gmtime(0).\n\
690\n\
691The other representation is a tuple of 9 integers giving local time.\n\
692The tuple items are:\n\
693 year (four digits, e.g. 1998)\n\
694 month (1-12)\n\
695 day (1-31)\n\
696 hours (0-23)\n\
697 minutes (0-59)\n\
698 seconds (0-59)\n\
699 weekday (0-6, Monday is 0)\n\
700 Julian day (day in the year, 1-366)\n\
701 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
702If the DST flag is 0, the time is given in the regular time zone;\n\
703if it is 1, the time is given in the DST time zone;\n\
704if it is -1, mktime() should guess based on the date and time.\n\
705\n\
706Variables:\n\
707\n\
708timezone -- difference in seconds between UTC and local standard time\n\
709altzone -- difference in seconds between UTC and local DST time\n\
710daylight -- whether local time should reflect DST\n\
711tzname -- tuple of (standard time zone name, DST time zone name)\n\
712\n\
713Functions:\n\
714\n\
715time() -- return current time in seconds since the Epoch as a float\n\
716clock() -- return CPU time since process start as a float\n\
717sleep() -- delay for a number of seconds given as a float\n\
718gmtime() -- convert seconds since Epoch to UTC tuple\n\
719localtime() -- convert seconds since Epoch to local time tuple\n\
720asctime() -- convert time tuple to string\n\
721ctime() -- convert time in seconds to string\n\
722mktime() -- convert local time tuple to seconds since Epoch\n\
723strftime() -- convert time tuple to string according to format specification\n\
724strptime() -- parse string to time tuple according to format specification\n\
725tzset() -- change the local timezone");
726
727
728PyMODINIT_FUNC
729inittime(void)
730{
731 PyObject *m;
732 char *p;
733 m = Py_InitModule3("time", time_methods, module_doc);
734
735 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
736 p = Py_GETENV("PYTHONY2K");
737 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
738 /* Squirrel away the module's dictionary for the y2k check */
739 moddict = PyModule_GetDict(m);
740 Py_INCREF(moddict);
741
742 /* Set, or reset, module variables like time.timezone */
743 inittimezone(m);
744
Mark Hammond975e3922002-07-16 01:29:19 +0000745#ifdef MS_WINDOWS
746 /* Helper to allow interrupts for Windows.
747 If Ctrl+C event delivered while not sleeping
748 it will be ignored.
749 */
750 main_thread = PyThread_get_thread_ident();
751 hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
752 SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
753#endif /* MS_WINDOWS */
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000754 PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
Fred Drake9bb74322002-04-01 14:49:59 +0000755 Py_INCREF(&StructTimeType);
756 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000757}
758
759
Guido van Rossumb6775db1994-08-01 11:34:53 +0000760/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000761
Guido van Rossumb6775db1994-08-01 11:34:53 +0000762static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000763floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000764{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000765 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000766 (1) gettimeofday() -- resolution in microseconds
767 (2) ftime() -- resolution in milliseconds
768 (3) time() -- resolution in seconds
769 In all cases the return value is a float in seconds.
770 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
771 fail, so we fall back on ftime() or time().
772 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000773#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000774 {
775 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000776#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000777 if (gettimeofday(&t) == 0)
778 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000779#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000780 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
781 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000782#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000783 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000784#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000785 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000786#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000787 struct timeb t;
788 ftime(&t);
789 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000790#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000791 time_t secs;
792 time(&secs);
793 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000794#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000795 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000796}
797
Guido van Rossumb6775db1994-08-01 11:34:53 +0000798
799/* Implement floatsleep() for various platforms.
800 When interrupted (or when another error occurs), return -1 and
801 set an exception; else return 0. */
802
803static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000804floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000805{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000806/* XXX Should test for MS_WINDOWS first! */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000807#if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000808 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000809 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000810 frac = fmod(secs, 1.0);
811 secs = floor(secs);
812 t.tv_sec = (long)secs;
813 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000814 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000815 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000816#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000817 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000818#else
819 if (1) {
820#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000821 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000822 PyErr_SetFromErrno(PyExc_IOError);
823 return -1;
824 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000825 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000826 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000827#elif defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000828 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000829 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000830 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000831 Py_END_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000832#elif defined(MS_WINDOWS)
Fred Drake0e123952000-06-29 21:31:02 +0000833 {
834 double millisecs = secs * 1000.0;
Tim Peters513a1cd2003-01-19 04:54:58 +0000835 unsigned long ul_millis;
836
Fred Drake0e123952000-06-29 21:31:02 +0000837 if (millisecs > (double)ULONG_MAX) {
Tim Peters513a1cd2003-01-19 04:54:58 +0000838 PyErr_SetString(PyExc_OverflowError,
839 "sleep length is too large");
Fred Drake0e123952000-06-29 21:31:02 +0000840 return -1;
841 }
Fred Drake0e123952000-06-29 21:31:02 +0000842 Py_BEGIN_ALLOW_THREADS
Tim Peters513a1cd2003-01-19 04:54:58 +0000843 /* Allow sleep(0) to maintain win32 semantics, and as decreed
844 * by Guido, only the main thread can be interrupted.
845 */
846 ul_millis = (unsigned long)millisecs;
847 if (ul_millis == 0 ||
848 main_thread != PyThread_get_thread_ident())
849 Sleep(ul_millis);
Mark Hammond975e3922002-07-16 01:29:19 +0000850 else {
851 DWORD rc;
852 ResetEvent(hInterruptEvent);
Tim Peters513a1cd2003-01-19 04:54:58 +0000853 rc = WaitForSingleObject(hInterruptEvent, ul_millis);
854 if (rc == WAIT_OBJECT_0) {
855 /* Yield to make sure real Python signal
856 * handler called.
857 */
Mark Hammond975e3922002-07-16 01:29:19 +0000858 Sleep(1);
859 Py_BLOCK_THREADS
Mark Hammond975e3922002-07-16 01:29:19 +0000860 errno = EINTR;
861 PyErr_SetFromErrno(PyExc_IOError);
862 return -1;
863 }
864 }
Fred Drake0e123952000-06-29 21:31:02 +0000865 Py_END_ALLOW_THREADS
866 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000867#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000868 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000869 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000870 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000871 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000872 PyErr_SetFromErrno(PyExc_IOError);
873 return -1;
874 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000875 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000876#elif defined(__BEOS__)
Guido van Rossumbcc20741998-08-04 22:53:56 +0000877 /* This sleep *CAN BE* interrupted. */
878 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000879 if( secs <= 0.0 ) {
880 return;
881 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000882
Guido van Rossumbcc20741998-08-04 22:53:56 +0000883 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000884 /* BeOS snooze() is in microseconds... */
885 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000886 Py_BLOCK_THREADS
887 PyErr_SetFromErrno( PyExc_IOError );
888 return -1;
889 }
890 Py_END_ALLOW_THREADS
891 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000892#elif defined(RISCOS)
Guido van Rossumbceccf52001-04-10 22:07:43 +0000893 if (secs <= 0.0)
894 return 0;
895 Py_BEGIN_ALLOW_THREADS
896 /* This sleep *CAN BE* interrupted. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000897 if ( riscos_sleep(secs) )
Guido van Rossumbceccf52001-04-10 22:07:43 +0000898 return -1;
899 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000900#elif defined(PLAN9)
901 {
902 double millisecs = secs * 1000.0;
903 if (millisecs > (double)LONG_MAX) {
904 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
905 return -1;
906 }
907 /* This sleep *CAN BE* interrupted. */
908 Py_BEGIN_ALLOW_THREADS
909 if(sleep((long)millisecs) < 0){
910 Py_BLOCK_THREADS
911 PyErr_SetFromErrno(PyExc_IOError);
912 return -1;
913 }
914 Py_END_ALLOW_THREADS
915 }
916#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000917 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000918 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000919 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000920 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000921#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000922
Guido van Rossumb6775db1994-08-01 11:34:53 +0000923 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000924}
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000925
926