blob: b009ea17b9ba1e1175d106edfa863dd9df0a2ae3 [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>
Jack Jansen63596ae2000-12-12 22:42:30 +000012#ifdef USE_GUSI211
Guido van Rossumc410e922000-04-26 20:40:13 +000013/* GUSI, the I/O library which has the time() function and such uses the
14** Mac epoch of 1904. MSL, the C library which has localtime() and so uses
15** the ANSI epoch of 1900.
16*/
17#define GUSI_TO_MSL_EPOCH (4*365*24*60*60)
18#endif /* USE_GUSI2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000019#else
20#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000021#endif
22
Guido van Rossumb6775db1994-08-01 11:34:53 +000023#ifdef QUICKWIN
24#include <io.h>
25#endif
26
Guido van Rossumb6775db1994-08-01 11:34:53 +000027#ifdef HAVE_FTIME
28#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000029#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000030extern int ftime(struct timeb *);
Guido van Rossum52174571996-12-09 18:38:52 +000031#endif /* MS_WINDOWS */
32#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033
Guido van Rossum7bf22de1997-12-02 20:34:19 +000034#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000035#include <i86.h>
36#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000037#ifdef MS_WINDOWS
Guido van Rossum258ccd42001-03-02 06:53:29 +000038#include <windows.h>
Tim Peters58e0a8c2001-05-14 22:32:33 +000039#if defined(MS_WIN16) || defined(__BORLANDC__)
Guido van Rossumb2fb3641996-09-07 00:47:35 +000040/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000041#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000042#define tzname _tzname
43#define daylight _daylight
Tim Peters58e0a8c2001-05-14 22:32:33 +000044#endif /* MS_WIN16 || __BORLANDC__ */
45#ifdef MS_WIN16
Guido van Rossumcc081121995-03-14 15:05:41 +000046#define altzone _altzone
Guido van Rossumb2fb3641996-09-07 00:47:35 +000047#endif /* MS_WIN16 */
Guido van Rossumcac6c721996-09-06 13:34:02 +000048#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000049#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000050
Tim Peters58e0a8c2001-05-14 22:32:33 +000051#if defined(MS_WIN32) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Fred Drakedfb4ebd2000-06-29 20:56:28 +000052/* Win32 has better clock replacement
53 XXX Win64 does not yet, but might when the platform matures. */
Guido van Rossum3917c221997-04-02 05:35:28 +000054#undef HAVE_CLOCK /* We have our own version down below */
Fred Drakedfb4ebd2000-06-29 20:56:28 +000055#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +000056
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000057#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000058#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000059#endif
60
Guido van Rossumbcc20741998-08-04 22:53:56 +000061#ifdef __BEOS__
Fred Drake56221a72000-08-15 18:52:33 +000062#include <time.h>
Guido van Rossumbcc20741998-08-04 22:53:56 +000063/* For bigtime_t, snooze(). - [cjh] */
64#include <support/SupportDefs.h>
65#include <kernel/OS.h>
66#endif
67
Guido van Rossum234f9421993-06-17 12:35:49 +000068/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000069static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000070static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000072/* For Y2K check */
73static PyObject *moddict;
74
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000075#ifdef macintosh
76/* Our own timezone. We have enough information to deduce whether
77** DST is on currently, but unfortunately we cannot put it to good
78** use because we don't know the rules (and that is needed to have
79** localtime() return correct tm_isdst values for times other than
80** the current time. So, we cop out and only tell the user the current
81** timezone.
82*/
83static long timezone;
84
Guido van Rossum10b164a2001-09-25 13:59:01 +000085static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000086initmactimezone(void)
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000087{
88 MachineLocation loc;
89 long delta;
90
91 ReadLocation(&loc);
Guido van Rossum10b164a2001-09-25 13:59:01 +000092
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000093 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
94 return;
Guido van Rossum10b164a2001-09-25 13:59:01 +000095
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000096 delta = loc.u.gmtDelta & 0x00FFFFFF;
Guido van Rossum10b164a2001-09-25 13:59:01 +000097
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000098 if (delta & 0x00800000)
99 delta |= 0xFF000000;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000100
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000101 timezone = -delta;
102}
103#endif /* macintosh */
104
105
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000106static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000107time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000109 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000110 if (!PyArg_ParseTuple(args, ":time"))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000111 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000112 secs = floattime();
113 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000114 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000115 return NULL;
116 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000117 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000118}
119
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000120static char time_doc[] =
121"time() -> floating point number\n\
122\n\
123Return the current time in seconds since the Epoch.\n\
124Fractions of a second may be present if the system clock provides them.";
125
Guido van Rossumb6775db1994-08-01 11:34:53 +0000126#ifdef HAVE_CLOCK
127
128#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000129#ifdef CLK_TCK
130#define CLOCKS_PER_SEC CLK_TCK
131#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000132#define CLOCKS_PER_SEC 1000000
133#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000134#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000135
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000136static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000137time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000138{
Thomas Woutersfe385252001-01-19 23:16:56 +0000139 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000141 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000143#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144
Tim Peters58e0a8c2001-05-14 22:32:33 +0000145#if defined(MS_WIN32) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Mark Hammond7ba5e812002-02-12 04:02:33 +0000146/* Due to Mark Hammond and Tim Peters */
Guido van Rossum3917c221997-04-02 05:35:28 +0000147static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000148time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000149{
Mark Hammond7ba5e812002-02-12 04:02:33 +0000150 static LONG_LONG ctrStart;
151 static double divisor = 0.0;
152 LONG_LONG now;
153 double diff;
Guido van Rossum3917c221997-04-02 05:35:28 +0000154
Mark Hammond7ba5e812002-02-12 04:02:33 +0000155 assert(sizeof(LONG_LONG) == sizeof(LARGE_INTEGER));
Thomas Woutersfe385252001-01-19 23:16:56 +0000156 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum3917c221997-04-02 05:35:28 +0000157 return NULL;
158
Mark Hammond7ba5e812002-02-12 04:02:33 +0000159 if (divisor == 0.0) {
160 LONG_LONG freq;
161 QueryPerformanceCounter((LARGE_INTEGER*)&ctrStart);
162 if (!QueryPerformanceFrequency((LARGE_INTEGER*)&freq) ||
163 freq == 0) {
164 /* Unlikely to happen - this works on all intel
165 machines at least! Revert to clock() */
Guido van Rossum3917c221997-04-02 05:35:28 +0000166 return PyFloat_FromDouble(clock());
167 }
Mark Hammond7ba5e812002-02-12 04:02:33 +0000168 divisor = (double)freq;
Guido van Rossum3917c221997-04-02 05:35:28 +0000169 }
Mark Hammond7ba5e812002-02-12 04:02:33 +0000170 QueryPerformanceCounter((LARGE_INTEGER*)&now);
171 diff = (double)(now - ctrStart);
172 return PyFloat_FromDouble(diff / divisor);
Guido van Rossum3917c221997-04-02 05:35:28 +0000173}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000174
Guido van Rossum3917c221997-04-02 05:35:28 +0000175#define HAVE_CLOCK /* So it gets included in the methods */
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000176#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000177
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000178#ifdef HAVE_CLOCK
179static char clock_doc[] =
180"clock() -> floating point number\n\
181\n\
182Return the CPU time or real time since the start of the process or since\n\
183the first call to clock(). This has as much precision as the system records.";
184#endif
185
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000186static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000187time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000189 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000190 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000192 if (floatsleep(secs) != 0)
193 return NULL;
194 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000195 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000196}
197
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000198static char sleep_doc[] =
199"sleep(seconds)\n\
200\n\
201Delay execution for a given number of seconds. The argument may be\n\
202a floating point number for subsecond precision.";
203
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000204static PyStructSequence_Field struct_time_type_fields[] = {
205 {"tm_year", NULL},
206 {"tm_mon", NULL},
207 {"tm_mday", NULL},
208 {"tm_hour", NULL},
209 {"tm_min", NULL},
210 {"tm_sec", NULL},
211 {"tm_wday", NULL},
212 {"tm_yday", NULL},
213 {"tm_isdst", NULL},
214 {0}
215};
216
217static PyStructSequence_Desc struct_time_type_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000218 "time.struct_time",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000219 NULL,
220 struct_time_type_fields,
221 9,
222};
223
224static PyTypeObject StructTimeType;
225
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000226static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000227tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000228{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000229 PyObject *v = PyStructSequence_New(&StructTimeType);
230 if (v == NULL)
231 return NULL;
232
233#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
234
235 SET(0, p->tm_year + 1900);
236 SET(1, p->tm_mon + 1); /* Want January == 1 */
237 SET(2, p->tm_mday);
238 SET(3, p->tm_hour);
239 SET(4, p->tm_min);
240 SET(5, p->tm_sec);
241 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
242 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
243 SET(8, p->tm_isdst);
244#undef SET
245 if (PyErr_Occurred()) {
246 Py_XDECREF(v);
247 return NULL;
248 }
249
250 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000251}
252
253static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000254time_convert(time_t when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000255{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000256 struct tm *p;
257 errno = 0;
Jack Jansenee398fa2000-07-03 21:37:27 +0000258#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000259 when = when + GUSI_TO_MSL_EPOCH;
260#endif
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000261 p = function(&when);
262 if (p == NULL) {
263#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000264 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000265 errno = EINVAL;
266#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000267 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000268 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000269 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000270}
271
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000272static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000273time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000274{
275 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000276 if (PyTuple_Size(args) == 0)
277 when = floattime();
278 if (!PyArg_ParseTuple(args, "|d:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000279 return NULL;
280 return time_convert((time_t)when, gmtime);
281}
282
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000283static char gmtime_doc[] =
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000284"gmtime([seconds]) -> (year,month,day,hour,minute,second,weekday,dayofyear,dst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000285\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000286Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
287GMT). When 'seconds' is not passed in, convert the current time instead.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000288
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000289static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000290time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000291{
292 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000293 if (PyTuple_Size(args) == 0)
294 when = floattime();
295 if (!PyArg_ParseTuple(args, "|d:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000296 return NULL;
297 return time_convert((time_t)when, localtime);
298}
299
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000300static char localtime_doc[] =
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000301"localtime([seconds]) -> (year,month,day,hour,minute,second,weekday,dayofyear,dst)\n\
302\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000303Convert seconds since the Epoch to a time tuple expressing local time.\n\
304When 'seconds' is not passed in, convert the current time instead.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000305
Guido van Rossum9e90a671993-06-24 11:10:19 +0000306static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000307gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000308{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000309 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000310 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000311
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000312 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000313 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000314 &p->tm_mon,
315 &p->tm_mday,
316 &p->tm_hour,
317 &p->tm_min,
318 &p->tm_sec,
319 &p->tm_wday,
320 &p->tm_yday,
321 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000322 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000323 if (y < 1900) {
324 PyObject *accept = PyDict_GetItemString(moddict,
325 "accept2dyear");
326 if (accept == NULL || !PyInt_Check(accept) ||
327 PyInt_AsLong(accept) == 0) {
328 PyErr_SetString(PyExc_ValueError,
329 "year >= 1900 required");
330 return 0;
331 }
332 if (69 <= y && y <= 99)
333 y += 1900;
334 else if (0 <= y && y <= 68)
335 y += 2000;
336 else {
337 PyErr_SetString(PyExc_ValueError,
Skip Montanaro1a10aac2001-08-22 12:39:16 +0000338 "year out of range");
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000339 return 0;
340 }
341 }
342 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000343 p->tm_mon--;
344 p->tm_wday = (p->tm_wday + 1) % 7;
345 p->tm_yday--;
346 return 1;
347}
348
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000349#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000350static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000351time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000352{
Thomas Woutersfe385252001-01-19 23:16:56 +0000353 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000354 struct tm buf;
355 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000356 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000357 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000358 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000359
Thomas Wouters334fb892000-07-25 12:56:38 +0000360 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000361
Thomas Woutersfe385252001-01-19 23:16:56 +0000362 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000363 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000364
365 if (tup == NULL) {
366 time_t tt = time(NULL);
367 buf = *localtime(&tt);
368 } else if (!gettmarg(tup, &buf))
369 return NULL;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000370
Guido van Rossumc222ec21999-02-23 00:00:10 +0000371 fmtlen = strlen(fmt);
372
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000373 /* I hate these functions that presume you know how big the output
374 * will be ahead of time...
375 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000376 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000377 outbuf = malloc(i);
378 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000379 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000380 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000381 buflen = strftime(outbuf, i, fmt, &buf);
382 if (buflen > 0 || i >= 256 * fmtlen) {
383 /* If the buffer is 256 times as long as the format,
384 it's probably not failing for lack of room!
385 More likely, the format yields an empty result,
386 e.g. an empty format, or %Z when the timezone
387 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000388 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000389 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000390 free(outbuf);
391 return ret;
392 }
393 free(outbuf);
394 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000395}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000396
397static char strftime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000398"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000399\n\
400Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000401See the library reference manual for formatting codes. When the time tuple\n\
402is not present, current time as returned by localtime() is used.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000403#endif /* HAVE_STRFTIME */
404
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000405#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000406
407#if 0
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000408/* Enable this if it's not declared in <time.h> */
409extern char *strptime(const char *, const char *, struct tm *);
Fred Drakeaff60182000-05-09 19:52:40 +0000410#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000411
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000412static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000413time_strptime(PyObject *self, PyObject *args)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000414{
415 struct tm tm;
416 char *fmt = "%a %b %d %H:%M:%S %Y";
417 char *buf;
418 char *s;
419
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000420 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
421 return NULL;
Thomas Wouters334fb892000-07-25 12:56:38 +0000422 memset((void *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000423 s = strptime(buf, fmt, &tm);
424 if (s == NULL) {
425 PyErr_SetString(PyExc_ValueError, "format mismatch");
426 return NULL;
427 }
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000428 while (*s && isspace(Py_CHARMASK(*s)))
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000429 s++;
430 if (*s) {
431 PyErr_Format(PyExc_ValueError,
432 "unconverted data remains: '%.400s'", s);
433 return NULL;
434 }
435 return tmtotuple(&tm);
436}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000437
438static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000439"strptime(string, format) -> tuple\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\
442See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000443#endif /* HAVE_STRPTIME */
444
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
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000464static char 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\
469is 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 }
Jack Jansenee398fa2000-07-03 21:37:27 +0000485#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000486 tt = tt + GUSI_TO_MSL_EPOCH;
487#endif
Guido van Rossum9e90a671993-06-24 11:10:19 +0000488 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000489 if (p == NULL) {
490 PyErr_SetString(PyExc_ValueError, "unconvertible time");
491 return NULL;
492 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000493 if (p[24] == '\n')
494 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000495 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000496}
497
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000498static char ctime_doc[] =
499"ctime(seconds) -> string\n\
500\n\
501Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000502This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
503not present, current time as returned by localtime() is used.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000504
Guido van Rossum60cd8131998-03-06 17:16:21 +0000505#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000506static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000507time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000508{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000509 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000510 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000511 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000512 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000513 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000514 tt = time(&tt);
515 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000516 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000517 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000518 tt = mktime(&buf);
519 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000520 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000521 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000522 return NULL;
523 }
Jack Jansen63596ae2000-12-12 22:42:30 +0000524#if defined(macintosh) && defined(USE_GUSI211)
Guido van Rossumc410e922000-04-26 20:40:13 +0000525 tt = tt - GUSI_TO_MSL_EPOCH;
526#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000527 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000528}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000529
530static char mktime_doc[] =
531"mktime(tuple) -> floating point number\n\
532\n\
533Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000534#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000535
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000536static PyMethodDef time_methods[] = {
Thomas Woutersfe385252001-01-19 23:16:56 +0000537 {"time", time_time, METH_VARARGS, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000538#ifdef HAVE_CLOCK
Thomas Woutersfe385252001-01-19 23:16:56 +0000539 {"clock", time_clock, METH_VARARGS, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000540#endif
Thomas Woutersfe385252001-01-19 23:16:56 +0000541 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
542 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
543 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000544 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
Thomas Woutersfe385252001-01-19 23:16:56 +0000545 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000546#ifdef HAVE_MKTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000547 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000548#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000549#ifdef HAVE_STRFTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000550 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000551#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000552#ifdef HAVE_STRPTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000553 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000554#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555 {NULL, NULL} /* sentinel */
556};
557
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000558static void
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000559ins(PyObject *d, char *name, PyObject *v)
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000560{
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000561 /* Don't worry too much about errors, they'll be caught by the
562 * caller of inittime().
563 */
564 if (v)
565 PyDict_SetItemString(d, name, v);
566 Py_XDECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000567}
568
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000569
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000570static char module_doc[] =
571"This module provides various functions to manipulate time values.\n\
572\n\
573There are two standard representations of time. One is the number\n\
574of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
575or a floating point number (to represent fractions of seconds).\n\
576The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
577The actual value can be retrieved by calling gmtime(0).\n\
578\n\
579The other representation is a tuple of 9 integers giving local time.\n\
580The tuple items are:\n\
581 year (four digits, e.g. 1998)\n\
582 month (1-12)\n\
583 day (1-31)\n\
584 hours (0-23)\n\
585 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000586 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000587 weekday (0-6, Monday is 0)\n\
588 Julian day (day in the year, 1-366)\n\
589 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
590If the DST flag is 0, the time is given in the regular time zone;\n\
591if it is 1, the time is given in the DST time zone;\n\
592if it is -1, mktime() should guess based on the date and time.\n\
593\n\
594Variables:\n\
595\n\
596timezone -- difference in seconds between UTC and local standard time\n\
597altzone -- difference in seconds between UTC and local DST time\n\
598daylight -- whether local time should reflect DST\n\
599tzname -- tuple of (standard time zone name, DST time zone name)\n\
600\n\
601Functions:\n\
602\n\
603time() -- return current time in seconds since the Epoch as a float\n\
604clock() -- return CPU time since process start as a float\n\
605sleep() -- delay for a number of seconds given as a float\n\
606gmtime() -- convert seconds since Epoch to UTC tuple\n\
607localtime() -- convert seconds since Epoch to local time tuple\n\
608asctime() -- convert time tuple to string\n\
609ctime() -- convert time in seconds to string\n\
610mktime() -- convert local time tuple to seconds since Epoch\n\
611strftime() -- convert time tuple to string according to format specification\n\
612strptime() -- parse string to time tuple according to format specification\n\
613";
Guido van Rossum10b164a2001-09-25 13:59:01 +0000614
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000615
Guido van Rossum3886bb61998-12-04 18:50:17 +0000616DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000617inittime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000618{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000619 PyObject *m, *d;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000620 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000621 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000622 d = PyModule_GetDict(m);
Guido van Rossumc2068731998-10-07 16:35:25 +0000623 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000624 p = Py_GETENV("PYTHONY2K");
Guido van Rossumc2068731998-10-07 16:35:25 +0000625 ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
626 /* Squirrel away the module's dictionary for the y2k check */
627 Py_INCREF(d);
628 moddict = d;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000629#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000630 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000631#ifdef PYOS_OS2
632 ins(d, "timezone", PyInt_FromLong((long)_timezone));
633#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000634 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossum26452411998-09-28 22:07:11 +0000635#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000636#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000637 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000638#else
Guido van Rossum26452411998-09-28 22:07:11 +0000639#ifdef PYOS_OS2
640 ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
641#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000642 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossum26452411998-09-28 22:07:11 +0000643#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000644#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000645 ins(d, "daylight", PyInt_FromLong((long)daylight));
646 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000647#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000648#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000649 {
650#define YEAR ((time_t)((365 * 24 + 6) * 3600))
651 time_t t;
652 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000653 long janzone, julyzone;
654 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000655 t = (time((time_t *)0) / YEAR) * YEAR;
656 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000657 janzone = -p->tm_gmtoff;
658 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
659 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000660 t += YEAR/2;
661 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000662 julyzone = -p->tm_gmtoff;
663 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
664 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000665
Guido van Rossum57731601999-03-29 19:12:04 +0000666 if( janzone < julyzone ) {
667 /* DST is reversed in the southern hemisphere */
668 ins(d, "timezone", PyInt_FromLong(julyzone));
669 ins(d, "altzone", PyInt_FromLong(janzone));
670 ins(d, "daylight",
671 PyInt_FromLong((long)(janzone != julyzone)));
672 ins(d, "tzname",
673 Py_BuildValue("(zz)", julyname, janname));
674 } else {
675 ins(d, "timezone", PyInt_FromLong(janzone));
676 ins(d, "altzone", PyInt_FromLong(julyzone));
677 ins(d, "daylight",
678 PyInt_FromLong((long)(janzone != julyzone)));
679 ins(d, "tzname",
680 Py_BuildValue("(zz)", janname, julyname));
681 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000682 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000683#else
684#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000685 /* The only thing we can obtain is the current timezone
686 ** (and whether dst is currently _active_, but that is not what
687 ** we're looking for:-( )
688 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000689 initmactimezone();
690 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000691 ins(d, "altzone", PyInt_FromLong(timezone));
692 ins(d, "daylight", PyInt_FromLong((long)0));
693 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000694#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000695#endif /* HAVE_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000696#ifdef __CYGWIN__
697 tzset();
698 ins(d, "timezone", PyInt_FromLong(_timezone));
699 ins(d, "altzone", PyInt_FromLong(_timezone));
700 ins(d, "daylight", PyInt_FromLong(_daylight));
701 ins(d, "tzname", Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
702#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000703#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000704
705 PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
706 PyDict_SetItemString(d, "struct_time", (PyObject*) &StructTimeType);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000707}
708
709
Guido van Rossumb6775db1994-08-01 11:34:53 +0000710/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000711
Guido van Rossumb6775db1994-08-01 11:34:53 +0000712static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000713floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000714{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000715 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000716 (1) gettimeofday() -- resolution in microseconds
717 (2) ftime() -- resolution in milliseconds
718 (3) time() -- resolution in seconds
719 In all cases the return value is a float in seconds.
720 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
721 fail, so we fall back on ftime() or time().
722 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000723#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000724 {
725 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000726#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000727 if (gettimeofday(&t) == 0)
728 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000729#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000730 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
731 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000732#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000733 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000734#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000735 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000736#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000737 struct timeb t;
738 ftime(&t);
739 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000740#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000741 time_t secs;
742 time(&secs);
743 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000744#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000745 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000746}
747
Guido van Rossumb6775db1994-08-01 11:34:53 +0000748
749/* Implement floatsleep() for various platforms.
750 When interrupted (or when another error occurs), return -1 and
751 set an exception; else return 0. */
752
753static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000754floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000755{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000756/* XXX Should test for MS_WIN32 first! */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000757#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000758 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000759 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000760 frac = fmod(secs, 1.0);
761 secs = floor(secs);
762 t.tv_sec = (long)secs;
763 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000764 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000765 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000766#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000767 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000768#else
769 if (1) {
770#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000771 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000772 PyErr_SetFromErrno(PyExc_IOError);
773 return -1;
774 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000775 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000776 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000777#elif defined(macintosh)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000778#define MacTicks (* (long *)0x16A)
779 long deadline;
780 deadline = MacTicks + (long)(secs * 60.0);
781 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000782 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000783 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000784 return -1;
785 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000786#elif defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000787 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000788 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000789 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000790 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000791#elif defined(MSDOS)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000792 struct timeb t1, t2;
793 double frac;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000794 extern double fmod(double, double);
795 extern double floor(double);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000796 if (secs <= 0.0)
797 return;
798 frac = fmod(secs, 1.0);
799 secs = floor(secs);
800 ftime(&t1);
801 t2.time = t1.time + (int)secs;
802 t2.millitm = t1.millitm + (int)(frac*1000.0);
803 while (t2.millitm >= 1000) {
804 t2.time++;
805 t2.millitm -= 1000;
806 }
807 for (;;) {
808#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000809 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000810 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000811 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000812#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000813 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000814 return -1;
815 ftime(&t1);
816 if (t1.time > t2.time ||
817 t1.time == t2.time && t1.millitm >= t2.millitm)
818 break;
819 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000820#elif defined(MS_WIN32)
Fred Drake0e123952000-06-29 21:31:02 +0000821 {
822 double millisecs = secs * 1000.0;
823 if (millisecs > (double)ULONG_MAX) {
824 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
825 return -1;
826 }
827 /* XXX Can't interrupt this sleep */
828 Py_BEGIN_ALLOW_THREADS
829 Sleep((unsigned long)millisecs);
830 Py_END_ALLOW_THREADS
831 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000832#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000833 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000834 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000835 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000836 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000837 PyErr_SetFromErrno(PyExc_IOError);
838 return -1;
839 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000840 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000841#elif defined(__BEOS__)
Guido van Rossumbcc20741998-08-04 22:53:56 +0000842 /* This sleep *CAN BE* interrupted. */
843 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000844 if( secs <= 0.0 ) {
845 return;
846 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000847
Guido van Rossumbcc20741998-08-04 22:53:56 +0000848 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000849 /* BeOS snooze() is in microseconds... */
850 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000851 Py_BLOCK_THREADS
852 PyErr_SetFromErrno( PyExc_IOError );
853 return -1;
854 }
855 Py_END_ALLOW_THREADS
856 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000857#elif defined(RISCOS)
Guido van Rossumbceccf52001-04-10 22:07:43 +0000858 if (secs <= 0.0)
859 return 0;
860 Py_BEGIN_ALLOW_THREADS
861 /* This sleep *CAN BE* interrupted. */
862 if ( sleep(secs) )
863 return -1;
864 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000865#elif defined(PLAN9)
866 {
867 double millisecs = secs * 1000.0;
868 if (millisecs > (double)LONG_MAX) {
869 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
870 return -1;
871 }
872 /* This sleep *CAN BE* interrupted. */
873 Py_BEGIN_ALLOW_THREADS
874 if(sleep((long)millisecs) < 0){
875 Py_BLOCK_THREADS
876 PyErr_SetFromErrno(PyExc_IOError);
877 return -1;
878 }
879 Py_END_ALLOW_THREADS
880 }
881#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000882 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000883 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000884 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000885 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000886#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000887
Guido van Rossumb6775db1994-08-01 11:34:53 +0000888 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000889}