blob: fc81ca4069a394cb03f7205050beb452017ecb3a [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
Guido van Rossum234f9421993-06-17 12:35:49 +000081/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000082static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000083static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000085/* For Y2K check */
86static PyObject *moddict;
87
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000088#ifdef macintosh
89/* Our own timezone. We have enough information to deduce whether
90** DST is on currently, but unfortunately we cannot put it to good
91** use because we don't know the rules (and that is needed to have
92** localtime() return correct tm_isdst values for times other than
93** the current time. So, we cop out and only tell the user the current
94** timezone.
95*/
96static long timezone;
97
Guido van Rossum10b164a2001-09-25 13:59:01 +000098static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000099initmactimezone(void)
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000100{
101 MachineLocation loc;
102 long delta;
103
104 ReadLocation(&loc);
Guido van Rossum10b164a2001-09-25 13:59:01 +0000105
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000106 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
107 return;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000108
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000109 delta = loc.u.gmtDelta & 0x00FFFFFF;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000110
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000111 if (delta & 0x00800000)
112 delta |= 0xFF000000;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000113
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000114 timezone = -delta;
115}
116#endif /* macintosh */
117
118
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000119static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000120time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000122 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000123 if (!PyArg_ParseTuple(args, ":time"))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000124 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000125 secs = floattime();
126 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000127 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000128 return NULL;
129 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000130 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000131}
132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000133PyDoc_STRVAR(time_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000134"time() -> floating point number\n\
135\n\
136Return the current time in seconds since the Epoch.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000137Fractions of a second may be present if the system clock provides them.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000138
Guido van Rossumb6775db1994-08-01 11:34:53 +0000139#ifdef HAVE_CLOCK
140
141#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000142#ifdef CLK_TCK
143#define CLOCKS_PER_SEC CLK_TCK
144#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145#define CLOCKS_PER_SEC 1000000
146#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000147#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000148
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000149static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000150time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000151{
Thomas Woutersfe385252001-01-19 23:16:56 +0000152 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000154 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000156#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000158#if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Mark Hammond7ba5e812002-02-12 04:02:33 +0000159/* Due to Mark Hammond and Tim Peters */
Guido van Rossum3917c221997-04-02 05:35:28 +0000160static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000161time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000162{
Tim Peters9ad4b682002-02-13 05:14:18 +0000163 static LARGE_INTEGER ctrStart;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000164 static double divisor = 0.0;
Tim Peters9ad4b682002-02-13 05:14:18 +0000165 LARGE_INTEGER now;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000166 double diff;
Guido van Rossum3917c221997-04-02 05:35:28 +0000167
Thomas Woutersfe385252001-01-19 23:16:56 +0000168 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum3917c221997-04-02 05:35:28 +0000169 return NULL;
170
Mark Hammond7ba5e812002-02-12 04:02:33 +0000171 if (divisor == 0.0) {
Tim Peters9ad4b682002-02-13 05:14:18 +0000172 LARGE_INTEGER freq;
173 QueryPerformanceCounter(&ctrStart);
174 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
Mark Hammond7ba5e812002-02-12 04:02:33 +0000175 /* Unlikely to happen - this works on all intel
176 machines at least! Revert to clock() */
Guido van Rossum3917c221997-04-02 05:35:28 +0000177 return PyFloat_FromDouble(clock());
178 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000179 divisor = (double)freq.QuadPart;
Guido van Rossum3917c221997-04-02 05:35:28 +0000180 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000181 QueryPerformanceCounter(&now);
182 diff = (double)(now.QuadPart - ctrStart.QuadPart);
Mark Hammond7ba5e812002-02-12 04:02:33 +0000183 return PyFloat_FromDouble(diff / divisor);
Guido van Rossum3917c221997-04-02 05:35:28 +0000184}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000185
Guido van Rossum3917c221997-04-02 05:35:28 +0000186#define HAVE_CLOCK /* So it gets included in the methods */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000187#endif /* MS_WINDOWS && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000188
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000189#ifdef HAVE_CLOCK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000190PyDoc_STRVAR(clock_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000191"clock() -> floating point number\n\
192\n\
193Return the CPU time or real time since the start of the process or since\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000194the first call to clock(). This has as much precision as the system\n\
195records.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000196#endif
197
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000198static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000199time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000200{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000201 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000202 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000204 if (floatsleep(secs) != 0)
205 return NULL;
206 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000207 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208}
209
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000210PyDoc_STRVAR(sleep_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000211"sleep(seconds)\n\
212\n\
213Delay execution for a given number of seconds. The argument may be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000214a floating point number for subsecond precision.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000215
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000216static PyStructSequence_Field struct_time_type_fields[] = {
217 {"tm_year", NULL},
218 {"tm_mon", NULL},
219 {"tm_mday", NULL},
220 {"tm_hour", NULL},
221 {"tm_min", NULL},
222 {"tm_sec", NULL},
223 {"tm_wday", NULL},
224 {"tm_yday", NULL},
225 {"tm_isdst", NULL},
226 {0}
227};
228
229static PyStructSequence_Desc struct_time_type_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000230 "time.struct_time",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000231 NULL,
232 struct_time_type_fields,
233 9,
234};
Tim Peters9ad4b682002-02-13 05:14:18 +0000235
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000236static PyTypeObject StructTimeType;
237
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000238static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000239tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000240{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000241 PyObject *v = PyStructSequence_New(&StructTimeType);
242 if (v == NULL)
243 return NULL;
Tim Peters9ad4b682002-02-13 05:14:18 +0000244
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000245#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
246
247 SET(0, p->tm_year + 1900);
248 SET(1, p->tm_mon + 1); /* Want January == 1 */
249 SET(2, p->tm_mday);
250 SET(3, p->tm_hour);
251 SET(4, p->tm_min);
252 SET(5, p->tm_sec);
253 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
254 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
255 SET(8, p->tm_isdst);
256#undef SET
257 if (PyErr_Occurred()) {
258 Py_XDECREF(v);
259 return NULL;
260 }
261
262 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000263}
264
265static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000266time_convert(time_t when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000267{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000268 struct tm *p;
269 errno = 0;
270 p = function(&when);
271 if (p == NULL) {
272#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000273 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000274 errno = EINVAL;
275#endif
Tim Peters8b19a932003-01-17 20:08:54 +0000276 return PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000277 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000278 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000279}
280
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000281static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000282time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000283{
284 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000285 if (PyTuple_Size(args) == 0)
286 when = floattime();
287 if (!PyArg_ParseTuple(args, "|d:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000288 return NULL;
289 return time_convert((time_t)when, gmtime);
290}
291
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000292PyDoc_STRVAR(gmtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000293"gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,\n\
294 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000295\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000296Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000297GMT). When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000298
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000299static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000300time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000301{
302 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000303 if (PyTuple_Size(args) == 0)
304 when = floattime();
305 if (!PyArg_ParseTuple(args, "|d:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000306 return NULL;
307 return time_convert((time_t)when, localtime);
308}
309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000310PyDoc_STRVAR(localtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000311"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 +0000312\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000313Convert seconds since the Epoch to a time tuple expressing local time.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000314When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000315
Guido van Rossum9e90a671993-06-24 11:10:19 +0000316static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000317gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000318{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000319 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000320 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000321
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000322 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000323 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000324 &p->tm_mon,
325 &p->tm_mday,
326 &p->tm_hour,
327 &p->tm_min,
328 &p->tm_sec,
329 &p->tm_wday,
330 &p->tm_yday,
331 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000332 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000333 if (y < 1900) {
334 PyObject *accept = PyDict_GetItemString(moddict,
335 "accept2dyear");
336 if (accept == NULL || !PyInt_Check(accept) ||
337 PyInt_AsLong(accept) == 0) {
338 PyErr_SetString(PyExc_ValueError,
339 "year >= 1900 required");
340 return 0;
341 }
342 if (69 <= y && y <= 99)
343 y += 1900;
344 else if (0 <= y && y <= 68)
345 y += 2000;
346 else {
347 PyErr_SetString(PyExc_ValueError,
Skip Montanaro1a10aac2001-08-22 12:39:16 +0000348 "year out of range");
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000349 return 0;
350 }
351 }
352 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000353 p->tm_mon--;
354 p->tm_wday = (p->tm_wday + 1) % 7;
355 p->tm_yday--;
356 return 1;
357}
358
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000359#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000360static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000361time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000362{
Thomas Woutersfe385252001-01-19 23:16:56 +0000363 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000364 struct tm buf;
365 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000366 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000367 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000368 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000369
Thomas Wouters334fb892000-07-25 12:56:38 +0000370 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000371
Thomas Woutersfe385252001-01-19 23:16:56 +0000372 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000373 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000374
375 if (tup == NULL) {
376 time_t tt = time(NULL);
377 buf = *localtime(&tt);
378 } else if (!gettmarg(tup, &buf))
379 return NULL;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000380
Guido van Rossumc222ec21999-02-23 00:00:10 +0000381 fmtlen = strlen(fmt);
382
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000383 /* I hate these functions that presume you know how big the output
384 * will be ahead of time...
385 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000386 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000387 outbuf = malloc(i);
388 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000389 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000390 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000391 buflen = strftime(outbuf, i, fmt, &buf);
392 if (buflen > 0 || i >= 256 * fmtlen) {
393 /* If the buffer is 256 times as long as the format,
394 it's probably not failing for lack of room!
395 More likely, the format yields an empty result,
396 e.g. an empty format, or %Z when the timezone
397 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000398 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000399 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000400 free(outbuf);
401 return ret;
402 }
403 free(outbuf);
404 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000405}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000406
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000407PyDoc_STRVAR(strftime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000408"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000409\n\
410Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000411See the library reference manual for formatting codes. When the time tuple\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000412is not present, current time as returned by localtime() is used.");
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000413#endif /* HAVE_STRFTIME */
414
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000415#undef HAVE_STRPTIME
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000416#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000417
418#if 0
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000419/* Enable this if it's not declared in <time.h> */
420extern char *strptime(const char *, const char *, struct tm *);
Fred Drakeaff60182000-05-09 19:52:40 +0000421#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000422
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000423static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000424time_strptime(PyObject *self, PyObject *args)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000425{
426 struct tm tm;
427 char *fmt = "%a %b %d %H:%M:%S %Y";
428 char *buf;
429 char *s;
430
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000431 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
432 return NULL;
Thomas Wouters334fb892000-07-25 12:56:38 +0000433 memset((void *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000434 s = strptime(buf, fmt, &tm);
435 if (s == NULL) {
436 PyErr_SetString(PyExc_ValueError, "format mismatch");
437 return NULL;
438 }
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000439 while (*s && isspace(Py_CHARMASK(*s)))
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000440 s++;
441 if (*s) {
442 PyErr_Format(PyExc_ValueError,
443 "unconverted data remains: '%.400s'", s);
444 return NULL;
445 }
446 return tmtotuple(&tm);
447}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000448
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000449#endif /* HAVE_STRPTIME */
450
451#ifndef HAVE_STRPTIME
452
453static PyObject *
454time_strptime(PyObject *self, PyObject *args)
455{
456 PyObject *strptime_module = PyImport_ImportModule("_strptime");
457
Tim Peters513a1cd2003-01-19 04:54:58 +0000458 if (!strptime_module)
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000459 return NULL;
460 return PyObject_CallMethod(strptime_module, "strptime", "O", args);
461}
462
463#endif /* !HAVE_STRPTIME */
464
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000465PyDoc_STRVAR(strptime_doc,
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000466"strptime(string, format) -> tuple\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000467\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000468Parse a string to a time tuple according to a format specification.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000469See the library reference manual for formatting codes (same as strftime()).");
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000470
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000471
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000472static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000473time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000474{
Thomas Woutersfe385252001-01-19 23:16:56 +0000475 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000476 struct tm buf;
477 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000478 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000479 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000480 if (tup == NULL) {
481 time_t tt = time(NULL);
482 buf = *localtime(&tt);
483 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000484 return NULL;
485 p = asctime(&buf);
486 if (p[24] == '\n')
487 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000488 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000489}
490
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000491PyDoc_STRVAR(asctime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000492"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000493\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000494Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
495When the time tuple is not present, current time as returned by localtime()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000496is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000497
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000498static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000499time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000500{
501 double dt;
502 time_t tt;
503 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000504
Thomas Woutersfe385252001-01-19 23:16:56 +0000505 if (PyTuple_Size(args) == 0)
506 tt = time(NULL);
507 else {
508 if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
509 return NULL;
510 tt = (time_t)dt;
511 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000512 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000513 if (p == NULL) {
514 PyErr_SetString(PyExc_ValueError, "unconvertible time");
515 return NULL;
516 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000517 if (p[24] == '\n')
518 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000519 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000520}
521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000522PyDoc_STRVAR(ctime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000523"ctime(seconds) -> string\n\
524\n\
525Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000526This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000527not present, current time as returned by localtime() is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000528
Guido van Rossum60cd8131998-03-06 17:16:21 +0000529#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000530static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000531time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000532{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000533 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000534 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000535 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000536 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000537 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000538 tt = time(&tt);
539 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000540 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000541 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000542 tt = mktime(&buf);
543 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000544 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000545 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000546 return NULL;
547 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000548 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000549}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000550
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000551PyDoc_STRVAR(mktime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000552"mktime(tuple) -> floating point number\n\
553\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000554Convert a time tuple in local time to seconds since the Epoch.");
Guido van Rossum60cd8131998-03-06 17:16:21 +0000555#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000556
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000557#ifdef HAVE_WORKING_TZSET
558void inittimezone(PyObject *module);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000559
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000560static PyObject *
561time_tzset(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000562{
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000563 PyObject* m;
Fred Drake9bb74322002-04-01 14:49:59 +0000564
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000565 if (!PyArg_ParseTuple(args, ":tzset"))
566 return NULL;
567
568 m = PyImport_ImportModule("time");
569 if (m == NULL) {
570 return NULL;
571 }
572
573 tzset();
574
575 /* Reset timezone, altzone, daylight and tzname */
576 inittimezone(m);
577 Py_DECREF(m);
578
579 Py_INCREF(Py_None);
580 return Py_None;
581}
582
583PyDoc_STRVAR(tzset_doc,
584"tzset(zone)\n\
585\n\
586Initialize, or reinitialize, the local timezone to the value stored in\n\
587os.environ['TZ']. The TZ environment variable should be specified in\n\
588standard Uniz timezone format as documented in the tzset man page\n\
589(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
590fall back to UTC. If the TZ environment variable is not set, the local\n\
591timezone is set to the systems best guess of wallclock time.\n\
592Changing the TZ environment variable without calling tzset *may* change\n\
593the local timezone used by methods such as localtime, but this behaviour\n\
594should not be relied on.");
595#endif /* HAVE_WORKING_TZSET */
596
597void inittimezone(PyObject *m) {
598 /* This code moved from inittime wholesale to allow calling it from
599 time_tzset. In the future, some parts of it can be moved back
600 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
601 are), and the extranious calls to tzset(3) should be removed.
602 I havn't done this yet, as I don't want to change this code as
603 little as possible when introducing the time.tzset and time.tzsetwall
604 methods. This should simply be a method of doing the following once,
605 at the top of this function and removing the call to tzset() from
606 time_tzset():
607
608 #ifdef HAVE_TZSET
609 tzset()
610 #endif
611
612 And I'm lazy and hate C so nyer.
613 */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000614#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000615 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000616#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000617 PyModule_AddIntConstant(m, "timezone", _timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000618#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000619 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000620#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000621#ifdef HAVE_ALTZONE
Fred Drake9bb74322002-04-01 14:49:59 +0000622 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000623#else
Guido van Rossum26452411998-09-28 22:07:11 +0000624#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000625 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000626#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000627 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000628#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000629#endif
Fred Drake9bb74322002-04-01 14:49:59 +0000630 PyModule_AddIntConstant(m, "daylight", daylight);
631 PyModule_AddObject(m, "tzname",
632 Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000633#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000634#ifdef HAVE_STRUCT_TM_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000635 {
636#define YEAR ((time_t)((365 * 24 + 6) * 3600))
637 time_t t;
638 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000639 long janzone, julyzone;
640 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000641 t = (time((time_t *)0) / YEAR) * YEAR;
642 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000643 janzone = -p->tm_gmtoff;
644 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
645 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000646 t += YEAR/2;
647 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000648 julyzone = -p->tm_gmtoff;
649 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
650 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000651
Guido van Rossum57731601999-03-29 19:12:04 +0000652 if( janzone < julyzone ) {
653 /* DST is reversed in the southern hemisphere */
Fred Drake9bb74322002-04-01 14:49:59 +0000654 PyModule_AddIntConstant(m, "timezone", julyzone);
655 PyModule_AddIntConstant(m, "altzone", janzone);
656 PyModule_AddIntConstant(m, "daylight",
657 janzone != julyzone);
658 PyModule_AddObject(m, "tzname",
659 Py_BuildValue("(zz)",
660 julyname, janname));
Guido van Rossum57731601999-03-29 19:12:04 +0000661 } else {
Fred Drake9bb74322002-04-01 14:49:59 +0000662 PyModule_AddIntConstant(m, "timezone", janzone);
663 PyModule_AddIntConstant(m, "altzone", julyzone);
664 PyModule_AddIntConstant(m, "daylight",
665 janzone != julyzone);
666 PyModule_AddObject(m, "tzname",
667 Py_BuildValue("(zz)",
668 janname, julyname));
Guido van Rossum57731601999-03-29 19:12:04 +0000669 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000670 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000671#else
672#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000673 /* The only thing we can obtain is the current timezone
674 ** (and whether dst is currently _active_, but that is not what
675 ** we're looking for:-( )
676 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000677 initmactimezone();
Fred Drake9bb74322002-04-01 14:49:59 +0000678 PyModule_AddIntConstant(m, "timezone", timezone);
679 PyModule_AddIntConstant(m, "altzone", timezone);
680 PyModule_AddIntConstant(m, "daylight", 0);
681 PyModule_AddObject(m, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000682#endif /* macintosh */
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000683#endif /* HAVE_STRUCT_TM_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000684#ifdef __CYGWIN__
685 tzset();
Fred Drake9bb74322002-04-01 14:49:59 +0000686 PyModule_AddIntConstant(m, "timezone", _timezone);
687 PyModule_AddIntConstant(m, "altzone", _timezone);
688 PyModule_AddIntConstant(m, "daylight", _daylight);
689 PyModule_AddObject(m, "tzname",
690 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +0000691#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000692#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000693}
694
695
696static PyMethodDef time_methods[] = {
697 {"time", time_time, METH_VARARGS, time_doc},
698#ifdef HAVE_CLOCK
699 {"clock", time_clock, METH_VARARGS, clock_doc},
700#endif
701 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
702 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
703 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
704 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
705 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
706#ifdef HAVE_MKTIME
707 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
708#endif
709#ifdef HAVE_STRFTIME
710 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
711#endif
712 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
713#ifdef HAVE_WORKING_TZSET
714 {"tzset", time_tzset, METH_VARARGS, tzset_doc},
715#endif
716 {NULL, NULL} /* sentinel */
717};
718
719
720PyDoc_STRVAR(module_doc,
721"This module provides various functions to manipulate time values.\n\
722\n\
723There are two standard representations of time. One is the number\n\
724of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
725or a floating point number (to represent fractions of seconds).\n\
726The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
727The actual value can be retrieved by calling gmtime(0).\n\
728\n\
729The other representation is a tuple of 9 integers giving local time.\n\
730The tuple items are:\n\
731 year (four digits, e.g. 1998)\n\
732 month (1-12)\n\
733 day (1-31)\n\
734 hours (0-23)\n\
735 minutes (0-59)\n\
736 seconds (0-59)\n\
737 weekday (0-6, Monday is 0)\n\
738 Julian day (day in the year, 1-366)\n\
739 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
740If the DST flag is 0, the time is given in the regular time zone;\n\
741if it is 1, the time is given in the DST time zone;\n\
742if it is -1, mktime() should guess based on the date and time.\n\
743\n\
744Variables:\n\
745\n\
746timezone -- difference in seconds between UTC and local standard time\n\
747altzone -- difference in seconds between UTC and local DST time\n\
748daylight -- whether local time should reflect DST\n\
749tzname -- tuple of (standard time zone name, DST time zone name)\n\
750\n\
751Functions:\n\
752\n\
753time() -- return current time in seconds since the Epoch as a float\n\
754clock() -- return CPU time since process start as a float\n\
755sleep() -- delay for a number of seconds given as a float\n\
756gmtime() -- convert seconds since Epoch to UTC tuple\n\
757localtime() -- convert seconds since Epoch to local time tuple\n\
758asctime() -- convert time tuple to string\n\
759ctime() -- convert time in seconds to string\n\
760mktime() -- convert local time tuple to seconds since Epoch\n\
761strftime() -- convert time tuple to string according to format specification\n\
762strptime() -- parse string to time tuple according to format specification\n\
763tzset() -- change the local timezone");
764
765
766PyMODINIT_FUNC
767inittime(void)
768{
769 PyObject *m;
770 char *p;
771 m = Py_InitModule3("time", time_methods, module_doc);
772
773 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
774 p = Py_GETENV("PYTHONY2K");
775 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
776 /* Squirrel away the module's dictionary for the y2k check */
777 moddict = PyModule_GetDict(m);
778 Py_INCREF(moddict);
779
780 /* Set, or reset, module variables like time.timezone */
781 inittimezone(m);
782
Mark Hammond975e3922002-07-16 01:29:19 +0000783#ifdef MS_WINDOWS
784 /* Helper to allow interrupts for Windows.
785 If Ctrl+C event delivered while not sleeping
786 it will be ignored.
787 */
788 main_thread = PyThread_get_thread_ident();
789 hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
790 SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
791#endif /* MS_WINDOWS */
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000792 PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
Fred Drake9bb74322002-04-01 14:49:59 +0000793 Py_INCREF(&StructTimeType);
794 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000795}
796
797
Guido van Rossumb6775db1994-08-01 11:34:53 +0000798/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000799
Guido van Rossumb6775db1994-08-01 11:34:53 +0000800static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000801floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000802{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000803 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000804 (1) gettimeofday() -- resolution in microseconds
805 (2) ftime() -- resolution in milliseconds
806 (3) time() -- resolution in seconds
807 In all cases the return value is a float in seconds.
808 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
809 fail, so we fall back on ftime() or time().
810 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000811#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000812 {
813 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000814#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000815 if (gettimeofday(&t) == 0)
816 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000817#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000818 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
819 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000820#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000821 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000822#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000823 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000824#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000825 struct timeb t;
826 ftime(&t);
827 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000828#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000829 time_t secs;
830 time(&secs);
831 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000832#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000833 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000834}
835
Guido van Rossumb6775db1994-08-01 11:34:53 +0000836
837/* Implement floatsleep() for various platforms.
838 When interrupted (or when another error occurs), return -1 and
839 set an exception; else return 0. */
840
841static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000842floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000843{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000844/* XXX Should test for MS_WINDOWS first! */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000845#if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000846 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000847 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000848 frac = fmod(secs, 1.0);
849 secs = floor(secs);
850 t.tv_sec = (long)secs;
851 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000852 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000853 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000854#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000855 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000856#else
857 if (1) {
858#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000859 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000860 PyErr_SetFromErrno(PyExc_IOError);
861 return -1;
862 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000863 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000864 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000865#elif defined(macintosh)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000866#define MacTicks (* (long *)0x16A)
867 long deadline;
868 deadline = MacTicks + (long)(secs * 60.0);
869 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000870 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000871 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000872 return -1;
873 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000874#elif defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000875 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000876 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000877 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000878 Py_END_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000879#elif defined(MS_WINDOWS)
Fred Drake0e123952000-06-29 21:31:02 +0000880 {
881 double millisecs = secs * 1000.0;
Tim Peters513a1cd2003-01-19 04:54:58 +0000882 unsigned long ul_millis;
883
Fred Drake0e123952000-06-29 21:31:02 +0000884 if (millisecs > (double)ULONG_MAX) {
Tim Peters513a1cd2003-01-19 04:54:58 +0000885 PyErr_SetString(PyExc_OverflowError,
886 "sleep length is too large");
Fred Drake0e123952000-06-29 21:31:02 +0000887 return -1;
888 }
Fred Drake0e123952000-06-29 21:31:02 +0000889 Py_BEGIN_ALLOW_THREADS
Tim Peters513a1cd2003-01-19 04:54:58 +0000890 /* Allow sleep(0) to maintain win32 semantics, and as decreed
891 * by Guido, only the main thread can be interrupted.
892 */
893 ul_millis = (unsigned long)millisecs;
894 if (ul_millis == 0 ||
895 main_thread != PyThread_get_thread_ident())
896 Sleep(ul_millis);
Mark Hammond975e3922002-07-16 01:29:19 +0000897 else {
898 DWORD rc;
899 ResetEvent(hInterruptEvent);
Tim Peters513a1cd2003-01-19 04:54:58 +0000900 rc = WaitForSingleObject(hInterruptEvent, ul_millis);
901 if (rc == WAIT_OBJECT_0) {
902 /* Yield to make sure real Python signal
903 * handler called.
904 */
Mark Hammond975e3922002-07-16 01:29:19 +0000905 Sleep(1);
906 Py_BLOCK_THREADS
Mark Hammond975e3922002-07-16 01:29:19 +0000907 errno = EINTR;
908 PyErr_SetFromErrno(PyExc_IOError);
909 return -1;
910 }
911 }
Fred Drake0e123952000-06-29 21:31:02 +0000912 Py_END_ALLOW_THREADS
913 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000914#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000915 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000916 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000917 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000918 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000919 PyErr_SetFromErrno(PyExc_IOError);
920 return -1;
921 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000922 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000923#elif defined(__BEOS__)
Guido van Rossumbcc20741998-08-04 22:53:56 +0000924 /* This sleep *CAN BE* interrupted. */
925 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000926 if( secs <= 0.0 ) {
927 return;
928 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000929
Guido van Rossumbcc20741998-08-04 22:53:56 +0000930 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000931 /* BeOS snooze() is in microseconds... */
932 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000933 Py_BLOCK_THREADS
934 PyErr_SetFromErrno( PyExc_IOError );
935 return -1;
936 }
937 Py_END_ALLOW_THREADS
938 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000939#elif defined(RISCOS)
Guido van Rossumbceccf52001-04-10 22:07:43 +0000940 if (secs <= 0.0)
941 return 0;
942 Py_BEGIN_ALLOW_THREADS
943 /* This sleep *CAN BE* interrupted. */
944 if ( sleep(secs) )
945 return -1;
946 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000947#elif defined(PLAN9)
948 {
949 double millisecs = secs * 1000.0;
950 if (millisecs > (double)LONG_MAX) {
951 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
952 return -1;
953 }
954 /* This sleep *CAN BE* interrupted. */
955 Py_BEGIN_ALLOW_THREADS
956 if(sleep((long)millisecs) < 0){
957 Py_BLOCK_THREADS
958 PyErr_SetFromErrno(PyExc_IOError);
959 return -1;
960 }
961 Py_END_ALLOW_THREADS
962 }
963#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000964 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000965 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000966 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000967 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000968#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000969
Guido van Rossumb6775db1994-08-01 11:34:53 +0000970 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000971}
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000972
973