blob: 8f5695dab4a683b2923e078e40cd711497d4e73a [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Time module */
33
Barry Warsaw9a2a8a81996-12-06 23:32:14 +000034#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000035
Guido van Rossum99d90c01996-08-08 19:17:45 +000036#ifdef HAVE_SELECT
37#include "mymath.h"
38#endif
39
Guido van Rossum6d946f91992-08-14 13:49:30 +000040#ifdef macintosh
Guido van Rossumb6775db1994-08-01 11:34:53 +000041#include <time.h>
42#else
43#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000044#endif
45
Guido van Rossumb6775db1994-08-01 11:34:53 +000046#ifdef QUICKWIN
47#include <io.h>
48#endif
49
50#ifdef HAVE_UNISTD_H
Guido van Rossum2762f251992-03-27 17:22:13 +000051#include <unistd.h>
52#endif
53
Guido van Rossumb6775db1994-08-01 11:34:53 +000054#ifdef HAVE_SELECT
55#include "myselect.h"
56#else
57#include "mytime.h"
58#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059
Guido van Rossumb6775db1994-08-01 11:34:53 +000060#ifdef HAVE_FTIME
61#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000062#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Guido van Rossum1bb126f1996-12-06 20:17:44 +000063extern int ftime();
Guido van Rossum52174571996-12-09 18:38:52 +000064#endif /* MS_WINDOWS */
65#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066
Guido van Rossum7bf22de1997-12-02 20:34:19 +000067#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000068#include <i86.h>
69#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000070#ifdef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +000071#include <windows.h>
Guido van Rossumb2fb3641996-09-07 00:47:35 +000072#ifdef MS_WIN16
73/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000074#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000075#define tzname _tzname
76#define daylight _daylight
77#define altzone _altzone
Guido van Rossumb2fb3641996-09-07 00:47:35 +000078#endif /* MS_WIN16 */
Guido van Rossumcac6c721996-09-06 13:34:02 +000079#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000080#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000081
Guido van Rossum3917c221997-04-02 05:35:28 +000082#ifdef MS_WIN32
83/* Win32 has better clock replacement */
84#include <largeint.h>
85#undef HAVE_CLOCK /* We have our own version down below */
86#endif /* MS_WIN32 */
87
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000088#if defined(PYOS_OS2)
89#define INCL_DOS
90#define INCL_DOSERRORS
91#define INCL_NOPMAPI
92#include <os2.h>
93#endif
94
95#if defined(PYCC_VACPP)
96#include <time.h>
97#define timezone _timezone
98#endif
99
Guido van Rossum234f9421993-06-17 12:35:49 +0000100/* Forward declarations */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000101static int floatsleep Py_PROTO((double));
102static double floattime Py_PROTO(());
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000104#ifdef macintosh
105/* Our own timezone. We have enough information to deduce whether
106** DST is on currently, but unfortunately we cannot put it to good
107** use because we don't know the rules (and that is needed to have
108** localtime() return correct tm_isdst values for times other than
109** the current time. So, we cop out and only tell the user the current
110** timezone.
111*/
112static long timezone;
113
114static void
115initmactimezone()
116{
117 MachineLocation loc;
118 long delta;
119
120 ReadLocation(&loc);
121
122 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
123 return;
124
125 delta = loc.u.gmtDelta & 0x00FFFFFF;
126
127 if (delta & 0x00800000)
128 delta |= 0xFF000000;
129
130 timezone = -delta;
131}
132#endif /* macintosh */
133
134
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000135static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136time_time(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000137 PyObject *self;
138 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000140 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000141 if (!PyArg_NoArgs(args))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000142 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000143 secs = floattime();
144 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000145 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000146 return NULL;
147 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000148 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000149}
150
151#ifdef HAVE_CLOCK
152
153#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000154#ifdef CLK_TCK
155#define CLOCKS_PER_SEC CLK_TCK
156#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000157#define CLOCKS_PER_SEC 1000000
158#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000159#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000160
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000161static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000162time_clock(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000163 PyObject *self;
164 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000165{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000166 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000167 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000168 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000170#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000171
Guido van Rossum3917c221997-04-02 05:35:28 +0000172#ifdef MS_WIN32
173/* Due to Mark Hammond */
174static PyObject *
175time_clock(self, args)
176 PyObject *self;
177 PyObject *args;
178{
179 static LARGE_INTEGER ctrStart;
180 static LARGE_INTEGER divisor = {0,0};
181 LARGE_INTEGER now, diff, rem;
182
183 if (!PyArg_NoArgs(args))
184 return NULL;
185
186 if (LargeIntegerEqualToZero(divisor)) {
187 QueryPerformanceCounter(&ctrStart);
188 if (!QueryPerformanceFrequency(&divisor) ||
189 LargeIntegerEqualToZero(divisor)) {
190 /* Unlikely to happen -
191 this works on all intel machines at least!
192 Revert to clock() */
193 return PyFloat_FromDouble(clock());
194 }
195 }
196 QueryPerformanceCounter(&now);
197 diff = LargeIntegerSubtract(now, ctrStart);
198 diff = LargeIntegerDivide(diff, divisor, &rem);
199 /* XXX - we assume both divide results fit in 32 bits. This is
200 true on Intels. First person who can afford a machine that
201 doesnt deserves to fix it :-)
202 */
203 return PyFloat_FromDouble((double)diff.LowPart +
204 ((double)rem.LowPart / (double)divisor.LowPart));
205}
206#define HAVE_CLOCK /* So it gets included in the methods */
207#endif /* MS_WIN32 */
208
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000209static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210time_sleep(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000211 PyObject *self;
212 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000214 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000215 if (!PyArg_Parse(args, "d", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000217 if (floatsleep(secs) != 0)
218 return NULL;
219 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000220 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221}
222
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000223static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000224time_convert(when, function)
225 time_t when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000226 struct tm * (*function) Py_PROTO((const time_t *));
Guido van Rossum234f9421993-06-17 12:35:49 +0000227{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000228 struct tm *p;
229 errno = 0;
230 p = function(&when);
231 if (p == NULL) {
232#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000233 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000234 errno = EINVAL;
235#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000236 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000237 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000238 return Py_BuildValue("(iiiiiiiii)",
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000239 p->tm_year + 1900,
240 p->tm_mon + 1, /* Want January == 1 */
241 p->tm_mday,
242 p->tm_hour,
243 p->tm_min,
244 p->tm_sec,
245 (p->tm_wday + 6) % 7, /* Want Monday == 0 */
246 p->tm_yday + 1, /* Want January, 1 == 1 */
247 p->tm_isdst);
Guido van Rossum234f9421993-06-17 12:35:49 +0000248}
249
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000250static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000251time_gmtime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000252 PyObject *self;
253 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000254{
255 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000256 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000257 return NULL;
258 return time_convert((time_t)when, gmtime);
259}
260
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000261static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000262time_localtime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000263 PyObject *self;
264 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000265{
266 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000267 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000268 return NULL;
269 return time_convert((time_t)when, localtime);
270}
271
Guido van Rossum9e90a671993-06-24 11:10:19 +0000272static int
273gettmarg(args, p)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000274 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000275 struct tm *p;
276{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000277 if (!PyArg_Parse(args, "(iiiiiiiii)",
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000278 &p->tm_year,
279 &p->tm_mon,
280 &p->tm_mday,
281 &p->tm_hour,
282 &p->tm_min,
283 &p->tm_sec,
284 &p->tm_wday,
285 &p->tm_yday,
286 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000287 return 0;
288 if (p->tm_year >= 1900)
289 p->tm_year -= 1900;
290 p->tm_mon--;
291 p->tm_wday = (p->tm_wday + 1) % 7;
292 p->tm_yday--;
293 return 1;
294}
295
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000296#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000297static PyObject *
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000298time_strftime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000299 PyObject *self;
300 PyObject *args;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000301{
302 struct tm buf;
303 const char *fmt;
304 char *outbuf = 0;
305 int i;
306
307 if (!PyArg_ParseTuple(args, "s(iiiiiiiii)",
308 &fmt,
309 &(buf.tm_year),
310 &(buf.tm_mon),
311 &(buf.tm_mday),
312 &(buf.tm_hour),
313 &(buf.tm_min),
314 &(buf.tm_sec),
315 &(buf.tm_wday),
316 &(buf.tm_yday),
317 &(buf.tm_isdst)))
318 return NULL;
319 if (buf.tm_year >= 1900)
320 buf.tm_year -= 1900;
321 buf.tm_mon--;
322 buf.tm_wday = (buf.tm_wday + 1) % 7;
323 buf.tm_yday--;
Guido van Rossum60cd8131998-03-06 17:16:21 +0000324#ifdef HAVE_MKTIME
325 /* This call is only there to adjust the numbers to be within
326 bounds. When we don't have mktime(), we say the caller is
327 responsible for that... */
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000328 (void) mktime(&buf);
Guido van Rossum60cd8131998-03-06 17:16:21 +0000329#endif
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000330 /* I hate these functions that presume you know how big the output
331 * will be ahead of time...
332 */
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000333 for (i = 1024 ; i <= 8192 ; i += 1024) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000334 outbuf = malloc(i);
335 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000336 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000337 }
338 if (strftime(outbuf, i-1, fmt, &buf) != 0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000339 PyObject *ret;
340 ret = PyString_FromString(outbuf);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000341 free(outbuf);
342 return ret;
343 }
344 free(outbuf);
345 }
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000346 PyErr_SetString(PyExc_ValueError,
347 "bad strftime format or result too big");
348 return NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000349}
350#endif /* HAVE_STRFTIME */
351
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000352static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000353time_asctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000354 PyObject *self;
355 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000356{
357 struct tm buf;
358 char *p;
359 if (!gettmarg(args, &buf))
360 return NULL;
361 p = asctime(&buf);
362 if (p[24] == '\n')
363 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000364 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000365}
366
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000367static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000368time_ctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000369 PyObject *self;
370 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000371{
372 double dt;
373 time_t tt;
374 char *p;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000375 if (!PyArg_Parse(args, "d", &dt))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000376 return NULL;
Guido van Rossumcac6c721996-09-06 13:34:02 +0000377 tt = (time_t)dt;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000378 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000379 if (p == NULL) {
380 PyErr_SetString(PyExc_ValueError, "unconvertible time");
381 return NULL;
382 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000383 if (p[24] == '\n')
384 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000385 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000386}
387
Guido van Rossum60cd8131998-03-06 17:16:21 +0000388#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000389static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000390time_mktime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000391 PyObject *self;
392 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000393{
394 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000395 time_t tt;
396 tt = time(&tt);
397 buf = *localtime(&tt);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000398 if (!gettmarg(args, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000399 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000400 tt = mktime(&buf);
401 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000402 PyErr_SetString(PyExc_OverflowError,
403 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000404 return NULL;
405 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000406 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000407}
Guido van Rossum60cd8131998-03-06 17:16:21 +0000408#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000409
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000410static PyMethodDef time_methods[] = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411 {"time", time_time},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000412#ifdef HAVE_CLOCK
413 {"clock", time_clock},
414#endif
415 {"sleep", time_sleep},
Guido van Rossum234f9421993-06-17 12:35:49 +0000416 {"gmtime", time_gmtime},
417 {"localtime", time_localtime},
Guido van Rossum9e90a671993-06-24 11:10:19 +0000418 {"asctime", time_asctime},
419 {"ctime", time_ctime},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000420#ifdef HAVE_MKTIME
Guido van Rossum234f9421993-06-17 12:35:49 +0000421 {"mktime", time_mktime},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000422#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000423#ifdef HAVE_STRFTIME
Guido van Rossum5416e201996-02-13 00:14:09 +0000424 {"strftime", time_strftime, 1},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000425#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426 {NULL, NULL} /* sentinel */
427};
428
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000429static void
430ins(d, name, v)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000431 PyObject *d;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000432 char *name;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000433 PyObject *v;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000434{
435 if (v == NULL)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000436 Py_FatalError("Can't initialize time module -- NULL value");
437 if (PyDict_SetItemString(d, name, v) != 0)
438 Py_FatalError(
Guido van Rossum52174571996-12-09 18:38:52 +0000439 "Can't initialize time module -- PyDict_SetItemString failed");
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000440 Py_DECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000441}
442
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443void
444inittime()
445{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000446 PyObject *m, *d;
447 m = Py_InitModule("time", time_methods);
448 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000449#ifdef HAVE_TZNAME
Guido van Rossum234f9421993-06-17 12:35:49 +0000450 tzset();
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000451 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000452#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000453 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000454#else
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000455 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000456#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000457 ins(d, "daylight", PyInt_FromLong((long)daylight));
458 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000459#else /* !HAVE_TZNAME */
460#if HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000461 {
462#define YEAR ((time_t)((365 * 24 + 6) * 3600))
463 time_t t;
464 struct tm *p;
465 long winterzone, summerzone;
466 char wintername[10], summername[10];
Guido van Rossumb6775db1994-08-01 11:34:53 +0000467 /* XXX This won't work on the southern hemisphere.
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000468 XXX Anybody got a better idea? */
Guido van Rossum234f9421993-06-17 12:35:49 +0000469 t = (time((time_t *)0) / YEAR) * YEAR;
470 p = localtime(&t);
471 winterzone = -p->tm_gmtoff;
472 strncpy(wintername, p->tm_zone ? p->tm_zone : " ", 9);
473 wintername[9] = '\0';
474 t += YEAR/2;
475 p = localtime(&t);
476 summerzone = -p->tm_gmtoff;
477 strncpy(summername, p->tm_zone ? p->tm_zone : " ", 9);
478 summername[9] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000479 ins(d, "timezone", PyInt_FromLong(winterzone));
480 ins(d, "altzone", PyInt_FromLong(summerzone));
481 ins(d, "daylight",
482 PyInt_FromLong((long)(winterzone != summerzone)));
483 ins(d, "tzname",
484 Py_BuildValue("(zz)", wintername, summername));
Guido van Rossum234f9421993-06-17 12:35:49 +0000485 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000486#else
487#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000488 /* The only thing we can obtain is the current timezone
489 ** (and whether dst is currently _active_, but that is not what
490 ** we're looking for:-( )
491 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000492 initmactimezone();
493 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000494 ins(d, "altzone", PyInt_FromLong(timezone));
495 ins(d, "daylight", PyInt_FromLong((long)0));
496 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000497#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000498#endif /* HAVE_TM_ZONE */
499#endif /* !HAVE_TZNAME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000500 if (PyErr_Occurred())
501 Py_FatalError("Can't initialize time module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000502}
503
504
Guido van Rossumb6775db1994-08-01 11:34:53 +0000505/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000506
Guido van Rossumb6775db1994-08-01 11:34:53 +0000507static double
508floattime()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000509{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000510 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000511 (1) gettimeofday() -- resolution in microseconds
512 (2) ftime() -- resolution in milliseconds
513 (3) time() -- resolution in seconds
514 In all cases the return value is a float in seconds.
515 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
516 fail, so we fall back on ftime() or time().
517 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000518#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000519 {
520 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000521#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000522 if (gettimeofday(&t) == 0)
523 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000524#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000525 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
526 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000527#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000528 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000529#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000530 {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000531#ifdef HAVE_FTIME
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000532 struct timeb t;
533 ftime(&t);
534 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000535#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000536 time_t secs;
537 time(&secs);
538 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000539#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000540 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000541}
542
Guido van Rossumb6775db1994-08-01 11:34:53 +0000543
544/* Implement floatsleep() for various platforms.
545 When interrupted (or when another error occurs), return -1 and
546 set an exception; else return 0. */
547
548static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000549#ifdef MPW
550floatsleep(double secs)
551#else
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000552 floatsleep(secs)
Guido van Rossum775f4da1993-01-09 17:18:52 +0000553 double secs;
Guido van Rossuma320fd31995-03-09 12:14:15 +0000554#endif /* MPW */
Guido van Rossum426035c1991-02-19 12:27:35 +0000555{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000556/* XXX Should test for MS_WIN32 first! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000557#ifdef HAVE_SELECT
Guido van Rossum426035c1991-02-19 12:27:35 +0000558 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000559 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000560 frac = fmod(secs, 1.0);
561 secs = floor(secs);
562 t.tv_sec = (long)secs;
563 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000564 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000565 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000566 Py_BLOCK_THREADS
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000567 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000568 return -1;
569 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000570 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000571#else /* !HAVE_SELECT */
572#ifdef macintosh
573#define MacTicks (* (long *)0x16A)
574 long deadline;
575 deadline = MacTicks + (long)(secs * 60.0);
576 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000577 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000578 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000579 return -1;
580 }
581#else /* !macintosh */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000582#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000583 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000584 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000585 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000586 Py_END_ALLOW_THREADS
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000587#else /* !__WATCOMC__ || __QNX__ */
Guido van Rossume22e6441993-07-09 10:51:31 +0000588#ifdef MSDOS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000589 struct timeb t1, t2;
590 double frac;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000591 extern double fmod Py_PROTO((double, double));
592 extern double floor Py_PROTO((double));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000593 if (secs <= 0.0)
594 return;
595 frac = fmod(secs, 1.0);
596 secs = floor(secs);
597 ftime(&t1);
598 t2.time = t1.time + (int)secs;
599 t2.millitm = t1.millitm + (int)(frac*1000.0);
600 while (t2.millitm >= 1000) {
601 t2.time++;
602 t2.millitm -= 1000;
603 }
604 for (;;) {
605#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000606 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000607 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000608 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000609#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000610 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000611 return -1;
612 ftime(&t1);
613 if (t1.time > t2.time ||
614 t1.time == t2.time && t1.millitm >= t2.millitm)
615 break;
616 }
617#else /* !MSDOS */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000618#ifdef MS_WIN32
Guido van Rossumb6775db1994-08-01 11:34:53 +0000619 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000620 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000621 Sleep((int)(secs*1000));
Guido van Rossum8607ae21997-11-03 22:04:46 +0000622 Py_END_ALLOW_THREADS
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000623#else /* !MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000624#ifdef PYOS_OS2
625 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000626 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000627 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000628 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000629 PyErr_SetFromErrno(PyExc_IOError);
630 return -1;
631 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000632 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000633#else /* !PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000634 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000635 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000636 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000637 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000638#endif /* !PYOS_OS2 */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000639#endif /* !MS_WIN32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000640#endif /* !MSDOS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000641#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000642#endif /* !macintosh */
643#endif /* !HAVE_SELECT */
644 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000645}