blob: 939682643074d8522bf5716c8c945f9ed7258ffa [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 Rossum6d946f91992-08-14 13:49:30 +00009#ifdef macintosh
Guido van Rossumb6775db1994-08-01 11:34:53 +000010#include <time.h>
Guido van Rossumc410e922000-04-26 20:40:13 +000011#include <OSUtils.h>
Jack Jansen63596ae2000-12-12 22:42:30 +000012#ifdef USE_GUSI211
Guido van Rossumc410e922000-04-26 20:40:13 +000013/* GUSI, the I/O library which has the time() function and such uses the
14** Mac epoch of 1904. MSL, the C library which has localtime() and so uses
15** the ANSI epoch of 1900.
16*/
17#define GUSI_TO_MSL_EPOCH (4*365*24*60*60)
18#endif /* USE_GUSI2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000019#else
20#include <sys/types.h>
Guido van Rossum6d946f91992-08-14 13:49:30 +000021#endif
22
Guido van Rossumb6775db1994-08-01 11:34:53 +000023#ifdef QUICKWIN
24#include <io.h>
25#endif
26
Guido van Rossumb6775db1994-08-01 11:34:53 +000027#ifdef HAVE_FTIME
28#include <sys/timeb.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000029#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000030extern int ftime(struct timeb *);
Guido van Rossum52174571996-12-09 18:38:52 +000031#endif /* MS_WINDOWS */
32#endif /* HAVE_FTIME */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033
Guido van Rossum7bf22de1997-12-02 20:34:19 +000034#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +000035#include <i86.h>
36#else
Guido van Rossumcac6c721996-09-06 13:34:02 +000037#ifdef MS_WINDOWS
Guido van Rossum258ccd42001-03-02 06:53:29 +000038#include <windows.h>
Tim Peters58e0a8c2001-05-14 22:32:33 +000039#if defined(MS_WIN16) || defined(__BORLANDC__)
Guido van Rossumb2fb3641996-09-07 00:47:35 +000040/* These overrides not needed for Win32 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000041#define timezone _timezone
Guido van Rossumcc081121995-03-14 15:05:41 +000042#define tzname _tzname
43#define daylight _daylight
Tim Peters58e0a8c2001-05-14 22:32:33 +000044#endif /* MS_WIN16 || __BORLANDC__ */
45#ifdef MS_WIN16
Guido van Rossumcc081121995-03-14 15:05:41 +000046#define altzone _altzone
Guido van Rossumb2fb3641996-09-07 00:47:35 +000047#endif /* MS_WIN16 */
Guido van Rossumcac6c721996-09-06 13:34:02 +000048#endif /* MS_WINDOWS */
Guido van Rossum7bf22de1997-12-02 20:34:19 +000049#endif /* !__WATCOMC__ || __QNX__ */
Guido van Rossum234f9421993-06-17 12:35:49 +000050
Tim Peters58e0a8c2001-05-14 22:32:33 +000051#if defined(MS_WIN32) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Fred Drakedfb4ebd2000-06-29 20:56:28 +000052/* Win32 has better clock replacement
53 XXX Win64 does not yet, but might when the platform matures. */
Guido van Rossum3917c221997-04-02 05:35:28 +000054#undef HAVE_CLOCK /* We have our own version down below */
Fred Drakedfb4ebd2000-06-29 20:56:28 +000055#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +000056
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000057#if defined(PYOS_OS2)
58#define INCL_DOS
59#define INCL_ERRORS
60#include <os2.h>
61#endif
62
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000063#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000064#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000065#endif
66
Guido van Rossumbcc20741998-08-04 22:53:56 +000067#ifdef __BEOS__
Fred Drake56221a72000-08-15 18:52:33 +000068#include <time.h>
Guido van Rossumbcc20741998-08-04 22:53:56 +000069/* For bigtime_t, snooze(). - [cjh] */
70#include <support/SupportDefs.h>
71#include <kernel/OS.h>
72#endif
73
Guido van Rossum234f9421993-06-17 12:35:49 +000074/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000075static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000076static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000078/* For Y2K check */
79static PyObject *moddict;
80
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000081#ifdef macintosh
82/* Our own timezone. We have enough information to deduce whether
83** DST is on currently, but unfortunately we cannot put it to good
84** use because we don't know the rules (and that is needed to have
85** localtime() return correct tm_isdst values for times other than
86** the current time. So, we cop out and only tell the user the current
87** timezone.
88*/
89static long timezone;
90
Guido van Rossum10b164a2001-09-25 13:59:01 +000091static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000092initmactimezone(void)
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000093{
94 MachineLocation loc;
95 long delta;
96
97 ReadLocation(&loc);
Guido van Rossum10b164a2001-09-25 13:59:01 +000098
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000099 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
100 return;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000101
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000102 delta = loc.u.gmtDelta & 0x00FFFFFF;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000103
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000104 if (delta & 0x00800000)
105 delta |= 0xFF000000;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000106
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000107 timezone = -delta;
108}
109#endif /* macintosh */
110
111
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000112static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000113time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000115 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000116 if (!PyArg_ParseTuple(args, ":time"))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000117 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000118 secs = floattime();
119 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000120 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000121 return NULL;
122 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000123 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000124}
125
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000126static char time_doc[] =
127"time() -> floating point number\n\
128\n\
129Return the current time in seconds since the Epoch.\n\
130Fractions of a second may be present if the system clock provides them.";
131
Guido van Rossumb6775db1994-08-01 11:34:53 +0000132#ifdef HAVE_CLOCK
133
134#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000135#ifdef CLK_TCK
136#define CLOCKS_PER_SEC CLK_TCK
137#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000138#define CLOCKS_PER_SEC 1000000
139#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000140#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000141
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000142static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000143time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000144{
Thomas Woutersfe385252001-01-19 23:16:56 +0000145 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000147 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000149#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150
Tim Peters58e0a8c2001-05-14 22:32:33 +0000151#if defined(MS_WIN32) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Mark Hammond7ba5e812002-02-12 04:02:33 +0000152/* Due to Mark Hammond and Tim Peters */
Guido van Rossum3917c221997-04-02 05:35:28 +0000153static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000154time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000155{
Tim Peters9ad4b682002-02-13 05:14:18 +0000156 static LARGE_INTEGER ctrStart;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000157 static double divisor = 0.0;
Tim Peters9ad4b682002-02-13 05:14:18 +0000158 LARGE_INTEGER now;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000159 double diff;
Guido van Rossum3917c221997-04-02 05:35:28 +0000160
Thomas Woutersfe385252001-01-19 23:16:56 +0000161 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum3917c221997-04-02 05:35:28 +0000162 return NULL;
163
Mark Hammond7ba5e812002-02-12 04:02:33 +0000164 if (divisor == 0.0) {
Tim Peters9ad4b682002-02-13 05:14:18 +0000165 LARGE_INTEGER freq;
166 QueryPerformanceCounter(&ctrStart);
167 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
Mark Hammond7ba5e812002-02-12 04:02:33 +0000168 /* Unlikely to happen - this works on all intel
169 machines at least! Revert to clock() */
Guido van Rossum3917c221997-04-02 05:35:28 +0000170 return PyFloat_FromDouble(clock());
171 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000172 divisor = (double)freq.QuadPart;
Guido van Rossum3917c221997-04-02 05:35:28 +0000173 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000174 QueryPerformanceCounter(&now);
175 diff = (double)(now.QuadPart - ctrStart.QuadPart);
Mark Hammond7ba5e812002-02-12 04:02:33 +0000176 return PyFloat_FromDouble(diff / divisor);
Guido van Rossum3917c221997-04-02 05:35:28 +0000177}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000178
Guido van Rossum3917c221997-04-02 05:35:28 +0000179#define HAVE_CLOCK /* So it gets included in the methods */
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000180#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000181
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000182#ifdef HAVE_CLOCK
183static char clock_doc[] =
184"clock() -> floating point number\n\
185\n\
186Return the CPU time or real time since the start of the process or since\n\
187the first call to clock(). This has as much precision as the system records.";
188#endif
189
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000190static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000191time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000192{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000193 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000194 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000195 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000196 if (floatsleep(secs) != 0)
197 return NULL;
198 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000199 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000200}
201
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000202static char sleep_doc[] =
203"sleep(seconds)\n\
204\n\
205Delay execution for a given number of seconds. The argument may be\n\
206a floating point number for subsecond precision.";
207
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000208static PyStructSequence_Field struct_time_type_fields[] = {
209 {"tm_year", NULL},
210 {"tm_mon", NULL},
211 {"tm_mday", NULL},
212 {"tm_hour", NULL},
213 {"tm_min", NULL},
214 {"tm_sec", NULL},
215 {"tm_wday", NULL},
216 {"tm_yday", NULL},
217 {"tm_isdst", NULL},
218 {0}
219};
220
221static PyStructSequence_Desc struct_time_type_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000222 "time.struct_time",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000223 NULL,
224 struct_time_type_fields,
225 9,
226};
Tim Peters9ad4b682002-02-13 05:14:18 +0000227
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000228static PyTypeObject StructTimeType;
229
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000230static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000231tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000232{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000233 PyObject *v = PyStructSequence_New(&StructTimeType);
234 if (v == NULL)
235 return NULL;
Tim Peters9ad4b682002-02-13 05:14:18 +0000236
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000237#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
238
239 SET(0, p->tm_year + 1900);
240 SET(1, p->tm_mon + 1); /* Want January == 1 */
241 SET(2, p->tm_mday);
242 SET(3, p->tm_hour);
243 SET(4, p->tm_min);
244 SET(5, p->tm_sec);
245 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
246 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
247 SET(8, p->tm_isdst);
248#undef SET
249 if (PyErr_Occurred()) {
250 Py_XDECREF(v);
251 return NULL;
252 }
253
254 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000255}
256
257static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000258time_convert(time_t when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000259{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000260 struct tm *p;
261 errno = 0;
Jack Jansenee398fa2000-07-03 21:37:27 +0000262#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000263 when = when + GUSI_TO_MSL_EPOCH;
264#endif
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000265 p = function(&when);
266 if (p == NULL) {
267#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000268 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000269 errno = EINVAL;
270#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000271 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000272 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000273 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000274}
275
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000276static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000277time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000278{
279 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000280 if (PyTuple_Size(args) == 0)
281 when = floattime();
282 if (!PyArg_ParseTuple(args, "|d:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000283 return NULL;
284 return time_convert((time_t)when, gmtime);
285}
286
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000287static char gmtime_doc[] =
Fred Drake193a3f62002-03-12 21:38:49 +0000288"gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,\n\
289 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000290\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000291Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
292GMT). When 'seconds' is not passed in, convert the current time instead.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000293
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000294static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000295time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000296{
297 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000298 if (PyTuple_Size(args) == 0)
299 when = floattime();
300 if (!PyArg_ParseTuple(args, "|d:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000301 return NULL;
302 return time_convert((time_t)when, localtime);
303}
304
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000305static char localtime_doc[] =
Fred Drake193a3f62002-03-12 21:38:49 +0000306"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 +0000307\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000308Convert seconds since the Epoch to a time tuple expressing local time.\n\
309When 'seconds' is not passed in, convert the current time instead.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000310
Guido van Rossum9e90a671993-06-24 11:10:19 +0000311static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000312gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000313{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000314 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000315 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000316
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000317 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000318 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000319 &p->tm_mon,
320 &p->tm_mday,
321 &p->tm_hour,
322 &p->tm_min,
323 &p->tm_sec,
324 &p->tm_wday,
325 &p->tm_yday,
326 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000327 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000328 if (y < 1900) {
329 PyObject *accept = PyDict_GetItemString(moddict,
330 "accept2dyear");
331 if (accept == NULL || !PyInt_Check(accept) ||
332 PyInt_AsLong(accept) == 0) {
333 PyErr_SetString(PyExc_ValueError,
334 "year >= 1900 required");
335 return 0;
336 }
337 if (69 <= y && y <= 99)
338 y += 1900;
339 else if (0 <= y && y <= 68)
340 y += 2000;
341 else {
342 PyErr_SetString(PyExc_ValueError,
Skip Montanaro1a10aac2001-08-22 12:39:16 +0000343 "year out of range");
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000344 return 0;
345 }
346 }
347 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000348 p->tm_mon--;
349 p->tm_wday = (p->tm_wday + 1) % 7;
350 p->tm_yday--;
351 return 1;
352}
353
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000354#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000355static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000356time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000357{
Thomas Woutersfe385252001-01-19 23:16:56 +0000358 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000359 struct tm buf;
360 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000361 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000362 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000363 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000364
Thomas Wouters334fb892000-07-25 12:56:38 +0000365 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000366
Thomas Woutersfe385252001-01-19 23:16:56 +0000367 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000368 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000369
370 if (tup == NULL) {
371 time_t tt = time(NULL);
372 buf = *localtime(&tt);
373 } else if (!gettmarg(tup, &buf))
374 return NULL;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000375
Guido van Rossumc222ec21999-02-23 00:00:10 +0000376 fmtlen = strlen(fmt);
377
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000378 /* I hate these functions that presume you know how big the output
379 * will be ahead of time...
380 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000381 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000382 outbuf = malloc(i);
383 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000384 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000385 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000386 buflen = strftime(outbuf, i, fmt, &buf);
387 if (buflen > 0 || i >= 256 * fmtlen) {
388 /* If the buffer is 256 times as long as the format,
389 it's probably not failing for lack of room!
390 More likely, the format yields an empty result,
391 e.g. an empty format, or %Z when the timezone
392 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000393 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000394 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000395 free(outbuf);
396 return ret;
397 }
398 free(outbuf);
399 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000400}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000401
402static char strftime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000403"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000404\n\
405Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000406See the library reference manual for formatting codes. When the time tuple\n\
407is not present, current time as returned by localtime() is used.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000408#endif /* HAVE_STRFTIME */
409
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000410#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000411
412#if 0
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000413/* Enable this if it's not declared in <time.h> */
414extern char *strptime(const char *, const char *, struct tm *);
Fred Drakeaff60182000-05-09 19:52:40 +0000415#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000416
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000417static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000418time_strptime(PyObject *self, PyObject *args)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000419{
420 struct tm tm;
421 char *fmt = "%a %b %d %H:%M:%S %Y";
422 char *buf;
423 char *s;
424
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000425 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
426 return NULL;
Thomas Wouters334fb892000-07-25 12:56:38 +0000427 memset((void *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000428 s = strptime(buf, fmt, &tm);
429 if (s == NULL) {
430 PyErr_SetString(PyExc_ValueError, "format mismatch");
431 return NULL;
432 }
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000433 while (*s && isspace(Py_CHARMASK(*s)))
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000434 s++;
435 if (*s) {
436 PyErr_Format(PyExc_ValueError,
437 "unconverted data remains: '%.400s'", s);
438 return NULL;
439 }
440 return tmtotuple(&tm);
441}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000442
443static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000444"strptime(string, format) -> tuple\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000445\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000446Parse a string to a time tuple according to a format specification.\n\
447See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000448#endif /* HAVE_STRPTIME */
449
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000450static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000451time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000452{
Thomas Woutersfe385252001-01-19 23:16:56 +0000453 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000454 struct tm buf;
455 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000456 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000457 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000458 if (tup == NULL) {
459 time_t tt = time(NULL);
460 buf = *localtime(&tt);
461 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000462 return NULL;
463 p = asctime(&buf);
464 if (p[24] == '\n')
465 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000466 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000467}
468
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000469static char asctime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000470"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000471\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000472Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
473When the time tuple is not present, current time as returned by localtime()\n\
474is used.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000475
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000476static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000477time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000478{
479 double dt;
480 time_t tt;
481 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000482
Thomas Woutersfe385252001-01-19 23:16:56 +0000483 if (PyTuple_Size(args) == 0)
484 tt = time(NULL);
485 else {
486 if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
487 return NULL;
488 tt = (time_t)dt;
489 }
Jack Jansenee398fa2000-07-03 21:37:27 +0000490#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000491 tt = tt + GUSI_TO_MSL_EPOCH;
492#endif
Guido van Rossum9e90a671993-06-24 11:10:19 +0000493 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000494 if (p == NULL) {
495 PyErr_SetString(PyExc_ValueError, "unconvertible time");
496 return NULL;
497 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000498 if (p[24] == '\n')
499 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000500 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000501}
502
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000503static char ctime_doc[] =
504"ctime(seconds) -> string\n\
505\n\
506Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000507This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
508not present, current time as returned by localtime() is used.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000509
Guido van Rossum60cd8131998-03-06 17:16:21 +0000510#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000511static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000512time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000513{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000514 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000515 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000516 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000517 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000518 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000519 tt = time(&tt);
520 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000521 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000522 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000523 tt = mktime(&buf);
524 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000525 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000526 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000527 return NULL;
528 }
Jack Jansen63596ae2000-12-12 22:42:30 +0000529#if defined(macintosh) && defined(USE_GUSI211)
Guido van Rossumc410e922000-04-26 20:40:13 +0000530 tt = tt - GUSI_TO_MSL_EPOCH;
531#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000532 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000533}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000534
535static char mktime_doc[] =
536"mktime(tuple) -> floating point number\n\
537\n\
538Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000539#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000540
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000541static PyMethodDef time_methods[] = {
Thomas Woutersfe385252001-01-19 23:16:56 +0000542 {"time", time_time, METH_VARARGS, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000543#ifdef HAVE_CLOCK
Thomas Woutersfe385252001-01-19 23:16:56 +0000544 {"clock", time_clock, METH_VARARGS, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000545#endif
Thomas Woutersfe385252001-01-19 23:16:56 +0000546 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
547 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
548 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000549 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
Thomas Woutersfe385252001-01-19 23:16:56 +0000550 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000551#ifdef HAVE_MKTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000552 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000553#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000554#ifdef HAVE_STRFTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000555 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000556#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000557#ifdef HAVE_STRPTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000558 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000559#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560 {NULL, NULL} /* sentinel */
561};
562
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000563
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000564static char module_doc[] =
565"This module provides various functions to manipulate time values.\n\
566\n\
567There are two standard representations of time. One is the number\n\
568of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
569or a floating point number (to represent fractions of seconds).\n\
570The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
571The actual value can be retrieved by calling gmtime(0).\n\
572\n\
573The other representation is a tuple of 9 integers giving local time.\n\
574The tuple items are:\n\
575 year (four digits, e.g. 1998)\n\
576 month (1-12)\n\
577 day (1-31)\n\
578 hours (0-23)\n\
579 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000580 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000581 weekday (0-6, Monday is 0)\n\
582 Julian day (day in the year, 1-366)\n\
583 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
584If the DST flag is 0, the time is given in the regular time zone;\n\
585if it is 1, the time is given in the DST time zone;\n\
586if it is -1, mktime() should guess based on the date and time.\n\
587\n\
588Variables:\n\
589\n\
590timezone -- difference in seconds between UTC and local standard time\n\
591altzone -- difference in seconds between UTC and local DST time\n\
592daylight -- whether local time should reflect DST\n\
593tzname -- tuple of (standard time zone name, DST time zone name)\n\
594\n\
595Functions:\n\
596\n\
597time() -- return current time in seconds since the Epoch as a float\n\
598clock() -- return CPU time since process start as a float\n\
599sleep() -- delay for a number of seconds given as a float\n\
600gmtime() -- convert seconds since Epoch to UTC tuple\n\
601localtime() -- convert seconds since Epoch to local time tuple\n\
602asctime() -- convert time tuple to string\n\
603ctime() -- convert time in seconds to string\n\
604mktime() -- convert local time tuple to seconds since Epoch\n\
605strftime() -- convert time tuple to string according to format specification\n\
606strptime() -- parse string to time tuple according to format specification\n\
607";
Guido van Rossum10b164a2001-09-25 13:59:01 +0000608
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000609
Guido van Rossum3886bb61998-12-04 18:50:17 +0000610DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000611inittime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000612{
Fred Drake9bb74322002-04-01 14:49:59 +0000613 PyObject *m;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000614 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000615 m = Py_InitModule3("time", time_methods, module_doc);
Fred Drake9bb74322002-04-01 14:49:59 +0000616
Guido van Rossumc2068731998-10-07 16:35:25 +0000617 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000618 p = Py_GETENV("PYTHONY2K");
Fred Drake9bb74322002-04-01 14:49:59 +0000619 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
Guido van Rossumc2068731998-10-07 16:35:25 +0000620 /* Squirrel away the module's dictionary for the y2k check */
Fred Drake9bb74322002-04-01 14:49:59 +0000621 moddict = PyModule_GetDict(m);
622 Py_INCREF(moddict);
Guido van Rossum10b164a2001-09-25 13:59:01 +0000623#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000624 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000625#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000626 PyModule_AddIntConstant(m, "timezone", _timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000627#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000628 PyModule_AddIntConstant(m, "timezone", timezone);
Guido van Rossum26452411998-09-28 22:07:11 +0000629#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000630#ifdef HAVE_ALTZONE
Fred Drake9bb74322002-04-01 14:49:59 +0000631 PyModule_AddIntConstant(m, "altzone", altzone);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000632#else
Guido van Rossum26452411998-09-28 22:07:11 +0000633#ifdef PYOS_OS2
Fred Drake9bb74322002-04-01 14:49:59 +0000634 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000635#else /* !PYOS_OS2 */
Fred Drake9bb74322002-04-01 14:49:59 +0000636 PyModule_AddIntConstant(m, "altzone", timezone-3600);
Guido van Rossum26452411998-09-28 22:07:11 +0000637#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000638#endif
Fred Drake9bb74322002-04-01 14:49:59 +0000639 PyModule_AddIntConstant(m, "daylight", daylight);
640 PyModule_AddObject(m, "tzname",
641 Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000642#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000643#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000644 {
645#define YEAR ((time_t)((365 * 24 + 6) * 3600))
646 time_t t;
647 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000648 long janzone, julyzone;
649 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000650 t = (time((time_t *)0) / YEAR) * YEAR;
651 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000652 janzone = -p->tm_gmtoff;
653 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
654 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000655 t += YEAR/2;
656 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000657 julyzone = -p->tm_gmtoff;
658 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
659 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000660
Guido van Rossum57731601999-03-29 19:12:04 +0000661 if( janzone < julyzone ) {
662 /* DST is reversed in the southern hemisphere */
Fred Drake9bb74322002-04-01 14:49:59 +0000663 PyModule_AddIntConstant(m, "timezone", julyzone);
664 PyModule_AddIntConstant(m, "altzone", janzone);
665 PyModule_AddIntConstant(m, "daylight",
666 janzone != julyzone);
667 PyModule_AddObject(m, "tzname",
668 Py_BuildValue("(zz)",
669 julyname, janname));
Guido van Rossum57731601999-03-29 19:12:04 +0000670 } else {
Fred Drake9bb74322002-04-01 14:49:59 +0000671 PyModule_AddIntConstant(m, "timezone", janzone);
672 PyModule_AddIntConstant(m, "altzone", julyzone);
673 PyModule_AddIntConstant(m, "daylight",
674 janzone != julyzone);
675 PyModule_AddObject(m, "tzname",
676 Py_BuildValue("(zz)",
677 janname, julyname));
Guido van Rossum57731601999-03-29 19:12:04 +0000678 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000679 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000680#else
681#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000682 /* The only thing we can obtain is the current timezone
683 ** (and whether dst is currently _active_, but that is not what
684 ** we're looking for:-( )
685 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000686 initmactimezone();
Fred Drake9bb74322002-04-01 14:49:59 +0000687 PyModule_AddIntConstant(m, "timezone", timezone);
688 PyModule_AddIntConstant(m, "altzone", timezone);
689 PyModule_AddIntConstant(m, "daylight", 0);
690 PyModule_AddObject(m, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000691#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000692#endif /* HAVE_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000693#ifdef __CYGWIN__
694 tzset();
Fred Drake9bb74322002-04-01 14:49:59 +0000695 PyModule_AddIntConstant(m, "timezone", _timezone);
696 PyModule_AddIntConstant(m, "altzone", _timezone);
697 PyModule_AddIntConstant(m, "daylight", _daylight);
698 PyModule_AddObject(m, "tzname",
699 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
Tim Peters26ae7cd2001-03-20 03:26:49 +0000700#endif /* __CYGWIN__ */
Guido van Rossum10b164a2001-09-25 13:59:01 +0000701#endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000702
703 PyStructSequence_InitType(&StructTimeType, &struct_time_type_desc);
Fred Drake9bb74322002-04-01 14:49:59 +0000704 Py_INCREF(&StructTimeType);
705 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000706}
707
708
Guido van Rossumb6775db1994-08-01 11:34:53 +0000709/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000710
Guido van Rossumb6775db1994-08-01 11:34:53 +0000711static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000712floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000713{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000714 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000715 (1) gettimeofday() -- resolution in microseconds
716 (2) ftime() -- resolution in milliseconds
717 (3) time() -- resolution in seconds
718 In all cases the return value is a float in seconds.
719 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
720 fail, so we fall back on ftime() or time().
721 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000722#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000723 {
724 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000725#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000726 if (gettimeofday(&t) == 0)
727 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000728#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000729 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
730 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000731#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000732 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000733#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000734 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000735#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000736 struct timeb t;
737 ftime(&t);
738 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000739#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000740 time_t secs;
741 time(&secs);
742 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000743#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000744 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000745}
746
Guido van Rossumb6775db1994-08-01 11:34:53 +0000747
748/* Implement floatsleep() for various platforms.
749 When interrupted (or when another error occurs), return -1 and
750 set an exception; else return 0. */
751
752static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000753floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000754{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000755/* XXX Should test for MS_WIN32 first! */
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000756#if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000757 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000758 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000759 frac = fmod(secs, 1.0);
760 secs = floor(secs);
761 t.tv_sec = (long)secs;
762 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000763 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000764 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000765#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000766 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000767#else
768 if (1) {
769#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000770 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000771 PyErr_SetFromErrno(PyExc_IOError);
772 return -1;
773 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000774 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000775 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000776#elif defined(macintosh)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000777#define MacTicks (* (long *)0x16A)
778 long deadline;
779 deadline = MacTicks + (long)(secs * 60.0);
780 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000781 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000782 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000783 return -1;
784 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000785#elif defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000786 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000787 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000788 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000789 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000790#elif defined(MSDOS)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000791 struct timeb t1, t2;
792 double frac;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000793 extern double fmod(double, double);
794 extern double floor(double);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000795 if (secs <= 0.0)
796 return;
797 frac = fmod(secs, 1.0);
798 secs = floor(secs);
799 ftime(&t1);
800 t2.time = t1.time + (int)secs;
801 t2.millitm = t1.millitm + (int)(frac*1000.0);
802 while (t2.millitm >= 1000) {
803 t2.time++;
804 t2.millitm -= 1000;
805 }
806 for (;;) {
807#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000808 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000809 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000810 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000811#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000812 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000813 return -1;
814 ftime(&t1);
815 if (t1.time > t2.time ||
816 t1.time == t2.time && t1.millitm >= t2.millitm)
817 break;
818 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000819#elif defined(MS_WIN32)
Fred Drake0e123952000-06-29 21:31:02 +0000820 {
821 double millisecs = secs * 1000.0;
822 if (millisecs > (double)ULONG_MAX) {
823 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
824 return -1;
825 }
826 /* XXX Can't interrupt this sleep */
827 Py_BEGIN_ALLOW_THREADS
828 Sleep((unsigned long)millisecs);
829 Py_END_ALLOW_THREADS
830 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000831#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000832 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000833 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000834 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000835 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000836 PyErr_SetFromErrno(PyExc_IOError);
837 return -1;
838 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000839 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000840#elif defined(__BEOS__)
Guido van Rossumbcc20741998-08-04 22:53:56 +0000841 /* This sleep *CAN BE* interrupted. */
842 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000843 if( secs <= 0.0 ) {
844 return;
845 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000846
Guido van Rossumbcc20741998-08-04 22:53:56 +0000847 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000848 /* BeOS snooze() is in microseconds... */
849 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000850 Py_BLOCK_THREADS
851 PyErr_SetFromErrno( PyExc_IOError );
852 return -1;
853 }
854 Py_END_ALLOW_THREADS
855 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000856#elif defined(RISCOS)
Guido van Rossumbceccf52001-04-10 22:07:43 +0000857 if (secs <= 0.0)
858 return 0;
859 Py_BEGIN_ALLOW_THREADS
860 /* This sleep *CAN BE* interrupted. */
861 if ( sleep(secs) )
862 return -1;
863 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000864#elif defined(PLAN9)
865 {
866 double millisecs = secs * 1000.0;
867 if (millisecs > (double)LONG_MAX) {
868 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
869 return -1;
870 }
871 /* This sleep *CAN BE* interrupted. */
872 Py_BEGIN_ALLOW_THREADS
873 if(sleep((long)millisecs) < 0){
874 Py_BLOCK_THREADS
875 PyErr_SetFromErrno(PyExc_IOError);
876 return -1;
877 }
878 Py_END_ALLOW_THREADS
879 }
880#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000881 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000882 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000883 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000884 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000885#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000886
Guido van Rossumb6775db1994-08-01 11:34:53 +0000887 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000888}