blob: 0f7b1434f1f41e257f2a9ddf2133fdc912338da4 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Time module */
3
Barry Warsaw9a2a8a81996-12-06 23:32:14 +00004#include "Python.h"
Guido van Rossum98bf58f2001-10-18 20:34:25 +00005#include "structseq.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00006
Guido van Rossum87ce7bb1998-06-09 16:30:31 +00007#include <ctype.h>
8
Guido van Rossum6d946f91992-08-14 13:49:30 +00009#ifdef macintosh
Guido van Rossumb6775db1994-08-01 11:34:53 +000010#include <time.h>
Guido van Rossumc410e922000-04-26 20:40:13 +000011#include <OSUtils.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +000012#else
13#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000014#endif
15
Guido van Rossumb6775db1994-08-01 11:34:53 +000016#ifdef QUICKWIN
17#include <io.h>
18#endif
19
Guido van Rossumb6775db1994-08-01 11:34:53 +000020#ifdef HAVE_FTIME
21#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000022#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000023extern int ftime(struct timeb *);
Guido van Rossum52174571996-12-09 18:38:52 +000024#endif /* MS_WINDOWS */
25#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000026
Guido van Rossum7bf22de1997-12-02 20:34:19 +000027#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000028#include <i86.h>
29#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000030#ifdef MS_WINDOWS
Mark Hammond975e3922002-07-16 01:29:19 +000031#define WIN32_LEAN_AND_MEAN
Guido van Rossum258ccd42001-03-02 06:53:29 +000032#include <windows.h>
Mark Hammond975e3922002-07-16 01:29:19 +000033#include "pythread.h"
34
35/* helper to allow us to interrupt sleep() on Windows*/
36static HANDLE hInterruptEvent = NULL;
37static BOOL WINAPI PyCtrlHandler(DWORD dwCtrlType)
38{
39 SetEvent(hInterruptEvent);
40 /* allow other default handlers to be called.
41 Default Python handler will setup the
42 KeyboardInterrupt exception.
43 */
44 return FALSE;
45}
46static long main_thread;
47
48
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000049#if defined(__BORLANDC__)
Guido van Rossumb2fb3641996-09-07 00:47:35 +000050/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000051#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000052#define tzname _tzname
53#define daylight _daylight
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000054#endif /* __BORLANDC__ */
Guido van Rossumcac6c721996-09-06 13:34:02 +000055#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000056#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000057
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000058#if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Fred Drakedfb4ebd2000-06-29 20:56:28 +000059/* Win32 has better clock replacement
60 XXX Win64 does not yet, but might when the platform matures. */
Guido van Rossum3917c221997-04-02 05:35:28 +000061#undef HAVE_CLOCK /* We have our own version down below */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000062#endif /* MS_WINDOWS && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +000063
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000064#if defined(PYOS_OS2)
65#define INCL_DOS
66#define INCL_ERRORS
67#include <os2.h>
68#endif
69
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000070#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000071#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000072#endif
73
Guido van Rossumbcc20741998-08-04 22:53:56 +000074#ifdef __BEOS__
Fred Drake56221a72000-08-15 18:52:33 +000075#include <time.h>
Guido van Rossumbcc20741998-08-04 22:53:56 +000076/* For bigtime_t, snooze(). - [cjh] */
77#include <support/SupportDefs.h>
78#include <kernel/OS.h>
79#endif
80
Martin v. Löwisa94568a2003-05-10 07:36:56 +000081#ifdef RISCOS
82extern int riscos_sleep(double);
83#endif
84
Guido van Rossum234f9421993-06-17 12:35:49 +000085/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000086static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000087static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000089/* For Y2K check */
90static PyObject *moddict;
91
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000092#ifdef macintosh
93/* Our own timezone. We have enough information to deduce whether
94** DST is on currently, but unfortunately we cannot put it to good
95** use because we don't know the rules (and that is needed to have
96** localtime() return correct tm_isdst values for times other than
97** the current time. So, we cop out and only tell the user the current
98** timezone.
99*/
100static long timezone;
101
Guido van Rossum10b164a2001-09-25 13:59:01 +0000102static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000103initmactimezone(void)
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000104{
105 MachineLocation loc;
106 long delta;
107
108 ReadLocation(&loc);
Guido van Rossum10b164a2001-09-25 13:59:01 +0000109
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000110 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
111 return;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000112
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000113 delta = loc.u.gmtDelta & 0x00FFFFFF;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000114
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000115 if (delta & 0x00800000)
116 delta |= 0xFF000000;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000117
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000118 timezone = -delta;
119}
120#endif /* macintosh */
121
122
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000123static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000124time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000126 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000127 if (!PyArg_ParseTuple(args, ":time"))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000128 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000129 secs = floattime();
130 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000131 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000132 return NULL;
133 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000134 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000135}
136
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000137PyDoc_STRVAR(time_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000138"time() -> floating point number\n\
139\n\
140Return the current time in seconds since the Epoch.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000141Fractions of a second may be present if the system clock provides them.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000142
Guido van Rossumb6775db1994-08-01 11:34:53 +0000143#ifdef HAVE_CLOCK
144
145#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000146#ifdef CLK_TCK
147#define CLOCKS_PER_SEC CLK_TCK
148#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000149#define CLOCKS_PER_SEC 1000000
150#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000151#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000153static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000154time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000155{
Thomas Woutersfe385252001-01-19 23:16:56 +0000156 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000158 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000160#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000162#if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Mark Hammond7ba5e812002-02-12 04:02:33 +0000163/* Due to Mark Hammond and Tim Peters */
Guido van Rossum3917c221997-04-02 05:35:28 +0000164static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000165time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000166{
Tim Peters9ad4b682002-02-13 05:14:18 +0000167 static LARGE_INTEGER ctrStart;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000168 static double divisor = 0.0;
Tim Peters9ad4b682002-02-13 05:14:18 +0000169 LARGE_INTEGER now;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000170 double diff;
Guido van Rossum3917c221997-04-02 05:35:28 +0000171
Thomas Woutersfe385252001-01-19 23:16:56 +0000172 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum3917c221997-04-02 05:35:28 +0000173 return NULL;
174
Mark Hammond7ba5e812002-02-12 04:02:33 +0000175 if (divisor == 0.0) {
Tim Peters9ad4b682002-02-13 05:14:18 +0000176 LARGE_INTEGER freq;
177 QueryPerformanceCounter(&ctrStart);
178 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
Mark Hammond7ba5e812002-02-12 04:02:33 +0000179 /* Unlikely to happen - this works on all intel
180 machines at least! Revert to clock() */
Guido van Rossum3917c221997-04-02 05:35:28 +0000181 return PyFloat_FromDouble(clock());
182 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000183 divisor = (double)freq.QuadPart;
Guido van Rossum3917c221997-04-02 05:35:28 +0000184 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000185 QueryPerformanceCounter(&now);
186 diff = (double)(now.QuadPart - ctrStart.QuadPart);
Mark Hammond7ba5e812002-02-12 04:02:33 +0000187 return PyFloat_FromDouble(diff / divisor);
Guido van Rossum3917c221997-04-02 05:35:28 +0000188}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000189
Guido van Rossum3917c221997-04-02 05:35:28 +0000190#define HAVE_CLOCK /* So it gets included in the methods */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000191#endif /* MS_WINDOWS && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000192
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000193#ifdef HAVE_CLOCK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000194PyDoc_STRVAR(clock_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000195"clock() -> floating point number\n\
196\n\
197Return the CPU time or real time since the start of the process or since\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000198the first call to clock(). This has as much precision as the system\n\
199records.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000200#endif
201
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000202static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000203time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000205 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000206 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000208 if (floatsleep(secs) != 0)
209 return NULL;
210 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000211 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000212}
213
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000214PyDoc_STRVAR(sleep_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000215"sleep(seconds)\n\
216\n\
217Delay execution for a given number of seconds. The argument may be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000218a floating point number for subsecond precision.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000219
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000220static PyStructSequence_Field struct_time_type_fields[] = {
221 {"tm_year", NULL},
222 {"tm_mon", NULL},
223 {"tm_mday", NULL},
224 {"tm_hour", NULL},
225 {"tm_min", NULL},
226 {"tm_sec", NULL},
227 {"tm_wday", NULL},
228 {"tm_yday", NULL},
229 {"tm_isdst", NULL},
230 {0}
231};
232
233static PyStructSequence_Desc struct_time_type_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000234 "time.struct_time",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000235 NULL,
236 struct_time_type_fields,
237 9,
238};
Tim Peters9ad4b682002-02-13 05:14:18 +0000239
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000240static PyTypeObject StructTimeType;
241
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000242static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000243tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000244{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000245 PyObject *v = PyStructSequence_New(&StructTimeType);
246 if (v == NULL)
247 return NULL;
Tim Peters9ad4b682002-02-13 05:14:18 +0000248
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000249#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
250
251 SET(0, p->tm_year + 1900);
252 SET(1, p->tm_mon + 1); /* Want January == 1 */
253 SET(2, p->tm_mday);
254 SET(3, p->tm_hour);
255 SET(4, p->tm_min);
256 SET(5, p->tm_sec);
257 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
258 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
259 SET(8, p->tm_isdst);
260#undef SET
261 if (PyErr_Occurred()) {
262 Py_XDECREF(v);
263 return NULL;
264 }
265
266 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000267}
268
269static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000270time_convert(time_t when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000271{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000272 struct tm *p;
273 errno = 0;
274 p = function(&when);
275 if (p == NULL) {
276#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000277 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000278 errno = EINVAL;
279#endif
Tim Peters8b19a932003-01-17 20:08:54 +0000280 return PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000281 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000282 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000283}
284
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000285static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000286time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000287{
288 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000289 if (PyTuple_Size(args) == 0)
290 when = floattime();
291 if (!PyArg_ParseTuple(args, "|d:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000292 return NULL;
293 return time_convert((time_t)when, gmtime);
294}
295
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000296PyDoc_STRVAR(gmtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000297"gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,\n\
298 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000299\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000300Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000301GMT). When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000302
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000303static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000304time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000305{
306 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000307 if (PyTuple_Size(args) == 0)
308 when = floattime();
309 if (!PyArg_ParseTuple(args, "|d:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000310 return NULL;
311 return time_convert((time_t)when, localtime);
312}
313
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000314PyDoc_STRVAR(localtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000315"localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000316\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000317Convert seconds since the Epoch to a time tuple expressing local time.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000318When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000319
Guido van Rossum9e90a671993-06-24 11:10:19 +0000320static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000321gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000322{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000323 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000324 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000325
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000326 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000327 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000328 &p->tm_mon,
329 &p->tm_mday,
330 &p->tm_hour,
331 &p->tm_min,
332 &p->tm_sec,
333 &p->tm_wday,
334 &p->tm_yday,
335 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000336 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000337 if (y < 1900) {
338 PyObject *accept = PyDict_GetItemString(moddict,
339 "accept2dyear");
340 if (accept == NULL || !PyInt_Check(accept) ||
341 PyInt_AsLong(accept) == 0) {
342 PyErr_SetString(PyExc_ValueError,
343 "year >= 1900 required");
344 return 0;
345 }
346 if (69 <= y && y <= 99)
347 y += 1900;
348 else if (0 <= y && y <= 68)
349 y += 2000;
350 else {
351 PyErr_SetString(PyExc_ValueError,
Skip Montanaro1a10aac2001-08-22 12:39:16 +0000352 "year out of range");
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000353 return 0;
354 }
355 }
356 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000357 p->tm_mon--;
358 p->tm_wday = (p->tm_wday + 1) % 7;
359 p->tm_yday--;
360 return 1;
361}
362
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000363#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000364static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000365time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000366{
Thomas Woutersfe385252001-01-19 23:16:56 +0000367 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000368 struct tm buf;
369 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000370 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000371 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000372 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000373
Thomas Wouters334fb892000-07-25 12:56:38 +0000374 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000375
Thomas Woutersfe385252001-01-19 23:16:56 +0000376 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000377 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000378
379 if (tup == NULL) {
380 time_t tt = time(NULL);
381 buf = *localtime(&tt);
382 } else if (!gettmarg(tup, &buf))
383 return NULL;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000384
Guido van Rossumc222ec21999-02-23 00:00:10 +0000385 fmtlen = strlen(fmt);
386
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000387 /* I hate these functions that presume you know how big the output
388 * will be ahead of time...
389 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000390 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000391 outbuf = malloc(i);
392 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000393 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000394 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000395 buflen = strftime(outbuf, i, fmt, &buf);
396 if (buflen > 0 || i >= 256 * fmtlen) {
397 /* If the buffer is 256 times as long as the format,
398 it's probably not failing for lack of room!
399 More likely, the format yields an empty result,
400 e.g. an empty format, or %Z when the timezone
401 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000402 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000403 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000404 free(outbuf);
405 return ret;
406 }
407 free(outbuf);
408 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000409}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000410
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000411PyDoc_STRVAR(strftime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000412"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000413\n\
414Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000415See the library reference manual for formatting codes. When the time tuple\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000416is not present, current time as returned by localtime() is used.");
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000417#endif /* HAVE_STRFTIME */
418
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000419static PyObject *
420time_strptime(PyObject *self, PyObject *args)
421{
422 PyObject *strptime_module = PyImport_ImportModule("_strptime");
Raymond Hettinger502168a2003-04-10 16:03:22 +0000423 PyObject *strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000424
Tim Peters513a1cd2003-01-19 04:54:58 +0000425 if (!strptime_module)
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000426 return NULL;
Raymond Hettinger502168a2003-04-10 16:03:22 +0000427 strptime_result = PyObject_CallMethod(strptime_module, "strptime", "O", args);
428 Py_DECREF(strptime_module);
429 return strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000430}
431
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000432PyDoc_STRVAR(strptime_doc,
Brett Cannon20def8b2003-07-01 05:16:08 +0000433"strptime(string, format) -> struct_time\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000434\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000435Parse a string to a time tuple according to a format specification.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000436See the library reference manual for formatting codes (same as strftime()).");
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000437
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000438
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000439static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000440time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000441{
Thomas Woutersfe385252001-01-19 23:16:56 +0000442 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000443 struct tm buf;
444 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000445 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000446 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000447 if (tup == NULL) {
448 time_t tt = time(NULL);
449 buf = *localtime(&tt);
450 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000451 return NULL;
452 p = asctime(&buf);
453 if (p[24] == '\n')
454 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000455 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000456}
457
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000458PyDoc_STRVAR(asctime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000459"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000460\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000461Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
462When the time tuple is not present, current time as returned by localtime()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000463is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000464
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000465static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000466time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000467{
468 double dt;
469 time_t tt;
470 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000471
Thomas Woutersfe385252001-01-19 23:16:56 +0000472 if (PyTuple_Size(args) == 0)
473 tt = time(NULL);
474 else {
475 if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
476 return NULL;
477 tt = (time_t)dt;
478 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000479 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000480 if (p == NULL) {
481 PyErr_SetString(PyExc_ValueError, "unconvertible time");
482 return NULL;
483 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000484 if (p[24] == '\n')
485 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000486 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000487}
488
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000489PyDoc_STRVAR(ctime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000490"ctime(seconds) -> string\n\
491\n\
492Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000493This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000494not present, current time as returned by localtime() is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000495
Guido van Rossum60cd8131998-03-06 17:16:21 +0000496#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000497static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000498time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000499{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000500 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000501 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000502 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000503 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000504 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000505 tt = time(&tt);
506 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000507 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000508 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000509 tt = mktime(&buf);
510 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000511 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000512 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000513 return NULL;
514 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000515 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000516}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000518PyDoc_STRVAR(mktime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000519"mktime(tuple) -> floating point number\n\
520\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000521Convert a time tuple in local time to seconds since the Epoch.");
Guido van Rossum60cd8131998-03-06 17:16:21 +0000522#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000523
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000524#ifdef HAVE_WORKING_TZSET
525void inittimezone(PyObject *module);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000526
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000527static PyObject *
528time_tzset(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000529{
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000530 PyObject* m;
Fred Drake9bb74322002-04-01 14:49:59 +0000531
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000532 if (!PyArg_ParseTuple(args, ":tzset"))
533 return NULL;
534
535 m = PyImport_ImportModule("time");
536 if (m == NULL) {
537 return NULL;
538 }
539
540 tzset();
541
542 /* Reset timezone, altzone, daylight and tzname */
543 inittimezone(m);
544 Py_DECREF(m);
545
546 Py_INCREF(Py_None);
547 return Py_None;
548}
549
550PyDoc_STRVAR(tzset_doc,
551"tzset(zone)\n\
552\n\
553Initialize, or reinitialize, the local timezone to the value stored in\n\
554os.environ['TZ']. The TZ environment variable should be specified in\n\
555standard Uniz timezone format as documented in the tzset man page\n\
556(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
557fall back to UTC. If the TZ environment variable is not set, the local\n\
558timezone is set to the systems best guess of wallclock time.\n\
559Changing the TZ environment variable without calling tzset *may* change\n\
560the local timezone used by methods such as localtime, but this behaviour\n\
561should not be relied on.");
562#endif /* HAVE_WORKING_TZSET */
563
564void inittimezone(PyObject *m) {
565 /* This code moved from inittime wholesale to allow calling it from
566 time_tzset. In the future, some parts of it can be moved back
567 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
568 are), and the extranious calls to tzset(3) should be removed.
569 I havn't done this yet, as I don't want to change this code as
570 little as possible when introducing the time.tzset and time.tzsetwall
571 methods. This should simply be a method of doing the following once,
572 at the top of this function and removing the call to tzset() from
573 time_tzset():
574
575 #ifdef HAVE_TZSET
576 tzset()
577 #endif
578
579 And I'm lazy and hate C so nyer.
580 */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000581#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000582 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000583#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000584 PyModule_AddIntConstant(m, "timezone", _timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000585#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000586 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000587#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000588#ifdef HAVE_ALTZONE
Fred Drake9bb74322002-04-01 14:49:59 +0000589 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000590#else
Guido van Rossum26452411998-09-28 22:07:11 +0000591#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000592 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000593#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000594 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000595#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000596#endif
Fred Drake9bb74322002-04-01 14:49:59 +0000597 PyModule_AddIntConstant(m, "daylight", daylight);
598 PyModule_AddObject(m, "tzname",
599 Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000600#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000601#ifdef HAVE_STRUCT_TM_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000602 {
603#define YEAR ((time_t)((365 * 24 + 6) * 3600))
604 time_t t;
605 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000606 long janzone, julyzone;
607 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000608 t = (time((time_t *)0) / YEAR) * YEAR;
609 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000610 janzone = -p->tm_gmtoff;
611 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
612 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000613 t += YEAR/2;
614 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000615 julyzone = -p->tm_gmtoff;
616 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
617 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000618
Guido van Rossum57731601999-03-29 19:12:04 +0000619 if( janzone < julyzone ) {
620 /* DST is reversed in the southern hemisphere */
Fred Drake9bb74322002-04-01 14:49:59 +0000621 PyModule_AddIntConstant(m, "timezone", julyzone);
622 PyModule_AddIntConstant(m, "altzone", janzone);
623 PyModule_AddIntConstant(m, "daylight",
624 janzone != julyzone);
625 PyModule_AddObject(m, "tzname",
626 Py_BuildValue("(zz)",
627 julyname, janname));
Guido van Rossum57731601999-03-29 19:12:04 +0000628 } else {
Fred Drake9bb74322002-04-01 14:49:59 +0000629 PyModule_AddIntConstant(m, "timezone", janzone);
630 PyModule_AddIntConstant(m, "altzone", julyzone);
631 PyModule_AddIntConstant(m, "daylight",
632 janzone != julyzone);
633 PyModule_AddObject(m, "tzname",
634 Py_BuildValue("(zz)",
635 janname, julyname));
Guido van Rossum57731601999-03-29 19:12:04 +0000636 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000637 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000638#else
639#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000640 /* The only thing we can obtain is the current timezone
641 ** (and whether dst is currently _active_, but that is not what
642 ** we're looking for:-( )
643 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000644 initmactimezone();
Fred Drake9bb74322002-04-01 14:49:59 +0000645 PyModule_AddIntConstant(m, "timezone", timezone);
646 PyModule_AddIntConstant(m, "altzone", timezone);
647 PyModule_AddIntConstant(m, "daylight", 0);
648 PyModule_AddObject(m, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000649#endif /* macintosh */
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000650#endif /* HAVE_STRUCT_TM_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000651#ifdef __CYGWIN__
652 tzset();
Fred Drake9bb74322002-04-01 14:49:59 +0000653 PyModule_AddIntConstant(m, "timezone", _timezone);
654 PyModule_AddIntConstant(m, "altzone", _timezone);
655 PyModule_AddIntConstant(m, "daylight", _daylight);
656 PyModule_AddObject(m, "tzname",
657 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +0000658#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000659#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000660}
661
662
663static PyMethodDef time_methods[] = {
664 {"time", time_time, METH_VARARGS, time_doc},
665#ifdef HAVE_CLOCK
666 {"clock", time_clock, METH_VARARGS, clock_doc},
667#endif
668 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
669 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
670 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
671 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
672 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
673#ifdef HAVE_MKTIME
674 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
675#endif
676#ifdef HAVE_STRFTIME
677 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
678#endif
679 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
680#ifdef HAVE_WORKING_TZSET
681 {"tzset", time_tzset, METH_VARARGS, tzset_doc},
682#endif
683 {NULL, NULL} /* sentinel */
684};
685
686
687PyDoc_STRVAR(module_doc,
688"This module provides various functions to manipulate time values.\n\
689\n\
690There are two standard representations of time. One is the number\n\
691of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
692or a floating point number (to represent fractions of seconds).\n\
693The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
694The actual value can be retrieved by calling gmtime(0).\n\
695\n\
696The other representation is a tuple of 9 integers giving local time.\n\
697The tuple items are:\n\
698 year (four digits, e.g. 1998)\n\
699 month (1-12)\n\
700 day (1-31)\n\
701 hours (0-23)\n\
702 minutes (0-59)\n\
703 seconds (0-59)\n\
704 weekday (0-6, Monday is 0)\n\
705 Julian day (day in the year, 1-366)\n\
706 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
707If the DST flag is 0, the time is given in the regular time zone;\n\
708if it is 1, the time is given in the DST time zone;\n\
709if it is -1, mktime() should guess based on the date and time.\n\
710\n\
711Variables:\n\
712\n\
713timezone -- difference in seconds between UTC and local standard time\n\
714altzone -- difference in seconds between UTC and local DST time\n\
715daylight -- whether local time should reflect DST\n\
716tzname -- tuple of (standard time zone name, DST time zone name)\n\
717\n\
718Functions:\n\
719\n\
720time() -- return current time in seconds since the Epoch as a float\n\
721clock() -- return CPU time since process start as a float\n\
722sleep() -- delay for a number of seconds given as a float\n\
723gmtime() -- convert seconds since Epoch to UTC tuple\n\
724localtime() -- convert seconds since Epoch to local time tuple\n\
725asctime() -- convert time tuple to string\n\
726ctime() -- convert time in seconds to string\n\
727mktime() -- convert local time tuple to seconds since Epoch\n\
728strftime() -- convert time tuple to string according to format specification\n\
729strptime() -- parse string to time tuple according to format specification\n\
730tzset() -- change the local timezone");
731
732
733PyMODINIT_FUNC
734inittime(void)
735{
736 PyObject *m;
737 char *p;
738 m = Py_InitModule3("time", time_methods, module_doc);
739
740 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
741 p = Py_GETENV("PYTHONY2K");
742 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
743 /* Squirrel away the module's dictionary for the y2k check */
744 moddict = PyModule_GetDict(m);
745 Py_INCREF(moddict);
746
747 /* Set, or reset, module variables like time.timezone */
748 inittimezone(m);
749
Mark Hammond975e3922002-07-16 01:29:19 +0000750#ifdef MS_WINDOWS
751 /* Helper to allow interrupts for Windows.
752 If Ctrl+C event delivered while not sleeping
753 it will be ignored.
754 */
755 main_thread = PyThread_get_thread_ident();
756 hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
757 SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
758#endif /* MS_WINDOWS */
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000759 PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
Fred Drake9bb74322002-04-01 14:49:59 +0000760 Py_INCREF(&StructTimeType);
761 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000762}
763
764
Guido van Rossumb6775db1994-08-01 11:34:53 +0000765/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000766
Guido van Rossumb6775db1994-08-01 11:34:53 +0000767static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000768floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000769{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000770 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000771 (1) gettimeofday() -- resolution in microseconds
772 (2) ftime() -- resolution in milliseconds
773 (3) time() -- resolution in seconds
774 In all cases the return value is a float in seconds.
775 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
776 fail, so we fall back on ftime() or time().
777 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000778#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000779 {
780 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000781#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000782 if (gettimeofday(&t) == 0)
783 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000784#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000785 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
786 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000787#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000788 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000789#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000790 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000791#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000792 struct timeb t;
793 ftime(&t);
794 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000795#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000796 time_t secs;
797 time(&secs);
798 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000799#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000800 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000801}
802
Guido van Rossumb6775db1994-08-01 11:34:53 +0000803
804/* Implement floatsleep() for various platforms.
805 When interrupted (or when another error occurs), return -1 and
806 set an exception; else return 0. */
807
808static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000809floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000810{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000811/* XXX Should test for MS_WINDOWS first! */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000812#if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000813 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000814 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000815 frac = fmod(secs, 1.0);
816 secs = floor(secs);
817 t.tv_sec = (long)secs;
818 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000819 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000820 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000821#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000822 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000823#else
824 if (1) {
825#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000826 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000827 PyErr_SetFromErrno(PyExc_IOError);
828 return -1;
829 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000830 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000831 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000832#elif defined(macintosh)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000833#define MacTicks (* (long *)0x16A)
834 long deadline;
835 deadline = MacTicks + (long)(secs * 60.0);
836 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000837 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000838 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000839 return -1;
840 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000841#elif defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000842 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000843 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000844 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000845 Py_END_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000846#elif defined(MS_WINDOWS)
Fred Drake0e123952000-06-29 21:31:02 +0000847 {
848 double millisecs = secs * 1000.0;
Tim Peters513a1cd2003-01-19 04:54:58 +0000849 unsigned long ul_millis;
850
Fred Drake0e123952000-06-29 21:31:02 +0000851 if (millisecs > (double)ULONG_MAX) {
Tim Peters513a1cd2003-01-19 04:54:58 +0000852 PyErr_SetString(PyExc_OverflowError,
853 "sleep length is too large");
Fred Drake0e123952000-06-29 21:31:02 +0000854 return -1;
855 }
Fred Drake0e123952000-06-29 21:31:02 +0000856 Py_BEGIN_ALLOW_THREADS
Tim Peters513a1cd2003-01-19 04:54:58 +0000857 /* Allow sleep(0) to maintain win32 semantics, and as decreed
858 * by Guido, only the main thread can be interrupted.
859 */
860 ul_millis = (unsigned long)millisecs;
861 if (ul_millis == 0 ||
862 main_thread != PyThread_get_thread_ident())
863 Sleep(ul_millis);
Mark Hammond975e3922002-07-16 01:29:19 +0000864 else {
865 DWORD rc;
866 ResetEvent(hInterruptEvent);
Tim Peters513a1cd2003-01-19 04:54:58 +0000867 rc = WaitForSingleObject(hInterruptEvent, ul_millis);
868 if (rc == WAIT_OBJECT_0) {
869 /* Yield to make sure real Python signal
870 * handler called.
871 */
Mark Hammond975e3922002-07-16 01:29:19 +0000872 Sleep(1);
873 Py_BLOCK_THREADS
Mark Hammond975e3922002-07-16 01:29:19 +0000874 errno = EINTR;
875 PyErr_SetFromErrno(PyExc_IOError);
876 return -1;
877 }
878 }
Fred Drake0e123952000-06-29 21:31:02 +0000879 Py_END_ALLOW_THREADS
880 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000881#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000882 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000883 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000884 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000885 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000886 PyErr_SetFromErrno(PyExc_IOError);
887 return -1;
888 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000889 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000890#elif defined(__BEOS__)
Guido van Rossumbcc20741998-08-04 22:53:56 +0000891 /* This sleep *CAN BE* interrupted. */
892 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000893 if( secs <= 0.0 ) {
894 return;
895 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000896
Guido van Rossumbcc20741998-08-04 22:53:56 +0000897 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000898 /* BeOS snooze() is in microseconds... */
899 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000900 Py_BLOCK_THREADS
901 PyErr_SetFromErrno( PyExc_IOError );
902 return -1;
903 }
904 Py_END_ALLOW_THREADS
905 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000906#elif defined(RISCOS)
Guido van Rossumbceccf52001-04-10 22:07:43 +0000907 if (secs <= 0.0)
908 return 0;
909 Py_BEGIN_ALLOW_THREADS
910 /* This sleep *CAN BE* interrupted. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000911 if ( riscos_sleep(secs) )
Guido van Rossumbceccf52001-04-10 22:07:43 +0000912 return -1;
913 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000914#elif defined(PLAN9)
915 {
916 double millisecs = secs * 1000.0;
917 if (millisecs > (double)LONG_MAX) {
918 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
919 return -1;
920 }
921 /* This sleep *CAN BE* interrupted. */
922 Py_BEGIN_ALLOW_THREADS
923 if(sleep((long)millisecs) < 0){
924 Py_BLOCK_THREADS
925 PyErr_SetFromErrno(PyExc_IOError);
926 return -1;
927 }
928 Py_END_ALLOW_THREADS
929 }
930#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000931 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000932 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000933 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000934 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000935#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000936
Guido van Rossumb6775db1994-08-01 11:34:53 +0000937 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000938}
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000939
940