blob: 0783f7f783e0b62b3c30ddf45d99383d699af8c4 [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"
Guido van Rossum3f5da241990-12-20 15:06:42 +00006
Guido van Rossum87ce7bb1998-06-09 16:30:31 +00007#include <ctype.h>
8
Guido van Rossumb6775db1994-08-01 11:34:53 +00009#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000010
Guido van Rossumb6775db1994-08-01 11:34:53 +000011#ifdef QUICKWIN
12#include <io.h>
13#endif
14
Guido van Rossumb6775db1994-08-01 11:34:53 +000015#ifdef HAVE_FTIME
16#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000017#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000018extern int ftime(struct timeb *);
Guido van Rossum52174571996-12-09 18:38:52 +000019#endif /* MS_WINDOWS */
20#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021
Guido van Rossum7bf22de1997-12-02 20:34:19 +000022#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000023#include <i86.h>
24#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000025#ifdef MS_WINDOWS
Mark Hammond975e3922002-07-16 01:29:19 +000026#define WIN32_LEAN_AND_MEAN
Guido van Rossum258ccd42001-03-02 06:53:29 +000027#include <windows.h>
Mark Hammond975e3922002-07-16 01:29:19 +000028#include "pythread.h"
29
30/* helper to allow us to interrupt sleep() on Windows*/
31static HANDLE hInterruptEvent = NULL;
32static BOOL WINAPI PyCtrlHandler(DWORD dwCtrlType)
33{
34 SetEvent(hInterruptEvent);
35 /* allow other default handlers to be called.
36 Default Python handler will setup the
37 KeyboardInterrupt exception.
38 */
39 return FALSE;
40}
41static long main_thread;
42
43
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000044#if defined(__BORLANDC__)
Guido van Rossumb2fb3641996-09-07 00:47:35 +000045/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000046#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000047#define tzname _tzname
48#define daylight _daylight
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000049#endif /* __BORLANDC__ */
Guido van Rossumcac6c721996-09-06 13:34:02 +000050#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000051#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000052
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000053#if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Fred Drakedfb4ebd2000-06-29 20:56:28 +000054/* Win32 has better clock replacement
55 XXX Win64 does not yet, but might when the platform matures. */
Guido van Rossum3917c221997-04-02 05:35:28 +000056#undef HAVE_CLOCK /* We have our own version down below */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000057#endif /* MS_WINDOWS && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +000058
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000059#if defined(PYOS_OS2)
60#define INCL_DOS
61#define INCL_ERRORS
62#include <os2.h>
63#endif
64
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000065#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000066#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000067#endif
68
Guido van Rossumbcc20741998-08-04 22:53:56 +000069#ifdef __BEOS__
Fred Drake56221a72000-08-15 18:52:33 +000070#include <time.h>
Guido van Rossumbcc20741998-08-04 22:53:56 +000071/* For bigtime_t, snooze(). - [cjh] */
72#include <support/SupportDefs.h>
73#include <kernel/OS.h>
74#endif
75
Martin v. Löwisa94568a2003-05-10 07:36:56 +000076#ifdef RISCOS
77extern int riscos_sleep(double);
78#endif
79
Guido van Rossum234f9421993-06-17 12:35:49 +000080/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000081static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000082static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000084/* For Y2K check */
85static PyObject *moddict;
86
Brett Cannon298c3802004-06-19 20:48:43 +000087/* Cast double x to time_t, but raise ValueError if x is too large
88 * to fit in a time_t. ValueError is set on return iff the return
89 * value is (time_t)-1 and PyErr_Occurred().
90 */
91static time_t
92_PyTime_DoubleToTimet(double x)
93{
94 time_t result;
95 double diff;
96
97 result = (time_t)x;
98 /* How much info did we lose? time_t may be an integral or
99 * floating type, and we don't know which. If it's integral,
100 * we don't know whether C truncates, rounds, returns the floor,
101 * etc. If we lost a second or more, the C rounding is
102 * unreasonable, or the input just doesn't fit in a time_t;
103 * call it an error regardless. Note that the original cast to
104 * time_t can cause a C error too, but nothing we can do to
105 * worm around that.
106 */
107 diff = x - (double)result;
108 if (diff <= -1.0 || diff >= 1.0) {
109 PyErr_SetString(PyExc_ValueError,
110 "timestamp out of range for platform time_t");
111 result = (time_t)-1;
112 }
113 return result;
114}
115
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000116static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000117time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000119 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000120 if (!PyArg_ParseTuple(args, ":time"))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000121 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000122 secs = floattime();
123 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000124 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000125 return NULL;
126 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000127 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000128}
129
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000130PyDoc_STRVAR(time_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000131"time() -> floating point number\n\
132\n\
133Return the current time in seconds since the Epoch.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000134Fractions of a second may be present if the system clock provides them.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000135
Guido van Rossumb6775db1994-08-01 11:34:53 +0000136#ifdef HAVE_CLOCK
137
138#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000139#ifdef CLK_TCK
140#define CLOCKS_PER_SEC CLK_TCK
141#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000142#define CLOCKS_PER_SEC 1000000
143#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000144#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000146static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000147time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000148{
Thomas Woutersfe385252001-01-19 23:16:56 +0000149 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000151 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000153#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000155#if defined(MS_WINDOWS) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Mark Hammond7ba5e812002-02-12 04:02:33 +0000156/* Due to Mark Hammond and Tim Peters */
Guido van Rossum3917c221997-04-02 05:35:28 +0000157static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000158time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000159{
Tim Peters9ad4b682002-02-13 05:14:18 +0000160 static LARGE_INTEGER ctrStart;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000161 static double divisor = 0.0;
Tim Peters9ad4b682002-02-13 05:14:18 +0000162 LARGE_INTEGER now;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000163 double diff;
Guido van Rossum3917c221997-04-02 05:35:28 +0000164
Thomas Woutersfe385252001-01-19 23:16:56 +0000165 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum3917c221997-04-02 05:35:28 +0000166 return NULL;
167
Mark Hammond7ba5e812002-02-12 04:02:33 +0000168 if (divisor == 0.0) {
Tim Peters9ad4b682002-02-13 05:14:18 +0000169 LARGE_INTEGER freq;
170 QueryPerformanceCounter(&ctrStart);
171 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
Mark Hammond7ba5e812002-02-12 04:02:33 +0000172 /* Unlikely to happen - this works on all intel
173 machines at least! Revert to clock() */
Guido van Rossum3917c221997-04-02 05:35:28 +0000174 return PyFloat_FromDouble(clock());
175 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000176 divisor = (double)freq.QuadPart;
Guido van Rossum3917c221997-04-02 05:35:28 +0000177 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000178 QueryPerformanceCounter(&now);
179 diff = (double)(now.QuadPart - ctrStart.QuadPart);
Mark Hammond7ba5e812002-02-12 04:02:33 +0000180 return PyFloat_FromDouble(diff / divisor);
Guido van Rossum3917c221997-04-02 05:35:28 +0000181}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000182
Guido van Rossum3917c221997-04-02 05:35:28 +0000183#define HAVE_CLOCK /* So it gets included in the methods */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000184#endif /* MS_WINDOWS && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000185
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000186#ifdef HAVE_CLOCK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000187PyDoc_STRVAR(clock_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000188"clock() -> floating point number\n\
189\n\
190Return the CPU time or real time since the start of the process or since\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000191the first call to clock(). This has as much precision as the system\n\
192records.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000193#endif
194
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000195static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000196time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000198 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000199 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000200 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000201 if (floatsleep(secs) != 0)
202 return NULL;
203 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000204 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000205}
206
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000207PyDoc_STRVAR(sleep_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000208"sleep(seconds)\n\
209\n\
210Delay execution for a given number of seconds. The argument may be\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000211a floating point number for subsecond precision.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000212
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000213static PyStructSequence_Field struct_time_type_fields[] = {
214 {"tm_year", NULL},
215 {"tm_mon", NULL},
216 {"tm_mday", NULL},
217 {"tm_hour", NULL},
218 {"tm_min", NULL},
219 {"tm_sec", NULL},
220 {"tm_wday", NULL},
221 {"tm_yday", NULL},
222 {"tm_isdst", NULL},
223 {0}
224};
225
226static PyStructSequence_Desc struct_time_type_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000227 "time.struct_time",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000228 NULL,
229 struct_time_type_fields,
230 9,
231};
Tim Peters9ad4b682002-02-13 05:14:18 +0000232
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000233static PyTypeObject StructTimeType;
234
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000235static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000236tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000237{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000238 PyObject *v = PyStructSequence_New(&StructTimeType);
239 if (v == NULL)
240 return NULL;
Tim Peters9ad4b682002-02-13 05:14:18 +0000241
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000242#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
243
244 SET(0, p->tm_year + 1900);
245 SET(1, p->tm_mon + 1); /* Want January == 1 */
246 SET(2, p->tm_mday);
247 SET(3, p->tm_hour);
248 SET(4, p->tm_min);
249 SET(5, p->tm_sec);
250 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
251 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
252 SET(8, p->tm_isdst);
253#undef SET
254 if (PyErr_Occurred()) {
255 Py_XDECREF(v);
256 return NULL;
257 }
258
259 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000260}
261
262static PyObject *
Brett Cannon298c3802004-06-19 20:48:43 +0000263time_convert(double when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000264{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000265 struct tm *p;
Brett Cannon298c3802004-06-19 20:48:43 +0000266 time_t whent = _PyTime_DoubleToTimet(when);
267
268 if (whent == (time_t)-1 && PyErr_Occurred())
269 return NULL;
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000270 errno = 0;
Brett Cannon298c3802004-06-19 20:48:43 +0000271 p = function(&whent);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000272 if (p == NULL) {
273#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000274 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000275 errno = EINVAL;
276#endif
Tim Peters8b19a932003-01-17 20:08:54 +0000277 return PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000278 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000279 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000280}
281
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000282static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000283time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000284{
285 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000286 if (PyTuple_Size(args) == 0)
287 when = floattime();
288 if (!PyArg_ParseTuple(args, "|d:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000289 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000290 return time_convert(when, gmtime);
Guido van Rossum234f9421993-06-17 12:35:49 +0000291}
292
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000293PyDoc_STRVAR(gmtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000294"gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,\n\
295 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000296\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000297Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000298GMT). When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000299
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000300static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000301time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000302{
303 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000304 if (PyTuple_Size(args) == 0)
305 when = floattime();
306 if (!PyArg_ParseTuple(args, "|d:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000307 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000308 return time_convert(when, localtime);
Guido van Rossum234f9421993-06-17 12:35:49 +0000309}
310
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000311PyDoc_STRVAR(localtime_doc,
Fred Drake193a3f62002-03-12 21:38:49 +0000312"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 +0000313\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000314Convert seconds since the Epoch to a time tuple expressing local time.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000315When 'seconds' is not passed in, convert the current time instead.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000316
Guido van Rossum9e90a671993-06-24 11:10:19 +0000317static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000318gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000319{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000320 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000321 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000322
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000323 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000324 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000325 &p->tm_mon,
326 &p->tm_mday,
327 &p->tm_hour,
328 &p->tm_min,
329 &p->tm_sec,
330 &p->tm_wday,
331 &p->tm_yday,
332 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000333 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000334 if (y < 1900) {
335 PyObject *accept = PyDict_GetItemString(moddict,
336 "accept2dyear");
337 if (accept == NULL || !PyInt_Check(accept) ||
338 PyInt_AsLong(accept) == 0) {
339 PyErr_SetString(PyExc_ValueError,
340 "year >= 1900 required");
341 return 0;
342 }
343 if (69 <= y && y <= 99)
344 y += 1900;
345 else if (0 <= y && y <= 68)
346 y += 2000;
347 else {
348 PyErr_SetString(PyExc_ValueError,
Skip Montanaro1a10aac2001-08-22 12:39:16 +0000349 "year out of range");
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000350 return 0;
351 }
352 }
353 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000354 p->tm_mon--;
355 p->tm_wday = (p->tm_wday + 1) % 7;
356 p->tm_yday--;
357 return 1;
358}
359
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000360#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000361static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000362time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000363{
Thomas Woutersfe385252001-01-19 23:16:56 +0000364 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000365 struct tm buf;
366 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000367 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000368 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000369 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000370
Thomas Wouters334fb892000-07-25 12:56:38 +0000371 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000372
Thomas Woutersfe385252001-01-19 23:16:56 +0000373 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000374 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000375
376 if (tup == NULL) {
377 time_t tt = time(NULL);
378 buf = *localtime(&tt);
379 } else if (!gettmarg(tup, &buf))
380 return NULL;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000381
Brett Cannond1080a32004-03-02 04:38:10 +0000382 /* Checks added to make sure strftime() does not crash Python by
383 indexing blindly into some array for a textual representation
384 by some bad index (fixes bug #897625).
385
386 No check for year since handled in gettmarg().
387 */
388 if (buf.tm_mon < 0 || buf.tm_mon > 11) {
389 PyErr_SetString(PyExc_ValueError, "month out of range");
390 return NULL;
391 }
392 if (buf.tm_mday < 1 || buf.tm_mday > 31) {
393 PyErr_SetString(PyExc_ValueError, "day of month out of range");
394 return NULL;
395 }
396 if (buf.tm_hour < 0 || buf.tm_hour > 23) {
397 PyErr_SetString(PyExc_ValueError, "hour out of range");
398 return NULL;
399 }
400 if (buf.tm_min < 0 || buf.tm_min > 59) {
401 PyErr_SetString(PyExc_ValueError, "minute out of range");
402 return NULL;
403 }
404 if (buf.tm_sec < 0 || buf.tm_sec > 61) {
405 PyErr_SetString(PyExc_ValueError, "seconds out of range");
406 return NULL;
407 }
408 /* tm_wday does not need checking of its upper-bound since taking
409 ``% 7`` in gettmarg() automatically restricts the range. */
410 if (buf.tm_wday < 0) {
411 PyErr_SetString(PyExc_ValueError, "day of week out of range");
412 return NULL;
413 }
414 if (buf.tm_yday < 0 || buf.tm_yday > 365) {
415 PyErr_SetString(PyExc_ValueError, "day of year out of range");
416 return NULL;
417 }
418 if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
419 PyErr_SetString(PyExc_ValueError,
420 "daylight savings flag out of range");
421 return NULL;
422 }
423
Guido van Rossumc222ec21999-02-23 00:00:10 +0000424 fmtlen = strlen(fmt);
425
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000426 /* I hate these functions that presume you know how big the output
427 * will be ahead of time...
428 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000429 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000430 outbuf = malloc(i);
431 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000432 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000433 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000434 buflen = strftime(outbuf, i, fmt, &buf);
435 if (buflen > 0 || i >= 256 * fmtlen) {
436 /* If the buffer is 256 times as long as the format,
437 it's probably not failing for lack of room!
438 More likely, the format yields an empty result,
439 e.g. an empty format, or %Z when the timezone
440 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000441 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000442 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000443 free(outbuf);
444 return ret;
445 }
446 free(outbuf);
447 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000448}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000449
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000450PyDoc_STRVAR(strftime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000451"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000452\n\
453Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000454See the library reference manual for formatting codes. When the time tuple\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000455is not present, current time as returned by localtime() is used.");
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000456#endif /* HAVE_STRFTIME */
457
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000458static PyObject *
459time_strptime(PyObject *self, PyObject *args)
460{
461 PyObject *strptime_module = PyImport_ImportModule("_strptime");
Raymond Hettinger502168a2003-04-10 16:03:22 +0000462 PyObject *strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000463
Tim Peters513a1cd2003-01-19 04:54:58 +0000464 if (!strptime_module)
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000465 return NULL;
Raymond Hettinger502168a2003-04-10 16:03:22 +0000466 strptime_result = PyObject_CallMethod(strptime_module, "strptime", "O", args);
467 Py_DECREF(strptime_module);
468 return strptime_result;
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000469}
470
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000471PyDoc_STRVAR(strptime_doc,
Brett Cannon20def8b2003-07-01 05:16:08 +0000472"strptime(string, format) -> struct_time\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000473\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000474Parse a string to a time tuple according to a format specification.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000475See the library reference manual for formatting codes (same as strftime()).");
Guido van Rossumd3c46d52002-07-19 17:06:47 +0000476
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000477
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000478static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000479time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000480{
Thomas Woutersfe385252001-01-19 23:16:56 +0000481 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000482 struct tm buf;
483 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000484 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000485 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000486 if (tup == NULL) {
487 time_t tt = time(NULL);
488 buf = *localtime(&tt);
489 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000490 return NULL;
491 p = asctime(&buf);
492 if (p[24] == '\n')
493 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000494 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000495}
496
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000497PyDoc_STRVAR(asctime_doc,
Thomas Woutersfe385252001-01-19 23:16:56 +0000498"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000499\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000500Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
501When the time tuple is not present, current time as returned by localtime()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000502is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000503
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000504static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000505time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000506{
507 double dt;
508 time_t tt;
509 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000510
Thomas Woutersfe385252001-01-19 23:16:56 +0000511 if (PyTuple_Size(args) == 0)
512 tt = time(NULL);
513 else {
514 if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
515 return NULL;
Brett Cannon298c3802004-06-19 20:48:43 +0000516 tt = _PyTime_DoubleToTimet(dt);
517 if (tt == (time_t)-1 && PyErr_Occurred())
518 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000519 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000520 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000521 if (p == NULL) {
522 PyErr_SetString(PyExc_ValueError, "unconvertible time");
523 return NULL;
524 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000525 if (p[24] == '\n')
526 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000527 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000528}
529
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000530PyDoc_STRVAR(ctime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000531"ctime(seconds) -> string\n\
532\n\
533Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000534This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000535not present, current time as returned by localtime() is used.");
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000536
Guido van Rossum60cd8131998-03-06 17:16:21 +0000537#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000538static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000539time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000540{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000541 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000542 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000543 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000544 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000545 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000546 tt = time(&tt);
547 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000548 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000549 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000550 tt = mktime(&buf);
551 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000552 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000553 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000554 return NULL;
555 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000556 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000557}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000558
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000559PyDoc_STRVAR(mktime_doc,
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000560"mktime(tuple) -> floating point number\n\
561\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000562Convert a time tuple in local time to seconds since the Epoch.");
Guido van Rossum60cd8131998-03-06 17:16:21 +0000563#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000564
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000565#ifdef HAVE_WORKING_TZSET
566void inittimezone(PyObject *module);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000567
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000568static PyObject *
569time_tzset(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000570{
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000571 PyObject* m;
Fred Drake9bb74322002-04-01 14:49:59 +0000572
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000573 if (!PyArg_ParseTuple(args, ":tzset"))
574 return NULL;
575
576 m = PyImport_ImportModule("time");
577 if (m == NULL) {
578 return NULL;
579 }
580
581 tzset();
582
583 /* Reset timezone, altzone, daylight and tzname */
584 inittimezone(m);
585 Py_DECREF(m);
586
587 Py_INCREF(Py_None);
588 return Py_None;
589}
590
591PyDoc_STRVAR(tzset_doc,
592"tzset(zone)\n\
593\n\
594Initialize, or reinitialize, the local timezone to the value stored in\n\
595os.environ['TZ']. The TZ environment variable should be specified in\n\
596standard Uniz timezone format as documented in the tzset man page\n\
597(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
598fall back to UTC. If the TZ environment variable is not set, the local\n\
599timezone is set to the systems best guess of wallclock time.\n\
600Changing the TZ environment variable without calling tzset *may* change\n\
601the local timezone used by methods such as localtime, but this behaviour\n\
602should not be relied on.");
603#endif /* HAVE_WORKING_TZSET */
604
605void inittimezone(PyObject *m) {
606 /* This code moved from inittime wholesale to allow calling it from
607 time_tzset. In the future, some parts of it can be moved back
608 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
609 are), and the extranious calls to tzset(3) should be removed.
610 I havn't done this yet, as I don't want to change this code as
611 little as possible when introducing the time.tzset and time.tzsetwall
612 methods. This should simply be a method of doing the following once,
613 at the top of this function and removing the call to tzset() from
614 time_tzset():
615
616 #ifdef HAVE_TZSET
617 tzset()
618 #endif
619
620 And I'm lazy and hate C so nyer.
621 */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000622#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000623 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000624#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000625 PyModule_AddIntConstant(m, "timezone", _timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000626#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000627 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000628#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000629#ifdef HAVE_ALTZONE
Fred Drake9bb74322002-04-01 14:49:59 +0000630 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000631#else
Guido van Rossum26452411998-09-28 22:07:11 +0000632#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000633 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000634#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000635 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000636#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000637#endif
Fred Drake9bb74322002-04-01 14:49:59 +0000638 PyModule_AddIntConstant(m, "daylight", daylight);
639 PyModule_AddObject(m, "tzname",
640 Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000641#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000642#ifdef HAVE_STRUCT_TM_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000643 {
644#define YEAR ((time_t)((365 * 24 + 6) * 3600))
645 time_t t;
646 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000647 long janzone, julyzone;
648 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000649 t = (time((time_t *)0) / YEAR) * YEAR;
650 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000651 janzone = -p->tm_gmtoff;
652 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
653 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000654 t += YEAR/2;
655 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000656 julyzone = -p->tm_gmtoff;
657 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
658 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000659
Guido van Rossum57731601999-03-29 19:12:04 +0000660 if( janzone < julyzone ) {
661 /* DST is reversed in the southern hemisphere */
Fred Drake9bb74322002-04-01 14:49:59 +0000662 PyModule_AddIntConstant(m, "timezone", julyzone);
663 PyModule_AddIntConstant(m, "altzone", janzone);
664 PyModule_AddIntConstant(m, "daylight",
665 janzone != julyzone);
666 PyModule_AddObject(m, "tzname",
667 Py_BuildValue("(zz)",
668 julyname, janname));
Guido van Rossum57731601999-03-29 19:12:04 +0000669 } else {
Fred Drake9bb74322002-04-01 14:49:59 +0000670 PyModule_AddIntConstant(m, "timezone", janzone);
671 PyModule_AddIntConstant(m, "altzone", julyzone);
672 PyModule_AddIntConstant(m, "daylight",
673 janzone != julyzone);
674 PyModule_AddObject(m, "tzname",
675 Py_BuildValue("(zz)",
676 janname, julyname));
Guido van Rossum57731601999-03-29 19:12:04 +0000677 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000678 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000679#else
Martin v. Löwis60a5d722002-10-16 20:28:25 +0000680#endif /* HAVE_STRUCT_TM_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000681#ifdef __CYGWIN__
682 tzset();
Fred Drake9bb74322002-04-01 14:49:59 +0000683 PyModule_AddIntConstant(m, "timezone", _timezone);
684 PyModule_AddIntConstant(m, "altzone", _timezone);
685 PyModule_AddIntConstant(m, "daylight", _daylight);
686 PyModule_AddObject(m, "tzname",
687 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +0000688#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000689#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000690}
691
692
693static PyMethodDef time_methods[] = {
694 {"time", time_time, METH_VARARGS, time_doc},
695#ifdef HAVE_CLOCK
696 {"clock", time_clock, METH_VARARGS, clock_doc},
697#endif
698 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
699 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
700 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
701 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
702 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
703#ifdef HAVE_MKTIME
704 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
705#endif
706#ifdef HAVE_STRFTIME
707 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
708#endif
709 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
710#ifdef HAVE_WORKING_TZSET
711 {"tzset", time_tzset, METH_VARARGS, tzset_doc},
712#endif
713 {NULL, NULL} /* sentinel */
714};
715
716
717PyDoc_STRVAR(module_doc,
718"This module provides various functions to manipulate time values.\n\
719\n\
720There are two standard representations of time. One is the number\n\
721of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
722or a floating point number (to represent fractions of seconds).\n\
723The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
724The actual value can be retrieved by calling gmtime(0).\n\
725\n\
726The other representation is a tuple of 9 integers giving local time.\n\
727The tuple items are:\n\
728 year (four digits, e.g. 1998)\n\
729 month (1-12)\n\
730 day (1-31)\n\
731 hours (0-23)\n\
732 minutes (0-59)\n\
733 seconds (0-59)\n\
734 weekday (0-6, Monday is 0)\n\
735 Julian day (day in the year, 1-366)\n\
736 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
737If the DST flag is 0, the time is given in the regular time zone;\n\
738if it is 1, the time is given in the DST time zone;\n\
739if it is -1, mktime() should guess based on the date and time.\n\
740\n\
741Variables:\n\
742\n\
743timezone -- difference in seconds between UTC and local standard time\n\
744altzone -- difference in seconds between UTC and local DST time\n\
745daylight -- whether local time should reflect DST\n\
746tzname -- tuple of (standard time zone name, DST time zone name)\n\
747\n\
748Functions:\n\
749\n\
750time() -- return current time in seconds since the Epoch as a float\n\
751clock() -- return CPU time since process start as a float\n\
752sleep() -- delay for a number of seconds given as a float\n\
753gmtime() -- convert seconds since Epoch to UTC tuple\n\
754localtime() -- convert seconds since Epoch to local time tuple\n\
755asctime() -- convert time tuple to string\n\
756ctime() -- convert time in seconds to string\n\
757mktime() -- convert local time tuple to seconds since Epoch\n\
758strftime() -- convert time tuple to string according to format specification\n\
759strptime() -- parse string to time tuple according to format specification\n\
760tzset() -- change the local timezone");
761
762
763PyMODINIT_FUNC
764inittime(void)
765{
766 PyObject *m;
767 char *p;
768 m = Py_InitModule3("time", time_methods, module_doc);
769
770 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
771 p = Py_GETENV("PYTHONY2K");
772 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
773 /* Squirrel away the module's dictionary for the y2k check */
774 moddict = PyModule_GetDict(m);
775 Py_INCREF(moddict);
776
777 /* Set, or reset, module variables like time.timezone */
778 inittimezone(m);
779
Mark Hammond975e3922002-07-16 01:29:19 +0000780#ifdef MS_WINDOWS
781 /* Helper to allow interrupts for Windows.
782 If Ctrl+C event delivered while not sleeping
783 it will be ignored.
784 */
785 main_thread = PyThread_get_thread_ident();
786 hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
787 SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
788#endif /* MS_WINDOWS */
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000789 PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
Fred Drake9bb74322002-04-01 14:49:59 +0000790 Py_INCREF(&StructTimeType);
791 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000792}
793
794
Guido van Rossumb6775db1994-08-01 11:34:53 +0000795/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000796
Guido van Rossumb6775db1994-08-01 11:34:53 +0000797static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000798floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000799{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000800 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000801 (1) gettimeofday() -- resolution in microseconds
802 (2) ftime() -- resolution in milliseconds
803 (3) time() -- resolution in seconds
804 In all cases the return value is a float in seconds.
805 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
806 fail, so we fall back on ftime() or time().
807 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000808#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000809 {
810 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000811#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000812 if (gettimeofday(&t) == 0)
813 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000814#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000815 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
816 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000817#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000818 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000819#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000820 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000821#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000822 struct timeb t;
823 ftime(&t);
824 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000825#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000826 time_t secs;
827 time(&secs);
828 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000829#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000830 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000831}
832
Guido van Rossumb6775db1994-08-01 11:34:53 +0000833
834/* Implement floatsleep() for various platforms.
835 When interrupted (or when another error occurs), return -1 and
836 set an exception; else return 0. */
837
838static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000839floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000840{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000841/* XXX Should test for MS_WINDOWS first! */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000842#if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000843 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000844 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000845 frac = fmod(secs, 1.0);
846 secs = floor(secs);
847 t.tv_sec = (long)secs;
848 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000849 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000850 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000851#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000852 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000853#else
854 if (1) {
855#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000856 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000857 PyErr_SetFromErrno(PyExc_IOError);
858 return -1;
859 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000860 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000861 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000862#elif defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000863 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000864 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000865 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000866 Py_END_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000867#elif defined(MS_WINDOWS)
Fred Drake0e123952000-06-29 21:31:02 +0000868 {
869 double millisecs = secs * 1000.0;
Tim Peters513a1cd2003-01-19 04:54:58 +0000870 unsigned long ul_millis;
871
Fred Drake0e123952000-06-29 21:31:02 +0000872 if (millisecs > (double)ULONG_MAX) {
Tim Peters513a1cd2003-01-19 04:54:58 +0000873 PyErr_SetString(PyExc_OverflowError,
874 "sleep length is too large");
Fred Drake0e123952000-06-29 21:31:02 +0000875 return -1;
876 }
Fred Drake0e123952000-06-29 21:31:02 +0000877 Py_BEGIN_ALLOW_THREADS
Tim Peters513a1cd2003-01-19 04:54:58 +0000878 /* Allow sleep(0) to maintain win32 semantics, and as decreed
879 * by Guido, only the main thread can be interrupted.
880 */
881 ul_millis = (unsigned long)millisecs;
882 if (ul_millis == 0 ||
883 main_thread != PyThread_get_thread_ident())
884 Sleep(ul_millis);
Mark Hammond975e3922002-07-16 01:29:19 +0000885 else {
886 DWORD rc;
887 ResetEvent(hInterruptEvent);
Tim Peters513a1cd2003-01-19 04:54:58 +0000888 rc = WaitForSingleObject(hInterruptEvent, ul_millis);
889 if (rc == WAIT_OBJECT_0) {
890 /* Yield to make sure real Python signal
891 * handler called.
892 */
Mark Hammond975e3922002-07-16 01:29:19 +0000893 Sleep(1);
894 Py_BLOCK_THREADS
Mark Hammond975e3922002-07-16 01:29:19 +0000895 errno = EINTR;
896 PyErr_SetFromErrno(PyExc_IOError);
897 return -1;
898 }
899 }
Fred Drake0e123952000-06-29 21:31:02 +0000900 Py_END_ALLOW_THREADS
901 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000902#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000903 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000904 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000905 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000906 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000907 PyErr_SetFromErrno(PyExc_IOError);
908 return -1;
909 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000910 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000911#elif defined(__BEOS__)
Guido van Rossumbcc20741998-08-04 22:53:56 +0000912 /* This sleep *CAN BE* interrupted. */
913 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000914 if( secs <= 0.0 ) {
915 return;
916 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000917
Guido van Rossumbcc20741998-08-04 22:53:56 +0000918 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000919 /* BeOS snooze() is in microseconds... */
920 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000921 Py_BLOCK_THREADS
922 PyErr_SetFromErrno( PyExc_IOError );
923 return -1;
924 }
925 Py_END_ALLOW_THREADS
926 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000927#elif defined(RISCOS)
Guido van Rossumbceccf52001-04-10 22:07:43 +0000928 if (secs <= 0.0)
929 return 0;
930 Py_BEGIN_ALLOW_THREADS
931 /* This sleep *CAN BE* interrupted. */
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000932 if ( riscos_sleep(secs) )
Guido van Rossumbceccf52001-04-10 22:07:43 +0000933 return -1;
934 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000935#elif defined(PLAN9)
936 {
937 double millisecs = secs * 1000.0;
938 if (millisecs > (double)LONG_MAX) {
939 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
940 return -1;
941 }
942 /* This sleep *CAN BE* interrupted. */
943 Py_BEGIN_ALLOW_THREADS
944 if(sleep((long)millisecs) < 0){
945 Py_BLOCK_THREADS
946 PyErr_SetFromErrno(PyExc_IOError);
947 return -1;
948 }
949 Py_END_ALLOW_THREADS
950 }
951#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000952 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000953 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000954 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000955 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000956#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000957
Guido van Rossumb6775db1994-08-01 11:34:53 +0000958 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000959}
Guido van Rossumd11b62e2003-03-14 21:51:36 +0000960
961