blob: 43b247f1f50c2843999eaf8e305b789e25c41445 [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 Rossum87ce7bb1998-06-09 16:30:31 +000036#include <ctype.h>
37
Guido van Rossum99d90c01996-08-08 19:17:45 +000038#ifdef HAVE_SELECT
39#include "mymath.h"
40#endif
41
Guido van Rossum6d946f91992-08-14 13:49:30 +000042#ifdef macintosh
Guido van Rossumb6775db1994-08-01 11:34:53 +000043#include <time.h>
44#else
45#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000046#endif
47
Guido van Rossumb6775db1994-08-01 11:34:53 +000048#ifdef QUICKWIN
49#include <io.h>
50#endif
51
52#ifdef HAVE_UNISTD_H
Guido van Rossum2762f251992-03-27 17:22:13 +000053#include <unistd.h>
54#endif
55
Guido van Rossumb6775db1994-08-01 11:34:53 +000056#ifdef HAVE_SELECT
57#include "myselect.h"
58#else
59#include "mytime.h"
60#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061
Guido van Rossumb6775db1994-08-01 11:34:53 +000062#ifdef HAVE_FTIME
63#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000064#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Guido van Rossum1bb126f1996-12-06 20:17:44 +000065extern int ftime();
Guido van Rossum52174571996-12-09 18:38:52 +000066#endif /* MS_WINDOWS */
67#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068
Guido van Rossum7bf22de1997-12-02 20:34:19 +000069#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000070#include <i86.h>
71#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000072#ifdef MS_WINDOWS
Guido van Rossumb6775db1994-08-01 11:34:53 +000073#include <windows.h>
Guido van Rossumb2fb3641996-09-07 00:47:35 +000074#ifdef MS_WIN16
75/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000076#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000077#define tzname _tzname
78#define daylight _daylight
79#define altzone _altzone
Guido van Rossumb2fb3641996-09-07 00:47:35 +000080#endif /* MS_WIN16 */
Guido van Rossumcac6c721996-09-06 13:34:02 +000081#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000082#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000083
Guido van Rossum3917c221997-04-02 05:35:28 +000084#ifdef MS_WIN32
85/* Win32 has better clock replacement */
86#include <largeint.h>
87#undef HAVE_CLOCK /* We have our own version down below */
88#endif /* MS_WIN32 */
89
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000090#if defined(PYOS_OS2)
91#define INCL_DOS
92#define INCL_DOSERRORS
93#define INCL_NOPMAPI
94#include <os2.h>
95#endif
96
97#if defined(PYCC_VACPP)
98#include <time.h>
99#define timezone _timezone
100#endif
101
Guido van Rossum234f9421993-06-17 12:35:49 +0000102/* Forward declarations */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000103static int floatsleep Py_PROTO((double));
104static double floattime Py_PROTO(());
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000106#ifdef macintosh
107/* Our own timezone. We have enough information to deduce whether
108** DST is on currently, but unfortunately we cannot put it to good
109** use because we don't know the rules (and that is needed to have
110** localtime() return correct tm_isdst values for times other than
111** the current time. So, we cop out and only tell the user the current
112** timezone.
113*/
114static long timezone;
115
116static void
117initmactimezone()
118{
119 MachineLocation loc;
120 long delta;
121
122 ReadLocation(&loc);
123
124 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
125 return;
126
127 delta = loc.u.gmtDelta & 0x00FFFFFF;
128
129 if (delta & 0x00800000)
130 delta |= 0xFF000000;
131
132 timezone = -delta;
133}
134#endif /* macintosh */
135
136
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000137static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138time_time(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000139 PyObject *self;
140 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000142 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000143 if (!PyArg_NoArgs(args))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000144 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145 secs = floattime();
146 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000147 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000148 return NULL;
149 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000150 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000151}
152
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000153static char time_doc[] =
154"time() -> floating point number\n\
155\n\
156Return the current time in seconds since the Epoch.\n\
157Fractions of a second may be present if the system clock provides them.";
158
Guido van Rossumb6775db1994-08-01 11:34:53 +0000159#ifdef HAVE_CLOCK
160
161#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000162#ifdef CLK_TCK
163#define CLOCKS_PER_SEC CLK_TCK
164#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000165#define CLOCKS_PER_SEC 1000000
166#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000167#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000168
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000169static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000170time_clock(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000171 PyObject *self;
172 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000173{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000174 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000176 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000178#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000179
Guido van Rossum3917c221997-04-02 05:35:28 +0000180#ifdef MS_WIN32
181/* Due to Mark Hammond */
182static PyObject *
183time_clock(self, args)
184 PyObject *self;
185 PyObject *args;
186{
187 static LARGE_INTEGER ctrStart;
188 static LARGE_INTEGER divisor = {0,0};
189 LARGE_INTEGER now, diff, rem;
190
191 if (!PyArg_NoArgs(args))
192 return NULL;
193
194 if (LargeIntegerEqualToZero(divisor)) {
195 QueryPerformanceCounter(&ctrStart);
196 if (!QueryPerformanceFrequency(&divisor) ||
197 LargeIntegerEqualToZero(divisor)) {
198 /* Unlikely to happen -
199 this works on all intel machines at least!
200 Revert to clock() */
201 return PyFloat_FromDouble(clock());
202 }
203 }
204 QueryPerformanceCounter(&now);
205 diff = LargeIntegerSubtract(now, ctrStart);
206 diff = LargeIntegerDivide(diff, divisor, &rem);
207 /* XXX - we assume both divide results fit in 32 bits. This is
208 true on Intels. First person who can afford a machine that
209 doesnt deserves to fix it :-)
210 */
211 return PyFloat_FromDouble((double)diff.LowPart +
212 ((double)rem.LowPart / (double)divisor.LowPart));
213}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000214
Guido van Rossum3917c221997-04-02 05:35:28 +0000215#define HAVE_CLOCK /* So it gets included in the methods */
216#endif /* MS_WIN32 */
217
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000218#ifdef HAVE_CLOCK
219static char clock_doc[] =
220"clock() -> floating point number\n\
221\n\
222Return the CPU time or real time since the start of the process or since\n\
223the first call to clock(). This has as much precision as the system records.";
224#endif
225
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000226static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227time_sleep(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000228 PyObject *self;
229 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000231 double secs;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000232 if (!PyArg_Parse(args, "d", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000234 if (floatsleep(secs) != 0)
235 return NULL;
236 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000237 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000238}
239
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000240static char sleep_doc[] =
241"sleep(seconds)\n\
242\n\
243Delay execution for a given number of seconds. The argument may be\n\
244a floating point number for subsecond precision.";
245
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000246static PyObject *
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000247tmtotuple(p)
248 struct tm *p;
249{
250 return Py_BuildValue("(iiiiiiiii)",
251 p->tm_year + 1900,
252 p->tm_mon + 1, /* Want January == 1 */
253 p->tm_mday,
254 p->tm_hour,
255 p->tm_min,
256 p->tm_sec,
257 (p->tm_wday + 6) % 7, /* Want Monday == 0 */
258 p->tm_yday + 1, /* Want January, 1 == 1 */
259 p->tm_isdst);
260}
261
262static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000263time_convert(when, function)
264 time_t when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000265 struct tm * (*function) Py_PROTO((const time_t *));
Guido van Rossum234f9421993-06-17 12:35:49 +0000266{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000267 struct tm *p;
268 errno = 0;
269 p = function(&when);
270 if (p == NULL) {
271#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000272 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000273 errno = EINVAL;
274#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000275 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000276 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000277 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000278}
279
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000280static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000281time_gmtime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000282 PyObject *self;
283 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000284{
285 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000286 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000287 return NULL;
288 return time_convert((time_t)when, gmtime);
289}
290
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000291static char gmtime_doc[] =
292"gmtime(seconds) -> tuple\n\
293\n\
294Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT).";
295
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000296static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000297time_localtime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000298 PyObject *self;
299 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000300{
301 double when;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000302 if (!PyArg_Parse(args, "d", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000303 return NULL;
304 return time_convert((time_t)when, localtime);
305}
306
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000307static char localtime_doc[] =
308"localtime(seconds) -> tuple\n\
309Convert seconds since the Epoch to a time tuple expressing local time.";
310
Guido van Rossum9e90a671993-06-24 11:10:19 +0000311static int
312gettmarg(args, p)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000313 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000314 struct tm *p;
315{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000316 if (!PyArg_Parse(args, "(iiiiiiiii)",
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000317 &p->tm_year,
318 &p->tm_mon,
319 &p->tm_mday,
320 &p->tm_hour,
321 &p->tm_min,
322 &p->tm_sec,
323 &p->tm_wday,
324 &p->tm_yday,
325 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000326 return 0;
327 if (p->tm_year >= 1900)
328 p->tm_year -= 1900;
329 p->tm_mon--;
330 p->tm_wday = (p->tm_wday + 1) % 7;
331 p->tm_yday--;
332 return 1;
333}
334
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000335#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000336static PyObject *
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000337time_strftime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000338 PyObject *self;
339 PyObject *args;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000340{
341 struct tm buf;
342 const char *fmt;
343 char *outbuf = 0;
344 int i;
345
Guido van Rossum1f41f841998-04-27 19:04:26 +0000346 memset((ANY *) &buf, '\0', sizeof(buf));
347
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000348 if (!PyArg_ParseTuple(args, "s(iiiiiiiii)",
349 &fmt,
350 &(buf.tm_year),
351 &(buf.tm_mon),
352 &(buf.tm_mday),
353 &(buf.tm_hour),
354 &(buf.tm_min),
355 &(buf.tm_sec),
356 &(buf.tm_wday),
357 &(buf.tm_yday),
358 &(buf.tm_isdst)))
359 return NULL;
360 if (buf.tm_year >= 1900)
361 buf.tm_year -= 1900;
362 buf.tm_mon--;
363 buf.tm_wday = (buf.tm_wday + 1) % 7;
364 buf.tm_yday--;
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000365 /* I hate these functions that presume you know how big the output
366 * will be ahead of time...
367 */
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000368 for (i = 1024 ; i <= 8192 ; i += 1024) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000369 outbuf = malloc(i);
370 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000371 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000372 }
373 if (strftime(outbuf, i-1, fmt, &buf) != 0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000374 PyObject *ret;
375 ret = PyString_FromString(outbuf);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000376 free(outbuf);
377 return ret;
378 }
379 free(outbuf);
380 }
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000381 PyErr_SetString(PyExc_ValueError,
382 "bad strftime format or result too big");
383 return NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000384}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000385
386static char strftime_doc[] =
387"strftime(format, tuple) -> string\n\
388\n\
389Convert a time tuple to a string according to a format specification.\n\
390See the library reference manual for formatting codes.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000391#endif /* HAVE_STRFTIME */
392
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000393#ifdef HAVE_STRPTIME
394static PyObject *
395time_strptime(self, args)
396 PyObject *self;
397 PyObject *args;
398{
399 struct tm tm;
400 char *fmt = "%a %b %d %H:%M:%S %Y";
401 char *buf;
402 char *s;
403
404 if (!PyArg_ParseTuple(args, "s|s", &buf, &fmt)) {
405 PyErr_SetString(PyExc_ValueError, "invalid argument");
406 return NULL;
407 }
408 s = strptime(buf, fmt, &tm);
409 if (s == NULL) {
410 PyErr_SetString(PyExc_ValueError, "format mismatch");
411 return NULL;
412 }
413 while (*s && isspace(*s))
414 s++;
415 if (*s) {
416 PyErr_Format(PyExc_ValueError,
417 "unconverted data remains: '%.400s'", s);
418 return NULL;
419 }
420 return tmtotuple(&tm);
421}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000422
423static char strptime_doc[] =
424"strptime(format, string) -> tuple\n\
425Parse a string to a time tuple according to a format specification.\n\
426See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000427#endif /* HAVE_STRPTIME */
428
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000429static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000430time_asctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000431 PyObject *self;
432 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000433{
434 struct tm buf;
435 char *p;
436 if (!gettmarg(args, &buf))
437 return NULL;
438 p = asctime(&buf);
439 if (p[24] == '\n')
440 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000441 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000442}
443
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000444static char asctime_doc[] =
445"asctime(tuple) -> string\n\
446\n\
447Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.";
448
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000449static PyObject *
Guido van Rossum9e90a671993-06-24 11:10:19 +0000450time_ctime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000451 PyObject *self;
452 PyObject *args;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000453{
454 double dt;
455 time_t tt;
456 char *p;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000457 if (!PyArg_Parse(args, "d", &dt))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000458 return NULL;
Guido van Rossumcac6c721996-09-06 13:34:02 +0000459 tt = (time_t)dt;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000460 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000461 if (p == NULL) {
462 PyErr_SetString(PyExc_ValueError, "unconvertible time");
463 return NULL;
464 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000465 if (p[24] == '\n')
466 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000467 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000468}
469
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000470static char ctime_doc[] =
471"ctime(seconds) -> string\n\
472\n\
473Convert a time in seconds since the Epoch to a string in local time.\n\
474This is equivalent to asctime(localtime(seconds)).";
475
Guido van Rossum60cd8131998-03-06 17:16:21 +0000476#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000477static PyObject *
Guido van Rossum234f9421993-06-17 12:35:49 +0000478time_mktime(self, args)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000479 PyObject *self;
480 PyObject *args;
Guido van Rossum234f9421993-06-17 12:35:49 +0000481{
482 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000483 time_t tt;
484 tt = time(&tt);
485 buf = *localtime(&tt);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000486 if (!gettmarg(args, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000487 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000488 tt = mktime(&buf);
489 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000490 PyErr_SetString(PyExc_OverflowError,
491 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000492 return NULL;
493 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000494 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000495}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000496
497static char mktime_doc[] =
498"mktime(tuple) -> floating point number\n\
499\n\
500Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000501#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000502
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000503static PyMethodDef time_methods[] = {
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000504 {"time", time_time, 0, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000505#ifdef HAVE_CLOCK
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000506 {"clock", time_clock, 0, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000507#endif
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000508 {"sleep", time_sleep, 0, sleep_doc},
509 {"gmtime", time_gmtime, 0, gmtime_doc},
510 {"localtime", time_localtime, 0, localtime_doc},
511 {"asctime", time_asctime, 0, asctime_doc},
512 {"ctime", time_ctime, 0, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000513#ifdef HAVE_MKTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000514 {"mktime", time_mktime, 0, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000515#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000516#ifdef HAVE_STRFTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000517 {"strftime", time_strftime, 1, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000518#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000519#ifdef HAVE_STRPTIME
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000520 {"strptime", time_strptime, 1, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000521#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000522 {NULL, NULL} /* sentinel */
523};
524
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000525static void
526ins(d, name, v)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000527 PyObject *d;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000528 char *name;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000529 PyObject *v;
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000530{
531 if (v == NULL)
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000532 Py_FatalError("Can't initialize time module -- NULL value");
533 if (PyDict_SetItemString(d, name, v) != 0)
534 Py_FatalError(
Guido van Rossum52174571996-12-09 18:38:52 +0000535 "Can't initialize time module -- PyDict_SetItemString failed");
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000536 Py_DECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000537}
538
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000539static char module_doc[] =
540"This module provides various functions to manipulate time values.\n\
541\n\
542There are two standard representations of time. One is the number\n\
543of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
544or a floating point number (to represent fractions of seconds).\n\
545The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
546The actual value can be retrieved by calling gmtime(0).\n\
547\n\
548The other representation is a tuple of 9 integers giving local time.\n\
549The tuple items are:\n\
550 year (four digits, e.g. 1998)\n\
551 month (1-12)\n\
552 day (1-31)\n\
553 hours (0-23)\n\
554 minutes (0-59)\n\
555 seconds (0-61, to allow for leap seconds)\n\
556 weekday (0-6, Monday is 0)\n\
557 Julian day (day in the year, 1-366)\n\
558 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
559If the DST flag is 0, the time is given in the regular time zone;\n\
560if it is 1, the time is given in the DST time zone;\n\
561if it is -1, mktime() should guess based on the date and time.\n\
562\n\
563Variables:\n\
564\n\
565timezone -- difference in seconds between UTC and local standard time\n\
566altzone -- difference in seconds between UTC and local DST time\n\
567daylight -- whether local time should reflect DST\n\
568tzname -- tuple of (standard time zone name, DST time zone name)\n\
569\n\
570Functions:\n\
571\n\
572time() -- return current time in seconds since the Epoch as a float\n\
573clock() -- return CPU time since process start as a float\n\
574sleep() -- delay for a number of seconds given as a float\n\
575gmtime() -- convert seconds since Epoch to UTC tuple\n\
576localtime() -- convert seconds since Epoch to local time tuple\n\
577asctime() -- convert time tuple to string\n\
578ctime() -- convert time in seconds to string\n\
579mktime() -- convert local time tuple to seconds since Epoch\n\
580strftime() -- convert time tuple to string according to format specification\n\
581strptime() -- parse string to time tuple according to format specification\n\
582";
583
584
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000585void
586inittime()
587{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000588 PyObject *m, *d;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000589 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000590 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000591#ifdef HAVE_TZNAME
Guido van Rossum234f9421993-06-17 12:35:49 +0000592 tzset();
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000593 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000594#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000595 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000596#else
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000597 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000598#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000599 ins(d, "daylight", PyInt_FromLong((long)daylight));
600 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000601#else /* !HAVE_TZNAME */
602#if HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000603 {
604#define YEAR ((time_t)((365 * 24 + 6) * 3600))
605 time_t t;
606 struct tm *p;
607 long winterzone, summerzone;
608 char wintername[10], summername[10];
Guido van Rossumb6775db1994-08-01 11:34:53 +0000609 /* XXX This won't work on the southern hemisphere.
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000610 XXX Anybody got a better idea? */
Guido van Rossum234f9421993-06-17 12:35:49 +0000611 t = (time((time_t *)0) / YEAR) * YEAR;
612 p = localtime(&t);
613 winterzone = -p->tm_gmtoff;
614 strncpy(wintername, p->tm_zone ? p->tm_zone : " ", 9);
615 wintername[9] = '\0';
616 t += YEAR/2;
617 p = localtime(&t);
618 summerzone = -p->tm_gmtoff;
619 strncpy(summername, p->tm_zone ? p->tm_zone : " ", 9);
620 summername[9] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000621 ins(d, "timezone", PyInt_FromLong(winterzone));
622 ins(d, "altzone", PyInt_FromLong(summerzone));
623 ins(d, "daylight",
624 PyInt_FromLong((long)(winterzone != summerzone)));
625 ins(d, "tzname",
626 Py_BuildValue("(zz)", wintername, summername));
Guido van Rossum234f9421993-06-17 12:35:49 +0000627 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000628#else
629#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000630 /* The only thing we can obtain is the current timezone
631 ** (and whether dst is currently _active_, but that is not what
632 ** we're looking for:-( )
633 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000634 initmactimezone();
635 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000636 ins(d, "altzone", PyInt_FromLong(timezone));
637 ins(d, "daylight", PyInt_FromLong((long)0));
638 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000639#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000640#endif /* HAVE_TM_ZONE */
641#endif /* !HAVE_TZNAME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000642 if (PyErr_Occurred())
643 Py_FatalError("Can't initialize time module");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644}
645
646
Guido van Rossumb6775db1994-08-01 11:34:53 +0000647/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000648
Guido van Rossumb6775db1994-08-01 11:34:53 +0000649static double
650floattime()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000651{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000652 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000653 (1) gettimeofday() -- resolution in microseconds
654 (2) ftime() -- resolution in milliseconds
655 (3) time() -- resolution in seconds
656 In all cases the return value is a float in seconds.
657 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
658 fail, so we fall back on ftime() or time().
659 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000660#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000661 {
662 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000663#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000664 if (gettimeofday(&t) == 0)
665 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000666#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000667 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
668 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000669#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000670 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000671#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000672 {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000673#ifdef HAVE_FTIME
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000674 struct timeb t;
675 ftime(&t);
676 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000677#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000678 time_t secs;
679 time(&secs);
680 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000681#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000682 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000683}
684
Guido van Rossumb6775db1994-08-01 11:34:53 +0000685
686/* Implement floatsleep() for various platforms.
687 When interrupted (or when another error occurs), return -1 and
688 set an exception; else return 0. */
689
690static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000691#ifdef MPW
692floatsleep(double secs)
693#else
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000694 floatsleep(secs)
Guido van Rossum775f4da1993-01-09 17:18:52 +0000695 double secs;
Guido van Rossuma320fd31995-03-09 12:14:15 +0000696#endif /* MPW */
Guido van Rossum426035c1991-02-19 12:27:35 +0000697{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000698/* XXX Should test for MS_WIN32 first! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000699#ifdef HAVE_SELECT
Guido van Rossum426035c1991-02-19 12:27:35 +0000700 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000701 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000702 frac = fmod(secs, 1.0);
703 secs = floor(secs);
704 t.tv_sec = (long)secs;
705 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000706 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000707 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000708 Py_BLOCK_THREADS
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000709 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000710 return -1;
711 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000712 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000713#else /* !HAVE_SELECT */
714#ifdef macintosh
715#define MacTicks (* (long *)0x16A)
716 long deadline;
717 deadline = MacTicks + (long)(secs * 60.0);
718 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000719 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000720 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000721 return -1;
722 }
723#else /* !macintosh */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000724#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000725 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000726 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000727 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000728 Py_END_ALLOW_THREADS
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000729#else /* !__WATCOMC__ || __QNX__ */
Guido van Rossume22e6441993-07-09 10:51:31 +0000730#ifdef MSDOS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000731 struct timeb t1, t2;
732 double frac;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000733 extern double fmod Py_PROTO((double, double));
734 extern double floor Py_PROTO((double));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000735 if (secs <= 0.0)
736 return;
737 frac = fmod(secs, 1.0);
738 secs = floor(secs);
739 ftime(&t1);
740 t2.time = t1.time + (int)secs;
741 t2.millitm = t1.millitm + (int)(frac*1000.0);
742 while (t2.millitm >= 1000) {
743 t2.time++;
744 t2.millitm -= 1000;
745 }
746 for (;;) {
747#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000748 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000749 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000750 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000751#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000752 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000753 return -1;
754 ftime(&t1);
755 if (t1.time > t2.time ||
756 t1.time == t2.time && t1.millitm >= t2.millitm)
757 break;
758 }
759#else /* !MSDOS */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000760#ifdef MS_WIN32
Guido van Rossumb6775db1994-08-01 11:34:53 +0000761 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000762 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000763 Sleep((int)(secs*1000));
Guido van Rossum8607ae21997-11-03 22:04:46 +0000764 Py_END_ALLOW_THREADS
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000765#else /* !MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000766#ifdef PYOS_OS2
767 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000768 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000769 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000770 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000771 PyErr_SetFromErrno(PyExc_IOError);
772 return -1;
773 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000774 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000775#else /* !PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000776 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000777 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000778 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000779 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000780#endif /* !PYOS_OS2 */
Guido van Rossumb2fb3641996-09-07 00:47:35 +0000781#endif /* !MS_WIN32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000782#endif /* !MSDOS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +0000783#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000784#endif /* !macintosh */
785#endif /* !HAVE_SELECT */
786 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000787}