blob: f8557968f55feb6e4f361cb3671af8a90729aa92 [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"
Tim Peters1b6f7a92004-06-20 02:50:16 +00006#include "timefuncs.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00007
Guido van Rossum87ce7bb1998-06-09 16:30:31 +00008#include <ctype.h>
9
Guido van Rossumb6775db1994-08-01 11:34:53 +000010#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000011
Guido van Rossumb6775db1994-08-01 11:34:53 +000012#ifdef QUICKWIN
13#include <io.h>
14#endif
15
Guido van Rossumb6775db1994-08-01 11:34:53 +000016#ifdef HAVE_FTIME
17#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000018#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000019extern int ftime(struct timeb *);
Guido van Rossum52174571996-12-09 18:38:52 +000020#endif /* MS_WINDOWS */
21#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022
Guido van Rossum7bf22de1997-12-02 20:34:19 +000023#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000024#include <i86.h>
25#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000026#ifdef MS_WINDOWS
Mark Hammond975e3922002-07-16 01:29:19 +000027#define WIN32_LEAN_AND_MEAN
Guido van Rossum258ccd42001-03-02 06:53:29 +000028#include <windows.h>
Mark Hammond975e3922002-07-16 01:29:19 +000029#include "pythread.h"
30
31/* helper to allow us to interrupt sleep() on Windows*/
32static HANDLE hInterruptEvent = NULL;
33static BOOL WINAPI PyCtrlHandler(DWORD dwCtrlType)
34{
35 SetEvent(hInterruptEvent);
36 /* allow other default handlers to be called.
37 Default Python handler will setup the
38 KeyboardInterrupt exception.
39 */
40 return FALSE;
41}
42static long main_thread;
43
44
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000045#if defined(__BORLANDC__)
Guido van Rossumb2fb3641996-09-07 00:47:35 +000046/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000047#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000048#define tzname _tzname
49#define daylight _daylight
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000050#endif /* __BORLANDC__ */
Guido van Rossumcac6c721996-09-06 13:34:02 +000051#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000052#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000053
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000054#if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Fred Drakedfb4ebd2000-06-29 20:56:28 +000055/* Win32 has better clock replacement
56 XXX Win64 does not yet, but might when the platform matures. */
Guido van Rossum3917c221997-04-02 05:35:28 +000057#undef HAVE_CLOCK /* We have our own version down below */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000058#endif /* MS_WINDOWS && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +000059
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000060#if defined(PYOS_OS2)
61#define INCL_DOS
62#define INCL_ERRORS
63#include <os2.h>
64#endif
65
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000066#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000067#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000068#endif
69
Guido van Rossumbcc20741998-08-04 22:53:56 +000070#ifdef __BEOS__
Fred Drake56221a72000-08-15 18:52:33 +000071#include <time.h>
Guido van Rossumbcc20741998-08-04 22:53:56 +000072/* For bigtime_t, snooze(). - [cjh] */
73#include <support/SupportDefs.h>
74#include <kernel/OS.h>
75#endif
76
Martin v. Löwisa94568a2003-05-10 07:36:56 +000077#ifdef RISCOS
78extern int riscos_sleep(double);
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
Tim Peters1b6f7a92004-06-20 02:50:16 +000088/* Exposed in timefuncs.h. */
89time_t
Brett Cannon298c3802004-06-19 20:48:43 +000090_PyTime_DoubleToTimet(double x)
91{
92 time_t result;
93 double diff;
94
95 result = (time_t)x;
96 /* How much info did we lose? time_t may be an integral or
97 * floating type, and we don't know which. If it's integral,
98 * we don't know whether C truncates, rounds, returns the floor,
99 * etc. If we lost a second or more, the C rounding is
100 * unreasonable, or the input just doesn't fit in a time_t;
101 * call it an error regardless. Note that the original cast to
102 * time_t can cause a C error too, but nothing we can do to
103 * worm around that.
104 */
105 diff = x - (double)result;
106 if (diff <= -1.0 || diff >= 1.0) {
107 PyErr_SetString(PyExc_ValueError,
108 "timestamp out of range for platform time_t");
109 result = (time_t)-1;
110 }
111 return result;
112}
113
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000114static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000115time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000117 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000118 if (!PyArg_ParseTuple(args, ":time"))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000119 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000120 secs = floattime();
121 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000122 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000123 return NULL;
124 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000125 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000126}
127
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000128PyDoc_STRVAR(time_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000129"time() -> floating point number\n\
130\n\
131Return the current time in seconds since the Epoch.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000132Fractions of a second may be present if the system clock provides them.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000133
Guido van Rossumb6775db1994-08-01 11:34:53 +0000134#ifdef HAVE_CLOCK
135
136#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000137#ifdef CLK_TCK
138#define CLOCKS_PER_SEC CLK_TCK
139#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000140#define CLOCKS_PER_SEC 1000000
141#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000142#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000143
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000144static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000145time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000146{
Thomas Woutersfe385252001-01-19 23:16:56 +0000147 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000149 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000151#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000153#if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Mark Hammond7ba5e812002-02-12 04:02:33 +0000154/* Due to Mark Hammond and Tim Peters */
Guido van Rossum3917c221997-04-02 05:35:28 +0000155static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000156time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000157{
Tim Peters9ad4b682002-02-13 05:14:18 +0000158 static LARGE_INTEGER ctrStart;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000159 static double divisor = 0.0;
Tim Peters9ad4b682002-02-13 05:14:18 +0000160 LARGE_INTEGER now;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000161 double diff;
Guido van Rossum3917c221997-04-02 05:35:28 +0000162
Thomas Woutersfe385252001-01-19 23:16:56 +0000163 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum3917c221997-04-02 05:35:28 +0000164 return NULL;
165
Mark Hammond7ba5e812002-02-12 04:02:33 +0000166 if (divisor == 0.0) {
Tim Peters9ad4b682002-02-13 05:14:18 +0000167 LARGE_INTEGER freq;
168 QueryPerformanceCounter(&ctrStart);
169 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
Mark Hammond7ba5e812002-02-12 04:02:33 +0000170 /* Unlikely to happen - this works on all intel
171 machines at least! Revert to clock() */
Guido van Rossum3917c221997-04-02 05:35:28 +0000172 return PyFloat_FromDouble(clock());
173 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000174 divisor = (double)freq.QuadPart;
Guido van Rossum3917c221997-04-02 05:35:28 +0000175 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000176 QueryPerformanceCounter(&now);
177 diff = (double)(now.QuadPart - ctrStart.QuadPart);
Mark Hammond7ba5e812002-02-12 04:02:33 +0000178 return PyFloat_FromDouble(diff / divisor);
Guido van Rossum3917c221997-04-02 05:35:28 +0000179}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000180
Guido van Rossum3917c221997-04-02 05:35:28 +0000181#define HAVE_CLOCK /* So it gets included in the methods */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000182#endif /* MS_WINDOWS && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000183
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000184#ifdef HAVE_CLOCK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000185PyDoc_STRVAR(clock_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000186"clock() -> floating point number\n\
187\n\
188Return the CPU time or real time since the start of the process or since\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000189the first call to clock(). This has as much precision as the system\n\
190records.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000191#endif
192
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000193static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000194time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000196 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000197 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000199 if (floatsleep(secs) != 0)
200 return NULL;
201 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000202 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203}
204
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000205PyDoc_STRVAR(sleep_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000206"sleep(seconds)\n\
207\n\
208Delay execution for a given number of seconds. The argument may be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000209a floating point number for subsecond precision.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000210
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000211static PyStructSequence_Field struct_time_type_fields[] = {
212 {"tm_year", NULL},
213 {"tm_mon", NULL},
214 {"tm_mday", NULL},
215 {"tm_hour", NULL},
216 {"tm_min", NULL},
217 {"tm_sec", NULL},
218 {"tm_wday", NULL},
219 {"tm_yday", NULL},
220 {"tm_isdst", NULL},
221 {0}
222};
223
224static PyStructSequence_Desc struct_time_type_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000225 "time.struct_time",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000226 NULL,
227 struct_time_type_fields,
228 9,
229};
Tim Peters9ad4b682002-02-13 05:14:18 +0000230
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000231static PyTypeObject StructTimeType;
232
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000233static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000234tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000235{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000236 PyObject *v = PyStructSequence_New(&StructTimeType);
237 if (v == NULL)
238 return NULL;
Tim Peters9ad4b682002-02-13 05:14:18 +0000239
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000240#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
241
242 SET(0, p->tm_year + 1900);
243 SET(1, p->tm_mon + 1); /* Want January == 1 */
244 SET(2, p->tm_mday);
245 SET(3, p->tm_hour);
246 SET(4, p->tm_min);
247 SET(5, p->tm_sec);
248 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
249 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
250 SET(8, p->tm_isdst);
251#undef SET
252 if (PyErr_Occurred()) {
253 Py_XDECREF(v);
254 return NULL;
255 }
256
257 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000258}
259
260static PyObject *
Brett Cannon298c3802004-06-19 20:48:43 +0000261time_convert(double when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000262{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000263 struct tm *p;
Brett Cannon298c3802004-06-19 20:48:43 +0000264 time_t whent = _PyTime_DoubleToTimet(when);
265
266 if (whent == (time_t)-1 && PyErr_Occurred())
267 return NULL;
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000268 errno = 0;
Brett Cannon298c3802004-06-19 20:48:43 +0000269 p = function(&whent);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000270 if (p == NULL) {
271#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000272 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000273 errno = EINVAL;
274#endif
Tim Peters8b19a932003-01-17 20:08:54 +0000275 return PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000276 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000277 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000278}
279
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000280static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000281time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000282{
283 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000284 if (PyTuple_Size(args) == 0)
285 when = floattime();
286 if (!PyArg_ParseTuple(args, "|d:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000287 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000288 return time_convert(when, gmtime);
Guido van Rossum234f9421993-06-17 12:35:49 +0000289}
290
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000291PyDoc_STRVAR(gmtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000292"gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,\n\
293 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000294\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000295Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000296GMT). When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000297
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000298static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000299time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000300{
301 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000302 if (PyTuple_Size(args) == 0)
303 when = floattime();
304 if (!PyArg_ParseTuple(args, "|d:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000305 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000306 return time_convert(when, localtime);
Guido van Rossum234f9421993-06-17 12:35:49 +0000307}
308
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000309PyDoc_STRVAR(localtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000310"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 +0000311\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000312Convert seconds since the Epoch to a time tuple expressing local time.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000313When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000314
Guido van Rossum9e90a671993-06-24 11:10:19 +0000315static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000316gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000317{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000318 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000319 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000320
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000321 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000322 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000323 &p->tm_mon,
324 &p->tm_mday,
325 &p->tm_hour,
326 &p->tm_min,
327 &p->tm_sec,
328 &p->tm_wday,
329 &p->tm_yday,
330 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000331 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000332 if (y < 1900) {
333 PyObject *accept = PyDict_GetItemString(moddict,
334 "accept2dyear");
335 if (accept == NULL || !PyInt_Check(accept) ||
336 PyInt_AsLong(accept) == 0) {
337 PyErr_SetString(PyExc_ValueError,
338 "year >= 1900 required");
339 return 0;
340 }
341 if (69 <= y && y <= 99)
342 y += 1900;
343 else if (0 <= y && y <= 68)
344 y += 2000;
345 else {
346 PyErr_SetString(PyExc_ValueError,
Skip Montanaro1a10aac2001-08-22 12:39:16 +0000347 "year out of range");
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000348 return 0;
349 }
350 }
351 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000352 p->tm_mon--;
353 p->tm_wday = (p->tm_wday + 1) % 7;
354 p->tm_yday--;
355 return 1;
356}
357
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000358#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000359static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000360time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000361{
Thomas Woutersfe385252001-01-19 23:16:56 +0000362 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000363 struct tm buf;
364 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000365 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000366 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000367 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000368
Thomas Wouters334fb892000-07-25 12:56:38 +0000369 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000370
Thomas Woutersfe385252001-01-19 23:16:56 +0000371 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000372 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000373
374 if (tup == NULL) {
375 time_t tt = time(NULL);
376 buf = *localtime(&tt);
377 } else if (!gettmarg(tup, &buf))
378 return NULL;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000379
Brett Cannond1080a32004-03-02 04:38:10 +0000380 /* Checks added to make sure strftime() does not crash Python by
381 indexing blindly into some array for a textual representation
382 by some bad index (fixes bug #897625).
Tim Peters1b6f7a92004-06-20 02:50:16 +0000383
Brett Cannond1080a32004-03-02 04:38:10 +0000384 No check for year since handled in gettmarg().
385 */
386 if (buf.tm_mon < 0 || buf.tm_mon > 11) {
387 PyErr_SetString(PyExc_ValueError, "month out of range");
388 return NULL;
389 }
390 if (buf.tm_mday < 1 || buf.tm_mday > 31) {
391 PyErr_SetString(PyExc_ValueError, "day of month out of range");
392 return NULL;
393 }
394 if (buf.tm_hour < 0 || buf.tm_hour > 23) {
395 PyErr_SetString(PyExc_ValueError, "hour out of range");
396 return NULL;
397 }
398 if (buf.tm_min < 0 || buf.tm_min > 59) {
399 PyErr_SetString(PyExc_ValueError, "minute out of range");
400 return NULL;
401 }
402 if (buf.tm_sec < 0 || buf.tm_sec > 61) {
403 PyErr_SetString(PyExc_ValueError, "seconds out of range");
404 return NULL;
405 }
406 /* tm_wday does not need checking of its upper-bound since taking
407 ``% 7`` in gettmarg() automatically restricts the range. */
408 if (buf.tm_wday < 0) {
409 PyErr_SetString(PyExc_ValueError, "day of week out of range");
410 return NULL;
411 }
412 if (buf.tm_yday < 0 || buf.tm_yday > 365) {
413 PyErr_SetString(PyExc_ValueError, "day of year out of range");
414 return NULL;
415 }
416 if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
417 PyErr_SetString(PyExc_ValueError,
418 "daylight savings flag out of range");
419 return NULL;
420 }
421
Guido van Rossumc222ec21999-02-23 00:00:10 +0000422 fmtlen = strlen(fmt);
423
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000424 /* I hate these functions that presume you know how big the output
425 * will be ahead of time...
426 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000427 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000428 outbuf = malloc(i);
429 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000430 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000431 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000432 buflen = strftime(outbuf, i, fmt, &buf);
433 if (buflen > 0 || i >= 256 * fmtlen) {
434 /* If the buffer is 256 times as long as the format,
435 it's probably not failing for lack of room!
436 More likely, the format yields an empty result,
437 e.g. an empty format, or %Z when the timezone
438 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000439 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000440 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000441 free(outbuf);
442 return ret;
443 }
444 free(outbuf);
445 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000446}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000448PyDoc_STRVAR(strftime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000449"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000450\n\
451Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000452See the library reference manual for formatting codes. When the time tuple\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000453is not present, current time as returned by localtime() is used.");
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000454#endif /* HAVE_STRFTIME */
455
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000456static PyObject *
457time_strptime(PyObject *self, PyObject *args)
458{
459 PyObject *strptime_module = PyImport_ImportModule("_strptime");
Raymond Hettinger502168a2003-04-10 16:03:22 +0000460 PyObject *strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000461
Tim Peters513a1cd2003-01-19 04:54:58 +0000462 if (!strptime_module)
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000463 return NULL;
Raymond Hettinger502168a2003-04-10 16:03:22 +0000464 strptime_result = PyObject_CallMethod(strptime_module, "strptime", "O", args);
465 Py_DECREF(strptime_module);
466 return strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000467}
468
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000469PyDoc_STRVAR(strptime_doc,
Brett Cannon20def8b2003-07-01 05:16:08 +0000470"strptime(string, format) -> struct_time\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000471\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000472Parse a string to a time tuple according to a format specification.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000473See the library reference manual for formatting codes (same as strftime()).");
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000474
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000475
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000476static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000477time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000478{
Thomas Woutersfe385252001-01-19 23:16:56 +0000479 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000480 struct tm buf;
481 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000482 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000483 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000484 if (tup == NULL) {
485 time_t tt = time(NULL);
486 buf = *localtime(&tt);
487 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000488 return NULL;
489 p = asctime(&buf);
490 if (p[24] == '\n')
491 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000492 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000493}
494
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000495PyDoc_STRVAR(asctime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000496"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000497\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000498Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
499When the time tuple is not present, current time as returned by localtime()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000500is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000501
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000502static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000503time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000504{
505 double dt;
506 time_t tt;
507 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000508
Thomas Woutersfe385252001-01-19 23:16:56 +0000509 if (PyTuple_Size(args) == 0)
510 tt = time(NULL);
511 else {
512 if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
513 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000514 tt = _PyTime_DoubleToTimet(dt);
515 if (tt == (time_t)-1 && PyErr_Occurred())
516 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000517 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000518 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000519 if (p == NULL) {
520 PyErr_SetString(PyExc_ValueError, "unconvertible time");
521 return NULL;
522 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000523 if (p[24] == '\n')
524 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000525 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000526}
527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000528PyDoc_STRVAR(ctime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000529"ctime(seconds) -> string\n\
530\n\
531Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000532This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000533not present, current time as returned by localtime() is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000534
Guido van Rossum60cd8131998-03-06 17:16:21 +0000535#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000536static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000537time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000538{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000539 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000540 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000541 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000542 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000543 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000544 tt = time(&tt);
545 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000546 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000547 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000548 tt = mktime(&buf);
549 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000550 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000551 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000552 return NULL;
553 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000554 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000555}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000556
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000557PyDoc_STRVAR(mktime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000558"mktime(tuple) -> floating point number\n\
559\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000560Convert a time tuple in local time to seconds since the Epoch.");
Guido van Rossum60cd8131998-03-06 17:16:21 +0000561#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000562
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000563#ifdef HAVE_WORKING_TZSET
564void inittimezone(PyObject *module);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000566static PyObject *
567time_tzset(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568{
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000569 PyObject* m;
Fred Drake9bb74322002-04-01 14:49:59 +0000570
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000571 if (!PyArg_ParseTuple(args, ":tzset"))
572 return NULL;
573
574 m = PyImport_ImportModule("time");
575 if (m == NULL) {
576 return NULL;
577 }
578
579 tzset();
580
581 /* Reset timezone, altzone, daylight and tzname */
582 inittimezone(m);
583 Py_DECREF(m);
Tim Peters1b6f7a92004-06-20 02:50:16 +0000584
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000585 Py_INCREF(Py_None);
586 return Py_None;
587}
588
589PyDoc_STRVAR(tzset_doc,
590"tzset(zone)\n\
591\n\
592Initialize, or reinitialize, the local timezone to the value stored in\n\
593os.environ['TZ']. The TZ environment variable should be specified in\n\
594standard Uniz timezone format as documented in the tzset man page\n\
595(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
596fall back to UTC. If the TZ environment variable is not set, the local\n\
597timezone is set to the systems best guess of wallclock time.\n\
598Changing the TZ environment variable without calling tzset *may* change\n\
599the local timezone used by methods such as localtime, but this behaviour\n\
600should not be relied on.");
601#endif /* HAVE_WORKING_TZSET */
602
603void inittimezone(PyObject *m) {
604 /* This code moved from inittime wholesale to allow calling it from
605 time_tzset. In the future, some parts of it can be moved back
606 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
607 are), and the extranious calls to tzset(3) should be removed.
608 I havn't done this yet, as I don't want to change this code as
609 little as possible when introducing the time.tzset and time.tzsetwall
610 methods. This should simply be a method of doing the following once,
611 at the top of this function and removing the call to tzset() from
612 time_tzset():
613
614 #ifdef HAVE_TZSET
615 tzset()
616 #endif
617
618 And I'm lazy and hate C so nyer.
619 */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000620#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000621 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000622#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000623 PyModule_AddIntConstant(m, "timezone", _timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000624#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000625 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000626#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000627#ifdef HAVE_ALTZONE
Fred Drake9bb74322002-04-01 14:49:59 +0000628 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000629#else
Guido van Rossum26452411998-09-28 22:07:11 +0000630#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000631 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000632#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000633 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000634#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000635#endif
Fred Drake9bb74322002-04-01 14:49:59 +0000636 PyModule_AddIntConstant(m, "daylight", daylight);
637 PyModule_AddObject(m, "tzname",
638 Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000639#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000640#ifdef HAVE_STRUCT_TM_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000641 {
642#define YEAR ((time_t)((365 * 24 + 6) * 3600))
643 time_t t;
644 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000645 long janzone, julyzone;
646 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000647 t = (time((time_t *)0) / YEAR) * YEAR;
648 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000649 janzone = -p->tm_gmtoff;
650 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
651 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000652 t += YEAR/2;
653 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000654 julyzone = -p->tm_gmtoff;
655 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
656 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000657
Guido van Rossum57731601999-03-29 19:12:04 +0000658 if( janzone < julyzone ) {
659 /* DST is reversed in the southern hemisphere */
Fred Drake9bb74322002-04-01 14:49:59 +0000660 PyModule_AddIntConstant(m, "timezone", julyzone);
661 PyModule_AddIntConstant(m, "altzone", janzone);
662 PyModule_AddIntConstant(m, "daylight",
663 janzone != julyzone);
664 PyModule_AddObject(m, "tzname",
665 Py_BuildValue("(zz)",
666 julyname, janname));
Guido van Rossum57731601999-03-29 19:12:04 +0000667 } else {
Fred Drake9bb74322002-04-01 14:49:59 +0000668 PyModule_AddIntConstant(m, "timezone", janzone);
669 PyModule_AddIntConstant(m, "altzone", julyzone);
670 PyModule_AddIntConstant(m, "daylight",
671 janzone != julyzone);
672 PyModule_AddObject(m, "tzname",
673 Py_BuildValue("(zz)",
674 janname, julyname));
Guido van Rossum57731601999-03-29 19:12:04 +0000675 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000676 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000677#else
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000678#endif /* HAVE_STRUCT_TM_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000679#ifdef __CYGWIN__
680 tzset();
Fred Drake9bb74322002-04-01 14:49:59 +0000681 PyModule_AddIntConstant(m, "timezone", _timezone);
682 PyModule_AddIntConstant(m, "altzone", _timezone);
683 PyModule_AddIntConstant(m, "daylight", _daylight);
684 PyModule_AddObject(m, "tzname",
685 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +0000686#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000687#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000688}
689
690
691static PyMethodDef time_methods[] = {
692 {"time", time_time, METH_VARARGS, time_doc},
693#ifdef HAVE_CLOCK
694 {"clock", time_clock, METH_VARARGS, clock_doc},
695#endif
696 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
697 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
698 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
699 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
700 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
701#ifdef HAVE_MKTIME
702 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
703#endif
704#ifdef HAVE_STRFTIME
705 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
706#endif
707 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
708#ifdef HAVE_WORKING_TZSET
709 {"tzset", time_tzset, METH_VARARGS, tzset_doc},
710#endif
711 {NULL, NULL} /* sentinel */
712};
713
714
715PyDoc_STRVAR(module_doc,
716"This module provides various functions to manipulate time values.\n\
717\n\
718There are two standard representations of time. One is the number\n\
719of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
720or a floating point number (to represent fractions of seconds).\n\
721The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
722The actual value can be retrieved by calling gmtime(0).\n\
723\n\
724The other representation is a tuple of 9 integers giving local time.\n\
725The tuple items are:\n\
726 year (four digits, e.g. 1998)\n\
727 month (1-12)\n\
728 day (1-31)\n\
729 hours (0-23)\n\
730 minutes (0-59)\n\
731 seconds (0-59)\n\
732 weekday (0-6, Monday is 0)\n\
733 Julian day (day in the year, 1-366)\n\
734 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
735If the DST flag is 0, the time is given in the regular time zone;\n\
736if it is 1, the time is given in the DST time zone;\n\
737if it is -1, mktime() should guess based on the date and time.\n\
738\n\
739Variables:\n\
740\n\
741timezone -- difference in seconds between UTC and local standard time\n\
742altzone -- difference in seconds between UTC and local DST time\n\
743daylight -- whether local time should reflect DST\n\
744tzname -- tuple of (standard time zone name, DST time zone name)\n\
745\n\
746Functions:\n\
747\n\
748time() -- return current time in seconds since the Epoch as a float\n\
749clock() -- return CPU time since process start as a float\n\
750sleep() -- delay for a number of seconds given as a float\n\
751gmtime() -- convert seconds since Epoch to UTC tuple\n\
752localtime() -- convert seconds since Epoch to local time tuple\n\
753asctime() -- convert time tuple to string\n\
754ctime() -- convert time in seconds to string\n\
755mktime() -- convert local time tuple to seconds since Epoch\n\
756strftime() -- convert time tuple to string according to format specification\n\
757strptime() -- parse string to time tuple according to format specification\n\
758tzset() -- change the local timezone");
759
760
761PyMODINIT_FUNC
762inittime(void)
763{
764 PyObject *m;
765 char *p;
766 m = Py_InitModule3("time", time_methods, module_doc);
767
768 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
769 p = Py_GETENV("PYTHONY2K");
770 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
771 /* Squirrel away the module's dictionary for the y2k check */
772 moddict = PyModule_GetDict(m);
773 Py_INCREF(moddict);
774
775 /* Set, or reset, module variables like time.timezone */
776 inittimezone(m);
777
Mark Hammond975e3922002-07-16 01:29:19 +0000778#ifdef MS_WINDOWS
779 /* Helper to allow interrupts for Windows.
780 If Ctrl+C event delivered while not sleeping
781 it will be ignored.
782 */
783 main_thread = PyThread_get_thread_ident();
784 hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
785 SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
786#endif /* MS_WINDOWS */
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000787 PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
Fred Drake9bb74322002-04-01 14:49:59 +0000788 Py_INCREF(&StructTimeType);
789 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000790}
791
792
Guido van Rossumb6775db1994-08-01 11:34:53 +0000793/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000794
Guido van Rossumb6775db1994-08-01 11:34:53 +0000795static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000796floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000797{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000798 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000799 (1) gettimeofday() -- resolution in microseconds
800 (2) ftime() -- resolution in milliseconds
801 (3) time() -- resolution in seconds
802 In all cases the return value is a float in seconds.
803 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
804 fail, so we fall back on ftime() or time().
805 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000806#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000807 {
808 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000809#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000810 if (gettimeofday(&t) == 0)
811 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000812#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000813 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
814 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000815#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000816 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000817#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000818 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000819#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000820 struct timeb t;
821 ftime(&t);
822 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000823#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000824 time_t secs;
825 time(&secs);
826 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000827#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000828 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000829}
830
Guido van Rossumb6775db1994-08-01 11:34:53 +0000831
832/* Implement floatsleep() for various platforms.
833 When interrupted (or when another error occurs), return -1 and
834 set an exception; else return 0. */
835
836static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000837floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000838{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000839/* XXX Should test for MS_WINDOWS first! */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000840#if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000841 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000842 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000843 frac = fmod(secs, 1.0);
844 secs = floor(secs);
845 t.tv_sec = (long)secs;
846 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000847 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000848 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000849#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000850 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000851#else
852 if (1) {
853#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000854 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000855 PyErr_SetFromErrno(PyExc_IOError);
856 return -1;
857 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000858 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000859 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000860#elif defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000861 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000862 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000863 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000864 Py_END_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000865#elif defined(MS_WINDOWS)
Fred Drake0e123952000-06-29 21:31:02 +0000866 {
867 double millisecs = secs * 1000.0;
Tim Peters513a1cd2003-01-19 04:54:58 +0000868 unsigned long ul_millis;
869
Fred Drake0e123952000-06-29 21:31:02 +0000870 if (millisecs > (double)ULONG_MAX) {
Tim Peters513a1cd2003-01-19 04:54:58 +0000871 PyErr_SetString(PyExc_OverflowError,
872 "sleep length is too large");
Fred Drake0e123952000-06-29 21:31:02 +0000873 return -1;
874 }
Fred Drake0e123952000-06-29 21:31:02 +0000875 Py_BEGIN_ALLOW_THREADS
Tim Peters513a1cd2003-01-19 04:54:58 +0000876 /* Allow sleep(0) to maintain win32 semantics, and as decreed
877 * by Guido, only the main thread can be interrupted.
878 */
879 ul_millis = (unsigned long)millisecs;
880 if (ul_millis == 0 ||
881 main_thread != PyThread_get_thread_ident())
882 Sleep(ul_millis);
Mark Hammond975e3922002-07-16 01:29:19 +0000883 else {
884 DWORD rc;
885 ResetEvent(hInterruptEvent);
Tim Peters513a1cd2003-01-19 04:54:58 +0000886 rc = WaitForSingleObject(hInterruptEvent, ul_millis);
887 if (rc == WAIT_OBJECT_0) {
888 /* Yield to make sure real Python signal
889 * handler called.
890 */
Mark Hammond975e3922002-07-16 01:29:19 +0000891 Sleep(1);
892 Py_BLOCK_THREADS
Mark Hammond975e3922002-07-16 01:29:19 +0000893 errno = EINTR;
894 PyErr_SetFromErrno(PyExc_IOError);
895 return -1;
896 }
897 }
Fred Drake0e123952000-06-29 21:31:02 +0000898 Py_END_ALLOW_THREADS
899 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000900#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000901 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000902 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000903 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000904 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000905 PyErr_SetFromErrno(PyExc_IOError);
906 return -1;
907 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000908 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000909#elif defined(__BEOS__)
Guido van Rossumbcc20741998-08-04 22:53:56 +0000910 /* This sleep *CAN BE* interrupted. */
911 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000912 if( secs <= 0.0 ) {
913 return;
914 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000915
Guido van Rossumbcc20741998-08-04 22:53:56 +0000916 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000917 /* BeOS snooze() is in microseconds... */
918 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000919 Py_BLOCK_THREADS
920 PyErr_SetFromErrno( PyExc_IOError );
921 return -1;
922 }
923 Py_END_ALLOW_THREADS
924 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000925#elif defined(RISCOS)
Guido van Rossumbceccf52001-04-10 22:07:43 +0000926 if (secs <= 0.0)
927 return 0;
928 Py_BEGIN_ALLOW_THREADS
929 /* This sleep *CAN BE* interrupted. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000930 if ( riscos_sleep(secs) )
Guido van Rossumbceccf52001-04-10 22:07:43 +0000931 return -1;
932 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000933#elif defined(PLAN9)
934 {
935 double millisecs = secs * 1000.0;
936 if (millisecs > (double)LONG_MAX) {
937 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
938 return -1;
939 }
940 /* This sleep *CAN BE* interrupted. */
941 Py_BEGIN_ALLOW_THREADS
942 if(sleep((long)millisecs) < 0){
943 Py_BLOCK_THREADS
944 PyErr_SetFromErrno(PyExc_IOError);
945 return -1;
946 }
947 Py_END_ALLOW_THREADS
948 }
949#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000950 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000951 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000952 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000953 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000954#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000955
Guido van Rossumb6775db1994-08-01 11:34:53 +0000956 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000957}
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000958
959