blob: 2cd9a571f3dc1eab35d69fc75ddd495abd460585 [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
Fred Drakef901abd2004-08-03 17:58:55 +0000280/* Parse arg tuple that can contain an optional float-or-None value;
281 format needs to be "|O:name".
282 Returns non-zero on success (parallels PyArg_ParseTuple).
283*/
284static int
285parse_time_double_args(PyObject *args, char *format, double *pwhen)
286{
287 PyObject *ot = NULL;
288
289 if (!PyArg_ParseTuple(args, format, &ot))
290 return 0;
291 if (ot == NULL || ot == Py_None)
292 *pwhen = floattime();
293 else {
294 double when = PyFloat_AsDouble(ot);
295 if (PyErr_Occurred())
296 return 0;
297 *pwhen = when;
298 }
299 return 1;
300}
301
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000302static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000303time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000304{
305 double when;
Fred Drakef901abd2004-08-03 17:58:55 +0000306 if (!parse_time_double_args(args, "|O:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000307 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000308 return time_convert(when, gmtime);
Guido van Rossum234f9421993-06-17 12:35:49 +0000309}
310
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000311PyDoc_STRVAR(gmtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000312"gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,\n\
313 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000314\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000315Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000316GMT). When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000317
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000318static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000319time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000320{
321 double when;
Fred Drakef901abd2004-08-03 17:58:55 +0000322 if (!parse_time_double_args(args, "|O:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000323 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000324 return time_convert(when, localtime);
Guido van Rossum234f9421993-06-17 12:35:49 +0000325}
326
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000327PyDoc_STRVAR(localtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000328"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 +0000329\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000330Convert seconds since the Epoch to a time tuple expressing local time.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000331When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000332
Guido van Rossum9e90a671993-06-24 11:10:19 +0000333static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000334gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000335{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000336 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000337 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000338
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000339 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000340 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000341 &p->tm_mon,
342 &p->tm_mday,
343 &p->tm_hour,
344 &p->tm_min,
345 &p->tm_sec,
346 &p->tm_wday,
347 &p->tm_yday,
348 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000349 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000350 if (y < 1900) {
351 PyObject *accept = PyDict_GetItemString(moddict,
352 "accept2dyear");
353 if (accept == NULL || !PyInt_Check(accept) ||
354 PyInt_AsLong(accept) == 0) {
355 PyErr_SetString(PyExc_ValueError,
356 "year >= 1900 required");
357 return 0;
358 }
359 if (69 <= y && y <= 99)
360 y += 1900;
361 else if (0 <= y && y <= 68)
362 y += 2000;
363 else {
364 PyErr_SetString(PyExc_ValueError,
Skip Montanaro1a10aac2001-08-22 12:39:16 +0000365 "year out of range");
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000366 return 0;
367 }
368 }
369 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000370 p->tm_mon--;
371 p->tm_wday = (p->tm_wday + 1) % 7;
372 p->tm_yday--;
373 return 1;
374}
375
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000376#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000377static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000378time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000379{
Thomas Woutersfe385252001-01-19 23:16:56 +0000380 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000381 struct tm buf;
382 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000383 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000384 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000385 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000386
Thomas Wouters334fb892000-07-25 12:56:38 +0000387 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000388
Thomas Woutersfe385252001-01-19 23:16:56 +0000389 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000390 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000391
392 if (tup == NULL) {
393 time_t tt = time(NULL);
394 buf = *localtime(&tt);
395 } else if (!gettmarg(tup, &buf))
396 return NULL;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000397
Brett Cannond1080a32004-03-02 04:38:10 +0000398 /* Checks added to make sure strftime() does not crash Python by
399 indexing blindly into some array for a textual representation
400 by some bad index (fixes bug #897625).
Tim Peters1b6f7a92004-06-20 02:50:16 +0000401
Brett Cannond1080a32004-03-02 04:38:10 +0000402 No check for year since handled in gettmarg().
403 */
404 if (buf.tm_mon < 0 || buf.tm_mon > 11) {
405 PyErr_SetString(PyExc_ValueError, "month out of range");
406 return NULL;
407 }
408 if (buf.tm_mday < 1 || buf.tm_mday > 31) {
409 PyErr_SetString(PyExc_ValueError, "day of month out of range");
410 return NULL;
411 }
412 if (buf.tm_hour < 0 || buf.tm_hour > 23) {
413 PyErr_SetString(PyExc_ValueError, "hour out of range");
414 return NULL;
415 }
416 if (buf.tm_min < 0 || buf.tm_min > 59) {
417 PyErr_SetString(PyExc_ValueError, "minute out of range");
418 return NULL;
419 }
420 if (buf.tm_sec < 0 || buf.tm_sec > 61) {
421 PyErr_SetString(PyExc_ValueError, "seconds out of range");
422 return NULL;
423 }
424 /* tm_wday does not need checking of its upper-bound since taking
425 ``% 7`` in gettmarg() automatically restricts the range. */
426 if (buf.tm_wday < 0) {
427 PyErr_SetString(PyExc_ValueError, "day of week out of range");
428 return NULL;
429 }
430 if (buf.tm_yday < 0 || buf.tm_yday > 365) {
431 PyErr_SetString(PyExc_ValueError, "day of year out of range");
432 return NULL;
433 }
434 if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
435 PyErr_SetString(PyExc_ValueError,
436 "daylight savings flag out of range");
437 return NULL;
438 }
439
Guido van Rossumc222ec21999-02-23 00:00:10 +0000440 fmtlen = strlen(fmt);
441
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000442 /* I hate these functions that presume you know how big the output
443 * will be ahead of time...
444 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000445 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000446 outbuf = malloc(i);
447 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000448 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000449 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000450 buflen = strftime(outbuf, i, fmt, &buf);
451 if (buflen > 0 || i >= 256 * fmtlen) {
452 /* If the buffer is 256 times as long as the format,
453 it's probably not failing for lack of room!
454 More likely, the format yields an empty result,
455 e.g. an empty format, or %Z when the timezone
456 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000457 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000458 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000459 free(outbuf);
460 return ret;
461 }
462 free(outbuf);
463 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000464}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000465
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000466PyDoc_STRVAR(strftime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000467"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000468\n\
469Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000470See the library reference manual for formatting codes. When the time tuple\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000471is not present, current time as returned by localtime() is used.");
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000472#endif /* HAVE_STRFTIME */
473
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000474static PyObject *
475time_strptime(PyObject *self, PyObject *args)
476{
477 PyObject *strptime_module = PyImport_ImportModule("_strptime");
Raymond Hettinger502168a2003-04-10 16:03:22 +0000478 PyObject *strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000479
Tim Peters513a1cd2003-01-19 04:54:58 +0000480 if (!strptime_module)
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000481 return NULL;
Raymond Hettinger502168a2003-04-10 16:03:22 +0000482 strptime_result = PyObject_CallMethod(strptime_module, "strptime", "O", args);
483 Py_DECREF(strptime_module);
484 return strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000485}
486
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000487PyDoc_STRVAR(strptime_doc,
Brett Cannon20def8b2003-07-01 05:16:08 +0000488"strptime(string, format) -> struct_time\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000489\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000490Parse a string to a time tuple according to a format specification.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000491See the library reference manual for formatting codes (same as strftime()).");
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000492
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000493
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000494static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000495time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000496{
Thomas Woutersfe385252001-01-19 23:16:56 +0000497 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000498 struct tm buf;
499 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000500 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000501 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000502 if (tup == NULL) {
503 time_t tt = time(NULL);
504 buf = *localtime(&tt);
505 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000506 return NULL;
507 p = asctime(&buf);
508 if (p[24] == '\n')
509 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000510 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000511}
512
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000513PyDoc_STRVAR(asctime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000514"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000515\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000516Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
517When the time tuple is not present, current time as returned by localtime()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000518is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000519
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000520static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000521time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000522{
Fred Drakef901abd2004-08-03 17:58:55 +0000523 PyObject *ot = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000524 time_t tt;
525 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000526
Fred Drakef901abd2004-08-03 17:58:55 +0000527 if (!PyArg_ParseTuple(args, "|O:ctime", &ot))
528 return NULL;
529 if (ot == NULL || ot == Py_None)
Thomas Woutersfe385252001-01-19 23:16:56 +0000530 tt = time(NULL);
531 else {
Fred Drakef901abd2004-08-03 17:58:55 +0000532 double dt = PyFloat_AsDouble(ot);
533 if (PyErr_Occurred())
Thomas Woutersfe385252001-01-19 23:16:56 +0000534 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000535 tt = _PyTime_DoubleToTimet(dt);
536 if (tt == (time_t)-1 && PyErr_Occurred())
537 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000538 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000539 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000540 if (p == NULL) {
541 PyErr_SetString(PyExc_ValueError, "unconvertible time");
542 return NULL;
543 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000544 if (p[24] == '\n')
545 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000546 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000547}
548
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000549PyDoc_STRVAR(ctime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000550"ctime(seconds) -> string\n\
551\n\
552Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000553This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000554not present, current time as returned by localtime() is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000555
Guido van Rossum60cd8131998-03-06 17:16:21 +0000556#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000557static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000558time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000559{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000560 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000561 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000562 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000563 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000564 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000565 tt = time(&tt);
566 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000567 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000568 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000569 tt = mktime(&buf);
570 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000571 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000572 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000573 return NULL;
574 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000575 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000576}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000577
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000578PyDoc_STRVAR(mktime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000579"mktime(tuple) -> floating point number\n\
580\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000581Convert a time tuple in local time to seconds since the Epoch.");
Guido van Rossum60cd8131998-03-06 17:16:21 +0000582#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000583
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000584#ifdef HAVE_WORKING_TZSET
585void inittimezone(PyObject *module);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000586
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000587static PyObject *
588time_tzset(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589{
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000590 PyObject* m;
Fred Drake9bb74322002-04-01 14:49:59 +0000591
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000592 if (!PyArg_ParseTuple(args, ":tzset"))
593 return NULL;
594
595 m = PyImport_ImportModule("time");
596 if (m == NULL) {
597 return NULL;
598 }
599
600 tzset();
601
602 /* Reset timezone, altzone, daylight and tzname */
603 inittimezone(m);
604 Py_DECREF(m);
Tim Peters1b6f7a92004-06-20 02:50:16 +0000605
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000606 Py_INCREF(Py_None);
607 return Py_None;
608}
609
610PyDoc_STRVAR(tzset_doc,
611"tzset(zone)\n\
612\n\
613Initialize, or reinitialize, the local timezone to the value stored in\n\
614os.environ['TZ']. The TZ environment variable should be specified in\n\
Neal Norwitzdc8e1942004-07-20 22:34:37 +0000615standard Unix timezone format as documented in the tzset man page\n\
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000616(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
617fall back to UTC. If the TZ environment variable is not set, the local\n\
618timezone is set to the systems best guess of wallclock time.\n\
619Changing the TZ environment variable without calling tzset *may* change\n\
620the local timezone used by methods such as localtime, but this behaviour\n\
621should not be relied on.");
622#endif /* HAVE_WORKING_TZSET */
623
624void inittimezone(PyObject *m) {
625 /* This code moved from inittime wholesale to allow calling it from
626 time_tzset. In the future, some parts of it can be moved back
627 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
628 are), and the extranious calls to tzset(3) should be removed.
629 I havn't done this yet, as I don't want to change this code as
630 little as possible when introducing the time.tzset and time.tzsetwall
631 methods. This should simply be a method of doing the following once,
632 at the top of this function and removing the call to tzset() from
633 time_tzset():
634
635 #ifdef HAVE_TZSET
636 tzset()
637 #endif
638
639 And I'm lazy and hate C so nyer.
640 */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000641#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000642 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000643#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000644 PyModule_AddIntConstant(m, "timezone", _timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000645#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000646 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000647#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000648#ifdef HAVE_ALTZONE
Fred Drake9bb74322002-04-01 14:49:59 +0000649 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000650#else
Guido van Rossum26452411998-09-28 22:07:11 +0000651#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000652 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000653#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000654 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000655#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000656#endif
Fred Drake9bb74322002-04-01 14:49:59 +0000657 PyModule_AddIntConstant(m, "daylight", daylight);
658 PyModule_AddObject(m, "tzname",
659 Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000660#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000661#ifdef HAVE_STRUCT_TM_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000662 {
663#define YEAR ((time_t)((365 * 24 + 6) * 3600))
664 time_t t;
665 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000666 long janzone, julyzone;
667 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000668 t = (time((time_t *)0) / YEAR) * YEAR;
669 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000670 janzone = -p->tm_gmtoff;
671 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
672 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000673 t += YEAR/2;
674 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000675 julyzone = -p->tm_gmtoff;
676 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
677 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000678
Guido van Rossum57731601999-03-29 19:12:04 +0000679 if( janzone < julyzone ) {
680 /* DST is reversed in the southern hemisphere */
Fred Drake9bb74322002-04-01 14:49:59 +0000681 PyModule_AddIntConstant(m, "timezone", julyzone);
682 PyModule_AddIntConstant(m, "altzone", janzone);
683 PyModule_AddIntConstant(m, "daylight",
684 janzone != julyzone);
685 PyModule_AddObject(m, "tzname",
686 Py_BuildValue("(zz)",
687 julyname, janname));
Guido van Rossum57731601999-03-29 19:12:04 +0000688 } else {
Fred Drake9bb74322002-04-01 14:49:59 +0000689 PyModule_AddIntConstant(m, "timezone", janzone);
690 PyModule_AddIntConstant(m, "altzone", julyzone);
691 PyModule_AddIntConstant(m, "daylight",
692 janzone != julyzone);
693 PyModule_AddObject(m, "tzname",
694 Py_BuildValue("(zz)",
695 janname, julyname));
Guido van Rossum57731601999-03-29 19:12:04 +0000696 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000697 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000698#else
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000699#endif /* HAVE_STRUCT_TM_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000700#ifdef __CYGWIN__
701 tzset();
Fred Drake9bb74322002-04-01 14:49:59 +0000702 PyModule_AddIntConstant(m, "timezone", _timezone);
703 PyModule_AddIntConstant(m, "altzone", _timezone);
704 PyModule_AddIntConstant(m, "daylight", _daylight);
705 PyModule_AddObject(m, "tzname",
706 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +0000707#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000708#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000709}
710
711
712static PyMethodDef time_methods[] = {
713 {"time", time_time, METH_VARARGS, time_doc},
714#ifdef HAVE_CLOCK
715 {"clock", time_clock, METH_VARARGS, clock_doc},
716#endif
717 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
718 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
719 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
720 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
721 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
722#ifdef HAVE_MKTIME
723 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
724#endif
725#ifdef HAVE_STRFTIME
726 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
727#endif
728 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
729#ifdef HAVE_WORKING_TZSET
730 {"tzset", time_tzset, METH_VARARGS, tzset_doc},
731#endif
732 {NULL, NULL} /* sentinel */
733};
734
735
736PyDoc_STRVAR(module_doc,
737"This module provides various functions to manipulate time values.\n\
738\n\
739There are two standard representations of time. One is the number\n\
740of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
741or a floating point number (to represent fractions of seconds).\n\
742The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
743The actual value can be retrieved by calling gmtime(0).\n\
744\n\
745The other representation is a tuple of 9 integers giving local time.\n\
746The tuple items are:\n\
747 year (four digits, e.g. 1998)\n\
748 month (1-12)\n\
749 day (1-31)\n\
750 hours (0-23)\n\
751 minutes (0-59)\n\
752 seconds (0-59)\n\
753 weekday (0-6, Monday is 0)\n\
754 Julian day (day in the year, 1-366)\n\
755 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
756If the DST flag is 0, the time is given in the regular time zone;\n\
757if it is 1, the time is given in the DST time zone;\n\
758if it is -1, mktime() should guess based on the date and time.\n\
759\n\
760Variables:\n\
761\n\
762timezone -- difference in seconds between UTC and local standard time\n\
763altzone -- difference in seconds between UTC and local DST time\n\
764daylight -- whether local time should reflect DST\n\
765tzname -- tuple of (standard time zone name, DST time zone name)\n\
766\n\
767Functions:\n\
768\n\
769time() -- return current time in seconds since the Epoch as a float\n\
770clock() -- return CPU time since process start as a float\n\
771sleep() -- delay for a number of seconds given as a float\n\
772gmtime() -- convert seconds since Epoch to UTC tuple\n\
773localtime() -- convert seconds since Epoch to local time tuple\n\
774asctime() -- convert time tuple to string\n\
775ctime() -- convert time in seconds to string\n\
776mktime() -- convert local time tuple to seconds since Epoch\n\
777strftime() -- convert time tuple to string according to format specification\n\
778strptime() -- parse string to time tuple according to format specification\n\
779tzset() -- change the local timezone");
780
781
782PyMODINIT_FUNC
783inittime(void)
784{
785 PyObject *m;
786 char *p;
787 m = Py_InitModule3("time", time_methods, module_doc);
788
789 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
790 p = Py_GETENV("PYTHONY2K");
791 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
792 /* Squirrel away the module's dictionary for the y2k check */
793 moddict = PyModule_GetDict(m);
794 Py_INCREF(moddict);
795
796 /* Set, or reset, module variables like time.timezone */
797 inittimezone(m);
798
Mark Hammond975e3922002-07-16 01:29:19 +0000799#ifdef MS_WINDOWS
800 /* Helper to allow interrupts for Windows.
801 If Ctrl+C event delivered while not sleeping
802 it will be ignored.
803 */
804 main_thread = PyThread_get_thread_ident();
805 hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
806 SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
807#endif /* MS_WINDOWS */
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000808 PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
Fred Drake9bb74322002-04-01 14:49:59 +0000809 Py_INCREF(&StructTimeType);
810 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000811}
812
813
Guido van Rossumb6775db1994-08-01 11:34:53 +0000814/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000815
Guido van Rossumb6775db1994-08-01 11:34:53 +0000816static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000817floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000818{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000819 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000820 (1) gettimeofday() -- resolution in microseconds
821 (2) ftime() -- resolution in milliseconds
822 (3) time() -- resolution in seconds
823 In all cases the return value is a float in seconds.
824 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
825 fail, so we fall back on ftime() or time().
826 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000827#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000828 {
829 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000830#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000831 if (gettimeofday(&t) == 0)
832 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000833#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000834 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
835 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000836#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000837 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000838#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000839 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000840#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000841 struct timeb t;
842 ftime(&t);
843 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000844#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000845 time_t secs;
846 time(&secs);
847 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000848#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000849 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000850}
851
Guido van Rossumb6775db1994-08-01 11:34:53 +0000852
853/* Implement floatsleep() for various platforms.
854 When interrupted (or when another error occurs), return -1 and
855 set an exception; else return 0. */
856
857static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000858floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000859{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000860/* XXX Should test for MS_WINDOWS first! */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000861#if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000862 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000863 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000864 frac = fmod(secs, 1.0);
865 secs = floor(secs);
866 t.tv_sec = (long)secs;
867 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000868 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000869 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000870#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000871 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000872#else
873 if (1) {
874#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000875 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000876 PyErr_SetFromErrno(PyExc_IOError);
877 return -1;
878 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000879 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000880 Py_END_ALLOW_THREADS
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