blob: f089ecd8e950972a9f0d67f101365f329e929bbe [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
Ronald Oussorend06b6f22006-04-23 11:59:25 +00008#ifdef __APPLE__
9#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME)
10 /*
11 * floattime falls back to ftime when getttimeofday fails because the latter
12 * might fail on some platforms. This fallback is unwanted on MacOSX because
13 * that makes it impossible to use a binary build on OSX 10.4 on earlier
14 * releases of the OS. Therefore claim we don't support ftime.
15 */
16# undef HAVE_FTIME
17#endif
18#endif
19
Guido van Rossum87ce7bb1998-06-09 16:30:31 +000020#include <ctype.h>
21
Guido van Rossumb6775db1994-08-01 11:34:53 +000022#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000023
Guido van Rossumb6775db1994-08-01 11:34:53 +000024#ifdef QUICKWIN
25#include <io.h>
26#endif
27
Guido van Rossumb6775db1994-08-01 11:34:53 +000028#ifdef HAVE_FTIME
29#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000030#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000031extern int ftime(struct timeb *);
Guido van Rossum52174571996-12-09 18:38:52 +000032#endif /* MS_WINDOWS */
33#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034
Guido van Rossum7bf22de1997-12-02 20:34:19 +000035#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000036#include <i86.h>
37#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000038#ifdef MS_WINDOWS
Mark Hammond975e3922002-07-16 01:29:19 +000039#define WIN32_LEAN_AND_MEAN
Guido van Rossum258ccd42001-03-02 06:53:29 +000040#include <windows.h>
Mark Hammond975e3922002-07-16 01:29:19 +000041#include "pythread.h"
42
43/* helper to allow us to interrupt sleep() on Windows*/
44static HANDLE hInterruptEvent = NULL;
45static BOOL WINAPI PyCtrlHandler(DWORD dwCtrlType)
46{
47 SetEvent(hInterruptEvent);
48 /* allow other default handlers to be called.
49 Default Python handler will setup the
50 KeyboardInterrupt exception.
51 */
52 return FALSE;
53}
54static long main_thread;
55
56
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000057#if defined(__BORLANDC__)
Guido van Rossumb2fb3641996-09-07 00:47:35 +000058/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000059#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000060#define tzname _tzname
61#define daylight _daylight
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000062#endif /* __BORLANDC__ */
Guido van Rossumcac6c721996-09-06 13:34:02 +000063#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000064#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000065
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000066#if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Fred Drakedfb4ebd2000-06-29 20:56:28 +000067/* Win32 has better clock replacement
68 XXX Win64 does not yet, but might when the platform matures. */
Guido van Rossum3917c221997-04-02 05:35:28 +000069#undef HAVE_CLOCK /* We have our own version down below */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000070#endif /* MS_WINDOWS && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +000071
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000072#if defined(PYOS_OS2)
73#define INCL_DOS
74#define INCL_ERRORS
75#include <os2.h>
76#endif
77
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000078#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000079#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000080#endif
81
Guido van Rossumbcc20741998-08-04 22:53:56 +000082#ifdef __BEOS__
Fred Drake56221a72000-08-15 18:52:33 +000083#include <time.h>
Guido van Rossumbcc20741998-08-04 22:53:56 +000084/* For bigtime_t, snooze(). - [cjh] */
85#include <support/SupportDefs.h>
86#include <kernel/OS.h>
87#endif
88
Martin v. Löwisa94568a2003-05-10 07:36:56 +000089#ifdef RISCOS
90extern int riscos_sleep(double);
91#endif
92
Guido van Rossum234f9421993-06-17 12:35:49 +000093/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000094static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000095static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000097/* For Y2K check */
98static PyObject *moddict;
99
Tim Peters1b6f7a92004-06-20 02:50:16 +0000100/* Exposed in timefuncs.h. */
101time_t
Brett Cannon298c3802004-06-19 20:48:43 +0000102_PyTime_DoubleToTimet(double x)
103{
104 time_t result;
105 double diff;
106
107 result = (time_t)x;
108 /* How much info did we lose? time_t may be an integral or
109 * floating type, and we don't know which. If it's integral,
110 * we don't know whether C truncates, rounds, returns the floor,
111 * etc. If we lost a second or more, the C rounding is
112 * unreasonable, or the input just doesn't fit in a time_t;
113 * call it an error regardless. Note that the original cast to
114 * time_t can cause a C error too, but nothing we can do to
115 * worm around that.
116 */
117 diff = x - (double)result;
118 if (diff <= -1.0 || diff >= 1.0) {
119 PyErr_SetString(PyExc_ValueError,
120 "timestamp out of range for platform time_t");
121 result = (time_t)-1;
122 }
123 return result;
124}
125
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000126static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000127time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000129 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000130 if (!PyArg_ParseTuple(args, ":time"))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000131 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000132 secs = floattime();
133 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000134 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000135 return NULL;
136 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000137 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000138}
139
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000140PyDoc_STRVAR(time_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000141"time() -> floating point number\n\
142\n\
143Return the current time in seconds since the Epoch.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000144Fractions of a second may be present if the system clock provides them.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000145
Guido van Rossumb6775db1994-08-01 11:34:53 +0000146#ifdef HAVE_CLOCK
147
148#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000149#ifdef CLK_TCK
150#define CLOCKS_PER_SEC CLK_TCK
151#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152#define CLOCKS_PER_SEC 1000000
153#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000154#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000155
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000156static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000157time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000158{
Thomas Woutersfe385252001-01-19 23:16:56 +0000159 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000161 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000163#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000165#if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Mark Hammond7ba5e812002-02-12 04:02:33 +0000166/* Due to Mark Hammond and Tim Peters */
Guido van Rossum3917c221997-04-02 05:35:28 +0000167static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000168time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000169{
Tim Peters9ad4b682002-02-13 05:14:18 +0000170 static LARGE_INTEGER ctrStart;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000171 static double divisor = 0.0;
Tim Peters9ad4b682002-02-13 05:14:18 +0000172 LARGE_INTEGER now;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000173 double diff;
Guido van Rossum3917c221997-04-02 05:35:28 +0000174
Thomas Woutersfe385252001-01-19 23:16:56 +0000175 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum3917c221997-04-02 05:35:28 +0000176 return NULL;
177
Mark Hammond7ba5e812002-02-12 04:02:33 +0000178 if (divisor == 0.0) {
Tim Peters9ad4b682002-02-13 05:14:18 +0000179 LARGE_INTEGER freq;
180 QueryPerformanceCounter(&ctrStart);
181 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
Mark Hammond7ba5e812002-02-12 04:02:33 +0000182 /* Unlikely to happen - this works on all intel
183 machines at least! Revert to clock() */
Guido van Rossum3917c221997-04-02 05:35:28 +0000184 return PyFloat_FromDouble(clock());
185 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000186 divisor = (double)freq.QuadPart;
Guido van Rossum3917c221997-04-02 05:35:28 +0000187 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000188 QueryPerformanceCounter(&now);
189 diff = (double)(now.QuadPart - ctrStart.QuadPart);
Mark Hammond7ba5e812002-02-12 04:02:33 +0000190 return PyFloat_FromDouble(diff / divisor);
Guido van Rossum3917c221997-04-02 05:35:28 +0000191}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000192
Guido van Rossum3917c221997-04-02 05:35:28 +0000193#define HAVE_CLOCK /* So it gets included in the methods */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000194#endif /* MS_WINDOWS && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000195
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000196#ifdef HAVE_CLOCK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000197PyDoc_STRVAR(clock_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000198"clock() -> floating point number\n\
199\n\
200Return the CPU time or real time since the start of the process or since\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000201the first call to clock(). This has as much precision as the system\n\
202records.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000203#endif
204
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000205static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000206time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000208 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000209 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000211 if (floatsleep(secs) != 0)
212 return NULL;
213 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000214 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000215}
216
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000217PyDoc_STRVAR(sleep_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000218"sleep(seconds)\n\
219\n\
220Delay execution for a given number of seconds. The argument may be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000221a floating point number for subsecond precision.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000222
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000223static PyStructSequence_Field struct_time_type_fields[] = {
224 {"tm_year", NULL},
225 {"tm_mon", NULL},
226 {"tm_mday", NULL},
227 {"tm_hour", NULL},
228 {"tm_min", NULL},
229 {"tm_sec", NULL},
230 {"tm_wday", NULL},
231 {"tm_yday", NULL},
232 {"tm_isdst", NULL},
233 {0}
234};
235
236static PyStructSequence_Desc struct_time_type_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000237 "time.struct_time",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000238 NULL,
239 struct_time_type_fields,
240 9,
241};
Tim Peters9ad4b682002-02-13 05:14:18 +0000242
Martin v. Löwis19ab6c92006-04-16 18:55:50 +0000243static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000244static PyTypeObject StructTimeType;
245
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000246static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000247tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000248{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000249 PyObject *v = PyStructSequence_New(&StructTimeType);
250 if (v == NULL)
251 return NULL;
Tim Peters9ad4b682002-02-13 05:14:18 +0000252
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000253#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
254
255 SET(0, p->tm_year + 1900);
256 SET(1, p->tm_mon + 1); /* Want January == 1 */
257 SET(2, p->tm_mday);
258 SET(3, p->tm_hour);
259 SET(4, p->tm_min);
260 SET(5, p->tm_sec);
261 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
262 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
263 SET(8, p->tm_isdst);
264#undef SET
265 if (PyErr_Occurred()) {
266 Py_XDECREF(v);
267 return NULL;
268 }
269
270 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000271}
272
273static PyObject *
Brett Cannon298c3802004-06-19 20:48:43 +0000274time_convert(double when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000275{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000276 struct tm *p;
Brett Cannon298c3802004-06-19 20:48:43 +0000277 time_t whent = _PyTime_DoubleToTimet(when);
278
279 if (whent == (time_t)-1 && PyErr_Occurred())
280 return NULL;
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000281 errno = 0;
Brett Cannon298c3802004-06-19 20:48:43 +0000282 p = function(&whent);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000283 if (p == NULL) {
284#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000285 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000286 errno = EINVAL;
287#endif
Tim Peters8b19a932003-01-17 20:08:54 +0000288 return PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000289 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000290 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000291}
292
Fred Drakef901abd2004-08-03 17:58:55 +0000293/* Parse arg tuple that can contain an optional float-or-None value;
294 format needs to be "|O:name".
295 Returns non-zero on success (parallels PyArg_ParseTuple).
296*/
297static int
298parse_time_double_args(PyObject *args, char *format, double *pwhen)
299{
300 PyObject *ot = NULL;
301
302 if (!PyArg_ParseTuple(args, format, &ot))
303 return 0;
304 if (ot == NULL || ot == Py_None)
305 *pwhen = floattime();
306 else {
307 double when = PyFloat_AsDouble(ot);
308 if (PyErr_Occurred())
309 return 0;
310 *pwhen = when;
311 }
312 return 1;
313}
314
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000315static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000316time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000317{
318 double when;
Fred Drakef901abd2004-08-03 17:58:55 +0000319 if (!parse_time_double_args(args, "|O:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000320 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000321 return time_convert(when, gmtime);
Guido van Rossum234f9421993-06-17 12:35:49 +0000322}
323
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000324PyDoc_STRVAR(gmtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000325"gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,\n\
326 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000327\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000328Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000329GMT). When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000330
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000331static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000332time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000333{
334 double when;
Fred Drakef901abd2004-08-03 17:58:55 +0000335 if (!parse_time_double_args(args, "|O:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000336 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000337 return time_convert(when, localtime);
Guido van Rossum234f9421993-06-17 12:35:49 +0000338}
339
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000340PyDoc_STRVAR(localtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000341"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 +0000342\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000343Convert seconds since the Epoch to a time tuple expressing local time.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000344When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000345
Guido van Rossum9e90a671993-06-24 11:10:19 +0000346static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000347gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000348{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000349 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000350 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000351
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000352 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000353 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000354 &p->tm_mon,
355 &p->tm_mday,
356 &p->tm_hour,
357 &p->tm_min,
358 &p->tm_sec,
359 &p->tm_wday,
360 &p->tm_yday,
361 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000362 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000363 if (y < 1900) {
364 PyObject *accept = PyDict_GetItemString(moddict,
365 "accept2dyear");
366 if (accept == NULL || !PyInt_Check(accept) ||
367 PyInt_AsLong(accept) == 0) {
368 PyErr_SetString(PyExc_ValueError,
369 "year >= 1900 required");
370 return 0;
371 }
372 if (69 <= y && y <= 99)
373 y += 1900;
374 else if (0 <= y && y <= 68)
375 y += 2000;
376 else {
377 PyErr_SetString(PyExc_ValueError,
Skip Montanaro1a10aac2001-08-22 12:39:16 +0000378 "year out of range");
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000379 return 0;
380 }
381 }
382 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000383 p->tm_mon--;
384 p->tm_wday = (p->tm_wday + 1) % 7;
385 p->tm_yday--;
386 return 1;
387}
388
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000389#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000390static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000391time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000392{
Thomas Woutersfe385252001-01-19 23:16:56 +0000393 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000394 struct tm buf;
395 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000396 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000397 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000398 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000399
Thomas Wouters334fb892000-07-25 12:56:38 +0000400 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000401
Thomas Woutersfe385252001-01-19 23:16:56 +0000402 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000403 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000404
405 if (tup == NULL) {
406 time_t tt = time(NULL);
407 buf = *localtime(&tt);
408 } else if (!gettmarg(tup, &buf))
409 return NULL;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000410
Brett Cannond1080a32004-03-02 04:38:10 +0000411 /* Checks added to make sure strftime() does not crash Python by
412 indexing blindly into some array for a textual representation
413 by some bad index (fixes bug #897625).
Tim Peters1b6f7a92004-06-20 02:50:16 +0000414
Brett Cannond1080a32004-03-02 04:38:10 +0000415 No check for year since handled in gettmarg().
416 */
417 if (buf.tm_mon < 0 || buf.tm_mon > 11) {
418 PyErr_SetString(PyExc_ValueError, "month out of range");
419 return NULL;
420 }
421 if (buf.tm_mday < 1 || buf.tm_mday > 31) {
422 PyErr_SetString(PyExc_ValueError, "day of month out of range");
423 return NULL;
424 }
425 if (buf.tm_hour < 0 || buf.tm_hour > 23) {
426 PyErr_SetString(PyExc_ValueError, "hour out of range");
427 return NULL;
428 }
429 if (buf.tm_min < 0 || buf.tm_min > 59) {
430 PyErr_SetString(PyExc_ValueError, "minute out of range");
431 return NULL;
432 }
433 if (buf.tm_sec < 0 || buf.tm_sec > 61) {
434 PyErr_SetString(PyExc_ValueError, "seconds out of range");
435 return NULL;
436 }
437 /* tm_wday does not need checking of its upper-bound since taking
438 ``% 7`` in gettmarg() automatically restricts the range. */
439 if (buf.tm_wday < 0) {
440 PyErr_SetString(PyExc_ValueError, "day of week out of range");
441 return NULL;
442 }
443 if (buf.tm_yday < 0 || buf.tm_yday > 365) {
444 PyErr_SetString(PyExc_ValueError, "day of year out of range");
445 return NULL;
446 }
447 if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
448 PyErr_SetString(PyExc_ValueError,
449 "daylight savings flag out of range");
450 return NULL;
451 }
452
Guido van Rossumc222ec21999-02-23 00:00:10 +0000453 fmtlen = strlen(fmt);
454
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000455 /* I hate these functions that presume you know how big the output
456 * will be ahead of time...
457 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000458 for (i = 1024; ; i += i) {
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000459 outbuf = (char *)malloc(i);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000460 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000461 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000462 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000463 buflen = strftime(outbuf, i, fmt, &buf);
464 if (buflen > 0 || i >= 256 * fmtlen) {
465 /* If the buffer is 256 times as long as the format,
466 it's probably not failing for lack of room!
467 More likely, the format yields an empty result,
468 e.g. an empty format, or %Z when the timezone
469 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000470 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000471 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000472 free(outbuf);
473 return ret;
474 }
475 free(outbuf);
476 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000477}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000479PyDoc_STRVAR(strftime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000480"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000481\n\
482Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000483See the library reference manual for formatting codes. When the time tuple\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000484is not present, current time as returned by localtime() is used.");
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000485#endif /* HAVE_STRFTIME */
486
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000487static PyObject *
488time_strptime(PyObject *self, PyObject *args)
489{
490 PyObject *strptime_module = PyImport_ImportModule("_strptime");
Raymond Hettinger502168a2003-04-10 16:03:22 +0000491 PyObject *strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000492
Tim Peters513a1cd2003-01-19 04:54:58 +0000493 if (!strptime_module)
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000494 return NULL;
Raymond Hettinger502168a2003-04-10 16:03:22 +0000495 strptime_result = PyObject_CallMethod(strptime_module, "strptime", "O", args);
496 Py_DECREF(strptime_module);
497 return strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000498}
499
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000500PyDoc_STRVAR(strptime_doc,
Brett Cannon20def8b2003-07-01 05:16:08 +0000501"strptime(string, format) -> struct_time\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000502\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000503Parse a string to a time tuple according to a format specification.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000504See the library reference manual for formatting codes (same as strftime()).");
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000505
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000506
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000507static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000508time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000509{
Thomas Woutersfe385252001-01-19 23:16:56 +0000510 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000511 struct tm buf;
512 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000513 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000514 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000515 if (tup == NULL) {
516 time_t tt = time(NULL);
517 buf = *localtime(&tt);
518 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000519 return NULL;
520 p = asctime(&buf);
521 if (p[24] == '\n')
522 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000523 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000524}
525
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000526PyDoc_STRVAR(asctime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000527"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000528\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000529Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
530When the time tuple is not present, current time as returned by localtime()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000531is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000532
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000533static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000534time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000535{
Fred Drakef901abd2004-08-03 17:58:55 +0000536 PyObject *ot = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000537 time_t tt;
538 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000539
Fred Drakef901abd2004-08-03 17:58:55 +0000540 if (!PyArg_ParseTuple(args, "|O:ctime", &ot))
541 return NULL;
542 if (ot == NULL || ot == Py_None)
Thomas Woutersfe385252001-01-19 23:16:56 +0000543 tt = time(NULL);
544 else {
Fred Drakef901abd2004-08-03 17:58:55 +0000545 double dt = PyFloat_AsDouble(ot);
546 if (PyErr_Occurred())
Thomas Woutersfe385252001-01-19 23:16:56 +0000547 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000548 tt = _PyTime_DoubleToTimet(dt);
549 if (tt == (time_t)-1 && PyErr_Occurred())
550 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000551 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000552 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000553 if (p == NULL) {
554 PyErr_SetString(PyExc_ValueError, "unconvertible time");
555 return NULL;
556 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000557 if (p[24] == '\n')
558 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000559 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000560}
561
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000562PyDoc_STRVAR(ctime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000563"ctime(seconds) -> string\n\
564\n\
565Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000566This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000567not present, current time as returned by localtime() is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000568
Guido van Rossum60cd8131998-03-06 17:16:21 +0000569#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000570static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000571time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000572{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000573 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000574 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000575 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000576 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000577 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000578 tt = time(&tt);
579 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000580 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000581 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000582 tt = mktime(&buf);
583 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000584 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000585 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000586 return NULL;
587 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000588 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000589}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000590
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000591PyDoc_STRVAR(mktime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000592"mktime(tuple) -> floating point number\n\
593\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000594Convert a time tuple in local time to seconds since the Epoch.");
Guido van Rossum60cd8131998-03-06 17:16:21 +0000595#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000596
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000597#ifdef HAVE_WORKING_TZSET
598void inittimezone(PyObject *module);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000599
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000600static PyObject *
601time_tzset(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000602{
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000603 PyObject* m;
Fred Drake9bb74322002-04-01 14:49:59 +0000604
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000605 if (!PyArg_ParseTuple(args, ":tzset"))
606 return NULL;
607
608 m = PyImport_ImportModule("time");
609 if (m == NULL) {
610 return NULL;
611 }
612
613 tzset();
614
615 /* Reset timezone, altzone, daylight and tzname */
616 inittimezone(m);
617 Py_DECREF(m);
Tim Peters1b6f7a92004-06-20 02:50:16 +0000618
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000619 Py_INCREF(Py_None);
620 return Py_None;
621}
622
623PyDoc_STRVAR(tzset_doc,
624"tzset(zone)\n\
625\n\
626Initialize, or reinitialize, the local timezone to the value stored in\n\
627os.environ['TZ']. The TZ environment variable should be specified in\n\
Neal Norwitzdc8e1942004-07-20 22:34:37 +0000628standard Unix timezone format as documented in the tzset man page\n\
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000629(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
630fall back to UTC. If the TZ environment variable is not set, the local\n\
631timezone is set to the systems best guess of wallclock time.\n\
632Changing the TZ environment variable without calling tzset *may* change\n\
633the local timezone used by methods such as localtime, but this behaviour\n\
634should not be relied on.");
635#endif /* HAVE_WORKING_TZSET */
636
637void inittimezone(PyObject *m) {
638 /* This code moved from inittime wholesale to allow calling it from
639 time_tzset. In the future, some parts of it can be moved back
640 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
641 are), and the extranious calls to tzset(3) should be removed.
642 I havn't done this yet, as I don't want to change this code as
643 little as possible when introducing the time.tzset and time.tzsetwall
644 methods. This should simply be a method of doing the following once,
645 at the top of this function and removing the call to tzset() from
646 time_tzset():
647
648 #ifdef HAVE_TZSET
649 tzset()
650 #endif
651
652 And I'm lazy and hate C so nyer.
653 */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000654#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000655 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000656#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000657 PyModule_AddIntConstant(m, "timezone", _timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000658#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000659 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000660#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000661#ifdef HAVE_ALTZONE
Fred Drake9bb74322002-04-01 14:49:59 +0000662 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000663#else
Guido van Rossum26452411998-09-28 22:07:11 +0000664#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000665 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000666#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000667 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000668#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000669#endif
Fred Drake9bb74322002-04-01 14:49:59 +0000670 PyModule_AddIntConstant(m, "daylight", daylight);
671 PyModule_AddObject(m, "tzname",
672 Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000673#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000674#ifdef HAVE_STRUCT_TM_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000675 {
676#define YEAR ((time_t)((365 * 24 + 6) * 3600))
677 time_t t;
678 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000679 long janzone, julyzone;
680 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000681 t = (time((time_t *)0) / YEAR) * YEAR;
682 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000683 janzone = -p->tm_gmtoff;
684 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
685 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000686 t += YEAR/2;
687 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000688 julyzone = -p->tm_gmtoff;
689 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
690 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000691
Guido van Rossum57731601999-03-29 19:12:04 +0000692 if( janzone < julyzone ) {
693 /* DST is reversed in the southern hemisphere */
Fred Drake9bb74322002-04-01 14:49:59 +0000694 PyModule_AddIntConstant(m, "timezone", julyzone);
695 PyModule_AddIntConstant(m, "altzone", janzone);
696 PyModule_AddIntConstant(m, "daylight",
697 janzone != julyzone);
698 PyModule_AddObject(m, "tzname",
699 Py_BuildValue("(zz)",
700 julyname, janname));
Guido van Rossum57731601999-03-29 19:12:04 +0000701 } else {
Fred Drake9bb74322002-04-01 14:49:59 +0000702 PyModule_AddIntConstant(m, "timezone", janzone);
703 PyModule_AddIntConstant(m, "altzone", julyzone);
704 PyModule_AddIntConstant(m, "daylight",
705 janzone != julyzone);
706 PyModule_AddObject(m, "tzname",
707 Py_BuildValue("(zz)",
708 janname, julyname));
Guido van Rossum57731601999-03-29 19:12:04 +0000709 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000710 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000711#else
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000712#endif /* HAVE_STRUCT_TM_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000713#ifdef __CYGWIN__
714 tzset();
Fred Drake9bb74322002-04-01 14:49:59 +0000715 PyModule_AddIntConstant(m, "timezone", _timezone);
716 PyModule_AddIntConstant(m, "altzone", _timezone);
717 PyModule_AddIntConstant(m, "daylight", _daylight);
718 PyModule_AddObject(m, "tzname",
719 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +0000720#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000721#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000722}
723
724
725static PyMethodDef time_methods[] = {
726 {"time", time_time, METH_VARARGS, time_doc},
727#ifdef HAVE_CLOCK
728 {"clock", time_clock, METH_VARARGS, clock_doc},
729#endif
730 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
731 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
732 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
733 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
734 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
735#ifdef HAVE_MKTIME
736 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
737#endif
738#ifdef HAVE_STRFTIME
739 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
740#endif
741 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
742#ifdef HAVE_WORKING_TZSET
743 {"tzset", time_tzset, METH_VARARGS, tzset_doc},
744#endif
745 {NULL, NULL} /* sentinel */
746};
747
748
749PyDoc_STRVAR(module_doc,
750"This module provides various functions to manipulate time values.\n\
751\n\
752There are two standard representations of time. One is the number\n\
753of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
754or a floating point number (to represent fractions of seconds).\n\
755The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
756The actual value can be retrieved by calling gmtime(0).\n\
757\n\
758The other representation is a tuple of 9 integers giving local time.\n\
759The tuple items are:\n\
760 year (four digits, e.g. 1998)\n\
761 month (1-12)\n\
762 day (1-31)\n\
763 hours (0-23)\n\
764 minutes (0-59)\n\
765 seconds (0-59)\n\
766 weekday (0-6, Monday is 0)\n\
767 Julian day (day in the year, 1-366)\n\
768 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
769If the DST flag is 0, the time is given in the regular time zone;\n\
770if it is 1, the time is given in the DST time zone;\n\
771if it is -1, mktime() should guess based on the date and time.\n\
772\n\
773Variables:\n\
774\n\
775timezone -- difference in seconds between UTC and local standard time\n\
776altzone -- difference in seconds between UTC and local DST time\n\
777daylight -- whether local time should reflect DST\n\
778tzname -- tuple of (standard time zone name, DST time zone name)\n\
779\n\
780Functions:\n\
781\n\
782time() -- return current time in seconds since the Epoch as a float\n\
783clock() -- return CPU time since process start as a float\n\
784sleep() -- delay for a number of seconds given as a float\n\
785gmtime() -- convert seconds since Epoch to UTC tuple\n\
786localtime() -- convert seconds since Epoch to local time tuple\n\
787asctime() -- convert time tuple to string\n\
788ctime() -- convert time in seconds to string\n\
789mktime() -- convert local time tuple to seconds since Epoch\n\
790strftime() -- convert time tuple to string according to format specification\n\
791strptime() -- parse string to time tuple according to format specification\n\
792tzset() -- change the local timezone");
793
794
795PyMODINIT_FUNC
796inittime(void)
797{
798 PyObject *m;
799 char *p;
800 m = Py_InitModule3("time", time_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000801 if (m == NULL)
802 return;
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000803
804 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
805 p = Py_GETENV("PYTHONY2K");
806 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
807 /* Squirrel away the module's dictionary for the y2k check */
808 moddict = PyModule_GetDict(m);
809 Py_INCREF(moddict);
810
811 /* Set, or reset, module variables like time.timezone */
812 inittimezone(m);
813
Mark Hammond975e3922002-07-16 01:29:19 +0000814#ifdef MS_WINDOWS
815 /* Helper to allow interrupts for Windows.
816 If Ctrl+C event delivered while not sleeping
817 it will be ignored.
818 */
819 main_thread = PyThread_get_thread_ident();
820 hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
821 SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
822#endif /* MS_WINDOWS */
Martin v. Löwis19ab6c92006-04-16 18:55:50 +0000823 if (!initialized) {
824 PyStructSequence_InitType(&StructTimeType,
825 &struct_time_type_desc);
826 }
Fred Drake9bb74322002-04-01 14:49:59 +0000827 Py_INCREF(&StructTimeType);
828 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
Martin v. Löwis19ab6c92006-04-16 18:55:50 +0000829 initialized = 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000830}
831
832
Guido van Rossumb6775db1994-08-01 11:34:53 +0000833/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000834
Guido van Rossumb6775db1994-08-01 11:34:53 +0000835static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000836floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000837{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000838 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000839 (1) gettimeofday() -- resolution in microseconds
840 (2) ftime() -- resolution in milliseconds
841 (3) time() -- resolution in seconds
842 In all cases the return value is a float in seconds.
843 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
844 fail, so we fall back on ftime() or time().
845 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000846#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000847 {
848 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000849#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000850 if (gettimeofday(&t) == 0)
851 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000852#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000853 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
854 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000855#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000856 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +0000857
Guido van Rossumb6775db1994-08-01 11:34:53 +0000858#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000859 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000860#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000861 struct timeb t;
862 ftime(&t);
863 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000864#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000865 time_t secs;
866 time(&secs);
867 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000868#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000869 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000870}
871
Guido van Rossumb6775db1994-08-01 11:34:53 +0000872
873/* Implement floatsleep() for various platforms.
874 When interrupted (or when another error occurs), return -1 and
875 set an exception; else return 0. */
876
877static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000878floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000879{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000880/* XXX Should test for MS_WINDOWS first! */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000881#if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000882 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000883 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000884 frac = fmod(secs, 1.0);
885 secs = floor(secs);
886 t.tv_sec = (long)secs;
887 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000888 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000889 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000890#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000891 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000892#else
893 if (1) {
894#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000895 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000896 PyErr_SetFromErrno(PyExc_IOError);
897 return -1;
898 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000899 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000900 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000901#elif defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000902 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000903 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000904 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000905 Py_END_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000906#elif defined(MS_WINDOWS)
Fred Drake0e123952000-06-29 21:31:02 +0000907 {
908 double millisecs = secs * 1000.0;
Tim Peters513a1cd2003-01-19 04:54:58 +0000909 unsigned long ul_millis;
910
Fred Drake0e123952000-06-29 21:31:02 +0000911 if (millisecs > (double)ULONG_MAX) {
Tim Peters513a1cd2003-01-19 04:54:58 +0000912 PyErr_SetString(PyExc_OverflowError,
913 "sleep length is too large");
Fred Drake0e123952000-06-29 21:31:02 +0000914 return -1;
915 }
Fred Drake0e123952000-06-29 21:31:02 +0000916 Py_BEGIN_ALLOW_THREADS
Tim Peters513a1cd2003-01-19 04:54:58 +0000917 /* Allow sleep(0) to maintain win32 semantics, and as decreed
918 * by Guido, only the main thread can be interrupted.
919 */
920 ul_millis = (unsigned long)millisecs;
921 if (ul_millis == 0 ||
922 main_thread != PyThread_get_thread_ident())
923 Sleep(ul_millis);
Mark Hammond975e3922002-07-16 01:29:19 +0000924 else {
925 DWORD rc;
926 ResetEvent(hInterruptEvent);
Tim Peters513a1cd2003-01-19 04:54:58 +0000927 rc = WaitForSingleObject(hInterruptEvent, ul_millis);
928 if (rc == WAIT_OBJECT_0) {
929 /* Yield to make sure real Python signal
930 * handler called.
931 */
Mark Hammond975e3922002-07-16 01:29:19 +0000932 Sleep(1);
933 Py_BLOCK_THREADS
Mark Hammond975e3922002-07-16 01:29:19 +0000934 errno = EINTR;
935 PyErr_SetFromErrno(PyExc_IOError);
936 return -1;
937 }
938 }
Fred Drake0e123952000-06-29 21:31:02 +0000939 Py_END_ALLOW_THREADS
940 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000941#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000942 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000943 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000944 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000945 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000946 PyErr_SetFromErrno(PyExc_IOError);
947 return -1;
948 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000949 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000950#elif defined(__BEOS__)
Guido van Rossumbcc20741998-08-04 22:53:56 +0000951 /* This sleep *CAN BE* interrupted. */
952 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000953 if( secs <= 0.0 ) {
954 return;
955 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000956
Guido van Rossumbcc20741998-08-04 22:53:56 +0000957 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000958 /* BeOS snooze() is in microseconds... */
959 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000960 Py_BLOCK_THREADS
961 PyErr_SetFromErrno( PyExc_IOError );
962 return -1;
963 }
964 Py_END_ALLOW_THREADS
965 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000966#elif defined(RISCOS)
Guido van Rossumbceccf52001-04-10 22:07:43 +0000967 if (secs <= 0.0)
968 return 0;
969 Py_BEGIN_ALLOW_THREADS
970 /* This sleep *CAN BE* interrupted. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000971 if ( riscos_sleep(secs) )
Guido van Rossumbceccf52001-04-10 22:07:43 +0000972 return -1;
973 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000974#elif defined(PLAN9)
975 {
976 double millisecs = secs * 1000.0;
977 if (millisecs > (double)LONG_MAX) {
978 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
979 return -1;
980 }
981 /* This sleep *CAN BE* interrupted. */
982 Py_BEGIN_ALLOW_THREADS
983 if(sleep((long)millisecs) < 0){
984 Py_BLOCK_THREADS
985 PyErr_SetFromErrno(PyExc_IOError);
986 return -1;
987 }
988 Py_END_ALLOW_THREADS
989 }
990#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000991 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000992 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000993 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000994 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000995#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000996
Guido van Rossumb6775db1994-08-01 11:34:53 +0000997 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000998}
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000999
1000