blob: bb7f3587e752c083d1af1b61c410177d1625d5c2 [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
Guido van Rossum48a680c2001-03-02 06:34:14 +000020#ifndef RISCOS
Guido van Rossumb6775db1994-08-01 11:34:53 +000021#include <sys/types.h>
Guido van Rossum48a680c2001-03-02 06:34:14 +000022#endif /* RISCOS */
Guido van Rossum6d946f91992-08-14 13:49:30 +000023#endif
24
Guido van Rossumb6775db1994-08-01 11:34:53 +000025#ifdef QUICKWIN
26#include <io.h>
27#endif
28
29#ifdef HAVE_UNISTD_H
Guido van Rossum2762f251992-03-27 17:22:13 +000030#include <unistd.h>
31#endif
32
Guido van Rossumb6775db1994-08-01 11:34:53 +000033#ifdef HAVE_FTIME
34#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000035#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000036extern int ftime(struct timeb *);
Guido van Rossum52174571996-12-09 18:38:52 +000037#endif /* MS_WINDOWS */
38#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039
Guido van Rossum7bf22de1997-12-02 20:34:19 +000040#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000041#include <i86.h>
42#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000043#ifdef MS_WINDOWS
Guido van Rossum258ccd42001-03-02 06:53:29 +000044#include <windows.h>
Tim Peters58e0a8c2001-05-14 22:32:33 +000045#if defined(MS_WIN16) || defined(__BORLANDC__)
Guido van Rossumb2fb3641996-09-07 00:47:35 +000046/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000047#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000048#define tzname _tzname
49#define daylight _daylight
Tim Peters58e0a8c2001-05-14 22:32:33 +000050#endif /* MS_WIN16 || __BORLANDC__ */
51#ifdef MS_WIN16
Guido van Rossumcc081121995-03-14 15:05:41 +000052#define altzone _altzone
Guido van Rossumb2fb3641996-09-07 00:47:35 +000053#endif /* MS_WIN16 */
Guido van Rossumcac6c721996-09-06 13:34:02 +000054#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000055#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000056
Tim Peters58e0a8c2001-05-14 22:32:33 +000057#if defined(MS_WIN32) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Fred Drakedfb4ebd2000-06-29 20:56:28 +000058/* Win32 has better clock replacement
59 XXX Win64 does not yet, but might when the platform matures. */
Guido van Rossum3917c221997-04-02 05:35:28 +000060#include <largeint.h>
61#undef HAVE_CLOCK /* We have our own version down below */
Fred Drakedfb4ebd2000-06-29 20:56:28 +000062#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +000063
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000064#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000065#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000066#endif
67
Guido van Rossumbcc20741998-08-04 22:53:56 +000068#ifdef __BEOS__
Fred Drake56221a72000-08-15 18:52:33 +000069#include <time.h>
Guido van Rossumbcc20741998-08-04 22:53:56 +000070/* For bigtime_t, snooze(). - [cjh] */
71#include <support/SupportDefs.h>
72#include <kernel/OS.h>
73#endif
74
Guido van Rossum234f9421993-06-17 12:35:49 +000075/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000076static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000077static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000079/* For Y2K check */
80static PyObject *moddict;
81
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000082#ifdef macintosh
83/* Our own timezone. We have enough information to deduce whether
84** DST is on currently, but unfortunately we cannot put it to good
85** use because we don't know the rules (and that is needed to have
86** localtime() return correct tm_isdst values for times other than
87** the current time. So, we cop out and only tell the user the current
88** timezone.
89*/
90static long timezone;
91
Guido van Rossum10b164a2001-09-25 13:59:01 +000092static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000093initmactimezone(void)
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000094{
95 MachineLocation loc;
96 long delta;
97
98 ReadLocation(&loc);
Guido van Rossum10b164a2001-09-25 13:59:01 +000099
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000100 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
101 return;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000102
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000103 delta = loc.u.gmtDelta & 0x00FFFFFF;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000104
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000105 if (delta & 0x00800000)
106 delta |= 0xFF000000;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000107
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000108 timezone = -delta;
109}
110#endif /* macintosh */
111
112
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000113static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000114time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000116 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000117 if (!PyArg_ParseTuple(args, ":time"))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000118 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000119 secs = floattime();
120 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000121 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000122 return NULL;
123 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000124 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000125}
126
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000127static char time_doc[] =
128"time() -> floating point number\n\
129\n\
130Return the current time in seconds since the Epoch.\n\
131Fractions of a second may be present if the system clock provides them.";
132
Guido van Rossumb6775db1994-08-01 11:34:53 +0000133#ifdef HAVE_CLOCK
134
135#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000136#ifdef CLK_TCK
137#define CLOCKS_PER_SEC CLK_TCK
138#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000139#define CLOCKS_PER_SEC 1000000
140#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000141#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000142
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000143static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000144time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145{
Thomas Woutersfe385252001-01-19 23:16:56 +0000146 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000148 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000150#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151
Tim Peters58e0a8c2001-05-14 22:32:33 +0000152#if defined(MS_WIN32) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Guido van Rossum3917c221997-04-02 05:35:28 +0000153/* Due to Mark Hammond */
154static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000155time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000156{
157 static LARGE_INTEGER ctrStart;
158 static LARGE_INTEGER divisor = {0,0};
159 LARGE_INTEGER now, diff, rem;
160
Thomas Woutersfe385252001-01-19 23:16:56 +0000161 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum3917c221997-04-02 05:35:28 +0000162 return NULL;
163
164 if (LargeIntegerEqualToZero(divisor)) {
165 QueryPerformanceCounter(&ctrStart);
Guido van Rossum10b164a2001-09-25 13:59:01 +0000166 if (!QueryPerformanceFrequency(&divisor) ||
Guido van Rossum3917c221997-04-02 05:35:28 +0000167 LargeIntegerEqualToZero(divisor)) {
Guido van Rossum10b164a2001-09-25 13:59:01 +0000168 /* Unlikely to happen -
169 this works on all intel machines at least!
Guido van Rossum3917c221997-04-02 05:35:28 +0000170 Revert to clock() */
171 return PyFloat_FromDouble(clock());
172 }
173 }
174 QueryPerformanceCounter(&now);
175 diff = LargeIntegerSubtract(now, ctrStart);
176 diff = LargeIntegerDivide(diff, divisor, &rem);
177 /* XXX - we assume both divide results fit in 32 bits. This is
Guido van Rossum10b164a2001-09-25 13:59:01 +0000178 true on Intels. First person who can afford a machine that
Guido van Rossum3917c221997-04-02 05:35:28 +0000179 doesnt deserves to fix it :-)
180 */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000181 return PyFloat_FromDouble((double)diff.LowPart +
Guido van Rossum3917c221997-04-02 05:35:28 +0000182 ((double)rem.LowPart / (double)divisor.LowPart));
183}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000184
Guido van Rossum3917c221997-04-02 05:35:28 +0000185#define HAVE_CLOCK /* So it gets included in the methods */
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000186#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000187
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000188#ifdef HAVE_CLOCK
189static char clock_doc[] =
190"clock() -> floating point number\n\
191\n\
192Return the CPU time or real time since the start of the process or since\n\
193the first call to clock(). This has as much precision as the system records.";
194#endif
195
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000196static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000197time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000199 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000200 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000201 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000202 if (floatsleep(secs) != 0)
203 return NULL;
204 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000205 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206}
207
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000208static char sleep_doc[] =
209"sleep(seconds)\n\
210\n\
211Delay execution for a given number of seconds. The argument may be\n\
212a floating point number for subsecond precision.";
213
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000214static PyStructSequence_Field struct_time_type_fields[] = {
215 {"tm_year", NULL},
216 {"tm_mon", NULL},
217 {"tm_mday", NULL},
218 {"tm_hour", NULL},
219 {"tm_min", NULL},
220 {"tm_sec", NULL},
221 {"tm_wday", NULL},
222 {"tm_yday", NULL},
223 {"tm_isdst", NULL},
224 {0}
225};
226
227static PyStructSequence_Desc struct_time_type_desc = {
228 "struct_time",
229 NULL,
230 struct_time_type_fields,
231 9,
232};
233
234static PyTypeObject StructTimeType;
235
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000236static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000237tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000238{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000239 PyObject *v = PyStructSequence_New(&StructTimeType);
240 if (v == NULL)
241 return NULL;
242
243#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
244
245 SET(0, p->tm_year + 1900);
246 SET(1, p->tm_mon + 1); /* Want January == 1 */
247 SET(2, p->tm_mday);
248 SET(3, p->tm_hour);
249 SET(4, p->tm_min);
250 SET(5, p->tm_sec);
251 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
252 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
253 SET(8, p->tm_isdst);
254#undef SET
255 if (PyErr_Occurred()) {
256 Py_XDECREF(v);
257 return NULL;
258 }
259
260 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000261}
262
263static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000264time_convert(time_t when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000265{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000266 struct tm *p;
267 errno = 0;
Jack Jansenee398fa2000-07-03 21:37:27 +0000268#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000269 when = when + GUSI_TO_MSL_EPOCH;
270#endif
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000271 p = function(&when);
272 if (p == NULL) {
273#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000274 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000275 errno = EINVAL;
276#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000277 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000278 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000279 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000280}
281
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000282static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000283time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000284{
285 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000286 if (PyTuple_Size(args) == 0)
287 when = floattime();
288 if (!PyArg_ParseTuple(args, "|d:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000289 return NULL;
290 return time_convert((time_t)when, gmtime);
291}
292
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000293static char gmtime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000294"gmtime([seconds]) -> tuple\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000295\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000296Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
297GMT). When 'seconds' is not passed in, convert the current time instead.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000298
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000299static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000300time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000301{
302 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000303 if (PyTuple_Size(args) == 0)
304 when = floattime();
305 if (!PyArg_ParseTuple(args, "|d:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000306 return NULL;
307 return time_convert((time_t)when, localtime);
308}
309
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000310static char localtime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000311"localtime([seconds]) -> tuple\n\
312Convert seconds since the Epoch to a time tuple expressing local time.\n\
313When 'seconds' is not passed in, convert the current time instead.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000314
Guido van Rossum9e90a671993-06-24 11:10:19 +0000315static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000316gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000317{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000318 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000319 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000320
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000321 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000322 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000323 &p->tm_mon,
324 &p->tm_mday,
325 &p->tm_hour,
326 &p->tm_min,
327 &p->tm_sec,
328 &p->tm_wday,
329 &p->tm_yday,
330 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000331 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000332 if (y < 1900) {
333 PyObject *accept = PyDict_GetItemString(moddict,
334 "accept2dyear");
335 if (accept == NULL || !PyInt_Check(accept) ||
336 PyInt_AsLong(accept) == 0) {
337 PyErr_SetString(PyExc_ValueError,
338 "year >= 1900 required");
339 return 0;
340 }
341 if (69 <= y && y <= 99)
342 y += 1900;
343 else if (0 <= y && y <= 68)
344 y += 2000;
345 else {
346 PyErr_SetString(PyExc_ValueError,
Skip Montanaro1a10aac2001-08-22 12:39:16 +0000347 "year out of range");
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000348 return 0;
349 }
350 }
351 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000352 p->tm_mon--;
353 p->tm_wday = (p->tm_wday + 1) % 7;
354 p->tm_yday--;
355 return 1;
356}
357
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000358#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000359static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000360time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000361{
Thomas Woutersfe385252001-01-19 23:16:56 +0000362 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000363 struct tm buf;
364 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000365 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000366 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000367 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000368
Thomas Wouters334fb892000-07-25 12:56:38 +0000369 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000370
Thomas Woutersfe385252001-01-19 23:16:56 +0000371 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000372 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000373
374 if (tup == NULL) {
375 time_t tt = time(NULL);
376 buf = *localtime(&tt);
377 } else if (!gettmarg(tup, &buf))
378 return NULL;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000379
Guido van Rossumc222ec21999-02-23 00:00:10 +0000380 fmtlen = strlen(fmt);
381
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000382 /* I hate these functions that presume you know how big the output
383 * will be ahead of time...
384 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000385 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000386 outbuf = malloc(i);
387 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000388 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000389 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000390 buflen = strftime(outbuf, i, fmt, &buf);
391 if (buflen > 0 || i >= 256 * fmtlen) {
392 /* If the buffer is 256 times as long as the format,
393 it's probably not failing for lack of room!
394 More likely, the format yields an empty result,
395 e.g. an empty format, or %Z when the timezone
396 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000397 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000398 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000399 free(outbuf);
400 return ret;
401 }
402 free(outbuf);
403 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000404}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000405
406static char strftime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000407"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000408\n\
409Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000410See the library reference manual for formatting codes. When the time tuple\n\
411is not present, current time as returned by localtime() is used.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000412#endif /* HAVE_STRFTIME */
413
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000414#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000415
416#if 0
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000417/* Enable this if it's not declared in <time.h> */
418extern char *strptime(const char *, const char *, struct tm *);
Fred Drakeaff60182000-05-09 19:52:40 +0000419#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000420
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000421static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000422time_strptime(PyObject *self, PyObject *args)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000423{
424 struct tm tm;
425 char *fmt = "%a %b %d %H:%M:%S %Y";
426 char *buf;
427 char *s;
428
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000429 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
430 return NULL;
Thomas Wouters334fb892000-07-25 12:56:38 +0000431 memset((void *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000432 s = strptime(buf, fmt, &tm);
433 if (s == NULL) {
434 PyErr_SetString(PyExc_ValueError, "format mismatch");
435 return NULL;
436 }
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000437 while (*s && isspace(Py_CHARMASK(*s)))
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000438 s++;
439 if (*s) {
440 PyErr_Format(PyExc_ValueError,
441 "unconverted data remains: '%.400s'", s);
442 return NULL;
443 }
444 return tmtotuple(&tm);
445}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000446
447static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000448"strptime(string, format) -> tuple\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000449Parse a string to a time tuple according to a format specification.\n\
450See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000451#endif /* HAVE_STRPTIME */
452
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000453static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000454time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000455{
Thomas Woutersfe385252001-01-19 23:16:56 +0000456 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000457 struct tm buf;
458 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000459 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000460 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000461 if (tup == NULL) {
462 time_t tt = time(NULL);
463 buf = *localtime(&tt);
464 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000465 return NULL;
466 p = asctime(&buf);
467 if (p[24] == '\n')
468 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000469 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000470}
471
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000472static char asctime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000473"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000474\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000475Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
476When the time tuple is not present, current time as returned by localtime()\n\
477is used.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000478
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000479static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000480time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000481{
482 double dt;
483 time_t tt;
484 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000485
Thomas Woutersfe385252001-01-19 23:16:56 +0000486 if (PyTuple_Size(args) == 0)
487 tt = time(NULL);
488 else {
489 if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
490 return NULL;
491 tt = (time_t)dt;
492 }
Jack Jansenee398fa2000-07-03 21:37:27 +0000493#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000494 tt = tt + GUSI_TO_MSL_EPOCH;
495#endif
Guido van Rossum9e90a671993-06-24 11:10:19 +0000496 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000497 if (p == NULL) {
498 PyErr_SetString(PyExc_ValueError, "unconvertible time");
499 return NULL;
500 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000501 if (p[24] == '\n')
502 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000503 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000504}
505
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000506static char ctime_doc[] =
507"ctime(seconds) -> string\n\
508\n\
509Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000510This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
511not present, current time as returned by localtime() is used.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000512
Guido van Rossum60cd8131998-03-06 17:16:21 +0000513#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000514static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000515time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000516{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000517 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000518 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000519 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000520 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000521 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000522 tt = time(&tt);
523 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000524 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000525 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000526 tt = mktime(&buf);
527 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000528 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000529 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000530 return NULL;
531 }
Jack Jansen63596ae2000-12-12 22:42:30 +0000532#if defined(macintosh) && defined(USE_GUSI211)
Guido van Rossumc410e922000-04-26 20:40:13 +0000533 tt = tt - GUSI_TO_MSL_EPOCH;
534#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000535 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000536}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000537
538static char mktime_doc[] =
539"mktime(tuple) -> floating point number\n\
540\n\
541Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000542#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000543
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000544static PyMethodDef time_methods[] = {
Thomas Woutersfe385252001-01-19 23:16:56 +0000545 {"time", time_time, METH_VARARGS, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000546#ifdef HAVE_CLOCK
Thomas Woutersfe385252001-01-19 23:16:56 +0000547 {"clock", time_clock, METH_VARARGS, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000548#endif
Thomas Woutersfe385252001-01-19 23:16:56 +0000549 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
550 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
551 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000552 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
Thomas Woutersfe385252001-01-19 23:16:56 +0000553 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000554#ifdef HAVE_MKTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000555 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000556#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000557#ifdef HAVE_STRFTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000558 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000559#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000560#ifdef HAVE_STRPTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000561 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000562#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563 {NULL, NULL} /* sentinel */
564};
565
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000566static void
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000567ins(PyObject *d, char *name, PyObject *v)
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000568{
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000569 /* Don't worry too much about errors, they'll be caught by the
570 * caller of inittime().
571 */
572 if (v)
573 PyDict_SetItemString(d, name, v);
574 Py_XDECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000575}
576
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000577
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000578static char module_doc[] =
579"This module provides various functions to manipulate time values.\n\
580\n\
581There are two standard representations of time. One is the number\n\
582of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
583or a floating point number (to represent fractions of seconds).\n\
584The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
585The actual value can be retrieved by calling gmtime(0).\n\
586\n\
587The other representation is a tuple of 9 integers giving local time.\n\
588The tuple items are:\n\
589 year (four digits, e.g. 1998)\n\
590 month (1-12)\n\
591 day (1-31)\n\
592 hours (0-23)\n\
593 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000594 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000595 weekday (0-6, Monday is 0)\n\
596 Julian day (day in the year, 1-366)\n\
597 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
598If the DST flag is 0, the time is given in the regular time zone;\n\
599if it is 1, the time is given in the DST time zone;\n\
600if it is -1, mktime() should guess based on the date and time.\n\
601\n\
602Variables:\n\
603\n\
604timezone -- difference in seconds between UTC and local standard time\n\
605altzone -- difference in seconds between UTC and local DST time\n\
606daylight -- whether local time should reflect DST\n\
607tzname -- tuple of (standard time zone name, DST time zone name)\n\
608\n\
609Functions:\n\
610\n\
611time() -- return current time in seconds since the Epoch as a float\n\
612clock() -- return CPU time since process start as a float\n\
613sleep() -- delay for a number of seconds given as a float\n\
614gmtime() -- convert seconds since Epoch to UTC tuple\n\
615localtime() -- convert seconds since Epoch to local time tuple\n\
616asctime() -- convert time tuple to string\n\
617ctime() -- convert time in seconds to string\n\
618mktime() -- convert local time tuple to seconds since Epoch\n\
619strftime() -- convert time tuple to string according to format specification\n\
620strptime() -- parse string to time tuple according to format specification\n\
621";
Guido van Rossum10b164a2001-09-25 13:59:01 +0000622
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000623
Guido van Rossum3886bb61998-12-04 18:50:17 +0000624DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000625inittime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000627 PyObject *m, *d;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000628 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000629 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000630 d = PyModule_GetDict(m);
Guido van Rossumc2068731998-10-07 16:35:25 +0000631 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000632 p = Py_GETENV("PYTHONY2K");
Guido van Rossumc2068731998-10-07 16:35:25 +0000633 ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
634 /* Squirrel away the module's dictionary for the y2k check */
635 Py_INCREF(d);
636 moddict = d;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000637#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000638 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000639#ifdef PYOS_OS2
640 ins(d, "timezone", PyInt_FromLong((long)_timezone));
641#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000642 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossum26452411998-09-28 22:07:11 +0000643#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000644#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000645 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000646#else
Guido van Rossum26452411998-09-28 22:07:11 +0000647#ifdef PYOS_OS2
648 ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
649#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000650 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossum26452411998-09-28 22:07:11 +0000651#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000652#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000653 ins(d, "daylight", PyInt_FromLong((long)daylight));
654 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000655#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000656#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000657 {
658#define YEAR ((time_t)((365 * 24 + 6) * 3600))
659 time_t t;
660 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000661 long janzone, julyzone;
662 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000663 t = (time((time_t *)0) / YEAR) * YEAR;
664 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000665 janzone = -p->tm_gmtoff;
666 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
667 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000668 t += YEAR/2;
669 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000670 julyzone = -p->tm_gmtoff;
671 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
672 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000673
Guido van Rossum57731601999-03-29 19:12:04 +0000674 if( janzone < julyzone ) {
675 /* DST is reversed in the southern hemisphere */
676 ins(d, "timezone", PyInt_FromLong(julyzone));
677 ins(d, "altzone", PyInt_FromLong(janzone));
678 ins(d, "daylight",
679 PyInt_FromLong((long)(janzone != julyzone)));
680 ins(d, "tzname",
681 Py_BuildValue("(zz)", julyname, janname));
682 } else {
683 ins(d, "timezone", PyInt_FromLong(janzone));
684 ins(d, "altzone", PyInt_FromLong(julyzone));
685 ins(d, "daylight",
686 PyInt_FromLong((long)(janzone != julyzone)));
687 ins(d, "tzname",
688 Py_BuildValue("(zz)", janname, julyname));
689 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000690 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000691#else
692#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000693 /* The only thing we can obtain is the current timezone
694 ** (and whether dst is currently _active_, but that is not what
695 ** we're looking for:-( )
696 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000697 initmactimezone();
698 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000699 ins(d, "altzone", PyInt_FromLong(timezone));
700 ins(d, "daylight", PyInt_FromLong((long)0));
701 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000702#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000703#endif /* HAVE_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000704#ifdef __CYGWIN__
705 tzset();
706 ins(d, "timezone", PyInt_FromLong(_timezone));
707 ins(d, "altzone", PyInt_FromLong(_timezone));
708 ins(d, "daylight", PyInt_FromLong(_daylight));
709 ins(d, "tzname", Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
710#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000711#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000712
713 PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
714 PyDict_SetItemString(d, "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{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000764/* XXX Should test for MS_WIN32 first! */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000765#if defined(HAVE_SELECT) && !defined(__BEOS__)
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
Guido van Rossumbcc20741998-08-04 22:53:56 +0000785#else /* !HAVE_SELECT || __BEOS__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000786#ifdef macintosh
787#define MacTicks (* (long *)0x16A)
788 long deadline;
789 deadline = MacTicks + (long)(secs * 60.0);
790 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000791 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000792 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000793 return -1;
794 }
795#else /* !macintosh */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000796#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000797 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000798 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000799 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000800 Py_END_ALLOW_THREADS
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000801#else /* !__WATCOMC__ || __QNX__ */
Guido van Rossume22e6441993-07-09 10:51:31 +0000802#ifdef MSDOS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000803 struct timeb t1, t2;
804 double frac;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000805 extern double fmod(double, double);
806 extern double floor(double);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000807 if (secs <= 0.0)
808 return;
809 frac = fmod(secs, 1.0);
810 secs = floor(secs);
811 ftime(&t1);
812 t2.time = t1.time + (int)secs;
813 t2.millitm = t1.millitm + (int)(frac*1000.0);
814 while (t2.millitm >= 1000) {
815 t2.time++;
816 t2.millitm -= 1000;
817 }
818 for (;;) {
819#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000820 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000821 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000822 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000823#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000824 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000825 return -1;
826 ftime(&t1);
827 if (t1.time > t2.time ||
828 t1.time == t2.time && t1.millitm >= t2.millitm)
829 break;
830 }
831#else /* !MSDOS */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000832#ifdef MS_WIN32
Fred Drake0e123952000-06-29 21:31:02 +0000833 {
834 double millisecs = secs * 1000.0;
835 if (millisecs > (double)ULONG_MAX) {
836 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
837 return -1;
838 }
839 /* XXX Can't interrupt this sleep */
840 Py_BEGIN_ALLOW_THREADS
841 Sleep((unsigned long)millisecs);
842 Py_END_ALLOW_THREADS
843 }
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000844#else /* !MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000845#ifdef PYOS_OS2
846 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000847 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000848 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000849 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000850 PyErr_SetFromErrno(PyExc_IOError);
851 return -1;
852 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000853 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000854#else /* !PYOS_OS2 */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000855#ifdef __BEOS__
856 /* This sleep *CAN BE* interrupted. */
857 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000858 if( secs <= 0.0 ) {
859 return;
860 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000861
Guido van Rossumbcc20741998-08-04 22:53:56 +0000862 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000863 /* BeOS snooze() is in microseconds... */
864 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000865 Py_BLOCK_THREADS
866 PyErr_SetFromErrno( PyExc_IOError );
867 return -1;
868 }
869 Py_END_ALLOW_THREADS
870 }
871#else /* !__BEOS__ */
Guido van Rossumbceccf52001-04-10 22:07:43 +0000872#ifdef RISCOS
873 if (secs <= 0.0)
874 return 0;
875 Py_BEGIN_ALLOW_THREADS
876 /* This sleep *CAN BE* interrupted. */
877 if ( sleep(secs) )
878 return -1;
879 Py_END_ALLOW_THREADS
880#else /* !RISCOS */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000881 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000882 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000883 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000884 Py_END_ALLOW_THREADS
Guido van Rossumbceccf52001-04-10 22:07:43 +0000885#endif /* !RISCOS */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000886#endif /* !__BEOS__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000887#endif /* !PYOS_OS2 */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000888#endif /* !MS_WIN32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000889#endif /* !MSDOS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000890#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000891#endif /* !macintosh */
892#endif /* !HAVE_SELECT */
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000893
Guido van Rossumb6775db1994-08-01 11:34:53 +0000894 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000895}