blob: dda2ce4ee57a8e2b82189908a5b611aecdddff9e [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 Rossum6d946f91992-08-14 13:49:30 +00009#ifdef macintosh
Guido van Rossumb6775db1994-08-01 11:34:53 +000010#include <time.h>
Guido van Rossumc410e922000-04-26 20:40:13 +000011#include <OSUtils.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +000012#else
13#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000014#endif
15
Guido van Rossumb6775db1994-08-01 11:34:53 +000016#ifdef QUICKWIN
17#include <io.h>
18#endif
19
Guido van Rossumb6775db1994-08-01 11:34:53 +000020#ifdef HAVE_FTIME
21#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000022#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000023extern int ftime(struct timeb *);
Guido van Rossum52174571996-12-09 18:38:52 +000024#endif /* MS_WINDOWS */
25#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000026
Guido van Rossum7bf22de1997-12-02 20:34:19 +000027#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000028#include <i86.h>
29#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000030#ifdef MS_WINDOWS
Guido van Rossum258ccd42001-03-02 06:53:29 +000031#include <windows.h>
Tim Peters58e0a8c2001-05-14 22:32:33 +000032#if defined(MS_WIN16) || defined(__BORLANDC__)
Guido van Rossumb2fb3641996-09-07 00:47:35 +000033/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000034#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000035#define tzname _tzname
36#define daylight _daylight
Tim Peters58e0a8c2001-05-14 22:32:33 +000037#endif /* MS_WIN16 || __BORLANDC__ */
38#ifdef MS_WIN16
Guido van Rossumcc081121995-03-14 15:05:41 +000039#define altzone _altzone
Guido van Rossumb2fb3641996-09-07 00:47:35 +000040#endif /* MS_WIN16 */
Guido van Rossumcac6c721996-09-06 13:34:02 +000041#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000042#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000043
Tim Peters58e0a8c2001-05-14 22:32:33 +000044#if defined(MS_WIN32) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Fred Drakedfb4ebd2000-06-29 20:56:28 +000045/* Win32 has better clock replacement
46 XXX Win64 does not yet, but might when the platform matures. */
Guido van Rossum3917c221997-04-02 05:35:28 +000047#undef HAVE_CLOCK /* We have our own version down below */
Fred Drakedfb4ebd2000-06-29 20:56:28 +000048#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +000049
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000050#if defined(PYOS_OS2)
51#define INCL_DOS
52#define INCL_ERRORS
53#include <os2.h>
54#endif
55
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000056#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000057#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000058#endif
59
Guido van Rossumbcc20741998-08-04 22:53:56 +000060#ifdef __BEOS__
Fred Drake56221a72000-08-15 18:52:33 +000061#include <time.h>
Guido van Rossumbcc20741998-08-04 22:53:56 +000062/* For bigtime_t, snooze(). - [cjh] */
63#include <support/SupportDefs.h>
64#include <kernel/OS.h>
65#endif
66
Guido van Rossum234f9421993-06-17 12:35:49 +000067/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000068static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000069static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000071/* For Y2K check */
72static PyObject *moddict;
73
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000074#ifdef macintosh
75/* Our own timezone. We have enough information to deduce whether
76** DST is on currently, but unfortunately we cannot put it to good
77** use because we don't know the rules (and that is needed to have
78** localtime() return correct tm_isdst values for times other than
79** the current time. So, we cop out and only tell the user the current
80** timezone.
81*/
82static long timezone;
83
Guido van Rossum10b164a2001-09-25 13:59:01 +000084static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000085initmactimezone(void)
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000086{
87 MachineLocation loc;
88 long delta;
89
90 ReadLocation(&loc);
Guido van Rossum10b164a2001-09-25 13:59:01 +000091
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000092 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
93 return;
Guido van Rossum10b164a2001-09-25 13:59:01 +000094
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000095 delta = loc.u.gmtDelta & 0x00FFFFFF;
Guido van Rossum10b164a2001-09-25 13:59:01 +000096
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000097 if (delta & 0x00800000)
98 delta |= 0xFF000000;
Guido van Rossum10b164a2001-09-25 13:59:01 +000099
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000100 timezone = -delta;
101}
102#endif /* macintosh */
103
104
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000105static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000106time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000108 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000109 if (!PyArg_ParseTuple(args, ":time"))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000110 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000111 secs = floattime();
112 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000113 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000114 return NULL;
115 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000116 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000117}
118
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000119static char time_doc[] =
120"time() -> floating point number\n\
121\n\
122Return the current time in seconds since the Epoch.\n\
123Fractions of a second may be present if the system clock provides them.";
124
Guido van Rossumb6775db1994-08-01 11:34:53 +0000125#ifdef HAVE_CLOCK
126
127#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000128#ifdef CLK_TCK
129#define CLOCKS_PER_SEC CLK_TCK
130#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000131#define CLOCKS_PER_SEC 1000000
132#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000133#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000134
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000135static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000136time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000137{
Thomas Woutersfe385252001-01-19 23:16:56 +0000138 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000140 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000142#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143
Tim Peters58e0a8c2001-05-14 22:32:33 +0000144#if defined(MS_WIN32) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Mark Hammond7ba5e812002-02-12 04:02:33 +0000145/* Due to Mark Hammond and Tim Peters */
Guido van Rossum3917c221997-04-02 05:35:28 +0000146static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000147time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000148{
Tim Peters9ad4b682002-02-13 05:14:18 +0000149 static LARGE_INTEGER ctrStart;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000150 static double divisor = 0.0;
Tim Peters9ad4b682002-02-13 05:14:18 +0000151 LARGE_INTEGER now;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000152 double diff;
Guido van Rossum3917c221997-04-02 05:35:28 +0000153
Thomas Woutersfe385252001-01-19 23:16:56 +0000154 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum3917c221997-04-02 05:35:28 +0000155 return NULL;
156
Mark Hammond7ba5e812002-02-12 04:02:33 +0000157 if (divisor == 0.0) {
Tim Peters9ad4b682002-02-13 05:14:18 +0000158 LARGE_INTEGER freq;
159 QueryPerformanceCounter(&ctrStart);
160 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
Mark Hammond7ba5e812002-02-12 04:02:33 +0000161 /* Unlikely to happen - this works on all intel
162 machines at least! Revert to clock() */
Guido van Rossum3917c221997-04-02 05:35:28 +0000163 return PyFloat_FromDouble(clock());
164 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000165 divisor = (double)freq.QuadPart;
Guido van Rossum3917c221997-04-02 05:35:28 +0000166 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000167 QueryPerformanceCounter(&now);
168 diff = (double)(now.QuadPart - ctrStart.QuadPart);
Mark Hammond7ba5e812002-02-12 04:02:33 +0000169 return PyFloat_FromDouble(diff / divisor);
Guido van Rossum3917c221997-04-02 05:35:28 +0000170}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000171
Guido van Rossum3917c221997-04-02 05:35:28 +0000172#define HAVE_CLOCK /* So it gets included in the methods */
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000173#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000174
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000175#ifdef HAVE_CLOCK
176static char clock_doc[] =
177"clock() -> floating point number\n\
178\n\
179Return the CPU time or real time since the start of the process or since\n\
180the first call to clock(). This has as much precision as the system records.";
181#endif
182
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000183static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000184time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000186 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000187 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000189 if (floatsleep(secs) != 0)
190 return NULL;
191 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000192 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193}
194
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000195static char sleep_doc[] =
196"sleep(seconds)\n\
197\n\
198Delay execution for a given number of seconds. The argument may be\n\
199a floating point number for subsecond precision.";
200
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000201static PyStructSequence_Field struct_time_type_fields[] = {
202 {"tm_year", NULL},
203 {"tm_mon", NULL},
204 {"tm_mday", NULL},
205 {"tm_hour", NULL},
206 {"tm_min", NULL},
207 {"tm_sec", NULL},
208 {"tm_wday", NULL},
209 {"tm_yday", NULL},
210 {"tm_isdst", NULL},
211 {0}
212};
213
214static PyStructSequence_Desc struct_time_type_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000215 "time.struct_time",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000216 NULL,
217 struct_time_type_fields,
218 9,
219};
Tim Peters9ad4b682002-02-13 05:14:18 +0000220
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000221static PyTypeObject StructTimeType;
222
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000223static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000224tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000225{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000226 PyObject *v = PyStructSequence_New(&StructTimeType);
227 if (v == NULL)
228 return NULL;
Tim Peters9ad4b682002-02-13 05:14:18 +0000229
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000230#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
231
232 SET(0, p->tm_year + 1900);
233 SET(1, p->tm_mon + 1); /* Want January == 1 */
234 SET(2, p->tm_mday);
235 SET(3, p->tm_hour);
236 SET(4, p->tm_min);
237 SET(5, p->tm_sec);
238 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
239 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
240 SET(8, p->tm_isdst);
241#undef SET
242 if (PyErr_Occurred()) {
243 Py_XDECREF(v);
244 return NULL;
245 }
246
247 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000248}
249
250static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000251time_convert(time_t when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000252{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000253 struct tm *p;
254 errno = 0;
255 p = function(&when);
256 if (p == NULL) {
257#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000258 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000259 errno = EINVAL;
260#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000261 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000262 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000263 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000264}
265
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000266static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000267time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000268{
269 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000270 if (PyTuple_Size(args) == 0)
271 when = floattime();
272 if (!PyArg_ParseTuple(args, "|d:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000273 return NULL;
274 return time_convert((time_t)when, gmtime);
275}
276
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000277static char gmtime_doc[] =
Fred Drake193a3f62002-03-12 21:38:49 +0000278"gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,\n\
279 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000280\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000281Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
282GMT). When 'seconds' is not passed in, convert the current time instead.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000283
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000284static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000285time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000286{
287 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000288 if (PyTuple_Size(args) == 0)
289 when = floattime();
290 if (!PyArg_ParseTuple(args, "|d:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000291 return NULL;
292 return time_convert((time_t)when, localtime);
293}
294
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000295static char localtime_doc[] =
Fred Drake193a3f62002-03-12 21:38:49 +0000296"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 +0000297\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000298Convert seconds since the Epoch to a time tuple expressing local time.\n\
299When 'seconds' is not passed in, convert the current time instead.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000300
Guido van Rossum9e90a671993-06-24 11:10:19 +0000301static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000302gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000303{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000304 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000305 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000306
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000307 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000308 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000309 &p->tm_mon,
310 &p->tm_mday,
311 &p->tm_hour,
312 &p->tm_min,
313 &p->tm_sec,
314 &p->tm_wday,
315 &p->tm_yday,
316 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000317 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000318 if (y < 1900) {
319 PyObject *accept = PyDict_GetItemString(moddict,
320 "accept2dyear");
321 if (accept == NULL || !PyInt_Check(accept) ||
322 PyInt_AsLong(accept) == 0) {
323 PyErr_SetString(PyExc_ValueError,
324 "year >= 1900 required");
325 return 0;
326 }
327 if (69 <= y && y <= 99)
328 y += 1900;
329 else if (0 <= y && y <= 68)
330 y += 2000;
331 else {
332 PyErr_SetString(PyExc_ValueError,
Skip Montanaro1a10aac2001-08-22 12:39:16 +0000333 "year out of range");
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000334 return 0;
335 }
336 }
337 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000338 p->tm_mon--;
339 p->tm_wday = (p->tm_wday + 1) % 7;
340 p->tm_yday--;
341 return 1;
342}
343
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000344#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000345static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000346time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000347{
Thomas Woutersfe385252001-01-19 23:16:56 +0000348 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000349 struct tm buf;
350 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000351 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000352 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000353 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000354
Thomas Wouters334fb892000-07-25 12:56:38 +0000355 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000356
Thomas Woutersfe385252001-01-19 23:16:56 +0000357 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000358 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000359
360 if (tup == NULL) {
361 time_t tt = time(NULL);
362 buf = *localtime(&tt);
363 } else if (!gettmarg(tup, &buf))
364 return NULL;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000365
Guido van Rossumc222ec21999-02-23 00:00:10 +0000366 fmtlen = strlen(fmt);
367
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000368 /* I hate these functions that presume you know how big the output
369 * will be ahead of time...
370 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000371 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000372 outbuf = malloc(i);
373 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000374 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000375 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000376 buflen = strftime(outbuf, i, fmt, &buf);
377 if (buflen > 0 || i >= 256 * fmtlen) {
378 /* If the buffer is 256 times as long as the format,
379 it's probably not failing for lack of room!
380 More likely, the format yields an empty result,
381 e.g. an empty format, or %Z when the timezone
382 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000383 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000384 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000385 free(outbuf);
386 return ret;
387 }
388 free(outbuf);
389 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000390}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000391
392static char strftime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000393"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000394\n\
395Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000396See the library reference manual for formatting codes. When the time tuple\n\
397is not present, current time as returned by localtime() is used.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000398#endif /* HAVE_STRFTIME */
399
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000400#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000401
402#if 0
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000403/* Enable this if it's not declared in <time.h> */
404extern char *strptime(const char *, const char *, struct tm *);
Fred Drakeaff60182000-05-09 19:52:40 +0000405#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000406
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000407static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000408time_strptime(PyObject *self, PyObject *args)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000409{
410 struct tm tm;
411 char *fmt = "%a %b %d %H:%M:%S %Y";
412 char *buf;
413 char *s;
414
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000415 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
416 return NULL;
Thomas Wouters334fb892000-07-25 12:56:38 +0000417 memset((void *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000418 s = strptime(buf, fmt, &tm);
419 if (s == NULL) {
420 PyErr_SetString(PyExc_ValueError, "format mismatch");
421 return NULL;
422 }
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000423 while (*s && isspace(Py_CHARMASK(*s)))
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000424 s++;
425 if (*s) {
426 PyErr_Format(PyExc_ValueError,
427 "unconverted data remains: '%.400s'", s);
428 return NULL;
429 }
430 return tmtotuple(&tm);
431}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000432
433static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000434"strptime(string, format) -> tuple\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000435\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000436Parse a string to a time tuple according to a format specification.\n\
437See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000438#endif /* HAVE_STRPTIME */
439
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000440static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000441time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000442{
Thomas Woutersfe385252001-01-19 23:16:56 +0000443 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000444 struct tm buf;
445 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000446 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000447 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000448 if (tup == NULL) {
449 time_t tt = time(NULL);
450 buf = *localtime(&tt);
451 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000452 return NULL;
453 p = asctime(&buf);
454 if (p[24] == '\n')
455 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000456 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000457}
458
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000459static char asctime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000460"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000461\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000462Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
463When the time tuple is not present, current time as returned by localtime()\n\
464is used.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000465
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000466static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000467time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000468{
469 double dt;
470 time_t tt;
471 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000472
Thomas Woutersfe385252001-01-19 23:16:56 +0000473 if (PyTuple_Size(args) == 0)
474 tt = time(NULL);
475 else {
476 if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
477 return NULL;
478 tt = (time_t)dt;
479 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000480 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000481 if (p == NULL) {
482 PyErr_SetString(PyExc_ValueError, "unconvertible time");
483 return NULL;
484 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000485 if (p[24] == '\n')
486 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000487 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000488}
489
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000490static char ctime_doc[] =
491"ctime(seconds) -> string\n\
492\n\
493Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000494This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
495not present, current time as returned by localtime() is used.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000496
Guido van Rossum60cd8131998-03-06 17:16:21 +0000497#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000498static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000499time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000500{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000501 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000502 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000503 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000504 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000505 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000506 tt = time(&tt);
507 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000508 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000509 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000510 tt = mktime(&buf);
511 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000512 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000513 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000514 return NULL;
515 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000516 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000517}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000518
519static char mktime_doc[] =
520"mktime(tuple) -> floating point number\n\
521\n\
522Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000523#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000524
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000525static PyMethodDef time_methods[] = {
Thomas Woutersfe385252001-01-19 23:16:56 +0000526 {"time", time_time, METH_VARARGS, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000527#ifdef HAVE_CLOCK
Thomas Woutersfe385252001-01-19 23:16:56 +0000528 {"clock", time_clock, METH_VARARGS, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000529#endif
Thomas Woutersfe385252001-01-19 23:16:56 +0000530 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
531 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
532 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000533 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
Thomas Woutersfe385252001-01-19 23:16:56 +0000534 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000535#ifdef HAVE_MKTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000536 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000537#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000538#ifdef HAVE_STRFTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000539 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000540#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000541#ifdef HAVE_STRPTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000542 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000543#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000544 {NULL, NULL} /* sentinel */
545};
546
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000547
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000548static char module_doc[] =
549"This module provides various functions to manipulate time values.\n\
550\n\
551There are two standard representations of time. One is the number\n\
552of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
553or a floating point number (to represent fractions of seconds).\n\
554The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
555The actual value can be retrieved by calling gmtime(0).\n\
556\n\
557The other representation is a tuple of 9 integers giving local time.\n\
558The tuple items are:\n\
559 year (four digits, e.g. 1998)\n\
560 month (1-12)\n\
561 day (1-31)\n\
562 hours (0-23)\n\
563 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000564 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000565 weekday (0-6, Monday is 0)\n\
566 Julian day (day in the year, 1-366)\n\
567 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
568If the DST flag is 0, the time is given in the regular time zone;\n\
569if it is 1, the time is given in the DST time zone;\n\
570if it is -1, mktime() should guess based on the date and time.\n\
571\n\
572Variables:\n\
573\n\
574timezone -- difference in seconds between UTC and local standard time\n\
575altzone -- difference in seconds between UTC and local DST time\n\
576daylight -- whether local time should reflect DST\n\
577tzname -- tuple of (standard time zone name, DST time zone name)\n\
578\n\
579Functions:\n\
580\n\
581time() -- return current time in seconds since the Epoch as a float\n\
582clock() -- return CPU time since process start as a float\n\
583sleep() -- delay for a number of seconds given as a float\n\
584gmtime() -- convert seconds since Epoch to UTC tuple\n\
585localtime() -- convert seconds since Epoch to local time tuple\n\
586asctime() -- convert time tuple to string\n\
587ctime() -- convert time in seconds to string\n\
588mktime() -- convert local time tuple to seconds since Epoch\n\
589strftime() -- convert time tuple to string according to format specification\n\
590strptime() -- parse string to time tuple according to format specification\n\
591";
Guido van Rossum10b164a2001-09-25 13:59:01 +0000592
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000593
Guido van Rossum3886bb61998-12-04 18:50:17 +0000594DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000595inittime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000596{
Fred Drake9bb74322002-04-01 14:49:59 +0000597 PyObject *m;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000598 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000599 m = Py_InitModule3("time", time_methods, module_doc);
Fred Drake9bb74322002-04-01 14:49:59 +0000600
Guido van Rossumc2068731998-10-07 16:35:25 +0000601 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000602 p = Py_GETENV("PYTHONY2K");
Fred Drake9bb74322002-04-01 14:49:59 +0000603 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
Guido van Rossumc2068731998-10-07 16:35:25 +0000604 /* Squirrel away the module's dictionary for the y2k check */
Fred Drake9bb74322002-04-01 14:49:59 +0000605 moddict = PyModule_GetDict(m);
606 Py_INCREF(moddict);
Guido van Rossum10b164a2001-09-25 13:59:01 +0000607#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000608 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000609#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000610 PyModule_AddIntConstant(m, "timezone", _timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000611#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000612 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000613#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000614#ifdef HAVE_ALTZONE
Fred Drake9bb74322002-04-01 14:49:59 +0000615 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000616#else
Guido van Rossum26452411998-09-28 22:07:11 +0000617#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000618 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000619#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000620 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000621#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000622#endif
Fred Drake9bb74322002-04-01 14:49:59 +0000623 PyModule_AddIntConstant(m, "daylight", daylight);
624 PyModule_AddObject(m, "tzname",
625 Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000626#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000627#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000628 {
629#define YEAR ((time_t)((365 * 24 + 6) * 3600))
630 time_t t;
631 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000632 long janzone, julyzone;
633 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000634 t = (time((time_t *)0) / YEAR) * YEAR;
635 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000636 janzone = -p->tm_gmtoff;
637 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
638 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000639 t += YEAR/2;
640 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000641 julyzone = -p->tm_gmtoff;
642 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
643 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000644
Guido van Rossum57731601999-03-29 19:12:04 +0000645 if( janzone < julyzone ) {
646 /* DST is reversed in the southern hemisphere */
Fred Drake9bb74322002-04-01 14:49:59 +0000647 PyModule_AddIntConstant(m, "timezone", julyzone);
648 PyModule_AddIntConstant(m, "altzone", janzone);
649 PyModule_AddIntConstant(m, "daylight",
650 janzone != julyzone);
651 PyModule_AddObject(m, "tzname",
652 Py_BuildValue("(zz)",
653 julyname, janname));
Guido van Rossum57731601999-03-29 19:12:04 +0000654 } else {
Fred Drake9bb74322002-04-01 14:49:59 +0000655 PyModule_AddIntConstant(m, "timezone", janzone);
656 PyModule_AddIntConstant(m, "altzone", julyzone);
657 PyModule_AddIntConstant(m, "daylight",
658 janzone != julyzone);
659 PyModule_AddObject(m, "tzname",
660 Py_BuildValue("(zz)",
661 janname, julyname));
Guido van Rossum57731601999-03-29 19:12:04 +0000662 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000663 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000664#else
665#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000666 /* The only thing we can obtain is the current timezone
667 ** (and whether dst is currently _active_, but that is not what
668 ** we're looking for:-( )
669 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000670 initmactimezone();
Fred Drake9bb74322002-04-01 14:49:59 +0000671 PyModule_AddIntConstant(m, "timezone", timezone);
672 PyModule_AddIntConstant(m, "altzone", timezone);
673 PyModule_AddIntConstant(m, "daylight", 0);
674 PyModule_AddObject(m, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000675#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000676#endif /* HAVE_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000677#ifdef __CYGWIN__
678 tzset();
Fred Drake9bb74322002-04-01 14:49:59 +0000679 PyModule_AddIntConstant(m, "timezone", _timezone);
680 PyModule_AddIntConstant(m, "altzone", _timezone);
681 PyModule_AddIntConstant(m, "daylight", _daylight);
682 PyModule_AddObject(m, "tzname",
683 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +0000684#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000685#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000686
687 PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
Fred Drake9bb74322002-04-01 14:49:59 +0000688 Py_INCREF(&StructTimeType);
689 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000690}
691
692
Guido van Rossumb6775db1994-08-01 11:34:53 +0000693/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000694
Guido van Rossumb6775db1994-08-01 11:34:53 +0000695static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000696floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000697{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000698 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000699 (1) gettimeofday() -- resolution in microseconds
700 (2) ftime() -- resolution in milliseconds
701 (3) time() -- resolution in seconds
702 In all cases the return value is a float in seconds.
703 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
704 fail, so we fall back on ftime() or time().
705 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000706#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000707 {
708 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000709#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000710 if (gettimeofday(&t) == 0)
711 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000712#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000713 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
714 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000715#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000716 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000717#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000718 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000719#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000720 struct timeb t;
721 ftime(&t);
722 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000723#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000724 time_t secs;
725 time(&secs);
726 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000727#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000728 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000729}
730
Guido van Rossumb6775db1994-08-01 11:34:53 +0000731
732/* Implement floatsleep() for various platforms.
733 When interrupted (or when another error occurs), return -1 and
734 set an exception; else return 0. */
735
736static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000737floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000738{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000739/* XXX Should test for MS_WIN32 first! */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000740#if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000741 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000742 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000743 frac = fmod(secs, 1.0);
744 secs = floor(secs);
745 t.tv_sec = (long)secs;
746 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000747 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000748 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000749#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000750 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000751#else
752 if (1) {
753#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000754 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000755 PyErr_SetFromErrno(PyExc_IOError);
756 return -1;
757 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000758 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000759 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000760#elif defined(macintosh)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000761#define MacTicks (* (long *)0x16A)
762 long deadline;
763 deadline = MacTicks + (long)(secs * 60.0);
764 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000765 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000766 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000767 return -1;
768 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000769#elif defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000770 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000771 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000772 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000773 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000774#elif defined(MSDOS)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000775 struct timeb t1, t2;
776 double frac;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000777 extern double fmod(double, double);
778 extern double floor(double);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000779 if (secs <= 0.0)
780 return;
781 frac = fmod(secs, 1.0);
782 secs = floor(secs);
783 ftime(&t1);
784 t2.time = t1.time + (int)secs;
785 t2.millitm = t1.millitm + (int)(frac*1000.0);
786 while (t2.millitm >= 1000) {
787 t2.time++;
788 t2.millitm -= 1000;
789 }
790 for (;;) {
791#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000792 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000793 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000794 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000795#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000796 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000797 return -1;
798 ftime(&t1);
799 if (t1.time > t2.time ||
800 t1.time == t2.time && t1.millitm >= t2.millitm)
801 break;
802 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000803#elif defined(MS_WIN32)
Fred Drake0e123952000-06-29 21:31:02 +0000804 {
805 double millisecs = secs * 1000.0;
806 if (millisecs > (double)ULONG_MAX) {
807 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
808 return -1;
809 }
810 /* XXX Can't interrupt this sleep */
811 Py_BEGIN_ALLOW_THREADS
812 Sleep((unsigned long)millisecs);
813 Py_END_ALLOW_THREADS
814 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000815#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000816 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000817 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000818 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000819 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000820 PyErr_SetFromErrno(PyExc_IOError);
821 return -1;
822 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000823 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000824#elif defined(__BEOS__)
Guido van Rossumbcc20741998-08-04 22:53:56 +0000825 /* This sleep *CAN BE* interrupted. */
826 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000827 if( secs <= 0.0 ) {
828 return;
829 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000830
Guido van Rossumbcc20741998-08-04 22:53:56 +0000831 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000832 /* BeOS snooze() is in microseconds... */
833 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000834 Py_BLOCK_THREADS
835 PyErr_SetFromErrno( PyExc_IOError );
836 return -1;
837 }
838 Py_END_ALLOW_THREADS
839 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000840#elif defined(RISCOS)
Guido van Rossumbceccf52001-04-10 22:07:43 +0000841 if (secs <= 0.0)
842 return 0;
843 Py_BEGIN_ALLOW_THREADS
844 /* This sleep *CAN BE* interrupted. */
845 if ( sleep(secs) )
846 return -1;
847 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000848#elif defined(PLAN9)
849 {
850 double millisecs = secs * 1000.0;
851 if (millisecs > (double)LONG_MAX) {
852 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
853 return -1;
854 }
855 /* This sleep *CAN BE* interrupted. */
856 Py_BEGIN_ALLOW_THREADS
857 if(sleep((long)millisecs) < 0){
858 Py_BLOCK_THREADS
859 PyErr_SetFromErrno(PyExc_IOError);
860 return -1;
861 }
862 Py_END_ALLOW_THREADS
863 }
864#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000865 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000866 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000867 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000868 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000869#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000870
Guido van Rossumb6775db1994-08-01 11:34:53 +0000871 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000872}