blob: ce252811ec0b371b83cc9dc18bc914bcbcfdc543 [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 +0000419#undef HAVE_STRPTIME
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000420#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000421
422#if 0
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000423/* Enable this if it's not declared in <time.h> */
424extern char *strptime(const char *, const char *, struct tm *);
Fred Drakeaff60182000-05-09 19:52:40 +0000425#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000426
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000427static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000428time_strptime(PyObject *self, PyObject *args)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000429{
430 struct tm tm;
431 char *fmt = "%a %b %d %H:%M:%S %Y";
432 char *buf;
433 char *s;
434
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000435 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
436 return NULL;
Thomas Wouters334fb892000-07-25 12:56:38 +0000437 memset((void *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000438 s = strptime(buf, fmt, &tm);
439 if (s == NULL) {
440 PyErr_SetString(PyExc_ValueError, "format mismatch");
441 return NULL;
442 }
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000443 while (*s && isspace(Py_CHARMASK(*s)))
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000444 s++;
445 if (*s) {
446 PyErr_Format(PyExc_ValueError,
447 "unconverted data remains: '%.400s'", s);
448 return NULL;
449 }
450 return tmtotuple(&tm);
451}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000452
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000453#endif /* HAVE_STRPTIME */
454
455#ifndef HAVE_STRPTIME
456
457static PyObject *
458time_strptime(PyObject *self, PyObject *args)
459{
460 PyObject *strptime_module = PyImport_ImportModule("_strptime");
Raymond Hettinger502168a2003-04-10 16:03:22 +0000461 PyObject *strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000462
Tim Peters513a1cd2003-01-19 04:54:58 +0000463 if (!strptime_module)
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000464 return NULL;
Raymond Hettinger502168a2003-04-10 16:03:22 +0000465 strptime_result = PyObject_CallMethod(strptime_module, "strptime", "O", args);
466 Py_DECREF(strptime_module);
467 return strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000468}
469
470#endif /* !HAVE_STRPTIME */
471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000472PyDoc_STRVAR(strptime_doc,
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000473"strptime(string, format) -> tuple\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000474\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000475Parse a string to a time tuple according to a format specification.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000476See the library reference manual for formatting codes (same as strftime()).");
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000477
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000478
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000479static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000480time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000481{
Thomas Woutersfe385252001-01-19 23:16:56 +0000482 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000483 struct tm buf;
484 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000485 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000486 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000487 if (tup == NULL) {
488 time_t tt = time(NULL);
489 buf = *localtime(&tt);
490 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000491 return NULL;
492 p = asctime(&buf);
493 if (p[24] == '\n')
494 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000495 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000496}
497
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000498PyDoc_STRVAR(asctime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000499"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000500\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000501Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
502When the time tuple is not present, current time as returned by localtime()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000503is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000504
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000505static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000506time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000507{
508 double dt;
509 time_t tt;
510 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000511
Thomas Woutersfe385252001-01-19 23:16:56 +0000512 if (PyTuple_Size(args) == 0)
513 tt = time(NULL);
514 else {
515 if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
516 return NULL;
517 tt = (time_t)dt;
518 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000519 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000520 if (p == NULL) {
521 PyErr_SetString(PyExc_ValueError, "unconvertible time");
522 return NULL;
523 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000524 if (p[24] == '\n')
525 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000526 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000527}
528
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000529PyDoc_STRVAR(ctime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000530"ctime(seconds) -> string\n\
531\n\
532Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000533This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000534not present, current time as returned by localtime() is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000535
Guido van Rossum60cd8131998-03-06 17:16:21 +0000536#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000537static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000538time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000539{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000540 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000541 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000542 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000543 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000544 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000545 tt = time(&tt);
546 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000547 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000548 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000549 tt = mktime(&buf);
550 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000551 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000552 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000553 return NULL;
554 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000555 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000556}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000557
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000558PyDoc_STRVAR(mktime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000559"mktime(tuple) -> floating point number\n\
560\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000561Convert a time tuple in local time to seconds since the Epoch.");
Guido van Rossum60cd8131998-03-06 17:16:21 +0000562#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000563
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000564#ifdef HAVE_WORKING_TZSET
565void inittimezone(PyObject *module);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000566
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000567static PyObject *
568time_tzset(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569{
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000570 PyObject* m;
Fred Drake9bb74322002-04-01 14:49:59 +0000571
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000572 if (!PyArg_ParseTuple(args, ":tzset"))
573 return NULL;
574
575 m = PyImport_ImportModule("time");
576 if (m == NULL) {
577 return NULL;
578 }
579
580 tzset();
581
582 /* Reset timezone, altzone, daylight and tzname */
583 inittimezone(m);
584 Py_DECREF(m);
585
586 Py_INCREF(Py_None);
587 return Py_None;
588}
589
590PyDoc_STRVAR(tzset_doc,
591"tzset(zone)\n\
592\n\
593Initialize, or reinitialize, the local timezone to the value stored in\n\
594os.environ['TZ']. The TZ environment variable should be specified in\n\
595standard Uniz timezone format as documented in the tzset man page\n\
596(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
597fall back to UTC. If the TZ environment variable is not set, the local\n\
598timezone is set to the systems best guess of wallclock time.\n\
599Changing the TZ environment variable without calling tzset *may* change\n\
600the local timezone used by methods such as localtime, but this behaviour\n\
601should not be relied on.");
602#endif /* HAVE_WORKING_TZSET */
603
604void inittimezone(PyObject *m) {
605 /* This code moved from inittime wholesale to allow calling it from
606 time_tzset. In the future, some parts of it can be moved back
607 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
608 are), and the extranious calls to tzset(3) should be removed.
609 I havn't done this yet, as I don't want to change this code as
610 little as possible when introducing the time.tzset and time.tzsetwall
611 methods. This should simply be a method of doing the following once,
612 at the top of this function and removing the call to tzset() from
613 time_tzset():
614
615 #ifdef HAVE_TZSET
616 tzset()
617 #endif
618
619 And I'm lazy and hate C so nyer.
620 */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000621#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000622 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000623#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000624 PyModule_AddIntConstant(m, "timezone", _timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000625#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000626 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000627#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000628#ifdef HAVE_ALTZONE
Fred Drake9bb74322002-04-01 14:49:59 +0000629 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000630#else
Guido van Rossum26452411998-09-28 22:07:11 +0000631#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000632 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000633#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000634 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000635#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000636#endif
Fred Drake9bb74322002-04-01 14:49:59 +0000637 PyModule_AddIntConstant(m, "daylight", daylight);
638 PyModule_AddObject(m, "tzname",
639 Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000640#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000641#ifdef HAVE_STRUCT_TM_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000642 {
643#define YEAR ((time_t)((365 * 24 + 6) * 3600))
644 time_t t;
645 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000646 long janzone, julyzone;
647 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000648 t = (time((time_t *)0) / YEAR) * YEAR;
649 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000650 janzone = -p->tm_gmtoff;
651 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
652 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000653 t += YEAR/2;
654 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000655 julyzone = -p->tm_gmtoff;
656 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
657 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000658
Guido van Rossum57731601999-03-29 19:12:04 +0000659 if( janzone < julyzone ) {
660 /* DST is reversed in the southern hemisphere */
Fred Drake9bb74322002-04-01 14:49:59 +0000661 PyModule_AddIntConstant(m, "timezone", julyzone);
662 PyModule_AddIntConstant(m, "altzone", janzone);
663 PyModule_AddIntConstant(m, "daylight",
664 janzone != julyzone);
665 PyModule_AddObject(m, "tzname",
666 Py_BuildValue("(zz)",
667 julyname, janname));
Guido van Rossum57731601999-03-29 19:12:04 +0000668 } else {
Fred Drake9bb74322002-04-01 14:49:59 +0000669 PyModule_AddIntConstant(m, "timezone", janzone);
670 PyModule_AddIntConstant(m, "altzone", julyzone);
671 PyModule_AddIntConstant(m, "daylight",
672 janzone != julyzone);
673 PyModule_AddObject(m, "tzname",
674 Py_BuildValue("(zz)",
675 janname, julyname));
Guido van Rossum57731601999-03-29 19:12:04 +0000676 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000677 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000678#else
679#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000680 /* The only thing we can obtain is the current timezone
681 ** (and whether dst is currently _active_, but that is not what
682 ** we're looking for:-( )
683 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000684 initmactimezone();
Fred Drake9bb74322002-04-01 14:49:59 +0000685 PyModule_AddIntConstant(m, "timezone", timezone);
686 PyModule_AddIntConstant(m, "altzone", timezone);
687 PyModule_AddIntConstant(m, "daylight", 0);
688 PyModule_AddObject(m, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000689#endif /* macintosh */
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000690#endif /* HAVE_STRUCT_TM_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000691#ifdef __CYGWIN__
692 tzset();
Fred Drake9bb74322002-04-01 14:49:59 +0000693 PyModule_AddIntConstant(m, "timezone", _timezone);
694 PyModule_AddIntConstant(m, "altzone", _timezone);
695 PyModule_AddIntConstant(m, "daylight", _daylight);
696 PyModule_AddObject(m, "tzname",
697 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +0000698#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000699#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000700}
701
702
703static PyMethodDef time_methods[] = {
704 {"time", time_time, METH_VARARGS, time_doc},
705#ifdef HAVE_CLOCK
706 {"clock", time_clock, METH_VARARGS, clock_doc},
707#endif
708 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
709 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
710 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
711 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
712 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
713#ifdef HAVE_MKTIME
714 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
715#endif
716#ifdef HAVE_STRFTIME
717 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
718#endif
719 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
720#ifdef HAVE_WORKING_TZSET
721 {"tzset", time_tzset, METH_VARARGS, tzset_doc},
722#endif
723 {NULL, NULL} /* sentinel */
724};
725
726
727PyDoc_STRVAR(module_doc,
728"This module provides various functions to manipulate time values.\n\
729\n\
730There are two standard representations of time. One is the number\n\
731of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
732or a floating point number (to represent fractions of seconds).\n\
733The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
734The actual value can be retrieved by calling gmtime(0).\n\
735\n\
736The other representation is a tuple of 9 integers giving local time.\n\
737The tuple items are:\n\
738 year (four digits, e.g. 1998)\n\
739 month (1-12)\n\
740 day (1-31)\n\
741 hours (0-23)\n\
742 minutes (0-59)\n\
743 seconds (0-59)\n\
744 weekday (0-6, Monday is 0)\n\
745 Julian day (day in the year, 1-366)\n\
746 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
747If the DST flag is 0, the time is given in the regular time zone;\n\
748if it is 1, the time is given in the DST time zone;\n\
749if it is -1, mktime() should guess based on the date and time.\n\
750\n\
751Variables:\n\
752\n\
753timezone -- difference in seconds between UTC and local standard time\n\
754altzone -- difference in seconds between UTC and local DST time\n\
755daylight -- whether local time should reflect DST\n\
756tzname -- tuple of (standard time zone name, DST time zone name)\n\
757\n\
758Functions:\n\
759\n\
760time() -- return current time in seconds since the Epoch as a float\n\
761clock() -- return CPU time since process start as a float\n\
762sleep() -- delay for a number of seconds given as a float\n\
763gmtime() -- convert seconds since Epoch to UTC tuple\n\
764localtime() -- convert seconds since Epoch to local time tuple\n\
765asctime() -- convert time tuple to string\n\
766ctime() -- convert time in seconds to string\n\
767mktime() -- convert local time tuple to seconds since Epoch\n\
768strftime() -- convert time tuple to string according to format specification\n\
769strptime() -- parse string to time tuple according to format specification\n\
770tzset() -- change the local timezone");
771
772
773PyMODINIT_FUNC
774inittime(void)
775{
776 PyObject *m;
777 char *p;
778 m = Py_InitModule3("time", time_methods, module_doc);
779
780 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
781 p = Py_GETENV("PYTHONY2K");
782 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
783 /* Squirrel away the module's dictionary for the y2k check */
784 moddict = PyModule_GetDict(m);
785 Py_INCREF(moddict);
786
787 /* Set, or reset, module variables like time.timezone */
788 inittimezone(m);
789
Mark Hammond975e3922002-07-16 01:29:19 +0000790#ifdef MS_WINDOWS
791 /* Helper to allow interrupts for Windows.
792 If Ctrl+C event delivered while not sleeping
793 it will be ignored.
794 */
795 main_thread = PyThread_get_thread_ident();
796 hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
797 SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
798#endif /* MS_WINDOWS */
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000799 PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
Fred Drake9bb74322002-04-01 14:49:59 +0000800 Py_INCREF(&StructTimeType);
801 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000802}
803
804
Guido van Rossumb6775db1994-08-01 11:34:53 +0000805/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000806
Guido van Rossumb6775db1994-08-01 11:34:53 +0000807static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000808floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000809{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000810 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000811 (1) gettimeofday() -- resolution in microseconds
812 (2) ftime() -- resolution in milliseconds
813 (3) time() -- resolution in seconds
814 In all cases the return value is a float in seconds.
815 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
816 fail, so we fall back on ftime() or time().
817 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000818#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000819 {
820 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000821#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000822 if (gettimeofday(&t) == 0)
823 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000824#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000825 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
826 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000827#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000828 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000829#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000830 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000831#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000832 struct timeb t;
833 ftime(&t);
834 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000835#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000836 time_t secs;
837 time(&secs);
838 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000839#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000840 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000841}
842
Guido van Rossumb6775db1994-08-01 11:34:53 +0000843
844/* Implement floatsleep() for various platforms.
845 When interrupted (or when another error occurs), return -1 and
846 set an exception; else return 0. */
847
848static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000849floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000850{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000851/* XXX Should test for MS_WINDOWS first! */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000852#if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000853 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000854 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000855 frac = fmod(secs, 1.0);
856 secs = floor(secs);
857 t.tv_sec = (long)secs;
858 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000859 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000860 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000861#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000862 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000863#else
864 if (1) {
865#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000866 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000867 PyErr_SetFromErrno(PyExc_IOError);
868 return -1;
869 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000870 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000871 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000872#elif defined(macintosh)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000873#define MacTicks (* (long *)0x16A)
874 long deadline;
875 deadline = MacTicks + (long)(secs * 60.0);
876 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000877 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000878 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000879 return -1;
880 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000881#elif defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000882 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000883 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000884 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000885 Py_END_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000886#elif defined(MS_WINDOWS)
Fred Drake0e123952000-06-29 21:31:02 +0000887 {
888 double millisecs = secs * 1000.0;
Tim Peters513a1cd2003-01-19 04:54:58 +0000889 unsigned long ul_millis;
890
Fred Drake0e123952000-06-29 21:31:02 +0000891 if (millisecs > (double)ULONG_MAX) {
Tim Peters513a1cd2003-01-19 04:54:58 +0000892 PyErr_SetString(PyExc_OverflowError,
893 "sleep length is too large");
Fred Drake0e123952000-06-29 21:31:02 +0000894 return -1;
895 }
Fred Drake0e123952000-06-29 21:31:02 +0000896 Py_BEGIN_ALLOW_THREADS
Tim Peters513a1cd2003-01-19 04:54:58 +0000897 /* Allow sleep(0) to maintain win32 semantics, and as decreed
898 * by Guido, only the main thread can be interrupted.
899 */
900 ul_millis = (unsigned long)millisecs;
901 if (ul_millis == 0 ||
902 main_thread != PyThread_get_thread_ident())
903 Sleep(ul_millis);
Mark Hammond975e3922002-07-16 01:29:19 +0000904 else {
905 DWORD rc;
906 ResetEvent(hInterruptEvent);
Tim Peters513a1cd2003-01-19 04:54:58 +0000907 rc = WaitForSingleObject(hInterruptEvent, ul_millis);
908 if (rc == WAIT_OBJECT_0) {
909 /* Yield to make sure real Python signal
910 * handler called.
911 */
Mark Hammond975e3922002-07-16 01:29:19 +0000912 Sleep(1);
913 Py_BLOCK_THREADS
Mark Hammond975e3922002-07-16 01:29:19 +0000914 errno = EINTR;
915 PyErr_SetFromErrno(PyExc_IOError);
916 return -1;
917 }
918 }
Fred Drake0e123952000-06-29 21:31:02 +0000919 Py_END_ALLOW_THREADS
920 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000921#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000922 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000923 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000924 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000925 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000926 PyErr_SetFromErrno(PyExc_IOError);
927 return -1;
928 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000929 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000930#elif defined(__BEOS__)
Guido van Rossumbcc20741998-08-04 22:53:56 +0000931 /* This sleep *CAN BE* interrupted. */
932 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000933 if( secs <= 0.0 ) {
934 return;
935 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000936
Guido van Rossumbcc20741998-08-04 22:53:56 +0000937 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000938 /* BeOS snooze() is in microseconds... */
939 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000940 Py_BLOCK_THREADS
941 PyErr_SetFromErrno( PyExc_IOError );
942 return -1;
943 }
944 Py_END_ALLOW_THREADS
945 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000946#elif defined(RISCOS)
Guido van Rossumbceccf52001-04-10 22:07:43 +0000947 if (secs <= 0.0)
948 return 0;
949 Py_BEGIN_ALLOW_THREADS
950 /* This sleep *CAN BE* interrupted. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000951 if ( riscos_sleep(secs) )
Guido van Rossumbceccf52001-04-10 22:07:43 +0000952 return -1;
953 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000954#elif defined(PLAN9)
955 {
956 double millisecs = secs * 1000.0;
957 if (millisecs > (double)LONG_MAX) {
958 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
959 return -1;
960 }
961 /* This sleep *CAN BE* interrupted. */
962 Py_BEGIN_ALLOW_THREADS
963 if(sleep((long)millisecs) < 0){
964 Py_BLOCK_THREADS
965 PyErr_SetFromErrno(PyExc_IOError);
966 return -1;
967 }
968 Py_END_ALLOW_THREADS
969 }
970#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000971 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000972 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000973 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000974 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000975#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000976
Guido van Rossumb6775db1994-08-01 11:34:53 +0000977 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000978}
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000979
980