blob: d60f32038a5eac0a21632128c95dd452020519cd [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
Guido van Rossumc222ec21999-02-23 00:00:10 +0000349 fmtlen = strlen(fmt);
350
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000351 /* I hate these functions that presume you know how big the output
352 * will be ahead of time...
353 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000354 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000355 outbuf = malloc(i);
356 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000357 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000358 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000359 buflen = strftime(outbuf, i, fmt, &buf);
360 if (buflen > 0 || i >= 256 * fmtlen) {
361 /* If the buffer is 256 times as long as the format,
362 it's probably not failing for lack of room!
363 More likely, the format yields an empty result,
364 e.g. an empty format, or %Z when the timezone
365 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000366 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000367 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000368 free(outbuf);
369 return ret;
370 }
371 free(outbuf);
372 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000373}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000375PyDoc_STRVAR(strftime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000376"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000377\n\
378Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000379See the library reference manual for formatting codes. When the time tuple\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000380is not present, current time as returned by localtime() is used.");
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000381#endif /* HAVE_STRFTIME */
382
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000383static PyObject *
384time_strptime(PyObject *self, PyObject *args)
385{
386 PyObject *strptime_module = PyImport_ImportModule("_strptime");
Raymond Hettinger502168a2003-04-10 16:03:22 +0000387 PyObject *strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000388
Tim Peters513a1cd2003-01-19 04:54:58 +0000389 if (!strptime_module)
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000390 return NULL;
Raymond Hettinger502168a2003-04-10 16:03:22 +0000391 strptime_result = PyObject_CallMethod(strptime_module, "strptime", "O", args);
392 Py_DECREF(strptime_module);
393 return strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000394}
395
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000396PyDoc_STRVAR(strptime_doc,
Brett Cannon20def8b2003-07-01 05:16:08 +0000397"strptime(string, format) -> struct_time\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000398\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000399Parse a string to a time tuple according to a format specification.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000400See the library reference manual for formatting codes (same as strftime()).");
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000401
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000402
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000403static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000404time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000405{
Thomas Woutersfe385252001-01-19 23:16:56 +0000406 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000407 struct tm buf;
408 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000409 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000410 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000411 if (tup == NULL) {
412 time_t tt = time(NULL);
413 buf = *localtime(&tt);
414 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000415 return NULL;
416 p = asctime(&buf);
417 if (p[24] == '\n')
418 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000419 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000420}
421
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000422PyDoc_STRVAR(asctime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000423"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000424\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000425Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
426When the time tuple is not present, current time as returned by localtime()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000427is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000428
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000429static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000430time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000431{
432 double dt;
433 time_t tt;
434 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000435
Thomas Woutersfe385252001-01-19 23:16:56 +0000436 if (PyTuple_Size(args) == 0)
437 tt = time(NULL);
438 else {
439 if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
440 return NULL;
441 tt = (time_t)dt;
442 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000443 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000444 if (p == NULL) {
445 PyErr_SetString(PyExc_ValueError, "unconvertible time");
446 return NULL;
447 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000448 if (p[24] == '\n')
449 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000450 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000451}
452
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000453PyDoc_STRVAR(ctime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000454"ctime(seconds) -> string\n\
455\n\
456Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000457This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000458not present, current time as returned by localtime() is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000459
Guido van Rossum60cd8131998-03-06 17:16:21 +0000460#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000461static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000462time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000463{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000464 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000465 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000466 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000467 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000468 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000469 tt = time(&tt);
470 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000471 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000472 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000473 tt = mktime(&buf);
474 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000475 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000476 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000477 return NULL;
478 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000479 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000480}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000482PyDoc_STRVAR(mktime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000483"mktime(tuple) -> floating point number\n\
484\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000485Convert a time tuple in local time to seconds since the Epoch.");
Guido van Rossum60cd8131998-03-06 17:16:21 +0000486#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000487
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000488#ifdef HAVE_WORKING_TZSET
489void inittimezone(PyObject *module);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000490
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000491static PyObject *
492time_tzset(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493{
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000494 PyObject* m;
Fred Drake9bb74322002-04-01 14:49:59 +0000495
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000496 if (!PyArg_ParseTuple(args, ":tzset"))
497 return NULL;
498
499 m = PyImport_ImportModule("time");
500 if (m == NULL) {
501 return NULL;
502 }
503
504 tzset();
505
506 /* Reset timezone, altzone, daylight and tzname */
507 inittimezone(m);
508 Py_DECREF(m);
509
510 Py_INCREF(Py_None);
511 return Py_None;
512}
513
514PyDoc_STRVAR(tzset_doc,
515"tzset(zone)\n\
516\n\
517Initialize, or reinitialize, the local timezone to the value stored in\n\
518os.environ['TZ']. The TZ environment variable should be specified in\n\
519standard Uniz timezone format as documented in the tzset man page\n\
520(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
521fall back to UTC. If the TZ environment variable is not set, the local\n\
522timezone is set to the systems best guess of wallclock time.\n\
523Changing the TZ environment variable without calling tzset *may* change\n\
524the local timezone used by methods such as localtime, but this behaviour\n\
525should not be relied on.");
526#endif /* HAVE_WORKING_TZSET */
527
528void inittimezone(PyObject *m) {
529 /* This code moved from inittime wholesale to allow calling it from
530 time_tzset. In the future, some parts of it can be moved back
531 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
532 are), and the extranious calls to tzset(3) should be removed.
533 I havn't done this yet, as I don't want to change this code as
534 little as possible when introducing the time.tzset and time.tzsetwall
535 methods. This should simply be a method of doing the following once,
536 at the top of this function and removing the call to tzset() from
537 time_tzset():
538
539 #ifdef HAVE_TZSET
540 tzset()
541 #endif
542
543 And I'm lazy and hate C so nyer.
544 */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000545#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000546 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000547#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000548 PyModule_AddIntConstant(m, "timezone", _timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000549#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000550 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000551#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000552#ifdef HAVE_ALTZONE
Fred Drake9bb74322002-04-01 14:49:59 +0000553 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000554#else
Guido van Rossum26452411998-09-28 22:07:11 +0000555#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000556 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000557#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000558 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000559#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000560#endif
Fred Drake9bb74322002-04-01 14:49:59 +0000561 PyModule_AddIntConstant(m, "daylight", daylight);
562 PyModule_AddObject(m, "tzname",
563 Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000564#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000565#ifdef HAVE_STRUCT_TM_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000566 {
567#define YEAR ((time_t)((365 * 24 + 6) * 3600))
568 time_t t;
569 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000570 long janzone, julyzone;
571 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000572 t = (time((time_t *)0) / YEAR) * YEAR;
573 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000574 janzone = -p->tm_gmtoff;
575 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
576 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000577 t += YEAR/2;
578 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000579 julyzone = -p->tm_gmtoff;
580 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
581 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000582
Guido van Rossum57731601999-03-29 19:12:04 +0000583 if( janzone < julyzone ) {
584 /* DST is reversed in the southern hemisphere */
Fred Drake9bb74322002-04-01 14:49:59 +0000585 PyModule_AddIntConstant(m, "timezone", julyzone);
586 PyModule_AddIntConstant(m, "altzone", janzone);
587 PyModule_AddIntConstant(m, "daylight",
588 janzone != julyzone);
589 PyModule_AddObject(m, "tzname",
590 Py_BuildValue("(zz)",
591 julyname, janname));
Guido van Rossum57731601999-03-29 19:12:04 +0000592 } else {
Fred Drake9bb74322002-04-01 14:49:59 +0000593 PyModule_AddIntConstant(m, "timezone", janzone);
594 PyModule_AddIntConstant(m, "altzone", julyzone);
595 PyModule_AddIntConstant(m, "daylight",
596 janzone != julyzone);
597 PyModule_AddObject(m, "tzname",
598 Py_BuildValue("(zz)",
599 janname, julyname));
Guido van Rossum57731601999-03-29 19:12:04 +0000600 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000601 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000602#else
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000603#endif /* HAVE_STRUCT_TM_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000604#ifdef __CYGWIN__
605 tzset();
Fred Drake9bb74322002-04-01 14:49:59 +0000606 PyModule_AddIntConstant(m, "timezone", _timezone);
607 PyModule_AddIntConstant(m, "altzone", _timezone);
608 PyModule_AddIntConstant(m, "daylight", _daylight);
609 PyModule_AddObject(m, "tzname",
610 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +0000611#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000612#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000613}
614
615
616static PyMethodDef time_methods[] = {
617 {"time", time_time, METH_VARARGS, time_doc},
618#ifdef HAVE_CLOCK
619 {"clock", time_clock, METH_VARARGS, clock_doc},
620#endif
621 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
622 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
623 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
624 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
625 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
626#ifdef HAVE_MKTIME
627 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
628#endif
629#ifdef HAVE_STRFTIME
630 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
631#endif
632 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
633#ifdef HAVE_WORKING_TZSET
634 {"tzset", time_tzset, METH_VARARGS, tzset_doc},
635#endif
636 {NULL, NULL} /* sentinel */
637};
638
639
640PyDoc_STRVAR(module_doc,
641"This module provides various functions to manipulate time values.\n\
642\n\
643There are two standard representations of time. One is the number\n\
644of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
645or a floating point number (to represent fractions of seconds).\n\
646The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
647The actual value can be retrieved by calling gmtime(0).\n\
648\n\
649The other representation is a tuple of 9 integers giving local time.\n\
650The tuple items are:\n\
651 year (four digits, e.g. 1998)\n\
652 month (1-12)\n\
653 day (1-31)\n\
654 hours (0-23)\n\
655 minutes (0-59)\n\
656 seconds (0-59)\n\
657 weekday (0-6, Monday is 0)\n\
658 Julian day (day in the year, 1-366)\n\
659 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
660If the DST flag is 0, the time is given in the regular time zone;\n\
661if it is 1, the time is given in the DST time zone;\n\
662if it is -1, mktime() should guess based on the date and time.\n\
663\n\
664Variables:\n\
665\n\
666timezone -- difference in seconds between UTC and local standard time\n\
667altzone -- difference in seconds between UTC and local DST time\n\
668daylight -- whether local time should reflect DST\n\
669tzname -- tuple of (standard time zone name, DST time zone name)\n\
670\n\
671Functions:\n\
672\n\
673time() -- return current time in seconds since the Epoch as a float\n\
674clock() -- return CPU time since process start as a float\n\
675sleep() -- delay for a number of seconds given as a float\n\
676gmtime() -- convert seconds since Epoch to UTC tuple\n\
677localtime() -- convert seconds since Epoch to local time tuple\n\
678asctime() -- convert time tuple to string\n\
679ctime() -- convert time in seconds to string\n\
680mktime() -- convert local time tuple to seconds since Epoch\n\
681strftime() -- convert time tuple to string according to format specification\n\
682strptime() -- parse string to time tuple according to format specification\n\
683tzset() -- change the local timezone");
684
685
686PyMODINIT_FUNC
687inittime(void)
688{
689 PyObject *m;
690 char *p;
691 m = Py_InitModule3("time", time_methods, module_doc);
692
693 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
694 p = Py_GETENV("PYTHONY2K");
695 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
696 /* Squirrel away the module's dictionary for the y2k check */
697 moddict = PyModule_GetDict(m);
698 Py_INCREF(moddict);
699
700 /* Set, or reset, module variables like time.timezone */
701 inittimezone(m);
702
Mark Hammond975e3922002-07-16 01:29:19 +0000703#ifdef MS_WINDOWS
704 /* Helper to allow interrupts for Windows.
705 If Ctrl+C event delivered while not sleeping
706 it will be ignored.
707 */
708 main_thread = PyThread_get_thread_ident();
709 hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
710 SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
711#endif /* MS_WINDOWS */
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000712 PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
Fred Drake9bb74322002-04-01 14:49:59 +0000713 Py_INCREF(&StructTimeType);
714 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715}
716
717
Guido van Rossumb6775db1994-08-01 11:34:53 +0000718/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719
Guido van Rossumb6775db1994-08-01 11:34:53 +0000720static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000721floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000722{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000723 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000724 (1) gettimeofday() -- resolution in microseconds
725 (2) ftime() -- resolution in milliseconds
726 (3) time() -- resolution in seconds
727 In all cases the return value is a float in seconds.
728 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
729 fail, so we fall back on ftime() or time().
730 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000731#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000732 {
733 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000734#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000735 if (gettimeofday(&t) == 0)
736 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000737#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000738 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
739 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000740#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000741 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000742#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000743 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000744#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000745 struct timeb t;
746 ftime(&t);
747 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000748#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000749 time_t secs;
750 time(&secs);
751 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000752#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000753 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000754}
755
Guido van Rossumb6775db1994-08-01 11:34:53 +0000756
757/* Implement floatsleep() for various platforms.
758 When interrupted (or when another error occurs), return -1 and
759 set an exception; else return 0. */
760
761static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000762floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000763{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000764/* XXX Should test for MS_WINDOWS first! */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000765#if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000766 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000767 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000768 frac = fmod(secs, 1.0);
769 secs = floor(secs);
770 t.tv_sec = (long)secs;
771 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000772 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000773 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000774#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000775 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000776#else
777 if (1) {
778#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000779 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000780 PyErr_SetFromErrno(PyExc_IOError);
781 return -1;
782 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000783 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000784 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000785#elif defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000786 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000787 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000788 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000789 Py_END_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000790#elif defined(MS_WINDOWS)
Fred Drake0e123952000-06-29 21:31:02 +0000791 {
792 double millisecs = secs * 1000.0;
Tim Peters513a1cd2003-01-19 04:54:58 +0000793 unsigned long ul_millis;
794
Fred Drake0e123952000-06-29 21:31:02 +0000795 if (millisecs > (double)ULONG_MAX) {
Tim Peters513a1cd2003-01-19 04:54:58 +0000796 PyErr_SetString(PyExc_OverflowError,
797 "sleep length is too large");
Fred Drake0e123952000-06-29 21:31:02 +0000798 return -1;
799 }
Fred Drake0e123952000-06-29 21:31:02 +0000800 Py_BEGIN_ALLOW_THREADS
Tim Peters513a1cd2003-01-19 04:54:58 +0000801 /* Allow sleep(0) to maintain win32 semantics, and as decreed
802 * by Guido, only the main thread can be interrupted.
803 */
804 ul_millis = (unsigned long)millisecs;
805 if (ul_millis == 0 ||
806 main_thread != PyThread_get_thread_ident())
807 Sleep(ul_millis);
Mark Hammond975e3922002-07-16 01:29:19 +0000808 else {
809 DWORD rc;
810 ResetEvent(hInterruptEvent);
Tim Peters513a1cd2003-01-19 04:54:58 +0000811 rc = WaitForSingleObject(hInterruptEvent, ul_millis);
812 if (rc == WAIT_OBJECT_0) {
813 /* Yield to make sure real Python signal
814 * handler called.
815 */
Mark Hammond975e3922002-07-16 01:29:19 +0000816 Sleep(1);
817 Py_BLOCK_THREADS
Mark Hammond975e3922002-07-16 01:29:19 +0000818 errno = EINTR;
819 PyErr_SetFromErrno(PyExc_IOError);
820 return -1;
821 }
822 }
Fred Drake0e123952000-06-29 21:31:02 +0000823 Py_END_ALLOW_THREADS
824 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000825#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000826 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000827 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000828 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000829 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000830 PyErr_SetFromErrno(PyExc_IOError);
831 return -1;
832 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000833 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000834#elif defined(__BEOS__)
Guido van Rossumbcc20741998-08-04 22:53:56 +0000835 /* This sleep *CAN BE* interrupted. */
836 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000837 if( secs <= 0.0 ) {
838 return;
839 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000840
Guido van Rossumbcc20741998-08-04 22:53:56 +0000841 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000842 /* BeOS snooze() is in microseconds... */
843 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000844 Py_BLOCK_THREADS
845 PyErr_SetFromErrno( PyExc_IOError );
846 return -1;
847 }
848 Py_END_ALLOW_THREADS
849 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000850#elif defined(RISCOS)
Guido van Rossumbceccf52001-04-10 22:07:43 +0000851 if (secs <= 0.0)
852 return 0;
853 Py_BEGIN_ALLOW_THREADS
854 /* This sleep *CAN BE* interrupted. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000855 if ( riscos_sleep(secs) )
Guido van Rossumbceccf52001-04-10 22:07:43 +0000856 return -1;
857 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000858#elif defined(PLAN9)
859 {
860 double millisecs = secs * 1000.0;
861 if (millisecs > (double)LONG_MAX) {
862 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
863 return -1;
864 }
865 /* This sleep *CAN BE* interrupted. */
866 Py_BEGIN_ALLOW_THREADS
867 if(sleep((long)millisecs) < 0){
868 Py_BLOCK_THREADS
869 PyErr_SetFromErrno(PyExc_IOError);
870 return -1;
871 }
872 Py_END_ALLOW_THREADS
873 }
874#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000875 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000876 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000877 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000878 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000879#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000880
Guido van Rossumb6775db1994-08-01 11:34:53 +0000881 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000882}
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000883
884