blob: 6647ecc86909ff887a3d7037646dca6d12378ab8 [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
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000057#if defined(PYCC_VACPP)
Guido van Rossum26452411998-09-28 22:07:11 +000058#include <sys/time.h>
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000059#endif
60
Guido van Rossumbcc20741998-08-04 22:53:56 +000061#ifdef __BEOS__
Fred Drake56221a72000-08-15 18:52:33 +000062#include <time.h>
Guido van Rossumbcc20741998-08-04 22:53:56 +000063/* For bigtime_t, snooze(). - [cjh] */
64#include <support/SupportDefs.h>
65#include <kernel/OS.h>
66#endif
67
Guido van Rossum234f9421993-06-17 12:35:49 +000068/* Forward declarations */
Tim Petersdbd9ba62000-07-09 03:09:57 +000069static int floatsleep(double);
Thomas Woutersed77bac2000-07-24 15:26:39 +000070static double floattime(void);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071
Guido van Rossumcfbaecc1998-08-25 14:51:12 +000072/* For Y2K check */
73static PyObject *moddict;
74
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000075#ifdef macintosh
76/* Our own timezone. We have enough information to deduce whether
77** DST is on currently, but unfortunately we cannot put it to good
78** use because we don't know the rules (and that is needed to have
79** localtime() return correct tm_isdst values for times other than
80** the current time. So, we cop out and only tell the user the current
81** timezone.
82*/
83static long timezone;
84
Guido van Rossum10b164a2001-09-25 13:59:01 +000085static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000086initmactimezone(void)
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000087{
88 MachineLocation loc;
89 long delta;
90
91 ReadLocation(&loc);
Guido van Rossum10b164a2001-09-25 13:59:01 +000092
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000093 if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0)
94 return;
Guido van Rossum10b164a2001-09-25 13:59:01 +000095
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000096 delta = loc.u.gmtDelta & 0x00FFFFFF;
Guido van Rossum10b164a2001-09-25 13:59:01 +000097
Guido van Rossume6a4b7b1997-10-08 15:27:56 +000098 if (delta & 0x00800000)
99 delta |= 0xFF000000;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000100
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000101 timezone = -delta;
102}
103#endif /* macintosh */
104
105
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000106static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000107time_time(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000109 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000110 if (!PyArg_ParseTuple(args, ":time"))
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000111 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000112 secs = floattime();
113 if (secs == 0.0) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000114 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000115 return NULL;
116 }
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000117 return PyFloat_FromDouble(secs);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000118}
119
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000120static char time_doc[] =
121"time() -> floating point number\n\
122\n\
123Return the current time in seconds since the Epoch.\n\
124Fractions of a second may be present if the system clock provides them.";
125
Guido van Rossumb6775db1994-08-01 11:34:53 +0000126#ifdef HAVE_CLOCK
127
128#ifndef CLOCKS_PER_SEC
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000129#ifdef CLK_TCK
130#define CLOCKS_PER_SEC CLK_TCK
131#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000132#define CLOCKS_PER_SEC 1000000
133#endif
Guido van Rossum1b66a4f1996-02-25 04:50:33 +0000134#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000135
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000136static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000137time_clock(PyObject *self, PyObject *args)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000138{
Thomas Woutersfe385252001-01-19 23:16:56 +0000139 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140 return NULL;
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000141 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142}
Guido van Rossumb6775db1994-08-01 11:34:53 +0000143#endif /* HAVE_CLOCK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144
Tim Peters58e0a8c2001-05-14 22:32:33 +0000145#if defined(MS_WIN32) && !defined(MS_WIN64) && !defined(__BORLANDC__)
Mark Hammond7ba5e812002-02-12 04:02:33 +0000146/* Due to Mark Hammond and Tim Peters */
Guido van Rossum3917c221997-04-02 05:35:28 +0000147static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000148time_clock(PyObject *self, PyObject *args)
Guido van Rossum3917c221997-04-02 05:35:28 +0000149{
Tim Peters9ad4b682002-02-13 05:14:18 +0000150 static LARGE_INTEGER ctrStart;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000151 static double divisor = 0.0;
Tim Peters9ad4b682002-02-13 05:14:18 +0000152 LARGE_INTEGER now;
Mark Hammond7ba5e812002-02-12 04:02:33 +0000153 double diff;
Guido van Rossum3917c221997-04-02 05:35:28 +0000154
Thomas Woutersfe385252001-01-19 23:16:56 +0000155 if (!PyArg_ParseTuple(args, ":clock"))
Guido van Rossum3917c221997-04-02 05:35:28 +0000156 return NULL;
157
Mark Hammond7ba5e812002-02-12 04:02:33 +0000158 if (divisor == 0.0) {
Tim Peters9ad4b682002-02-13 05:14:18 +0000159 LARGE_INTEGER freq;
160 QueryPerformanceCounter(&ctrStart);
161 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
Mark Hammond7ba5e812002-02-12 04:02:33 +0000162 /* Unlikely to happen - this works on all intel
163 machines at least! Revert to clock() */
Guido van Rossum3917c221997-04-02 05:35:28 +0000164 return PyFloat_FromDouble(clock());
165 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000166 divisor = (double)freq.QuadPart;
Guido van Rossum3917c221997-04-02 05:35:28 +0000167 }
Tim Peters9ad4b682002-02-13 05:14:18 +0000168 QueryPerformanceCounter(&now);
169 diff = (double)(now.QuadPart - ctrStart.QuadPart);
Mark Hammond7ba5e812002-02-12 04:02:33 +0000170 return PyFloat_FromDouble(diff / divisor);
Guido van Rossum3917c221997-04-02 05:35:28 +0000171}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000172
Guido van Rossum3917c221997-04-02 05:35:28 +0000173#define HAVE_CLOCK /* So it gets included in the methods */
Fred Drakedfb4ebd2000-06-29 20:56:28 +0000174#endif /* MS_WIN32 && !MS_WIN64 */
Guido van Rossum3917c221997-04-02 05:35:28 +0000175
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000176#ifdef HAVE_CLOCK
177static char clock_doc[] =
178"clock() -> floating point number\n\
179\n\
180Return the CPU time or real time since the start of the process or since\n\
181the first call to clock(). This has as much precision as the system records.";
182#endif
183
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000184static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000185time_sleep(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186{
Guido van Rossum775f4da1993-01-09 17:18:52 +0000187 double secs;
Thomas Woutersfe385252001-01-19 23:16:56 +0000188 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189 return NULL;
Guido van Rossum8607ae21997-11-03 22:04:46 +0000190 if (floatsleep(secs) != 0)
191 return NULL;
192 Py_INCREF(Py_None);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000193 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194}
195
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000196static char sleep_doc[] =
197"sleep(seconds)\n\
198\n\
199Delay execution for a given number of seconds. The argument may be\n\
200a floating point number for subsecond precision.";
201
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000202static PyStructSequence_Field struct_time_type_fields[] = {
203 {"tm_year", NULL},
204 {"tm_mon", NULL},
205 {"tm_mday", NULL},
206 {"tm_hour", NULL},
207 {"tm_min", NULL},
208 {"tm_sec", NULL},
209 {"tm_wday", NULL},
210 {"tm_yday", NULL},
211 {"tm_isdst", NULL},
212 {0}
213};
214
215static PyStructSequence_Desc struct_time_type_desc = {
Guido van Rossum14648392001-12-08 18:02:58 +0000216 "time.struct_time",
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000217 NULL,
218 struct_time_type_fields,
219 9,
220};
Tim Peters9ad4b682002-02-13 05:14:18 +0000221
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000222static PyTypeObject StructTimeType;
223
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000224static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000225tmtotuple(struct tm *p)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000226{
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000227 PyObject *v = PyStructSequence_New(&StructTimeType);
228 if (v == NULL)
229 return NULL;
Tim Peters9ad4b682002-02-13 05:14:18 +0000230
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000231#define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
232
233 SET(0, p->tm_year + 1900);
234 SET(1, p->tm_mon + 1); /* Want January == 1 */
235 SET(2, p->tm_mday);
236 SET(3, p->tm_hour);
237 SET(4, p->tm_min);
238 SET(5, p->tm_sec);
239 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
240 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
241 SET(8, p->tm_isdst);
242#undef SET
243 if (PyErr_Occurred()) {
244 Py_XDECREF(v);
245 return NULL;
246 }
247
248 return v;
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000249}
250
251static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000252time_convert(time_t when, struct tm * (*function)(const time_t *))
Guido van Rossum234f9421993-06-17 12:35:49 +0000253{
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000254 struct tm *p;
255 errno = 0;
Jack Jansenee398fa2000-07-03 21:37:27 +0000256#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000257 when = when + GUSI_TO_MSL_EPOCH;
258#endif
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000259 p = function(&when);
260 if (p == NULL) {
261#ifdef EINVAL
Guido van Rossum0b1ff661996-11-02 17:31:22 +0000262 if (errno == 0)
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000263 errno = EINVAL;
264#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000265 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6e8583d1996-10-08 14:19:52 +0000266 }
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000267 return tmtotuple(p);
Guido van Rossum234f9421993-06-17 12:35:49 +0000268}
269
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000270static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000271time_gmtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000272{
273 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000274 if (PyTuple_Size(args) == 0)
275 when = floattime();
276 if (!PyArg_ParseTuple(args, "|d:gmtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000277 return NULL;
278 return time_convert((time_t)when, gmtime);
279}
280
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000281static char gmtime_doc[] =
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000282"gmtime([seconds]) -> (year,month,day,hour,minute,second,weekday,dayofyear,dst)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000283\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000284Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
285GMT). When 'seconds' is not passed in, convert the current time instead.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000286
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000287static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000288time_localtime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000289{
290 double when;
Thomas Woutersfe385252001-01-19 23:16:56 +0000291 if (PyTuple_Size(args) == 0)
292 when = floattime();
293 if (!PyArg_ParseTuple(args, "|d:localtime", &when))
Guido van Rossum234f9421993-06-17 12:35:49 +0000294 return NULL;
295 return time_convert((time_t)when, localtime);
296}
297
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000298static char localtime_doc[] =
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000299"localtime([seconds]) -> (year,month,day,hour,minute,second,weekday,dayofyear,dst)\n\
300\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000301Convert seconds since the Epoch to a time tuple expressing local time.\n\
302When 'seconds' is not passed in, convert the current time instead.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000303
Guido van Rossum9e90a671993-06-24 11:10:19 +0000304static int
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000305gettmarg(PyObject *args, struct tm *p)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000306{
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000307 int y;
Thomas Wouters334fb892000-07-25 12:56:38 +0000308 memset((void *) p, '\0', sizeof(struct tm));
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000309
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000310 if (!PyArg_Parse(args, "(iiiiiiiii)",
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000311 &y,
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000312 &p->tm_mon,
313 &p->tm_mday,
314 &p->tm_hour,
315 &p->tm_min,
316 &p->tm_sec,
317 &p->tm_wday,
318 &p->tm_yday,
319 &p->tm_isdst))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000320 return 0;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000321 if (y < 1900) {
322 PyObject *accept = PyDict_GetItemString(moddict,
323 "accept2dyear");
324 if (accept == NULL || !PyInt_Check(accept) ||
325 PyInt_AsLong(accept) == 0) {
326 PyErr_SetString(PyExc_ValueError,
327 "year >= 1900 required");
328 return 0;
329 }
330 if (69 <= y && y <= 99)
331 y += 1900;
332 else if (0 <= y && y <= 68)
333 y += 2000;
334 else {
335 PyErr_SetString(PyExc_ValueError,
Skip Montanaro1a10aac2001-08-22 12:39:16 +0000336 "year out of range");
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000337 return 0;
338 }
339 }
340 p->tm_year = y - 1900;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000341 p->tm_mon--;
342 p->tm_wday = (p->tm_wday + 1) % 7;
343 p->tm_yday--;
344 return 1;
345}
346
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000347#ifdef HAVE_STRFTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000348static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000349time_strftime(PyObject *self, PyObject *args)
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000350{
Thomas Woutersfe385252001-01-19 23:16:56 +0000351 PyObject *tup = NULL;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000352 struct tm buf;
353 const char *fmt;
Guido van Rossumfa481162000-06-28 21:33:59 +0000354 size_t fmtlen, buflen;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000355 char *outbuf = 0;
Guido van Rossumfa481162000-06-28 21:33:59 +0000356 size_t i;
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000357
Thomas Wouters334fb892000-07-25 12:56:38 +0000358 memset((void *) &buf, '\0', sizeof(buf));
Guido van Rossum1f41f841998-04-27 19:04:26 +0000359
Thomas Woutersfe385252001-01-19 23:16:56 +0000360 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000361 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000362
363 if (tup == NULL) {
364 time_t tt = time(NULL);
365 buf = *localtime(&tt);
366 } else if (!gettmarg(tup, &buf))
367 return NULL;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000368
Guido van Rossumc222ec21999-02-23 00:00:10 +0000369 fmtlen = strlen(fmt);
370
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000371 /* I hate these functions that presume you know how big the output
372 * will be ahead of time...
373 */
Guido van Rossumc222ec21999-02-23 00:00:10 +0000374 for (i = 1024; ; i += i) {
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000375 outbuf = malloc(i);
376 if (outbuf == NULL) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000377 return PyErr_NoMemory();
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000378 }
Guido van Rossumc222ec21999-02-23 00:00:10 +0000379 buflen = strftime(outbuf, i, fmt, &buf);
380 if (buflen > 0 || i >= 256 * fmtlen) {
381 /* If the buffer is 256 times as long as the format,
382 it's probably not failing for lack of room!
383 More likely, the format yields an empty result,
384 e.g. an empty format, or %Z when the timezone
385 is unknown. */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000386 PyObject *ret;
Guido van Rossumc222ec21999-02-23 00:00:10 +0000387 ret = PyString_FromStringAndSize(outbuf, buflen);
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000388 free(outbuf);
389 return ret;
390 }
391 free(outbuf);
392 }
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000393}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000394
395static char strftime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000396"strftime(format[, tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000397\n\
398Convert a time tuple to a string according to a format specification.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000399See the library reference manual for formatting codes. When the time tuple\n\
400is not present, current time as returned by localtime() is used.";
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000401#endif /* HAVE_STRFTIME */
402
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000403#ifdef HAVE_STRPTIME
Fred Drakeaff60182000-05-09 19:52:40 +0000404
405#if 0
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +0000406/* Enable this if it's not declared in <time.h> */
407extern char *strptime(const char *, const char *, struct tm *);
Fred Drakeaff60182000-05-09 19:52:40 +0000408#endif
Guido van Rossumc2068731998-10-07 16:35:25 +0000409
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000410static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000411time_strptime(PyObject *self, PyObject *args)
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000412{
413 struct tm tm;
414 char *fmt = "%a %b %d %H:%M:%S %Y";
415 char *buf;
416 char *s;
417
Jeremy Hylton7ceab652000-03-14 21:17:16 +0000418 if (!PyArg_ParseTuple(args, "s|s:strptime", &buf, &fmt))
419 return NULL;
Thomas Wouters334fb892000-07-25 12:56:38 +0000420 memset((void *) &tm, '\0', sizeof(tm));
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000421 s = strptime(buf, fmt, &tm);
422 if (s == NULL) {
423 PyErr_SetString(PyExc_ValueError, "format mismatch");
424 return NULL;
425 }
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000426 while (*s && isspace(Py_CHARMASK(*s)))
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000427 s++;
428 if (*s) {
429 PyErr_Format(PyExc_ValueError,
430 "unconverted data remains: '%.400s'", s);
431 return NULL;
432 }
433 return tmtotuple(&tm);
434}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000435
436static char strptime_doc[] =
Guido van Rossum446ccfe1999-01-07 18:29:26 +0000437"strptime(string, format) -> tuple\n\
Martin v. Löwisb3cfc1d2001-12-02 12:27:43 +0000438\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000439Parse a string to a time tuple according to a format specification.\n\
440See the library reference manual for formatting codes (same as strftime()).";
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000441#endif /* HAVE_STRPTIME */
442
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000443static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000444time_asctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000445{
Thomas Woutersfe385252001-01-19 23:16:56 +0000446 PyObject *tup = NULL;
Guido van Rossum9e90a671993-06-24 11:10:19 +0000447 struct tm buf;
448 char *p;
Thomas Woutersfe385252001-01-19 23:16:56 +0000449 if (!PyArg_ParseTuple(args, "|O:asctime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000450 return NULL;
Thomas Woutersfe385252001-01-19 23:16:56 +0000451 if (tup == NULL) {
452 time_t tt = time(NULL);
453 buf = *localtime(&tt);
454 } else if (!gettmarg(tup, &buf))
Guido van Rossum9e90a671993-06-24 11:10:19 +0000455 return NULL;
456 p = asctime(&buf);
457 if (p[24] == '\n')
458 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000459 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000460}
461
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000462static char asctime_doc[] =
Thomas Woutersfe385252001-01-19 23:16:56 +0000463"asctime([tuple]) -> string\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000464\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000465Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
466When the time tuple is not present, current time as returned by localtime()\n\
467is used.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000468
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000469static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000470time_ctime(PyObject *self, PyObject *args)
Guido van Rossum9e90a671993-06-24 11:10:19 +0000471{
472 double dt;
473 time_t tt;
474 char *p;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000475
Thomas Woutersfe385252001-01-19 23:16:56 +0000476 if (PyTuple_Size(args) == 0)
477 tt = time(NULL);
478 else {
479 if (!PyArg_ParseTuple(args, "|d:ctime", &dt))
480 return NULL;
481 tt = (time_t)dt;
482 }
Jack Jansenee398fa2000-07-03 21:37:27 +0000483#if defined(macintosh) && defined(USE_GUSI204)
Guido van Rossumc410e922000-04-26 20:40:13 +0000484 tt = tt + GUSI_TO_MSL_EPOCH;
485#endif
Guido van Rossum9e90a671993-06-24 11:10:19 +0000486 p = ctime(&tt);
Guido van Rossum78535701998-03-03 22:19:10 +0000487 if (p == NULL) {
488 PyErr_SetString(PyExc_ValueError, "unconvertible time");
489 return NULL;
490 }
Guido van Rossum9e90a671993-06-24 11:10:19 +0000491 if (p[24] == '\n')
492 p[24] = '\0';
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000493 return PyString_FromString(p);
Guido van Rossum9e90a671993-06-24 11:10:19 +0000494}
495
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000496static char ctime_doc[] =
497"ctime(seconds) -> string\n\
498\n\
499Convert a time in seconds since the Epoch to a string in local time.\n\
Thomas Woutersfe385252001-01-19 23:16:56 +0000500This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
501not present, current time as returned by localtime() is used.";
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000502
Guido van Rossum60cd8131998-03-06 17:16:21 +0000503#ifdef HAVE_MKTIME
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000504static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000505time_mktime(PyObject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000506{
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000507 PyObject *tup;
Guido van Rossum234f9421993-06-17 12:35:49 +0000508 struct tm buf;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000509 time_t tt;
Guido van Rossum43713e52000-02-29 13:59:29 +0000510 if (!PyArg_ParseTuple(args, "O:mktime", &tup))
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000511 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000512 tt = time(&tt);
513 buf = *localtime(&tt);
Guido van Rossumb2b42dd2000-01-12 16:38:20 +0000514 if (!gettmarg(tup, &buf))
Guido van Rossum234f9421993-06-17 12:35:49 +0000515 return NULL;
Guido van Rossumbceeac81996-05-23 22:53:47 +0000516 tt = mktime(&buf);
517 if (tt == (time_t)(-1)) {
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000518 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum10b164a2001-09-25 13:59:01 +0000519 "mktime argument out of range");
Guido van Rossumbceeac81996-05-23 22:53:47 +0000520 return NULL;
521 }
Jack Jansen63596ae2000-12-12 22:42:30 +0000522#if defined(macintosh) && defined(USE_GUSI211)
Guido van Rossumc410e922000-04-26 20:40:13 +0000523 tt = tt - GUSI_TO_MSL_EPOCH;
524#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000525 return PyFloat_FromDouble((double)tt);
Guido van Rossum234f9421993-06-17 12:35:49 +0000526}
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000527
528static char mktime_doc[] =
529"mktime(tuple) -> floating point number\n\
530\n\
531Convert a time tuple in local time to seconds since the Epoch.";
Guido van Rossum60cd8131998-03-06 17:16:21 +0000532#endif /* HAVE_MKTIME */
Guido van Rossum234f9421993-06-17 12:35:49 +0000533
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000534static PyMethodDef time_methods[] = {
Thomas Woutersfe385252001-01-19 23:16:56 +0000535 {"time", time_time, METH_VARARGS, time_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000536#ifdef HAVE_CLOCK
Thomas Woutersfe385252001-01-19 23:16:56 +0000537 {"clock", time_clock, METH_VARARGS, clock_doc},
Guido van Rossumb6775db1994-08-01 11:34:53 +0000538#endif
Thomas Woutersfe385252001-01-19 23:16:56 +0000539 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
540 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
541 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000542 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
Thomas Woutersfe385252001-01-19 23:16:56 +0000543 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000544#ifdef HAVE_MKTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000545 {"mktime", time_mktime, METH_VARARGS, mktime_doc},
Guido van Rossum60cd8131998-03-06 17:16:21 +0000546#endif
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000547#ifdef HAVE_STRFTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000548 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
Guido van Rossum8d8c1ee1995-09-13 17:38:35 +0000549#endif
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000550#ifdef HAVE_STRPTIME
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000551 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
Guido van Rossum87ce7bb1998-06-09 16:30:31 +0000552#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000553 {NULL, NULL} /* sentinel */
554};
555
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000556static void
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000557ins(PyObject *d, char *name, PyObject *v)
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000558{
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000559 /* Don't worry too much about errors, they'll be caught by the
560 * caller of inittime().
561 */
562 if (v)
563 PyDict_SetItemString(d, name, v);
564 Py_XDECREF(v);
Guido van Rossum8239f0f1995-01-22 00:49:01 +0000565}
566
Barry Warsaw9bfd2bf2000-09-01 09:01:32 +0000567
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000568static char module_doc[] =
569"This module provides various functions to manipulate time values.\n\
570\n\
571There are two standard representations of time. One is the number\n\
572of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
573or a floating point number (to represent fractions of seconds).\n\
574The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
575The actual value can be retrieved by calling gmtime(0).\n\
576\n\
577The other representation is a tuple of 9 integers giving local time.\n\
578The tuple items are:\n\
579 year (four digits, e.g. 1998)\n\
580 month (1-12)\n\
581 day (1-31)\n\
582 hours (0-23)\n\
583 minutes (0-59)\n\
Guido van Rossum360eb9f1999-02-22 16:19:52 +0000584 seconds (0-59)\n\
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000585 weekday (0-6, Monday is 0)\n\
586 Julian day (day in the year, 1-366)\n\
587 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
588If the DST flag is 0, the time is given in the regular time zone;\n\
589if it is 1, the time is given in the DST time zone;\n\
590if it is -1, mktime() should guess based on the date and time.\n\
591\n\
592Variables:\n\
593\n\
594timezone -- difference in seconds between UTC and local standard time\n\
595altzone -- difference in seconds between UTC and local DST time\n\
596daylight -- whether local time should reflect DST\n\
597tzname -- tuple of (standard time zone name, DST time zone name)\n\
598\n\
599Functions:\n\
600\n\
601time() -- return current time in seconds since the Epoch as a float\n\
602clock() -- return CPU time since process start as a float\n\
603sleep() -- delay for a number of seconds given as a float\n\
604gmtime() -- convert seconds since Epoch to UTC tuple\n\
605localtime() -- convert seconds since Epoch to local time tuple\n\
606asctime() -- convert time tuple to string\n\
607ctime() -- convert time in seconds to string\n\
608mktime() -- convert local time tuple to seconds since Epoch\n\
609strftime() -- convert time tuple to string according to format specification\n\
610strptime() -- parse string to time tuple according to format specification\n\
611";
Guido van Rossum10b164a2001-09-25 13:59:01 +0000612
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000613
Guido van Rossum3886bb61998-12-04 18:50:17 +0000614DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000615inittime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000616{
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000617 PyObject *m, *d;
Guido van Rossumcfbaecc1998-08-25 14:51:12 +0000618 char *p;
Guido van Rossum0ef577b1998-06-27 20:38:36 +0000619 m = Py_InitModule3("time", time_methods, module_doc);
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000620 d = PyModule_GetDict(m);
Guido van Rossumc2068731998-10-07 16:35:25 +0000621 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000622 p = Py_GETENV("PYTHONY2K");
Guido van Rossumc2068731998-10-07 16:35:25 +0000623 ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p)));
624 /* Squirrel away the module's dictionary for the y2k check */
625 Py_INCREF(d);
626 moddict = d;
Guido van Rossum10b164a2001-09-25 13:59:01 +0000627#if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
Guido van Rossum234f9421993-06-17 12:35:49 +0000628 tzset();
Guido van Rossum26452411998-09-28 22:07:11 +0000629#ifdef PYOS_OS2
630 ins(d, "timezone", PyInt_FromLong((long)_timezone));
631#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000632 ins(d, "timezone", PyInt_FromLong((long)timezone));
Guido van Rossum26452411998-09-28 22:07:11 +0000633#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000634#ifdef HAVE_ALTZONE
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000635 ins(d, "altzone", PyInt_FromLong((long)altzone));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000636#else
Guido van Rossum26452411998-09-28 22:07:11 +0000637#ifdef PYOS_OS2
638 ins(d, "altzone", PyInt_FromLong((long)_timezone-3600));
639#else /* !PYOS_OS2 */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000640 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
Guido van Rossum26452411998-09-28 22:07:11 +0000641#endif /* PYOS_OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000642#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000643 ins(d, "daylight", PyInt_FromLong((long)daylight));
644 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
Guido van Rossum10b164a2001-09-25 13:59:01 +0000645#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
Guido van Rossum0ffdd051999-04-05 21:54:14 +0000646#ifdef HAVE_TM_ZONE
Guido van Rossum234f9421993-06-17 12:35:49 +0000647 {
648#define YEAR ((time_t)((365 * 24 + 6) * 3600))
649 time_t t;
650 struct tm *p;
Guido van Rossum57731601999-03-29 19:12:04 +0000651 long janzone, julyzone;
652 char janname[10], julyname[10];
Guido van Rossum234f9421993-06-17 12:35:49 +0000653 t = (time((time_t *)0) / YEAR) * YEAR;
654 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000655 janzone = -p->tm_gmtoff;
656 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
657 janname[9] = '\0';
Guido van Rossum234f9421993-06-17 12:35:49 +0000658 t += YEAR/2;
659 p = localtime(&t);
Guido van Rossum57731601999-03-29 19:12:04 +0000660 julyzone = -p->tm_gmtoff;
661 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
662 julyname[9] = '\0';
Guido van Rossum10b164a2001-09-25 13:59:01 +0000663
Guido van Rossum57731601999-03-29 19:12:04 +0000664 if( janzone < julyzone ) {
665 /* DST is reversed in the southern hemisphere */
666 ins(d, "timezone", PyInt_FromLong(julyzone));
667 ins(d, "altzone", PyInt_FromLong(janzone));
668 ins(d, "daylight",
669 PyInt_FromLong((long)(janzone != julyzone)));
670 ins(d, "tzname",
671 Py_BuildValue("(zz)", julyname, janname));
672 } else {
673 ins(d, "timezone", PyInt_FromLong(janzone));
674 ins(d, "altzone", PyInt_FromLong(julyzone));
675 ins(d, "daylight",
676 PyInt_FromLong((long)(janzone != julyzone)));
677 ins(d, "tzname",
678 Py_BuildValue("(zz)", janname, julyname));
679 }
Guido van Rossum234f9421993-06-17 12:35:49 +0000680 }
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000681#else
682#ifdef macintosh
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000683 /* The only thing we can obtain is the current timezone
684 ** (and whether dst is currently _active_, but that is not what
685 ** we're looking for:-( )
686 */
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000687 initmactimezone();
688 ins(d, "timezone", PyInt_FromLong(timezone));
Guido van Rossumbe1eb0d1997-12-08 21:56:43 +0000689 ins(d, "altzone", PyInt_FromLong(timezone));
690 ins(d, "daylight", PyInt_FromLong((long)0));
691 ins(d, "tzname", Py_BuildValue("(zz)", "", ""));
Guido van Rossume6a4b7b1997-10-08 15:27:56 +0000692#endif /* macintosh */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000693#endif /* HAVE_TM_ZONE */
Tim Peters26ae7cd2001-03-20 03:26:49 +0000694#ifdef __CYGWIN__
695 tzset();
696 ins(d, "timezone", PyInt_FromLong(_timezone));
697 ins(d, "altzone", PyInt_FromLong(_timezone));
698 ins(d, "daylight", PyInt_FromLong(_daylight));
699 ins(d, "tzname", Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
700#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);
704 PyDict_SetItemString(d, "struct_time", (PyObject*) &StructTimeType);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000705}
706
707
Guido van Rossumb6775db1994-08-01 11:34:53 +0000708/* Implement floattime() for various platforms */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000709
Guido van Rossumb6775db1994-08-01 11:34:53 +0000710static double
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000711floattime(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000712{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000713 /* There are three ways to get the time:
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000714 (1) gettimeofday() -- resolution in microseconds
715 (2) ftime() -- resolution in milliseconds
716 (3) time() -- resolution in seconds
717 In all cases the return value is a float in seconds.
718 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
719 fail, so we fall back on ftime() or time().
720 Note: clock resolution does not imply clock accuracy! */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000721#ifdef HAVE_GETTIMEOFDAY
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000722 {
723 struct timeval t;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000724#ifdef GETTIMEOFDAY_NO_TZ
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000725 if (gettimeofday(&t) == 0)
726 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000727#else /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000728 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
729 return (double)t.tv_sec + t.tv_usec*0.000001;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000730#endif /* !GETTIMEOFDAY_NO_TZ */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000731 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000732#endif /* !HAVE_GETTIMEOFDAY */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000733 {
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000734#if defined(HAVE_FTIME)
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000735 struct timeb t;
736 ftime(&t);
737 return (double)t.time + (double)t.millitm * (double)0.001;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000738#else /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000739 time_t secs;
740 time(&secs);
741 return (double)secs;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000742#endif /* !HAVE_FTIME */
Barry Warsaw4a6cf411997-01-13 22:44:55 +0000743 }
Guido van Rossum426035c1991-02-19 12:27:35 +0000744}
745
Guido van Rossumb6775db1994-08-01 11:34:53 +0000746
747/* Implement floatsleep() for various platforms.
748 When interrupted (or when another error occurs), return -1 and
749 set an exception; else return 0. */
750
751static int
Guido van Rossuma320fd31995-03-09 12:14:15 +0000752floatsleep(double secs)
Guido van Rossum426035c1991-02-19 12:27:35 +0000753{
Guido van Rossuma78bfe11997-02-14 16:35:10 +0000754/* XXX Should test for MS_WIN32 first! */
Guido van Rossumbcc20741998-08-04 22:53:56 +0000755#if defined(HAVE_SELECT) && !defined(__BEOS__)
Guido van Rossum426035c1991-02-19 12:27:35 +0000756 struct timeval t;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000757 double frac;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000758 frac = fmod(secs, 1.0);
759 secs = floor(secs);
760 t.tv_sec = (long)secs;
761 t.tv_usec = (long)(frac*1000000.0);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000762 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000763 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000764#ifdef EINTR
Guido van Rossuma5456d51999-08-19 14:40:27 +0000765 if (errno != EINTR) {
Guido van Rossum09cbb011999-11-08 15:32:27 +0000766#else
767 if (1) {
768#endif
Andrew M. Kuchlingc24ca4b2000-03-24 20:35:20 +0000769 Py_BLOCK_THREADS
Guido van Rossuma5456d51999-08-19 14:40:27 +0000770 PyErr_SetFromErrno(PyExc_IOError);
771 return -1;
772 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000773 }
Guido van Rossum8607ae21997-11-03 22:04:46 +0000774 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000775#elif defined(macintosh)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000776#define MacTicks (* (long *)0x16A)
777 long deadline;
778 deadline = MacTicks + (long)(secs * 60.0);
779 while (MacTicks < deadline) {
Guido van Rossum8607ae21997-11-03 22:04:46 +0000780 /* XXX Should call some yielding function here */
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000781 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000782 return -1;
783 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000784#elif defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossumbceeac81996-05-23 22:53:47 +0000785 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000786 Py_BEGIN_ALLOW_THREADS
Guido van Rossumbceeac81996-05-23 22:53:47 +0000787 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000788 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000789#elif defined(MSDOS)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000790 struct timeb t1, t2;
791 double frac;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000792 extern double fmod(double, double);
793 extern double floor(double);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000794 if (secs <= 0.0)
795 return;
796 frac = fmod(secs, 1.0);
797 secs = floor(secs);
798 ftime(&t1);
799 t2.time = t1.time + (int)secs;
800 t2.millitm = t1.millitm + (int)(frac*1000.0);
801 while (t2.millitm >= 1000) {
802 t2.time++;
803 t2.millitm -= 1000;
804 }
805 for (;;) {
806#ifdef QUICKWIN
Guido van Rossum8607ae21997-11-03 22:04:46 +0000807 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000808 _wyield();
Guido van Rossum8607ae21997-11-03 22:04:46 +0000809 Py_END_ALLOW_THREADS
Guido van Rossum80c9d881991-04-16 08:47:51 +0000810#endif
Barry Warsaw9a2a8a81996-12-06 23:32:14 +0000811 if (PyErr_CheckSignals())
Guido van Rossumb6775db1994-08-01 11:34:53 +0000812 return -1;
813 ftime(&t1);
814 if (t1.time > t2.time ||
815 t1.time == t2.time && t1.millitm >= t2.millitm)
816 break;
817 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000818#elif defined(MS_WIN32)
Fred Drake0e123952000-06-29 21:31:02 +0000819 {
820 double millisecs = secs * 1000.0;
821 if (millisecs > (double)ULONG_MAX) {
822 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
823 return -1;
824 }
825 /* XXX Can't interrupt this sleep */
826 Py_BEGIN_ALLOW_THREADS
827 Sleep((unsigned long)millisecs);
828 Py_END_ALLOW_THREADS
829 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000830#elif defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000831 /* This Sleep *IS* Interruptable by Exceptions */
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000832 Py_BEGIN_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000833 if (DosSleep(secs * 1000) != NO_ERROR) {
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000834 Py_BLOCK_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000835 PyErr_SetFromErrno(PyExc_IOError);
836 return -1;
837 }
Guido van Rossum1d0d7e41997-12-29 20:03:10 +0000838 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000839#elif defined(__BEOS__)
Guido van Rossumbcc20741998-08-04 22:53:56 +0000840 /* This sleep *CAN BE* interrupted. */
841 {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000842 if( secs <= 0.0 ) {
843 return;
844 }
Guido van Rossum10b164a2001-09-25 13:59:01 +0000845
Guido van Rossumbcc20741998-08-04 22:53:56 +0000846 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd3eb5771999-03-09 16:07:23 +0000847 /* BeOS snooze() is in microseconds... */
848 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
Guido van Rossumbcc20741998-08-04 22:53:56 +0000849 Py_BLOCK_THREADS
850 PyErr_SetFromErrno( PyExc_IOError );
851 return -1;
852 }
853 Py_END_ALLOW_THREADS
854 }
Martin v. Löwis02af9642002-01-16 11:04:06 +0000855#elif defined(RISCOS)
Guido van Rossumbceccf52001-04-10 22:07:43 +0000856 if (secs <= 0.0)
857 return 0;
858 Py_BEGIN_ALLOW_THREADS
859 /* This sleep *CAN BE* interrupted. */
860 if ( sleep(secs) )
861 return -1;
862 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000863#elif defined(PLAN9)
864 {
865 double millisecs = secs * 1000.0;
866 if (millisecs > (double)LONG_MAX) {
867 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
868 return -1;
869 }
870 /* This sleep *CAN BE* interrupted. */
871 Py_BEGIN_ALLOW_THREADS
872 if(sleep((long)millisecs) < 0){
873 Py_BLOCK_THREADS
874 PyErr_SetFromErrno(PyExc_IOError);
875 return -1;
876 }
877 Py_END_ALLOW_THREADS
878 }
879#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000880 /* XXX Can't interrupt this sleep */
Guido van Rossum8607ae21997-11-03 22:04:46 +0000881 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000882 sleep((int)secs);
Guido van Rossum8607ae21997-11-03 22:04:46 +0000883 Py_END_ALLOW_THREADS
Martin v. Löwis02af9642002-01-16 11:04:06 +0000884#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +0000885
Guido van Rossumb6775db1994-08-01 11:34:53 +0000886 return 0;
Guido van Rossum80c9d881991-04-16 08:47:51 +0000887}