blob: eb9f4d0c9c2727472468ec74a87dcae1c8560ca3 [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
Tim Peters7a822da2006-05-25 21:50:17 +000066#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
Fred Drakedfb4ebd2000-06-29 20:56:28 +000067/* Win32 has better clock replacement
Guido van Rossum3917c221997-04-02 05:35:28 +000068#undef HAVE_CLOCK /* We have our own version down below */
Tim Peters7a822da2006-05-25 21:50:17 +000069#endif /* MS_WINDOWS && !defined(__BORLANDC__) */
Guido van Rossum3917c221997-04-02 05:35:28 +000070
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000071#if defined(PYOS_OS2)
72#define INCL_DOS
73#define INCL_ERRORS
74#include <os2.h>
75#endif
76
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000077#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000078#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000079#endif
80
Guido van Rossumbcc20741998-08-04 22:53:56 +000081#ifdef __BEOS__
Fred Drake56221a72000-08-15 18:52:33 +000082#include <time.h>
Guido van Rossumbcc20741998-08-04 22:53:56 +000083/* For bigtime_t, snooze(). - [cjh] */
84#include <support/SupportDefs.h>
85#include <kernel/OS.h>
86#endif
87
Martin v. Löwisa94568a2003-05-10 07:36:56 +000088#ifdef RISCOS
89extern int riscos_sleep(double);
90#endif
91
Guido van Rossum234f9421993-06-17 12:35:49 +000092/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000093static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000094static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000096/* For Y2K check */
97static PyObject *moddict;
98
Tim Peters1b6f7a92004-06-20 02:50:16 +000099/* Exposed in timefuncs.h. */
100time_t
Brett Cannon298c3802004-06-19 20:48:43 +0000101_PyTime_DoubleToTimet(double x)
102{
103 time_t result;
104 double diff;
105
106 result = (time_t)x;
107 /* How much info did we lose? time_t may be an integral or
108 * floating type, and we don't know which. If it's integral,
109 * we don't know whether C truncates, rounds, returns the floor,
110 * etc. If we lost a second or more, the C rounding is
111 * unreasonable, or the input just doesn't fit in a time_t;
112 * call it an error regardless. Note that the original cast to
113 * time_t can cause a C error too, but nothing we can do to
114 * worm around that.
115 */
116 diff = x - (double)result;
117 if (diff <= -1.0 || diff >= 1.0) {
118 PyErr_SetString(PyExc_ValueError,
119 "timestamp out of range for platform time_t");
120 result = (time_t)-1;
121 }
122 return result;
123}
124
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000125static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000126time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000128 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000129 if (!PyArg_ParseTuple(args, ":time"))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000130 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000131 secs = floattime();
132 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000133 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000134 return NULL;
135 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000136 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000137}
138
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000139PyDoc_STRVAR(time_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000140"time() -> floating point number\n\
141\n\
142Return the current time in seconds since the Epoch.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000143Fractions of a second may be present if the system clock provides them.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000144
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145#ifdef HAVE_CLOCK
146
147#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000148#ifdef CLK_TCK
149#define CLOCKS_PER_SEC CLK_TCK
150#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000151#define CLOCKS_PER_SEC 1000000
152#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000153#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000154
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000155static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000156time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000157{
Thomas Woutersfe385252001-01-19 23:16:56 +0000158 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000160 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000162#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000163
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000164#if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Mark Hammond7ba5e812002-02-12 04:02:33 +0000165/* Due to Mark Hammond and Tim Peters */
Guido van Rossum3917c221997-04-02 05:35:28 +0000166static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000167time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000168{
Tim Peters9ad4b682002-02-13 05:14:18 +0000169 static LARGE_INTEGER ctrStart;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000170 static double divisor = 0.0;
Tim Peters9ad4b682002-02-13 05:14:18 +0000171 LARGE_INTEGER now;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000172 double diff;
Guido van Rossum3917c221997-04-02 05:35:28 +0000173
Thomas Woutersfe385252001-01-19 23:16:56 +0000174 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum3917c221997-04-02 05:35:28 +0000175 return NULL;
176
Mark Hammond7ba5e812002-02-12 04:02:33 +0000177 if (divisor == 0.0) {
Tim Peters9ad4b682002-02-13 05:14:18 +0000178 LARGE_INTEGER freq;
179 QueryPerformanceCounter(&ctrStart);
180 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
Mark Hammond7ba5e812002-02-12 04:02:33 +0000181 /* Unlikely to happen - this works on all intel
182 machines at least! Revert to clock() */
Guido van Rossum3917c221997-04-02 05:35:28 +0000183 return PyFloat_FromDouble(clock());
184 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000185 divisor = (double)freq.QuadPart;
Guido van Rossum3917c221997-04-02 05:35:28 +0000186 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000187 QueryPerformanceCounter(&now);
188 diff = (double)(now.QuadPart - ctrStart.QuadPart);
Mark Hammond7ba5e812002-02-12 04:02:33 +0000189 return PyFloat_FromDouble(diff / divisor);
Guido van Rossum3917c221997-04-02 05:35:28 +0000190}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000191
Guido van Rossum3917c221997-04-02 05:35:28 +0000192#define HAVE_CLOCK /* So it gets included in the methods */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000193#endif /* MS_WINDOWS && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000194
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000195#ifdef HAVE_CLOCK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000196PyDoc_STRVAR(clock_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000197"clock() -> floating point number\n\
198\n\
199Return the CPU time or real time since the start of the process or since\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000200the first call to clock(). This has as much precision as the system\n\
201records.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000202#endif
203
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000204static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000205time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000207 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000208 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000210 if (floatsleep(secs) != 0)
211 return NULL;
212 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000213 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214}
215
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000216PyDoc_STRVAR(sleep_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000217"sleep(seconds)\n\
218\n\
219Delay execution for a given number of seconds. The argument may be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000220a floating point number for subsecond precision.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000221
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000222static PyStructSequence_Field struct_time_type_fields[] = {
223 {"tm_year", NULL},
224 {"tm_mon", NULL},
225 {"tm_mday", NULL},
226 {"tm_hour", NULL},
227 {"tm_min", NULL},
228 {"tm_sec", NULL},
229 {"tm_wday", NULL},
230 {"tm_yday", NULL},
231 {"tm_isdst", NULL},
232 {0}
233};
234
235static PyStructSequence_Desc struct_time_type_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000236 "time.struct_time",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000237 NULL,
238 struct_time_type_fields,
239 9,
240};
Tim Peters9ad4b682002-02-13 05:14:18 +0000241
Martin v. Löwis19ab6c92006-04-16 18:55:50 +0000242static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000243static PyTypeObject StructTimeType;
244
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000245static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000246tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000247{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000248 PyObject *v = PyStructSequence_New(&StructTimeType);
249 if (v == NULL)
250 return NULL;
Tim Peters9ad4b682002-02-13 05:14:18 +0000251
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000252#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
253
254 SET(0, p->tm_year + 1900);
255 SET(1, p->tm_mon + 1); /* Want January == 1 */
256 SET(2, p->tm_mday);
257 SET(3, p->tm_hour);
258 SET(4, p->tm_min);
259 SET(5, p->tm_sec);
260 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
261 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
262 SET(8, p->tm_isdst);
263#undef SET
264 if (PyErr_Occurred()) {
265 Py_XDECREF(v);
266 return NULL;
267 }
268
269 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000270}
271
272static PyObject *
Brett Cannon298c3802004-06-19 20:48:43 +0000273time_convert(double when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000274{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000275 struct tm *p;
Brett Cannon298c3802004-06-19 20:48:43 +0000276 time_t whent = _PyTime_DoubleToTimet(when);
277
278 if (whent == (time_t)-1 && PyErr_Occurred())
279 return NULL;
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000280 errno = 0;
Brett Cannon298c3802004-06-19 20:48:43 +0000281 p = function(&whent);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000282 if (p == NULL) {
283#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000284 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000285 errno = EINVAL;
286#endif
Tim Peters8b19a932003-01-17 20:08:54 +0000287 return PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000288 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000289 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000290}
291
Fred Drakef901abd2004-08-03 17:58:55 +0000292/* Parse arg tuple that can contain an optional float-or-None value;
293 format needs to be "|O:name".
294 Returns non-zero on success (parallels PyArg_ParseTuple).
295*/
296static int
297parse_time_double_args(PyObject *args, char *format, double *pwhen)
298{
299 PyObject *ot = NULL;
300
301 if (!PyArg_ParseTuple(args, format, &ot))
302 return 0;
303 if (ot == NULL || ot == Py_None)
304 *pwhen = floattime();
305 else {
306 double when = PyFloat_AsDouble(ot);
307 if (PyErr_Occurred())
308 return 0;
309 *pwhen = when;
310 }
311 return 1;
312}
313
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000314static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000315time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000316{
317 double when;
Fred Drakef901abd2004-08-03 17:58:55 +0000318 if (!parse_time_double_args(args, "|O:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000319 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000320 return time_convert(when, gmtime);
Guido van Rossum234f9421993-06-17 12:35:49 +0000321}
322
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000323PyDoc_STRVAR(gmtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000324"gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,\n\
325 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000326\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000327Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000328GMT). When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000329
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000330static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000331time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000332{
333 double when;
Fred Drakef901abd2004-08-03 17:58:55 +0000334 if (!parse_time_double_args(args, "|O:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000335 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000336 return time_convert(when, localtime);
Guido van Rossum234f9421993-06-17 12:35:49 +0000337}
338
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000339PyDoc_STRVAR(localtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000340"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 +0000341\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000342Convert seconds since the Epoch to a time tuple expressing local time.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000343When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000344
Guido van Rossum9e90a671993-06-24 11:10:19 +0000345static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000346gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000347{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000348 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000349 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000350
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000351 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000352 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000353 &p->tm_mon,
354 &p->tm_mday,
355 &p->tm_hour,
356 &p->tm_min,
357 &p->tm_sec,
358 &p->tm_wday,
359 &p->tm_yday,
360 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000361 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000362 if (y < 1900) {
363 PyObject *accept = PyDict_GetItemString(moddict,
364 "accept2dyear");
365 if (accept == NULL || !PyInt_Check(accept) ||
366 PyInt_AsLong(accept) == 0) {
367 PyErr_SetString(PyExc_ValueError,
368 "year >= 1900 required");
369 return 0;
370 }
371 if (69 <= y && y <= 99)
372 y += 1900;
373 else if (0 <= y && y <= 68)
374 y += 2000;
375 else {
376 PyErr_SetString(PyExc_ValueError,
Skip Montanaro1a10aac2001-08-22 12:39:16 +0000377 "year out of range");
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000378 return 0;
379 }
380 }
381 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000382 p->tm_mon--;
383 p->tm_wday = (p->tm_wday + 1) % 7;
384 p->tm_yday--;
385 return 1;
386}
387
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000388#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000389static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000390time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000391{
Thomas Woutersfe385252001-01-19 23:16:56 +0000392 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000393 struct tm buf;
394 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000395 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000396 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000397 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000398
Thomas Wouters334fb892000-07-25 12:56:38 +0000399 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000400
Thomas Woutersfe385252001-01-19 23:16:56 +0000401 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000402 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000403
404 if (tup == NULL) {
405 time_t tt = time(NULL);
406 buf = *localtime(&tt);
407 } else if (!gettmarg(tup, &buf))
408 return NULL;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000409
Brett Cannond1080a32004-03-02 04:38:10 +0000410 /* Checks added to make sure strftime() does not crash Python by
411 indexing blindly into some array for a textual representation
412 by some bad index (fixes bug #897625).
Tim Peters1b6f7a92004-06-20 02:50:16 +0000413
Brett Cannond1080a32004-03-02 04:38:10 +0000414 No check for year since handled in gettmarg().
415 */
416 if (buf.tm_mon < 0 || buf.tm_mon > 11) {
417 PyErr_SetString(PyExc_ValueError, "month out of range");
418 return NULL;
419 }
420 if (buf.tm_mday < 1 || buf.tm_mday > 31) {
421 PyErr_SetString(PyExc_ValueError, "day of month out of range");
422 return NULL;
423 }
424 if (buf.tm_hour < 0 || buf.tm_hour > 23) {
425 PyErr_SetString(PyExc_ValueError, "hour out of range");
426 return NULL;
427 }
428 if (buf.tm_min < 0 || buf.tm_min > 59) {
429 PyErr_SetString(PyExc_ValueError, "minute out of range");
430 return NULL;
431 }
432 if (buf.tm_sec < 0 || buf.tm_sec > 61) {
433 PyErr_SetString(PyExc_ValueError, "seconds out of range");
434 return NULL;
435 }
436 /* tm_wday does not need checking of its upper-bound since taking
437 ``% 7`` in gettmarg() automatically restricts the range. */
438 if (buf.tm_wday < 0) {
439 PyErr_SetString(PyExc_ValueError, "day of week out of range");
440 return NULL;
441 }
442 if (buf.tm_yday < 0 || buf.tm_yday > 365) {
443 PyErr_SetString(PyExc_ValueError, "day of year out of range");
444 return NULL;
445 }
446 if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
447 PyErr_SetString(PyExc_ValueError,
448 "daylight savings flag out of range");
449 return NULL;
450 }
451
Guido van Rossumc222ec21999-02-23 00:00:10 +0000452 fmtlen = strlen(fmt);
453
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000454 /* I hate these functions that presume you know how big the output
455 * will be ahead of time...
456 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000457 for (i = 1024; ; i += i) {
Anthony Baxter7cbc0f52006-04-13 07:19:01 +0000458 outbuf = (char *)malloc(i);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000459 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000460 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000461 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000462 buflen = strftime(outbuf, i, fmt, &buf);
463 if (buflen > 0 || i >= 256 * fmtlen) {
464 /* If the buffer is 256 times as long as the format,
465 it's probably not failing for lack of room!
466 More likely, the format yields an empty result,
467 e.g. an empty format, or %Z when the timezone
468 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000469 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000470 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000471 free(outbuf);
472 return ret;
473 }
474 free(outbuf);
475 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000476}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000478PyDoc_STRVAR(strftime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000479"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000480\n\
481Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000482See the library reference manual for formatting codes. When the time tuple\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000483is not present, current time as returned by localtime() is used.");
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000484#endif /* HAVE_STRFTIME */
485
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000486static PyObject *
487time_strptime(PyObject *self, PyObject *args)
488{
489 PyObject *strptime_module = PyImport_ImportModule("_strptime");
Raymond Hettinger502168a2003-04-10 16:03:22 +0000490 PyObject *strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000491
Tim Peters513a1cd2003-01-19 04:54:58 +0000492 if (!strptime_module)
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000493 return NULL;
Raymond Hettinger502168a2003-04-10 16:03:22 +0000494 strptime_result = PyObject_CallMethod(strptime_module, "strptime", "O", args);
495 Py_DECREF(strptime_module);
496 return strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000497}
498
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000499PyDoc_STRVAR(strptime_doc,
Brett Cannon20def8b2003-07-01 05:16:08 +0000500"strptime(string, format) -> struct_time\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000501\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000502Parse a string to a time tuple according to a format specification.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000503See the library reference manual for formatting codes (same as strftime()).");
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000504
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000505
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000506static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000507time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000508{
Thomas Woutersfe385252001-01-19 23:16:56 +0000509 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000510 struct tm buf;
511 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000512 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000513 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000514 if (tup == NULL) {
515 time_t tt = time(NULL);
516 buf = *localtime(&tt);
517 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000518 return NULL;
519 p = asctime(&buf);
520 if (p[24] == '\n')
521 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000522 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000523}
524
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000525PyDoc_STRVAR(asctime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000526"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000527\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000528Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
529When the time tuple is not present, current time as returned by localtime()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000530is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000531
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000532static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000533time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000534{
Fred Drakef901abd2004-08-03 17:58:55 +0000535 PyObject *ot = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000536 time_t tt;
537 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000538
Fred Drakef901abd2004-08-03 17:58:55 +0000539 if (!PyArg_ParseTuple(args, "|O:ctime", &ot))
540 return NULL;
541 if (ot == NULL || ot == Py_None)
Thomas Woutersfe385252001-01-19 23:16:56 +0000542 tt = time(NULL);
543 else {
Fred Drakef901abd2004-08-03 17:58:55 +0000544 double dt = PyFloat_AsDouble(ot);
545 if (PyErr_Occurred())
Thomas Woutersfe385252001-01-19 23:16:56 +0000546 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000547 tt = _PyTime_DoubleToTimet(dt);
548 if (tt == (time_t)-1 && PyErr_Occurred())
549 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000550 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000551 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000552 if (p == NULL) {
553 PyErr_SetString(PyExc_ValueError, "unconvertible time");
554 return NULL;
555 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000556 if (p[24] == '\n')
557 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000558 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000559}
560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000561PyDoc_STRVAR(ctime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000562"ctime(seconds) -> string\n\
563\n\
564Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000565This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000566not present, current time as returned by localtime() is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000567
Guido van Rossum60cd8131998-03-06 17:16:21 +0000568#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000569static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000570time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000571{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000572 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000573 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000574 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000575 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000576 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000577 tt = time(&tt);
578 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000579 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000580 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000581 tt = mktime(&buf);
582 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000583 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000584 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000585 return NULL;
586 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000587 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000588}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000589
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000590PyDoc_STRVAR(mktime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000591"mktime(tuple) -> floating point number\n\
592\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000593Convert a time tuple in local time to seconds since the Epoch.");
Guido van Rossum60cd8131998-03-06 17:16:21 +0000594#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000595
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000596#ifdef HAVE_WORKING_TZSET
597void inittimezone(PyObject *module);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000598
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000599static PyObject *
600time_tzset(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000601{
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000602 PyObject* m;
Fred Drake9bb74322002-04-01 14:49:59 +0000603
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000604 if (!PyArg_ParseTuple(args, ":tzset"))
605 return NULL;
606
607 m = PyImport_ImportModule("time");
608 if (m == NULL) {
609 return NULL;
610 }
611
612 tzset();
613
614 /* Reset timezone, altzone, daylight and tzname */
615 inittimezone(m);
616 Py_DECREF(m);
Tim Peters1b6f7a92004-06-20 02:50:16 +0000617
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000618 Py_INCREF(Py_None);
619 return Py_None;
620}
621
622PyDoc_STRVAR(tzset_doc,
623"tzset(zone)\n\
624\n\
625Initialize, or reinitialize, the local timezone to the value stored in\n\
626os.environ['TZ']. The TZ environment variable should be specified in\n\
Neal Norwitzdc8e1942004-07-20 22:34:37 +0000627standard Unix timezone format as documented in the tzset man page\n\
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000628(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
629fall back to UTC. If the TZ environment variable is not set, the local\n\
630timezone is set to the systems best guess of wallclock time.\n\
631Changing the TZ environment variable without calling tzset *may* change\n\
632the local timezone used by methods such as localtime, but this behaviour\n\
633should not be relied on.");
634#endif /* HAVE_WORKING_TZSET */
635
636void inittimezone(PyObject *m) {
637 /* This code moved from inittime wholesale to allow calling it from
638 time_tzset. In the future, some parts of it can be moved back
639 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
640 are), and the extranious calls to tzset(3) should be removed.
641 I havn't done this yet, as I don't want to change this code as
642 little as possible when introducing the time.tzset and time.tzsetwall
643 methods. This should simply be a method of doing the following once,
644 at the top of this function and removing the call to tzset() from
645 time_tzset():
646
647 #ifdef HAVE_TZSET
648 tzset()
649 #endif
650
651 And I'm lazy and hate C so nyer.
652 */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000653#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000654 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000655#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000656 PyModule_AddIntConstant(m, "timezone", _timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000657#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000658 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000659#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000660#ifdef HAVE_ALTZONE
Fred Drake9bb74322002-04-01 14:49:59 +0000661 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000662#else
Guido van Rossum26452411998-09-28 22:07:11 +0000663#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000664 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000665#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000666 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000667#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000668#endif
Fred Drake9bb74322002-04-01 14:49:59 +0000669 PyModule_AddIntConstant(m, "daylight", daylight);
670 PyModule_AddObject(m, "tzname",
671 Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000672#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000673#ifdef HAVE_STRUCT_TM_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000674 {
675#define YEAR ((time_t)((365 * 24 + 6) * 3600))
676 time_t t;
677 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000678 long janzone, julyzone;
679 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000680 t = (time((time_t *)0) / YEAR) * YEAR;
681 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000682 janzone = -p->tm_gmtoff;
683 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
684 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000685 t += YEAR/2;
686 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000687 julyzone = -p->tm_gmtoff;
688 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
689 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000690
Guido van Rossum57731601999-03-29 19:12:04 +0000691 if( janzone < julyzone ) {
692 /* DST is reversed in the southern hemisphere */
Fred Drake9bb74322002-04-01 14:49:59 +0000693 PyModule_AddIntConstant(m, "timezone", julyzone);
694 PyModule_AddIntConstant(m, "altzone", janzone);
695 PyModule_AddIntConstant(m, "daylight",
696 janzone != julyzone);
697 PyModule_AddObject(m, "tzname",
698 Py_BuildValue("(zz)",
699 julyname, janname));
Guido van Rossum57731601999-03-29 19:12:04 +0000700 } else {
Fred Drake9bb74322002-04-01 14:49:59 +0000701 PyModule_AddIntConstant(m, "timezone", janzone);
702 PyModule_AddIntConstant(m, "altzone", julyzone);
703 PyModule_AddIntConstant(m, "daylight",
704 janzone != julyzone);
705 PyModule_AddObject(m, "tzname",
706 Py_BuildValue("(zz)",
707 janname, julyname));
Guido van Rossum57731601999-03-29 19:12:04 +0000708 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000709 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000710#else
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000711#endif /* HAVE_STRUCT_TM_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000712#ifdef __CYGWIN__
713 tzset();
Fred Drake9bb74322002-04-01 14:49:59 +0000714 PyModule_AddIntConstant(m, "timezone", _timezone);
Georg Brandl378d5922006-05-17 14:26:50 +0000715 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Fred Drake9bb74322002-04-01 14:49:59 +0000716 PyModule_AddIntConstant(m, "daylight", _daylight);
717 PyModule_AddObject(m, "tzname",
718 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +0000719#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000720#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000721}
722
723
724static PyMethodDef time_methods[] = {
725 {"time", time_time, METH_VARARGS, time_doc},
726#ifdef HAVE_CLOCK
727 {"clock", time_clock, METH_VARARGS, clock_doc},
728#endif
729 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
730 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
731 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
732 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
733 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
734#ifdef HAVE_MKTIME
735 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
736#endif
737#ifdef HAVE_STRFTIME
738 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
739#endif
740 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
741#ifdef HAVE_WORKING_TZSET
742 {"tzset", time_tzset, METH_VARARGS, tzset_doc},
743#endif
744 {NULL, NULL} /* sentinel */
745};
746
747
748PyDoc_STRVAR(module_doc,
749"This module provides various functions to manipulate time values.\n\
750\n\
751There are two standard representations of time. One is the number\n\
752of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
753or a floating point number (to represent fractions of seconds).\n\
754The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
755The actual value can be retrieved by calling gmtime(0).\n\
756\n\
757The other representation is a tuple of 9 integers giving local time.\n\
758The tuple items are:\n\
759 year (four digits, e.g. 1998)\n\
760 month (1-12)\n\
761 day (1-31)\n\
762 hours (0-23)\n\
763 minutes (0-59)\n\
764 seconds (0-59)\n\
765 weekday (0-6, Monday is 0)\n\
766 Julian day (day in the year, 1-366)\n\
767 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
768If the DST flag is 0, the time is given in the regular time zone;\n\
769if it is 1, the time is given in the DST time zone;\n\
770if it is -1, mktime() should guess based on the date and time.\n\
771\n\
772Variables:\n\
773\n\
774timezone -- difference in seconds between UTC and local standard time\n\
775altzone -- difference in seconds between UTC and local DST time\n\
776daylight -- whether local time should reflect DST\n\
777tzname -- tuple of (standard time zone name, DST time zone name)\n\
778\n\
779Functions:\n\
780\n\
781time() -- return current time in seconds since the Epoch as a float\n\
782clock() -- return CPU time since process start as a float\n\
783sleep() -- delay for a number of seconds given as a float\n\
784gmtime() -- convert seconds since Epoch to UTC tuple\n\
785localtime() -- convert seconds since Epoch to local time tuple\n\
786asctime() -- convert time tuple to string\n\
787ctime() -- convert time in seconds to string\n\
788mktime() -- convert local time tuple to seconds since Epoch\n\
789strftime() -- convert time tuple to string according to format specification\n\
790strptime() -- parse string to time tuple according to format specification\n\
791tzset() -- change the local timezone");
792
793
794PyMODINIT_FUNC
795inittime(void)
796{
797 PyObject *m;
798 char *p;
799 m = Py_InitModule3("time", time_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000800 if (m == NULL)
801 return;
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000802
803 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
804 p = Py_GETENV("PYTHONY2K");
805 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
806 /* Squirrel away the module's dictionary for the y2k check */
807 moddict = PyModule_GetDict(m);
808 Py_INCREF(moddict);
809
810 /* Set, or reset, module variables like time.timezone */
811 inittimezone(m);
812
Mark Hammond975e3922002-07-16 01:29:19 +0000813#ifdef MS_WINDOWS
814 /* Helper to allow interrupts for Windows.
815 If Ctrl+C event delivered while not sleeping
816 it will be ignored.
817 */
818 main_thread = PyThread_get_thread_ident();
819 hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
820 SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
821#endif /* MS_WINDOWS */
Martin v. Löwis19ab6c92006-04-16 18:55:50 +0000822 if (!initialized) {
Tim Peters7a822da2006-05-25 21:50:17 +0000823 PyStructSequence_InitType(&StructTimeType,
Martin v. Löwis19ab6c92006-04-16 18:55:50 +0000824 &struct_time_type_desc);
825 }
Fred Drake9bb74322002-04-01 14:49:59 +0000826 Py_INCREF(&StructTimeType);
827 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
Martin v. Löwis19ab6c92006-04-16 18:55:50 +0000828 initialized = 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000829}
830
831
Guido van Rossumb6775db1994-08-01 11:34:53 +0000832/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000833
Guido van Rossumb6775db1994-08-01 11:34:53 +0000834static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000835floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000836{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000837 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000838 (1) gettimeofday() -- resolution in microseconds
839 (2) ftime() -- resolution in milliseconds
840 (3) time() -- resolution in seconds
841 In all cases the return value is a float in seconds.
842 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
843 fail, so we fall back on ftime() or time().
844 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000845#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000846 {
847 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000848#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000849 if (gettimeofday(&t) == 0)
850 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000851#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000852 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
853 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000854#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000855 }
Ronald Oussorend06b6f22006-04-23 11:59:25 +0000856
Guido van Rossumb6775db1994-08-01 11:34:53 +0000857#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000858 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000859#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000860 struct timeb t;
861 ftime(&t);
862 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000863#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000864 time_t secs;
865 time(&secs);
866 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000867#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000868 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000869}
870
Guido van Rossumb6775db1994-08-01 11:34:53 +0000871
872/* Implement floatsleep() for various platforms.
873 When interrupted (or when another error occurs), return -1 and
874 set an exception; else return 0. */
875
876static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000877floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000878{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000879/* XXX Should test for MS_WINDOWS first! */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000880#if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000881 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000882 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000883 frac = fmod(secs, 1.0);
884 secs = floor(secs);
885 t.tv_sec = (long)secs;
886 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000887 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000888 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000889#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000890 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000891#else
892 if (1) {
893#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000894 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000895 PyErr_SetFromErrno(PyExc_IOError);
896 return -1;
897 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000898 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000899 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000900#elif defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000901 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000902 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000903 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000904 Py_END_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000905#elif defined(MS_WINDOWS)
Fred Drake0e123952000-06-29 21:31:02 +0000906 {
907 double millisecs = secs * 1000.0;
Tim Peters513a1cd2003-01-19 04:54:58 +0000908 unsigned long ul_millis;
909
Fred Drake0e123952000-06-29 21:31:02 +0000910 if (millisecs > (double)ULONG_MAX) {
Tim Peters513a1cd2003-01-19 04:54:58 +0000911 PyErr_SetString(PyExc_OverflowError,
912 "sleep length is too large");
Fred Drake0e123952000-06-29 21:31:02 +0000913 return -1;
914 }
Fred Drake0e123952000-06-29 21:31:02 +0000915 Py_BEGIN_ALLOW_THREADS
Tim Peters513a1cd2003-01-19 04:54:58 +0000916 /* Allow sleep(0) to maintain win32 semantics, and as decreed
917 * by Guido, only the main thread can be interrupted.
918 */
919 ul_millis = (unsigned long)millisecs;
920 if (ul_millis == 0 ||
921 main_thread != PyThread_get_thread_ident())
922 Sleep(ul_millis);
Mark Hammond975e3922002-07-16 01:29:19 +0000923 else {
924 DWORD rc;
925 ResetEvent(hInterruptEvent);
Tim Peters513a1cd2003-01-19 04:54:58 +0000926 rc = WaitForSingleObject(hInterruptEvent, ul_millis);
927 if (rc == WAIT_OBJECT_0) {
928 /* Yield to make sure real Python signal
929 * handler called.
930 */
Mark Hammond975e3922002-07-16 01:29:19 +0000931 Sleep(1);
932 Py_BLOCK_THREADS
Mark Hammond975e3922002-07-16 01:29:19 +0000933 errno = EINTR;
934 PyErr_SetFromErrno(PyExc_IOError);
935 return -1;
936 }
937 }
Fred Drake0e123952000-06-29 21:31:02 +0000938 Py_END_ALLOW_THREADS
939 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000940#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000941 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000942 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000943 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000944 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000945 PyErr_SetFromErrno(PyExc_IOError);
946 return -1;
947 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000948 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000949#elif defined(__BEOS__)
Guido van Rossumbcc20741998-08-04 22:53:56 +0000950 /* This sleep *CAN BE* interrupted. */
951 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000952 if( secs <= 0.0 ) {
953 return;
954 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000955
Guido van Rossumbcc20741998-08-04 22:53:56 +0000956 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000957 /* BeOS snooze() is in microseconds... */
958 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000959 Py_BLOCK_THREADS
960 PyErr_SetFromErrno( PyExc_IOError );
961 return -1;
962 }
963 Py_END_ALLOW_THREADS
964 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000965#elif defined(RISCOS)
Guido van Rossumbceccf52001-04-10 22:07:43 +0000966 if (secs <= 0.0)
967 return 0;
968 Py_BEGIN_ALLOW_THREADS
969 /* This sleep *CAN BE* interrupted. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000970 if ( riscos_sleep(secs) )
Guido van Rossumbceccf52001-04-10 22:07:43 +0000971 return -1;
972 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000973#elif defined(PLAN9)
974 {
975 double millisecs = secs * 1000.0;
976 if (millisecs > (double)LONG_MAX) {
977 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
978 return -1;
979 }
980 /* This sleep *CAN BE* interrupted. */
981 Py_BEGIN_ALLOW_THREADS
982 if(sleep((long)millisecs) < 0){
983 Py_BLOCK_THREADS
984 PyErr_SetFromErrno(PyExc_IOError);
985 return -1;
986 }
987 Py_END_ALLOW_THREADS
988 }
989#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000990 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000991 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000992 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000993 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000994#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000995
Guido van Rossumb6775db1994-08-01 11:34:53 +0000996 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000997}
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000998
999