blob: ebea4fc6aee789b0efc9f9058ae4857426b6e2f4 [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
Thomas Wouters477c8d52006-05-27 19:21:47 +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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000022#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000023#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000024#endif /* HAVE_SYS_TYPES_H */
Guido van Rossum6d946f91992-08-14 13:49:30 +000025
Guido van Rossumb6775db1994-08-01 11:34:53 +000026#ifdef QUICKWIN
27#include <io.h>
28#endif
29
Guido van Rossumb6775db1994-08-01 11:34:53 +000030#ifdef HAVE_FTIME
31#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000032#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000033extern int ftime(struct timeb *);
Guido van Rossum52174571996-12-09 18:38:52 +000034#endif /* MS_WINDOWS */
35#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036
Guido van Rossum7bf22de1997-12-02 20:34:19 +000037#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000038#include <i86.h>
39#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000040#ifdef MS_WINDOWS
Mark Hammond975e3922002-07-16 01:29:19 +000041#define WIN32_LEAN_AND_MEAN
Guido van Rossum258ccd42001-03-02 06:53:29 +000042#include <windows.h>
Mark Hammond975e3922002-07-16 01:29:19 +000043#include "pythread.h"
44
45/* helper to allow us to interrupt sleep() on Windows*/
46static HANDLE hInterruptEvent = NULL;
47static BOOL WINAPI PyCtrlHandler(DWORD dwCtrlType)
48{
49 SetEvent(hInterruptEvent);
50 /* allow other default handlers to be called.
51 Default Python handler will setup the
52 KeyboardInterrupt exception.
53 */
54 return FALSE;
55}
56static long main_thread;
57
Martin v. Löwis3bb00702007-08-30 14:37:48 +000058#define TZNAME_ENCODING "utf-8"
Mark Hammond975e3922002-07-16 01:29:19 +000059
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000060#if defined(__BORLANDC__)
Guido van Rossumb2fb3641996-09-07 00:47:35 +000061/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000062#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000063#define tzname _tzname
64#define daylight _daylight
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000065#endif /* __BORLANDC__ */
Guido van Rossumcac6c721996-09-06 13:34:02 +000066#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000067#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000068
Thomas Wouters477c8d52006-05-27 19:21:47 +000069#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
70/* Win32 has better clock replacement; we have our own version below. */
71#undef HAVE_CLOCK
Martin v. Löwis3bb00702007-08-30 14:37:48 +000072#undef TZNAME_ENCODING
73#define TZNAME_ENCODING "mbcs"
Thomas Wouters477c8d52006-05-27 19:21:47 +000074#endif /* MS_WINDOWS && !defined(__BORLANDC__) */
Guido van Rossum3917c221997-04-02 05:35:28 +000075
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000076#if defined(PYOS_OS2)
77#define INCL_DOS
78#define INCL_ERRORS
79#include <os2.h>
80#endif
81
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000082#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000083#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000084#endif
85
Guido van Rossum234f9421993-06-17 12:35:49 +000086/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000087static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000088static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000090/* For Y2K check */
91static PyObject *moddict;
92
Tim Peters1b6f7a92004-06-20 02:50:16 +000093/* Exposed in timefuncs.h. */
94time_t
Brett Cannon298c3802004-06-19 20:48:43 +000095_PyTime_DoubleToTimet(double x)
96{
97 time_t result;
98 double diff;
99
100 result = (time_t)x;
101 /* How much info did we lose? time_t may be an integral or
102 * floating type, and we don't know which. If it's integral,
103 * we don't know whether C truncates, rounds, returns the floor,
104 * etc. If we lost a second or more, the C rounding is
105 * unreasonable, or the input just doesn't fit in a time_t;
106 * call it an error regardless. Note that the original cast to
107 * time_t can cause a C error too, but nothing we can do to
108 * worm around that.
109 */
110 diff = x - (double)result;
111 if (diff <= -1.0 || diff >= 1.0) {
112 PyErr_SetString(PyExc_ValueError,
113 "timestamp out of range for platform time_t");
114 result = (time_t)-1;
115 }
116 return result;
117}
118
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000119static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000120time_time(PyObject *self, PyObject *unused)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000122 double secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000123 secs = floattime();
124 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000125 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000126 return NULL;
127 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000128 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000129}
130
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000131PyDoc_STRVAR(time_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000132"time() -> floating point number\n\
133\n\
134Return the current time in seconds since the Epoch.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000135Fractions of a second may be present if the system clock provides them.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000136
Guido van Rossumb6775db1994-08-01 11:34:53 +0000137#ifdef HAVE_CLOCK
138
139#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000140#ifdef CLK_TCK
141#define CLOCKS_PER_SEC CLK_TCK
142#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000143#define CLOCKS_PER_SEC 1000000
144#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000145#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000146
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000147static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000148time_clock(PyObject *self, PyObject *unused)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000149{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000150 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000152#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153
Thomas Wouters477c8d52006-05-27 19:21:47 +0000154#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
Mark Hammond7ba5e812002-02-12 04:02:33 +0000155/* Due to Mark Hammond and Tim Peters */
Guido van Rossum3917c221997-04-02 05:35:28 +0000156static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000157time_clock(PyObject *self, PyObject *unused)
Guido van Rossum3917c221997-04-02 05:35:28 +0000158{
Tim Peters9ad4b682002-02-13 05:14:18 +0000159 static LARGE_INTEGER ctrStart;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000160 static double divisor = 0.0;
Tim Peters9ad4b682002-02-13 05:14:18 +0000161 LARGE_INTEGER now;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000162 double diff;
Guido van Rossum3917c221997-04-02 05:35:28 +0000163
Mark Hammond7ba5e812002-02-12 04:02:33 +0000164 if (divisor == 0.0) {
Tim Peters9ad4b682002-02-13 05:14:18 +0000165 LARGE_INTEGER freq;
166 QueryPerformanceCounter(&ctrStart);
167 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
Mark Hammond7ba5e812002-02-12 04:02:33 +0000168 /* Unlikely to happen - this works on all intel
169 machines at least! Revert to clock() */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000170 return PyFloat_FromDouble(((double)clock()) /
171 CLOCKS_PER_SEC);
Guido van Rossum3917c221997-04-02 05:35:28 +0000172 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000173 divisor = (double)freq.QuadPart;
Guido van Rossum3917c221997-04-02 05:35:28 +0000174 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000175 QueryPerformanceCounter(&now);
176 diff = (double)(now.QuadPart - ctrStart.QuadPart);
Mark Hammond7ba5e812002-02-12 04:02:33 +0000177 return PyFloat_FromDouble(diff / divisor);
Guido van Rossum3917c221997-04-02 05:35:28 +0000178}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000179
Guido van Rossum3917c221997-04-02 05:35:28 +0000180#define HAVE_CLOCK /* So it gets included in the methods */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000181#endif /* MS_WINDOWS && !defined(__BORLANDC__) */
Guido van Rossum3917c221997-04-02 05:35:28 +0000182
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000183#ifdef HAVE_CLOCK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000184PyDoc_STRVAR(clock_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000185"clock() -> floating point number\n\
186\n\
187Return the CPU time or real time since the start of the process or since\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000188the first call to clock(). This has as much precision as the system\n\
189records.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000190#endif
191
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000192static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000193time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000195 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000196 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000198 if (floatsleep(secs) != 0)
199 return NULL;
200 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000201 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000202}
203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000204PyDoc_STRVAR(sleep_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000205"sleep(seconds)\n\
206\n\
207Delay execution for a given number of seconds. The argument may be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000208a floating point number for subsecond precision.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000209
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000210static PyStructSequence_Field struct_time_type_fields[] = {
211 {"tm_year", NULL},
212 {"tm_mon", NULL},
213 {"tm_mday", NULL},
214 {"tm_hour", NULL},
215 {"tm_min", NULL},
216 {"tm_sec", NULL},
217 {"tm_wday", NULL},
218 {"tm_yday", NULL},
219 {"tm_isdst", NULL},
220 {0}
221};
222
223static PyStructSequence_Desc struct_time_type_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000224 "time.struct_time",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000225 NULL,
226 struct_time_type_fields,
227 9,
228};
Tim Peters9ad4b682002-02-13 05:14:18 +0000229
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000230static int initialized;
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 *
Skip Montanaro41cfce92007-08-24 21:11:00 +0000261structtime_totuple(PyObject *t)
262{
263 PyObject *x = NULL;
264 unsigned int i;
265 PyObject *v = PyTuple_New(9);
266 if (v == NULL)
267 return NULL;
268
269 for (i=0; i<9; i++) {
270 x = PyStructSequence_GET_ITEM(t, i);
271 Py_INCREF(x);
272 PyTuple_SET_ITEM(v, i, x);
273 }
274
275 if (PyErr_Occurred()) {
276 Py_XDECREF(v);
277 return NULL;
278 }
279
280 return v;
281}
282
283static PyObject *
Brett Cannon298c3802004-06-19 20:48:43 +0000284time_convert(double when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000285{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000286 struct tm *p;
Brett Cannon298c3802004-06-19 20:48:43 +0000287 time_t whent = _PyTime_DoubleToTimet(when);
288
289 if (whent == (time_t)-1 && PyErr_Occurred())
290 return NULL;
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000291 errno = 0;
Brett Cannon298c3802004-06-19 20:48:43 +0000292 p = function(&whent);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000293 if (p == NULL) {
294#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000295 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000296 errno = EINVAL;
297#endif
Tim Peters8b19a932003-01-17 20:08:54 +0000298 return PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000299 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000300 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000301}
302
Fred Drakef901abd2004-08-03 17:58:55 +0000303/* Parse arg tuple that can contain an optional float-or-None value;
304 format needs to be "|O:name".
305 Returns non-zero on success (parallels PyArg_ParseTuple).
306*/
307static int
308parse_time_double_args(PyObject *args, char *format, double *pwhen)
309{
310 PyObject *ot = NULL;
311
312 if (!PyArg_ParseTuple(args, format, &ot))
313 return 0;
314 if (ot == NULL || ot == Py_None)
315 *pwhen = floattime();
316 else {
317 double when = PyFloat_AsDouble(ot);
318 if (PyErr_Occurred())
319 return 0;
320 *pwhen = when;
321 }
322 return 1;
323}
324
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000325static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000326time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000327{
328 double when;
Fred Drakef901abd2004-08-03 17:58:55 +0000329 if (!parse_time_double_args(args, "|O:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000330 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000331 return time_convert(when, gmtime);
Guido van Rossum234f9421993-06-17 12:35:49 +0000332}
333
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000334PyDoc_STRVAR(gmtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000335"gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,\n\
336 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000337\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000338Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000339GMT). When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000340
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000341static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000342time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000343{
344 double when;
Fred Drakef901abd2004-08-03 17:58:55 +0000345 if (!parse_time_double_args(args, "|O:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000346 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000347 return time_convert(when, localtime);
Guido van Rossum234f9421993-06-17 12:35:49 +0000348}
349
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000350PyDoc_STRVAR(localtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000351"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 +0000352\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000353Convert seconds since the Epoch to a time tuple expressing local time.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000354When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000355
Guido van Rossum9e90a671993-06-24 11:10:19 +0000356static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000357gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000358{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000359 int y;
Skip Montanaro41cfce92007-08-24 21:11:00 +0000360 PyObject *t = NULL;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000361
Guido van Rossumb9081262007-08-25 03:14:09 +0000362 memset((void *) p, '\0', sizeof(struct tm));
363
Skip Montanaro41cfce92007-08-24 21:11:00 +0000364 if (PyTuple_Check(args)) {
365 t = args;
366 Py_INCREF(t);
367 }
368 else if (Py_Type(args) == &StructTimeType) {
369 t = structtime_totuple(args);
370 }
371 else {
372 PyErr_SetString(PyExc_TypeError,
373 "Tuple or struct_time argument required");
Guido van Rossum9e90a671993-06-24 11:10:19 +0000374 return 0;
Skip Montanaro41cfce92007-08-24 21:11:00 +0000375 }
376
377 if (t == NULL || !PyArg_ParseTuple(t, "iiiiiiiii",
378 &y,
379 &p->tm_mon,
380 &p->tm_mday,
381 &p->tm_hour,
382 &p->tm_min,
383 &p->tm_sec,
384 &p->tm_wday,
385 &p->tm_yday,
386 &p->tm_isdst)) {
387 Py_XDECREF(t);
388 return 0;
389 }
390 Py_DECREF(t);
391
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000392 if (y < 1900) {
393 PyObject *accept = PyDict_GetItemString(moddict,
394 "accept2dyear");
Guido van Rossumddefaf32007-01-14 03:31:43 +0000395 if (accept == NULL || !PyInt_CheckExact(accept) ||
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000396 PyInt_AsLong(accept) == 0) {
397 PyErr_SetString(PyExc_ValueError,
398 "year >= 1900 required");
399 return 0;
400 }
401 if (69 <= y && y <= 99)
402 y += 1900;
403 else if (0 <= y && y <= 68)
404 y += 2000;
405 else {
406 PyErr_SetString(PyExc_ValueError,
Skip Montanaro1a10aac2001-08-22 12:39:16 +0000407 "year out of range");
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000408 return 0;
409 }
410 }
411 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000412 p->tm_mon--;
413 p->tm_wday = (p->tm_wday + 1) % 7;
414 p->tm_yday--;
415 return 1;
416}
417
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000418#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000419static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000420time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000421{
Thomas Woutersfe385252001-01-19 23:16:56 +0000422 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000423 struct tm buf;
424 const char *fmt;
Guido van Rossumeda12ec2007-08-24 03:51:52 +0000425 PyObject *format;
Guido van Rossumfa481162000-06-28 21:33:59 +0000426 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000427 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000428 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000429
Thomas Wouters334fb892000-07-25 12:56:38 +0000430 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000431
Guido van Rossumeda12ec2007-08-24 03:51:52 +0000432 /* Will always expect a unicode string to be passed as format.
433 Given that there's no str type anymore in py3k this seems safe.
434 */
435 if (!PyArg_ParseTuple(args, "U|O:strftime", &format, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000436 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000437
438 if (tup == NULL) {
439 time_t tt = time(NULL);
440 buf = *localtime(&tt);
441 } else if (!gettmarg(tup, &buf))
442 return NULL;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000443
Brett Cannond1080a32004-03-02 04:38:10 +0000444 /* Checks added to make sure strftime() does not crash Python by
445 indexing blindly into some array for a textual representation
446 by some bad index (fixes bug #897625).
Tim Peters1b6f7a92004-06-20 02:50:16 +0000447
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000448 Also support values of zero from Python code for arguments in which
449 that is out of range by forcing that value to the lowest value that
450 is valid (fixed bug #1520914).
451
452 Valid ranges based on what is allowed in struct tm:
453
454 - tm_year: [0, max(int)] (1)
455 - tm_mon: [0, 11] (2)
456 - tm_mday: [1, 31]
457 - tm_hour: [0, 23]
458 - tm_min: [0, 59]
459 - tm_sec: [0, 60]
460 - tm_wday: [0, 6] (1)
461 - tm_yday: [0, 365] (2)
462 - tm_isdst: [-max(int), max(int)]
463
464 (1) gettmarg() handles bounds-checking.
465 (2) Python's acceptable range is one greater than the range in C,
466 thus need to check against automatic decrement by gettmarg().
Brett Cannond1080a32004-03-02 04:38:10 +0000467 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000468 if (buf.tm_mon == -1)
469 buf.tm_mon = 0;
470 else if (buf.tm_mon < 0 || buf.tm_mon > 11) {
Brett Cannond1080a32004-03-02 04:38:10 +0000471 PyErr_SetString(PyExc_ValueError, "month out of range");
472 return NULL;
473 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000474 if (buf.tm_mday == 0)
475 buf.tm_mday = 1;
476 else if (buf.tm_mday < 0 || buf.tm_mday > 31) {
Brett Cannond1080a32004-03-02 04:38:10 +0000477 PyErr_SetString(PyExc_ValueError, "day of month out of range");
478 return NULL;
479 }
480 if (buf.tm_hour < 0 || buf.tm_hour > 23) {
481 PyErr_SetString(PyExc_ValueError, "hour out of range");
482 return NULL;
483 }
484 if (buf.tm_min < 0 || buf.tm_min > 59) {
485 PyErr_SetString(PyExc_ValueError, "minute out of range");
486 return NULL;
487 }
488 if (buf.tm_sec < 0 || buf.tm_sec > 61) {
489 PyErr_SetString(PyExc_ValueError, "seconds out of range");
490 return NULL;
491 }
492 /* tm_wday does not need checking of its upper-bound since taking
493 ``% 7`` in gettmarg() automatically restricts the range. */
494 if (buf.tm_wday < 0) {
495 PyErr_SetString(PyExc_ValueError, "day of week out of range");
496 return NULL;
497 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000498 if (buf.tm_yday == -1)
499 buf.tm_yday = 0;
500 else if (buf.tm_yday < 0 || buf.tm_yday > 365) {
Brett Cannond1080a32004-03-02 04:38:10 +0000501 PyErr_SetString(PyExc_ValueError, "day of year out of range");
502 return NULL;
503 }
504 if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
505 PyErr_SetString(PyExc_ValueError,
506 "daylight savings flag out of range");
507 return NULL;
508 }
509
Guido van Rossumeda12ec2007-08-24 03:51:52 +0000510 /* Convert the unicode string to an ascii one */
511 fmt = PyUnicode_AsString(format);
512
Guido van Rossumc222ec21999-02-23 00:00:10 +0000513 fmtlen = strlen(fmt);
514
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000515 /* I hate these functions that presume you know how big the output
516 * will be ahead of time...
517 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000518 for (i = 1024; ; i += i) {
Walter Dörwaldcf47af42007-05-31 19:23:17 +0000519 outbuf = (char *)PyMem_Malloc(i);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000520 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000521 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000522 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000523 buflen = strftime(outbuf, i, fmt, &buf);
524 if (buflen > 0 || i >= 256 * fmtlen) {
525 /* If the buffer is 256 times as long as the format,
526 it's probably not failing for lack of room!
527 More likely, the format yields an empty result,
528 e.g. an empty format, or %Z when the timezone
529 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000530 PyObject *ret;
Walter Dörwaldcf47af42007-05-31 19:23:17 +0000531 ret = PyUnicode_FromStringAndSize(outbuf, buflen);
532 PyMem_Free(outbuf);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000533 return ret;
534 }
Walter Dörwaldcf47af42007-05-31 19:23:17 +0000535 PyMem_Free(outbuf);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000536#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
537 /* VisualStudio .NET 2005 does this properly */
538 if (buflen == 0 && errno == EINVAL) {
539 PyErr_SetString(PyExc_ValueError, "Invalid format string");
540 return 0;
541 }
542#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000543 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000544}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000545
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000546PyDoc_STRVAR(strftime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000547"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000548\n\
549Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000550See the library reference manual for formatting codes. When the time tuple\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000551is not present, current time as returned by localtime() is used.");
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000552#endif /* HAVE_STRFTIME */
553
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000554static PyObject *
555time_strptime(PyObject *self, PyObject *args)
556{
557 PyObject *strptime_module = PyImport_ImportModule("_strptime");
Raymond Hettinger502168a2003-04-10 16:03:22 +0000558 PyObject *strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000559
Tim Peters513a1cd2003-01-19 04:54:58 +0000560 if (!strptime_module)
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000561 return NULL;
Raymond Hettinger502168a2003-04-10 16:03:22 +0000562 strptime_result = PyObject_CallMethod(strptime_module, "strptime", "O", args);
563 Py_DECREF(strptime_module);
564 return strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000565}
566
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000567PyDoc_STRVAR(strptime_doc,
Brett Cannon20def8b2003-07-01 05:16:08 +0000568"strptime(string, format) -> struct_time\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000569\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000570Parse a string to a time tuple according to a format specification.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000571See the library reference manual for formatting codes (same as strftime()).");
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000572
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000573
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000574static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000575time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000576{
Thomas Woutersfe385252001-01-19 23:16:56 +0000577 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000578 struct tm buf;
579 char *p;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000580 if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000581 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000582 if (tup == NULL) {
583 time_t tt = time(NULL);
584 buf = *localtime(&tt);
585 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000586 return NULL;
587 p = asctime(&buf);
588 if (p[24] == '\n')
589 p[24] = '\0';
Guido van Rossumeda12ec2007-08-24 03:51:52 +0000590 return PyUnicode_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000591}
592
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000593PyDoc_STRVAR(asctime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000594"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000595\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000596Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
597When the time tuple is not present, current time as returned by localtime()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000598is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000599
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000600static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000601time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000602{
Fred Drakef901abd2004-08-03 17:58:55 +0000603 PyObject *ot = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000604 time_t tt;
605 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000606
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000607 if (!PyArg_UnpackTuple(args, "ctime", 0, 1, &ot))
Fred Drakef901abd2004-08-03 17:58:55 +0000608 return NULL;
609 if (ot == NULL || ot == Py_None)
Thomas Woutersfe385252001-01-19 23:16:56 +0000610 tt = time(NULL);
611 else {
Fred Drakef901abd2004-08-03 17:58:55 +0000612 double dt = PyFloat_AsDouble(ot);
613 if (PyErr_Occurred())
Thomas Woutersfe385252001-01-19 23:16:56 +0000614 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000615 tt = _PyTime_DoubleToTimet(dt);
616 if (tt == (time_t)-1 && PyErr_Occurred())
617 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000618 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000619 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000620 if (p == NULL) {
621 PyErr_SetString(PyExc_ValueError, "unconvertible time");
622 return NULL;
623 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000624 if (p[24] == '\n')
625 p[24] = '\0';
Guido van Rossumeda12ec2007-08-24 03:51:52 +0000626 return PyUnicode_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000627}
628
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000629PyDoc_STRVAR(ctime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000630"ctime(seconds) -> string\n\
631\n\
632Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000633This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000634not present, current time as returned by localtime() is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000635
Guido van Rossum60cd8131998-03-06 17:16:21 +0000636#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000637static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000638time_mktime(PyObject *self, PyObject *tup)
Guido van Rossum234f9421993-06-17 12:35:49 +0000639{
640 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000641 time_t tt;
642 tt = time(&tt);
643 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000644 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000645 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000646 tt = mktime(&buf);
647 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000648 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000649 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000650 return NULL;
651 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000652 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000653}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000654
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000655PyDoc_STRVAR(mktime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000656"mktime(tuple) -> floating point number\n\
657\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000658Convert a time tuple in local time to seconds since the Epoch.");
Guido van Rossum60cd8131998-03-06 17:16:21 +0000659#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000660
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000661#ifdef HAVE_WORKING_TZSET
662void inittimezone(PyObject *module);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000663
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000664static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000665time_tzset(PyObject *self, PyObject *unused)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000666{
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000667 PyObject* m;
Fred Drake9bb74322002-04-01 14:49:59 +0000668
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000669 m = PyImport_ImportModule("time");
670 if (m == NULL) {
671 return NULL;
672 }
673
674 tzset();
675
676 /* Reset timezone, altzone, daylight and tzname */
677 inittimezone(m);
678 Py_DECREF(m);
Tim Peters1b6f7a92004-06-20 02:50:16 +0000679
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000680 Py_INCREF(Py_None);
681 return Py_None;
682}
683
684PyDoc_STRVAR(tzset_doc,
685"tzset(zone)\n\
686\n\
687Initialize, or reinitialize, the local timezone to the value stored in\n\
688os.environ['TZ']. The TZ environment variable should be specified in\n\
Neal Norwitzdc8e1942004-07-20 22:34:37 +0000689standard Unix timezone format as documented in the tzset man page\n\
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000690(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
691fall back to UTC. If the TZ environment variable is not set, the local\n\
692timezone is set to the systems best guess of wallclock time.\n\
693Changing the TZ environment variable without calling tzset *may* change\n\
694the local timezone used by methods such as localtime, but this behaviour\n\
695should not be relied on.");
696#endif /* HAVE_WORKING_TZSET */
697
698void inittimezone(PyObject *m) {
699 /* This code moved from inittime wholesale to allow calling it from
700 time_tzset. In the future, some parts of it can be moved back
701 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
702 are), and the extranious calls to tzset(3) should be removed.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000703 I haven't done this yet, as I don't want to change this code as
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000704 little as possible when introducing the time.tzset and time.tzsetwall
705 methods. This should simply be a method of doing the following once,
706 at the top of this function and removing the call to tzset() from
707 time_tzset():
708
709 #ifdef HAVE_TZSET
710 tzset()
711 #endif
712
713 And I'm lazy and hate C so nyer.
714 */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000715#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Martin v. Löwis3bb00702007-08-30 14:37:48 +0000716 PyObject *otz0, *otz1;
Guido van Rossum234f9421993-06-17 12:35:49 +0000717 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000718#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000719 PyModule_AddIntConstant(m, "timezone", _timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000720#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000721 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000722#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000723#ifdef HAVE_ALTZONE
Fred Drake9bb74322002-04-01 14:49:59 +0000724 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000725#else
Guido van Rossum26452411998-09-28 22:07:11 +0000726#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000727 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000728#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000729 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000730#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000731#endif
Fred Drake9bb74322002-04-01 14:49:59 +0000732 PyModule_AddIntConstant(m, "daylight", daylight);
Martin v. Löwis3bb00702007-08-30 14:37:48 +0000733 otz0 = PyUnicode_Decode(tzname[0], strlen(tzname[0]), TZNAME_ENCODING, NULL);
734 otz1 = PyUnicode_Decode(tzname[1], strlen(tzname[1]), TZNAME_ENCODING, NULL);
735 PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000736#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000737#ifdef HAVE_STRUCT_TM_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000738 {
739#define YEAR ((time_t)((365 * 24 + 6) * 3600))
740 time_t t;
741 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000742 long janzone, julyzone;
743 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000744 t = (time((time_t *)0) / YEAR) * YEAR;
745 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000746 janzone = -p->tm_gmtoff;
747 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
748 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000749 t += YEAR/2;
750 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000751 julyzone = -p->tm_gmtoff;
752 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
753 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000754
Guido van Rossum57731601999-03-29 19:12:04 +0000755 if( janzone < julyzone ) {
756 /* DST is reversed in the southern hemisphere */
Fred Drake9bb74322002-04-01 14:49:59 +0000757 PyModule_AddIntConstant(m, "timezone", julyzone);
758 PyModule_AddIntConstant(m, "altzone", janzone);
759 PyModule_AddIntConstant(m, "daylight",
760 janzone != julyzone);
761 PyModule_AddObject(m, "tzname",
762 Py_BuildValue("(zz)",
763 julyname, janname));
Guido van Rossum57731601999-03-29 19:12:04 +0000764 } else {
Fred Drake9bb74322002-04-01 14:49:59 +0000765 PyModule_AddIntConstant(m, "timezone", janzone);
766 PyModule_AddIntConstant(m, "altzone", julyzone);
767 PyModule_AddIntConstant(m, "daylight",
768 janzone != julyzone);
769 PyModule_AddObject(m, "tzname",
770 Py_BuildValue("(zz)",
771 janname, julyname));
Guido van Rossum57731601999-03-29 19:12:04 +0000772 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000773 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000774#else
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000775#endif /* HAVE_STRUCT_TM_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000776#ifdef __CYGWIN__
777 tzset();
Fred Drake9bb74322002-04-01 14:49:59 +0000778 PyModule_AddIntConstant(m, "timezone", _timezone);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000779 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Fred Drake9bb74322002-04-01 14:49:59 +0000780 PyModule_AddIntConstant(m, "daylight", _daylight);
781 PyModule_AddObject(m, "tzname",
782 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +0000783#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000784#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000785}
786
787
788static PyMethodDef time_methods[] = {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000789 {"time", time_time, METH_NOARGS, time_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000790#ifdef HAVE_CLOCK
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000791 {"clock", time_clock, METH_NOARGS, clock_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000792#endif
793 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
794 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
795 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
796 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
797 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
798#ifdef HAVE_MKTIME
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000799 {"mktime", time_mktime, METH_O, mktime_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000800#endif
801#ifdef HAVE_STRFTIME
802 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
803#endif
804 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
805#ifdef HAVE_WORKING_TZSET
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000806 {"tzset", time_tzset, METH_NOARGS, tzset_doc},
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000807#endif
808 {NULL, NULL} /* sentinel */
809};
810
811
812PyDoc_STRVAR(module_doc,
813"This module provides various functions to manipulate time values.\n\
814\n\
815There are two standard representations of time. One is the number\n\
816of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
817or a floating point number (to represent fractions of seconds).\n\
818The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
819The actual value can be retrieved by calling gmtime(0).\n\
820\n\
821The other representation is a tuple of 9 integers giving local time.\n\
822The tuple items are:\n\
823 year (four digits, e.g. 1998)\n\
824 month (1-12)\n\
825 day (1-31)\n\
826 hours (0-23)\n\
827 minutes (0-59)\n\
828 seconds (0-59)\n\
829 weekday (0-6, Monday is 0)\n\
830 Julian day (day in the year, 1-366)\n\
831 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
832If the DST flag is 0, the time is given in the regular time zone;\n\
833if it is 1, the time is given in the DST time zone;\n\
834if it is -1, mktime() should guess based on the date and time.\n\
835\n\
836Variables:\n\
837\n\
838timezone -- difference in seconds between UTC and local standard time\n\
839altzone -- difference in seconds between UTC and local DST time\n\
840daylight -- whether local time should reflect DST\n\
841tzname -- tuple of (standard time zone name, DST time zone name)\n\
842\n\
843Functions:\n\
844\n\
845time() -- return current time in seconds since the Epoch as a float\n\
846clock() -- return CPU time since process start as a float\n\
847sleep() -- delay for a number of seconds given as a float\n\
848gmtime() -- convert seconds since Epoch to UTC tuple\n\
849localtime() -- convert seconds since Epoch to local time tuple\n\
850asctime() -- convert time tuple to string\n\
851ctime() -- convert time in seconds to string\n\
852mktime() -- convert local time tuple to seconds since Epoch\n\
853strftime() -- convert time tuple to string according to format specification\n\
854strptime() -- parse string to time tuple according to format specification\n\
855tzset() -- change the local timezone");
856
857
858PyMODINIT_FUNC
859inittime(void)
860{
861 PyObject *m;
862 char *p;
863 m = Py_InitModule3("time", time_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000864 if (m == NULL)
865 return;
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000866
867 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
868 p = Py_GETENV("PYTHONY2K");
869 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
870 /* Squirrel away the module's dictionary for the y2k check */
871 moddict = PyModule_GetDict(m);
872 Py_INCREF(moddict);
873
874 /* Set, or reset, module variables like time.timezone */
875 inittimezone(m);
876
Mark Hammond975e3922002-07-16 01:29:19 +0000877#ifdef MS_WINDOWS
878 /* Helper to allow interrupts for Windows.
879 If Ctrl+C event delivered while not sleeping
880 it will be ignored.
881 */
882 main_thread = PyThread_get_thread_ident();
883 hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
884 SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
885#endif /* MS_WINDOWS */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000886 if (!initialized) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000887 PyStructSequence_InitType(&StructTimeType,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000888 &struct_time_type_desc);
889 }
Fred Drake9bb74322002-04-01 14:49:59 +0000890 Py_INCREF(&StructTimeType);
891 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000892 initialized = 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000893}
894
895
Guido van Rossumb6775db1994-08-01 11:34:53 +0000896/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000897
Guido van Rossumb6775db1994-08-01 11:34:53 +0000898static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000899floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000900{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000901 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000902 (1) gettimeofday() -- resolution in microseconds
903 (2) ftime() -- resolution in milliseconds
904 (3) time() -- resolution in seconds
905 In all cases the return value is a float in seconds.
906 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
907 fail, so we fall back on ftime() or time().
908 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000909#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000910 {
911 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000912#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000913 if (gettimeofday(&t) == 0)
914 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000915#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000916 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
917 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000918#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000919 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000920
Guido van Rossumb6775db1994-08-01 11:34:53 +0000921#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000922 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000923#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000924 struct timeb t;
925 ftime(&t);
926 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000927#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000928 time_t secs;
929 time(&secs);
930 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000931#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000932 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000933}
934
Guido van Rossumb6775db1994-08-01 11:34:53 +0000935
936/* Implement floatsleep() for various platforms.
937 When interrupted (or when another error occurs), return -1 and
938 set an exception; else return 0. */
939
940static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000941floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000942{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000943/* XXX Should test for MS_WINDOWS first! */
Skip Montanaroeb33e5a2007-08-17 12:57:41 +0000944#if defined(HAVE_SELECT) && !defined(__EMX__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000945 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000946 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000947 frac = fmod(secs, 1.0);
948 secs = floor(secs);
949 t.tv_sec = (long)secs;
950 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000951 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000952 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000953#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000954 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000955#else
956 if (1) {
957#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000958 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000959 PyErr_SetFromErrno(PyExc_IOError);
960 return -1;
961 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000962 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000963 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000964#elif defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000965 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000966 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000967 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000968 Py_END_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000969#elif defined(MS_WINDOWS)
Fred Drake0e123952000-06-29 21:31:02 +0000970 {
971 double millisecs = secs * 1000.0;
Tim Peters513a1cd2003-01-19 04:54:58 +0000972 unsigned long ul_millis;
973
Fred Drake0e123952000-06-29 21:31:02 +0000974 if (millisecs > (double)ULONG_MAX) {
Tim Peters513a1cd2003-01-19 04:54:58 +0000975 PyErr_SetString(PyExc_OverflowError,
976 "sleep length is too large");
Fred Drake0e123952000-06-29 21:31:02 +0000977 return -1;
978 }
Fred Drake0e123952000-06-29 21:31:02 +0000979 Py_BEGIN_ALLOW_THREADS
Tim Peters513a1cd2003-01-19 04:54:58 +0000980 /* Allow sleep(0) to maintain win32 semantics, and as decreed
981 * by Guido, only the main thread can be interrupted.
982 */
983 ul_millis = (unsigned long)millisecs;
984 if (ul_millis == 0 ||
985 main_thread != PyThread_get_thread_ident())
986 Sleep(ul_millis);
Mark Hammond975e3922002-07-16 01:29:19 +0000987 else {
988 DWORD rc;
989 ResetEvent(hInterruptEvent);
Tim Peters513a1cd2003-01-19 04:54:58 +0000990 rc = WaitForSingleObject(hInterruptEvent, ul_millis);
991 if (rc == WAIT_OBJECT_0) {
992 /* Yield to make sure real Python signal
993 * handler called.
994 */
Mark Hammond975e3922002-07-16 01:29:19 +0000995 Sleep(1);
996 Py_BLOCK_THREADS
Mark Hammond975e3922002-07-16 01:29:19 +0000997 errno = EINTR;
998 PyErr_SetFromErrno(PyExc_IOError);
999 return -1;
1000 }
1001 }
Fred Drake0e123952000-06-29 21:31:02 +00001002 Py_END_ALLOW_THREADS
1003 }
Martin v. Löwis02af9642002-01-16 11:04:06 +00001004#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001005 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +00001006 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001007 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +00001008 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001009 PyErr_SetFromErrno(PyExc_IOError);
1010 return -1;
1011 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +00001012 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +00001013#elif defined(PLAN9)
1014 {
1015 double millisecs = secs * 1000.0;
1016 if (millisecs > (double)LONG_MAX) {
1017 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
1018 return -1;
1019 }
1020 /* This sleep *CAN BE* interrupted. */
1021 Py_BEGIN_ALLOW_THREADS
1022 if(sleep((long)millisecs) < 0){
1023 Py_BLOCK_THREADS
1024 PyErr_SetFromErrno(PyExc_IOError);
1025 return -1;
1026 }
1027 Py_END_ALLOW_THREADS
1028 }
1029#else
Guido van Rossumb6775db1994-08-01 11:34:53 +00001030 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +00001031 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +00001032 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +00001033 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +00001034#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001035
Guido van Rossumb6775db1994-08-01 11:34:53 +00001036 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +00001037}
Guido van Rossumd11b62e2003-03-14 21:51:36 +00001038
1039