Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Time module */ |
| 33 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 36 | #include <ctype.h> |
| 37 | |
Guido van Rossum | 99d90c0 | 1996-08-08 19:17:45 +0000 | [diff] [blame] | 38 | #ifdef HAVE_SELECT |
| 39 | #include "mymath.h" |
| 40 | #endif |
| 41 | |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 42 | #ifdef macintosh |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 43 | #include <time.h> |
| 44 | #else |
| 45 | #include <sys/types.h> |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 48 | #ifdef QUICKWIN |
| 49 | #include <io.h> |
| 50 | #endif |
| 51 | |
| 52 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 53 | #include <unistd.h> |
| 54 | #endif |
| 55 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 56 | #if defined(HAVE_SELECT) && !defined(__BEOS__) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 57 | #include "myselect.h" |
| 58 | #else |
| 59 | #include "mytime.h" |
| 60 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 62 | #ifdef HAVE_FTIME |
| 63 | #include <sys/timeb.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 64 | #if !defined(MS_WINDOWS) && !defined(PYOS_OS2) |
Guido van Rossum | 1bb126f | 1996-12-06 20:17:44 +0000 | [diff] [blame] | 65 | extern int ftime(); |
Guido van Rossum | 5217457 | 1996-12-09 18:38:52 +0000 | [diff] [blame] | 66 | #endif /* MS_WINDOWS */ |
| 67 | #endif /* HAVE_FTIME */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 69 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 70 | #include <i86.h> |
| 71 | #else |
Guido van Rossum | cac6c72 | 1996-09-06 13:34:02 +0000 | [diff] [blame] | 72 | #ifdef MS_WINDOWS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 73 | #include <windows.h> |
Guido van Rossum | b2fb364 | 1996-09-07 00:47:35 +0000 | [diff] [blame] | 74 | #ifdef MS_WIN16 |
| 75 | /* These overrides not needed for Win32 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 76 | #define timezone _timezone |
Guido van Rossum | cc08112 | 1995-03-14 15:05:41 +0000 | [diff] [blame] | 77 | #define tzname _tzname |
| 78 | #define daylight _daylight |
| 79 | #define altzone _altzone |
Guido van Rossum | b2fb364 | 1996-09-07 00:47:35 +0000 | [diff] [blame] | 80 | #endif /* MS_WIN16 */ |
Guido van Rossum | cac6c72 | 1996-09-06 13:34:02 +0000 | [diff] [blame] | 81 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 82 | #endif /* !__WATCOMC__ || __QNX__ */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 84 | #ifdef MS_WIN32 |
| 85 | /* Win32 has better clock replacement */ |
| 86 | #include <largeint.h> |
| 87 | #undef HAVE_CLOCK /* We have our own version down below */ |
| 88 | #endif /* MS_WIN32 */ |
| 89 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 90 | #if defined(PYCC_VACPP) |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 91 | #include <sys/time.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 92 | #endif |
| 93 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 94 | #ifdef __BEOS__ |
| 95 | /* For bigtime_t, snooze(). - [cjh] */ |
| 96 | #include <support/SupportDefs.h> |
| 97 | #include <kernel/OS.h> |
Guido van Rossum | d3eb577 | 1999-03-09 16:07:23 +0000 | [diff] [blame] | 98 | #ifndef CLOCKS_PER_SEC |
| 99 | /* C'mon, fix the bloody headers... - [cjh] */ |
| 100 | #define CLOCKS_PER_SEC 1000 |
| 101 | #endif |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 102 | #endif |
| 103 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 104 | /* Forward declarations */ |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 105 | static int floatsleep Py_PROTO((double)); |
| 106 | static double floattime Py_PROTO(()); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 108 | /* For Y2K check */ |
| 109 | static PyObject *moddict; |
| 110 | |
Guido van Rossum | e6a4b7b | 1997-10-08 15:27:56 +0000 | [diff] [blame] | 111 | #ifdef macintosh |
| 112 | /* Our own timezone. We have enough information to deduce whether |
| 113 | ** DST is on currently, but unfortunately we cannot put it to good |
| 114 | ** use because we don't know the rules (and that is needed to have |
| 115 | ** localtime() return correct tm_isdst values for times other than |
| 116 | ** the current time. So, we cop out and only tell the user the current |
| 117 | ** timezone. |
| 118 | */ |
| 119 | static long timezone; |
| 120 | |
| 121 | static void |
| 122 | initmactimezone() |
| 123 | { |
| 124 | MachineLocation loc; |
| 125 | long delta; |
| 126 | |
| 127 | ReadLocation(&loc); |
| 128 | |
| 129 | if (loc.latitude == 0 && loc.longitude == 0 && loc.u.gmtDelta == 0) |
| 130 | return; |
| 131 | |
| 132 | delta = loc.u.gmtDelta & 0x00FFFFFF; |
| 133 | |
| 134 | if (delta & 0x00800000) |
| 135 | delta |= 0xFF000000; |
| 136 | |
| 137 | timezone = -delta; |
| 138 | } |
| 139 | #endif /* macintosh */ |
| 140 | |
| 141 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 142 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 143 | time_time(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 144 | PyObject *self; |
| 145 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 146 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 147 | double secs; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 148 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 149 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 150 | secs = floattime(); |
| 151 | if (secs == 0.0) { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 152 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 153 | return NULL; |
| 154 | } |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 155 | return PyFloat_FromDouble(secs); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 158 | static char time_doc[] = |
| 159 | "time() -> floating point number\n\ |
| 160 | \n\ |
| 161 | Return the current time in seconds since the Epoch.\n\ |
| 162 | Fractions of a second may be present if the system clock provides them."; |
| 163 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 164 | #ifdef HAVE_CLOCK |
| 165 | |
| 166 | #ifndef CLOCKS_PER_SEC |
Guido van Rossum | 1b66a4f | 1996-02-25 04:50:33 +0000 | [diff] [blame] | 167 | #ifdef CLK_TCK |
| 168 | #define CLOCKS_PER_SEC CLK_TCK |
| 169 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 170 | #define CLOCKS_PER_SEC 1000000 |
| 171 | #endif |
Guido van Rossum | 1b66a4f | 1996-02-25 04:50:33 +0000 | [diff] [blame] | 172 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 173 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 174 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 175 | time_clock(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 176 | PyObject *self; |
| 177 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 178 | { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 179 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 180 | return NULL; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 181 | return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 182 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 183 | #endif /* HAVE_CLOCK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 185 | #ifdef MS_WIN32 |
| 186 | /* Due to Mark Hammond */ |
| 187 | static PyObject * |
| 188 | time_clock(self, args) |
| 189 | PyObject *self; |
| 190 | PyObject *args; |
| 191 | { |
| 192 | static LARGE_INTEGER ctrStart; |
| 193 | static LARGE_INTEGER divisor = {0,0}; |
| 194 | LARGE_INTEGER now, diff, rem; |
| 195 | |
| 196 | if (!PyArg_NoArgs(args)) |
| 197 | return NULL; |
| 198 | |
| 199 | if (LargeIntegerEqualToZero(divisor)) { |
| 200 | QueryPerformanceCounter(&ctrStart); |
| 201 | if (!QueryPerformanceFrequency(&divisor) || |
| 202 | LargeIntegerEqualToZero(divisor)) { |
| 203 | /* Unlikely to happen - |
| 204 | this works on all intel machines at least! |
| 205 | Revert to clock() */ |
| 206 | return PyFloat_FromDouble(clock()); |
| 207 | } |
| 208 | } |
| 209 | QueryPerformanceCounter(&now); |
| 210 | diff = LargeIntegerSubtract(now, ctrStart); |
| 211 | diff = LargeIntegerDivide(diff, divisor, &rem); |
| 212 | /* XXX - we assume both divide results fit in 32 bits. This is |
| 213 | true on Intels. First person who can afford a machine that |
| 214 | doesnt deserves to fix it :-) |
| 215 | */ |
| 216 | return PyFloat_FromDouble((double)diff.LowPart + |
| 217 | ((double)rem.LowPart / (double)divisor.LowPart)); |
| 218 | } |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 220 | #define HAVE_CLOCK /* So it gets included in the methods */ |
| 221 | #endif /* MS_WIN32 */ |
| 222 | |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 223 | #ifdef HAVE_CLOCK |
| 224 | static char clock_doc[] = |
| 225 | "clock() -> floating point number\n\ |
| 226 | \n\ |
| 227 | Return the CPU time or real time since the start of the process or since\n\ |
| 228 | the first call to clock(). This has as much precision as the system records."; |
| 229 | #endif |
| 230 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 231 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 232 | time_sleep(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 233 | PyObject *self; |
| 234 | PyObject *args; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 235 | { |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 236 | double secs; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 237 | if (!PyArg_Parse(args, "d", &secs)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 238 | return NULL; |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 239 | if (floatsleep(secs) != 0) |
| 240 | return NULL; |
| 241 | Py_INCREF(Py_None); |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 242 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 245 | static char sleep_doc[] = |
| 246 | "sleep(seconds)\n\ |
| 247 | \n\ |
| 248 | Delay execution for a given number of seconds. The argument may be\n\ |
| 249 | a floating point number for subsecond precision."; |
| 250 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 251 | static PyObject * |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 252 | tmtotuple(p) |
| 253 | struct tm *p; |
| 254 | { |
| 255 | return Py_BuildValue("(iiiiiiiii)", |
| 256 | p->tm_year + 1900, |
| 257 | p->tm_mon + 1, /* Want January == 1 */ |
| 258 | p->tm_mday, |
| 259 | p->tm_hour, |
| 260 | p->tm_min, |
| 261 | p->tm_sec, |
| 262 | (p->tm_wday + 6) % 7, /* Want Monday == 0 */ |
| 263 | p->tm_yday + 1, /* Want January, 1 == 1 */ |
| 264 | p->tm_isdst); |
| 265 | } |
| 266 | |
| 267 | static PyObject * |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 268 | time_convert(when, function) |
| 269 | time_t when; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 270 | struct tm * (*function) Py_PROTO((const time_t *)); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 271 | { |
Guido van Rossum | 6e8583d | 1996-10-08 14:19:52 +0000 | [diff] [blame] | 272 | struct tm *p; |
| 273 | errno = 0; |
| 274 | p = function(&when); |
| 275 | if (p == NULL) { |
| 276 | #ifdef EINVAL |
Guido van Rossum | 0b1ff66 | 1996-11-02 17:31:22 +0000 | [diff] [blame] | 277 | if (errno == 0) |
Guido van Rossum | 6e8583d | 1996-10-08 14:19:52 +0000 | [diff] [blame] | 278 | errno = EINVAL; |
| 279 | #endif |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 280 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 6e8583d | 1996-10-08 14:19:52 +0000 | [diff] [blame] | 281 | } |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 282 | return tmtotuple(p); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 285 | static PyObject * |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 286 | time_gmtime(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 287 | PyObject *self; |
| 288 | PyObject *args; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 289 | { |
| 290 | double when; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 291 | if (!PyArg_Parse(args, "d", &when)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 292 | return NULL; |
| 293 | return time_convert((time_t)when, gmtime); |
| 294 | } |
| 295 | |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 296 | static char gmtime_doc[] = |
| 297 | "gmtime(seconds) -> tuple\n\ |
| 298 | \n\ |
| 299 | Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT)."; |
| 300 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 301 | static PyObject * |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 302 | time_localtime(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 303 | PyObject *self; |
| 304 | PyObject *args; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 305 | { |
| 306 | double when; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 307 | if (!PyArg_Parse(args, "d", &when)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 308 | return NULL; |
| 309 | return time_convert((time_t)when, localtime); |
| 310 | } |
| 311 | |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 312 | static char localtime_doc[] = |
| 313 | "localtime(seconds) -> tuple\n\ |
| 314 | Convert seconds since the Epoch to a time tuple expressing local time."; |
| 315 | |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 316 | static int |
| 317 | gettmarg(args, p) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 318 | PyObject *args; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 319 | struct tm *p; |
| 320 | { |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 321 | int y; |
| 322 | memset((ANY *) p, '\0', sizeof(struct tm)); |
| 323 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 324 | if (!PyArg_Parse(args, "(iiiiiiiii)", |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 325 | &y, |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 326 | &p->tm_mon, |
| 327 | &p->tm_mday, |
| 328 | &p->tm_hour, |
| 329 | &p->tm_min, |
| 330 | &p->tm_sec, |
| 331 | &p->tm_wday, |
| 332 | &p->tm_yday, |
| 333 | &p->tm_isdst)) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 334 | return 0; |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 335 | if (y < 1900) { |
| 336 | PyObject *accept = PyDict_GetItemString(moddict, |
| 337 | "accept2dyear"); |
| 338 | if (accept == NULL || !PyInt_Check(accept) || |
| 339 | PyInt_AsLong(accept) == 0) { |
| 340 | PyErr_SetString(PyExc_ValueError, |
| 341 | "year >= 1900 required"); |
| 342 | return 0; |
| 343 | } |
| 344 | if (69 <= y && y <= 99) |
| 345 | y += 1900; |
| 346 | else if (0 <= y && y <= 68) |
| 347 | y += 2000; |
| 348 | else { |
| 349 | PyErr_SetString(PyExc_ValueError, |
| 350 | "year out of range (00-99, 1900-*)"); |
| 351 | return 0; |
| 352 | } |
| 353 | } |
| 354 | p->tm_year = y - 1900; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 355 | p->tm_mon--; |
| 356 | p->tm_wday = (p->tm_wday + 1) % 7; |
| 357 | p->tm_yday--; |
| 358 | return 1; |
| 359 | } |
| 360 | |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 361 | #ifdef HAVE_STRFTIME |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 362 | static PyObject * |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 363 | time_strftime(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 364 | PyObject *self; |
| 365 | PyObject *args; |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 366 | { |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 367 | PyObject *tup; |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 368 | struct tm buf; |
| 369 | const char *fmt; |
Guido van Rossum | c222ec2 | 1999-02-23 00:00:10 +0000 | [diff] [blame] | 370 | int fmtlen, buflen; |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 371 | char *outbuf = 0; |
| 372 | int i; |
| 373 | |
Guido van Rossum | 1f41f84 | 1998-04-27 19:04:26 +0000 | [diff] [blame] | 374 | memset((ANY *) &buf, '\0', sizeof(buf)); |
| 375 | |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 376 | if (!PyArg_ParseTuple(args, "sO", &fmt, &tup) || !gettmarg(tup, &buf)) |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 377 | return NULL; |
Guido van Rossum | c222ec2 | 1999-02-23 00:00:10 +0000 | [diff] [blame] | 378 | fmtlen = strlen(fmt); |
| 379 | |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 380 | /* I hate these functions that presume you know how big the output |
| 381 | * will be ahead of time... |
| 382 | */ |
Guido van Rossum | c222ec2 | 1999-02-23 00:00:10 +0000 | [diff] [blame] | 383 | for (i = 1024; ; i += i) { |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 384 | outbuf = malloc(i); |
| 385 | if (outbuf == NULL) { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 386 | return PyErr_NoMemory(); |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 387 | } |
Guido van Rossum | c222ec2 | 1999-02-23 00:00:10 +0000 | [diff] [blame] | 388 | buflen = strftime(outbuf, i, fmt, &buf); |
| 389 | if (buflen > 0 || i >= 256 * fmtlen) { |
| 390 | /* If the buffer is 256 times as long as the format, |
| 391 | it's probably not failing for lack of room! |
| 392 | More likely, the format yields an empty result, |
| 393 | e.g. an empty format, or %Z when the timezone |
| 394 | is unknown. */ |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 395 | PyObject *ret; |
Guido van Rossum | c222ec2 | 1999-02-23 00:00:10 +0000 | [diff] [blame] | 396 | ret = PyString_FromStringAndSize(outbuf, buflen); |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 397 | free(outbuf); |
| 398 | return ret; |
| 399 | } |
| 400 | free(outbuf); |
| 401 | } |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 402 | } |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 403 | |
| 404 | static char strftime_doc[] = |
| 405 | "strftime(format, tuple) -> string\n\ |
| 406 | \n\ |
| 407 | Convert a time tuple to a string according to a format specification.\n\ |
| 408 | See the library reference manual for formatting codes."; |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 409 | #endif /* HAVE_STRFTIME */ |
| 410 | |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 411 | #ifdef HAVE_STRPTIME |
Guido van Rossum | 0a6363d | 1999-01-03 13:00:34 +0000 | [diff] [blame] | 412 | /* extern char *strptime(); /* Enable this if it's not declared in <time.h> */ |
Guido van Rossum | c206873 | 1998-10-07 16:35:25 +0000 | [diff] [blame] | 413 | |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 414 | static PyObject * |
| 415 | time_strptime(self, args) |
| 416 | PyObject *self; |
| 417 | PyObject *args; |
| 418 | { |
| 419 | struct tm tm; |
| 420 | char *fmt = "%a %b %d %H:%M:%S %Y"; |
| 421 | char *buf; |
| 422 | char *s; |
| 423 | |
| 424 | if (!PyArg_ParseTuple(args, "s|s", &buf, &fmt)) { |
| 425 | PyErr_SetString(PyExc_ValueError, "invalid argument"); |
| 426 | return NULL; |
| 427 | } |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 428 | memset((ANY *) &tm, '\0', sizeof(tm)); |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 429 | s = strptime(buf, fmt, &tm); |
| 430 | if (s == NULL) { |
| 431 | PyErr_SetString(PyExc_ValueError, "format mismatch"); |
| 432 | return NULL; |
| 433 | } |
| 434 | while (*s && isspace(*s)) |
| 435 | s++; |
| 436 | if (*s) { |
| 437 | PyErr_Format(PyExc_ValueError, |
| 438 | "unconverted data remains: '%.400s'", s); |
| 439 | return NULL; |
| 440 | } |
| 441 | return tmtotuple(&tm); |
| 442 | } |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 443 | |
| 444 | static char strptime_doc[] = |
Guido van Rossum | 446ccfe | 1999-01-07 18:29:26 +0000 | [diff] [blame] | 445 | "strptime(string, format) -> tuple\n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 446 | Parse a string to a time tuple according to a format specification.\n\ |
| 447 | See the library reference manual for formatting codes (same as strftime())."; |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 448 | #endif /* HAVE_STRPTIME */ |
| 449 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 450 | static PyObject * |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 451 | time_asctime(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 452 | PyObject *self; |
| 453 | PyObject *args; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 454 | { |
| 455 | struct tm buf; |
| 456 | char *p; |
| 457 | if (!gettmarg(args, &buf)) |
| 458 | return NULL; |
| 459 | p = asctime(&buf); |
| 460 | if (p[24] == '\n') |
| 461 | p[24] = '\0'; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 462 | return PyString_FromString(p); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 465 | static char asctime_doc[] = |
| 466 | "asctime(tuple) -> string\n\ |
| 467 | \n\ |
| 468 | Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'."; |
| 469 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 470 | static PyObject * |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 471 | time_ctime(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 472 | PyObject *self; |
| 473 | PyObject *args; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 474 | { |
| 475 | double dt; |
| 476 | time_t tt; |
| 477 | char *p; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 478 | if (!PyArg_Parse(args, "d", &dt)) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 479 | return NULL; |
Guido van Rossum | cac6c72 | 1996-09-06 13:34:02 +0000 | [diff] [blame] | 480 | tt = (time_t)dt; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 481 | p = ctime(&tt); |
Guido van Rossum | 7853570 | 1998-03-03 22:19:10 +0000 | [diff] [blame] | 482 | if (p == NULL) { |
| 483 | PyErr_SetString(PyExc_ValueError, "unconvertible time"); |
| 484 | return NULL; |
| 485 | } |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 486 | if (p[24] == '\n') |
| 487 | p[24] = '\0'; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 488 | return PyString_FromString(p); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 491 | static char ctime_doc[] = |
| 492 | "ctime(seconds) -> string\n\ |
| 493 | \n\ |
| 494 | Convert a time in seconds since the Epoch to a string in local time.\n\ |
| 495 | This is equivalent to asctime(localtime(seconds))."; |
| 496 | |
Guido van Rossum | 60cd813 | 1998-03-06 17:16:21 +0000 | [diff] [blame] | 497 | #ifdef HAVE_MKTIME |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 498 | static PyObject * |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 499 | time_mktime(self, args) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 500 | PyObject *self; |
| 501 | PyObject *args; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 502 | { |
| 503 | struct tm buf; |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 504 | time_t tt; |
| 505 | tt = time(&tt); |
| 506 | buf = *localtime(&tt); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 507 | if (!gettmarg(args, &buf)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 508 | return NULL; |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 509 | tt = mktime(&buf); |
| 510 | if (tt == (time_t)(-1)) { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 511 | PyErr_SetString(PyExc_OverflowError, |
| 512 | "mktime argument out of range"); |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 513 | return NULL; |
| 514 | } |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 515 | return PyFloat_FromDouble((double)tt); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 516 | } |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 517 | |
| 518 | static char mktime_doc[] = |
| 519 | "mktime(tuple) -> floating point number\n\ |
| 520 | \n\ |
| 521 | Convert a time tuple in local time to seconds since the Epoch."; |
Guido van Rossum | 60cd813 | 1998-03-06 17:16:21 +0000 | [diff] [blame] | 522 | #endif /* HAVE_MKTIME */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 523 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 524 | static PyMethodDef time_methods[] = { |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 525 | {"time", time_time, 0, time_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 526 | #ifdef HAVE_CLOCK |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 527 | {"clock", time_clock, 0, clock_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 528 | #endif |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 529 | {"sleep", time_sleep, 0, sleep_doc}, |
| 530 | {"gmtime", time_gmtime, 0, gmtime_doc}, |
| 531 | {"localtime", time_localtime, 0, localtime_doc}, |
| 532 | {"asctime", time_asctime, 0, asctime_doc}, |
| 533 | {"ctime", time_ctime, 0, ctime_doc}, |
Guido van Rossum | 60cd813 | 1998-03-06 17:16:21 +0000 | [diff] [blame] | 534 | #ifdef HAVE_MKTIME |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 535 | {"mktime", time_mktime, 0, mktime_doc}, |
Guido van Rossum | 60cd813 | 1998-03-06 17:16:21 +0000 | [diff] [blame] | 536 | #endif |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 537 | #ifdef HAVE_STRFTIME |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 538 | {"strftime", time_strftime, 1, strftime_doc}, |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 539 | #endif |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 540 | #ifdef HAVE_STRPTIME |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 541 | {"strptime", time_strptime, 1, strptime_doc}, |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 542 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 543 | {NULL, NULL} /* sentinel */ |
| 544 | }; |
| 545 | |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame] | 546 | static void |
| 547 | ins(d, name, v) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 548 | PyObject *d; |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame] | 549 | char *name; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 550 | PyObject *v; |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame] | 551 | { |
| 552 | if (v == NULL) |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 553 | Py_FatalError("Can't initialize time module -- NULL value"); |
| 554 | if (PyDict_SetItemString(d, name, v) != 0) |
| 555 | Py_FatalError( |
Guido van Rossum | 5217457 | 1996-12-09 18:38:52 +0000 | [diff] [blame] | 556 | "Can't initialize time module -- PyDict_SetItemString failed"); |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 557 | Py_DECREF(v); |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 560 | static char module_doc[] = |
| 561 | "This module provides various functions to manipulate time values.\n\ |
| 562 | \n\ |
| 563 | There are two standard representations of time. One is the number\n\ |
| 564 | of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\ |
| 565 | or a floating point number (to represent fractions of seconds).\n\ |
| 566 | The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\ |
| 567 | The actual value can be retrieved by calling gmtime(0).\n\ |
| 568 | \n\ |
| 569 | The other representation is a tuple of 9 integers giving local time.\n\ |
| 570 | The tuple items are:\n\ |
| 571 | year (four digits, e.g. 1998)\n\ |
| 572 | month (1-12)\n\ |
| 573 | day (1-31)\n\ |
| 574 | hours (0-23)\n\ |
| 575 | minutes (0-59)\n\ |
Guido van Rossum | 360eb9f | 1999-02-22 16:19:52 +0000 | [diff] [blame] | 576 | seconds (0-59)\n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 577 | weekday (0-6, Monday is 0)\n\ |
| 578 | Julian day (day in the year, 1-366)\n\ |
| 579 | DST (Daylight Savings Time) flag (-1, 0 or 1)\n\ |
| 580 | If the DST flag is 0, the time is given in the regular time zone;\n\ |
| 581 | if it is 1, the time is given in the DST time zone;\n\ |
| 582 | if it is -1, mktime() should guess based on the date and time.\n\ |
| 583 | \n\ |
| 584 | Variables:\n\ |
| 585 | \n\ |
| 586 | timezone -- difference in seconds between UTC and local standard time\n\ |
| 587 | altzone -- difference in seconds between UTC and local DST time\n\ |
| 588 | daylight -- whether local time should reflect DST\n\ |
| 589 | tzname -- tuple of (standard time zone name, DST time zone name)\n\ |
| 590 | \n\ |
| 591 | Functions:\n\ |
| 592 | \n\ |
| 593 | time() -- return current time in seconds since the Epoch as a float\n\ |
| 594 | clock() -- return CPU time since process start as a float\n\ |
| 595 | sleep() -- delay for a number of seconds given as a float\n\ |
| 596 | gmtime() -- convert seconds since Epoch to UTC tuple\n\ |
| 597 | localtime() -- convert seconds since Epoch to local time tuple\n\ |
| 598 | asctime() -- convert time tuple to string\n\ |
| 599 | ctime() -- convert time in seconds to string\n\ |
| 600 | mktime() -- convert local time tuple to seconds since Epoch\n\ |
| 601 | strftime() -- convert time tuple to string according to format specification\n\ |
| 602 | strptime() -- parse string to time tuple according to format specification\n\ |
| 603 | "; |
| 604 | |
| 605 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 606 | DL_EXPORT(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 607 | inittime() |
| 608 | { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 609 | PyObject *m, *d; |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 610 | char *p; |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 611 | m = Py_InitModule3("time", time_methods, module_doc); |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 612 | d = PyModule_GetDict(m); |
Guido van Rossum | c206873 | 1998-10-07 16:35:25 +0000 | [diff] [blame] | 613 | /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */ |
| 614 | p = getenv("PYTHONY2K"); |
| 615 | ins(d, "accept2dyear", PyInt_FromLong((long) (!p || !*p))); |
| 616 | /* Squirrel away the module's dictionary for the y2k check */ |
| 617 | Py_INCREF(d); |
| 618 | moddict = d; |
Guido van Rossum | ea424e1 | 1999-04-23 20:59:05 +0000 | [diff] [blame] | 619 | #if defined(HAVE_TZNAME) && !defined(__GLIBC__) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 620 | tzset(); |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 621 | #ifdef PYOS_OS2 |
| 622 | ins(d, "timezone", PyInt_FromLong((long)_timezone)); |
| 623 | #else /* !PYOS_OS2 */ |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 624 | ins(d, "timezone", PyInt_FromLong((long)timezone)); |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 625 | #endif /* PYOS_OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 626 | #ifdef HAVE_ALTZONE |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 627 | ins(d, "altzone", PyInt_FromLong((long)altzone)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 628 | #else |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 629 | #ifdef PYOS_OS2 |
| 630 | ins(d, "altzone", PyInt_FromLong((long)_timezone-3600)); |
| 631 | #else /* !PYOS_OS2 */ |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 632 | ins(d, "altzone", PyInt_FromLong((long)timezone-3600)); |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 633 | #endif /* PYOS_OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 634 | #endif |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 635 | ins(d, "daylight", PyInt_FromLong((long)daylight)); |
| 636 | ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1])); |
Guido van Rossum | ea424e1 | 1999-04-23 20:59:05 +0000 | [diff] [blame] | 637 | #else /* !HAVE_TZNAME || __GLIBC__ */ |
Guido van Rossum | 0ffdd05 | 1999-04-05 21:54:14 +0000 | [diff] [blame] | 638 | #ifdef HAVE_TM_ZONE |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 639 | { |
| 640 | #define YEAR ((time_t)((365 * 24 + 6) * 3600)) |
| 641 | time_t t; |
| 642 | struct tm *p; |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 643 | long janzone, julyzone; |
| 644 | char janname[10], julyname[10]; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 645 | t = (time((time_t *)0) / YEAR) * YEAR; |
| 646 | p = localtime(&t); |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 647 | janzone = -p->tm_gmtoff; |
| 648 | strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9); |
| 649 | janname[9] = '\0'; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 650 | t += YEAR/2; |
| 651 | p = localtime(&t); |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 652 | julyzone = -p->tm_gmtoff; |
| 653 | strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9); |
| 654 | julyname[9] = '\0'; |
| 655 | |
| 656 | if( janzone < julyzone ) { |
| 657 | /* DST is reversed in the southern hemisphere */ |
| 658 | ins(d, "timezone", PyInt_FromLong(julyzone)); |
| 659 | ins(d, "altzone", PyInt_FromLong(janzone)); |
| 660 | ins(d, "daylight", |
| 661 | PyInt_FromLong((long)(janzone != julyzone))); |
| 662 | ins(d, "tzname", |
| 663 | Py_BuildValue("(zz)", julyname, janname)); |
| 664 | } else { |
| 665 | ins(d, "timezone", PyInt_FromLong(janzone)); |
| 666 | ins(d, "altzone", PyInt_FromLong(julyzone)); |
| 667 | ins(d, "daylight", |
| 668 | PyInt_FromLong((long)(janzone != julyzone))); |
| 669 | ins(d, "tzname", |
| 670 | Py_BuildValue("(zz)", janname, julyname)); |
| 671 | } |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 672 | } |
Guido van Rossum | e6a4b7b | 1997-10-08 15:27:56 +0000 | [diff] [blame] | 673 | #else |
| 674 | #ifdef macintosh |
Guido van Rossum | be1eb0d | 1997-12-08 21:56:43 +0000 | [diff] [blame] | 675 | /* The only thing we can obtain is the current timezone |
| 676 | ** (and whether dst is currently _active_, but that is not what |
| 677 | ** we're looking for:-( ) |
| 678 | */ |
Guido van Rossum | e6a4b7b | 1997-10-08 15:27:56 +0000 | [diff] [blame] | 679 | initmactimezone(); |
| 680 | ins(d, "timezone", PyInt_FromLong(timezone)); |
Guido van Rossum | be1eb0d | 1997-12-08 21:56:43 +0000 | [diff] [blame] | 681 | ins(d, "altzone", PyInt_FromLong(timezone)); |
| 682 | ins(d, "daylight", PyInt_FromLong((long)0)); |
| 683 | ins(d, "tzname", Py_BuildValue("(zz)", "", "")); |
Guido van Rossum | e6a4b7b | 1997-10-08 15:27:56 +0000 | [diff] [blame] | 684 | #endif /* macintosh */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 685 | #endif /* HAVE_TM_ZONE */ |
Guido van Rossum | ea424e1 | 1999-04-23 20:59:05 +0000 | [diff] [blame] | 686 | #endif /* !HAVE_TZNAME || __GLIBC__ */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 687 | if (PyErr_Occurred()) |
| 688 | Py_FatalError("Can't initialize time module"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 692 | /* Implement floattime() for various platforms */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 693 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 694 | static double |
| 695 | floattime() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 696 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 697 | /* There are three ways to get the time: |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 698 | (1) gettimeofday() -- resolution in microseconds |
| 699 | (2) ftime() -- resolution in milliseconds |
| 700 | (3) time() -- resolution in seconds |
| 701 | In all cases the return value is a float in seconds. |
| 702 | Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may |
| 703 | fail, so we fall back on ftime() or time(). |
| 704 | Note: clock resolution does not imply clock accuracy! */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 705 | #ifdef HAVE_GETTIMEOFDAY |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 706 | { |
| 707 | struct timeval t; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 708 | #ifdef GETTIMEOFDAY_NO_TZ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 709 | if (gettimeofday(&t) == 0) |
| 710 | return (double)t.tv_sec + t.tv_usec*0.000001; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 711 | #else /* !GETTIMEOFDAY_NO_TZ */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 712 | if (gettimeofday(&t, (struct timezone *)NULL) == 0) |
| 713 | return (double)t.tv_sec + t.tv_usec*0.000001; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 714 | #endif /* !GETTIMEOFDAY_NO_TZ */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 715 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 716 | #endif /* !HAVE_GETTIMEOFDAY */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 717 | { |
Guido van Rossum | d3eb577 | 1999-03-09 16:07:23 +0000 | [diff] [blame] | 718 | #if defined(HAVE_FTIME) |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 719 | struct timeb t; |
| 720 | ftime(&t); |
| 721 | return (double)t.time + (double)t.millitm * (double)0.001; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 722 | #else /* !HAVE_FTIME */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 723 | time_t secs; |
| 724 | time(&secs); |
| 725 | return (double)secs; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 726 | #endif /* !HAVE_FTIME */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 727 | } |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 730 | |
| 731 | /* Implement floatsleep() for various platforms. |
| 732 | When interrupted (or when another error occurs), return -1 and |
| 733 | set an exception; else return 0. */ |
| 734 | |
| 735 | static int |
Guido van Rossum | a320fd3 | 1995-03-09 12:14:15 +0000 | [diff] [blame] | 736 | #ifdef MPW |
| 737 | floatsleep(double secs) |
| 738 | #else |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 739 | floatsleep(secs) |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 740 | double secs; |
Guido van Rossum | a320fd3 | 1995-03-09 12:14:15 +0000 | [diff] [blame] | 741 | #endif /* MPW */ |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 742 | { |
Guido van Rossum | a78bfe1 | 1997-02-14 16:35:10 +0000 | [diff] [blame] | 743 | /* XXX Should test for MS_WIN32 first! */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 744 | #if defined(HAVE_SELECT) && !defined(__BEOS__) |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 745 | struct timeval t; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 746 | double frac; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 747 | frac = fmod(secs, 1.0); |
| 748 | secs = floor(secs); |
| 749 | t.tv_sec = (long)secs; |
| 750 | t.tv_usec = (long)(frac*1000000.0); |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 751 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 752 | if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) { |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 753 | Py_BLOCK_THREADS |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 754 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 755 | return -1; |
| 756 | } |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 757 | Py_END_ALLOW_THREADS |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 758 | #else /* !HAVE_SELECT || __BEOS__ */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 759 | #ifdef macintosh |
| 760 | #define MacTicks (* (long *)0x16A) |
| 761 | long deadline; |
| 762 | deadline = MacTicks + (long)(secs * 60.0); |
| 763 | while (MacTicks < deadline) { |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 764 | /* XXX Should call some yielding function here */ |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 765 | if (PyErr_CheckSignals()) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 766 | return -1; |
| 767 | } |
| 768 | #else /* !macintosh */ |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 769 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 770 | /* XXX Can't interrupt this sleep */ |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 771 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 772 | delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */ |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 773 | Py_END_ALLOW_THREADS |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 774 | #else /* !__WATCOMC__ || __QNX__ */ |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 775 | #ifdef MSDOS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 776 | struct timeb t1, t2; |
| 777 | double frac; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 778 | extern double fmod Py_PROTO((double, double)); |
| 779 | extern double floor Py_PROTO((double)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 780 | if (secs <= 0.0) |
| 781 | return; |
| 782 | frac = fmod(secs, 1.0); |
| 783 | secs = floor(secs); |
| 784 | ftime(&t1); |
| 785 | t2.time = t1.time + (int)secs; |
| 786 | t2.millitm = t1.millitm + (int)(frac*1000.0); |
| 787 | while (t2.millitm >= 1000) { |
| 788 | t2.time++; |
| 789 | t2.millitm -= 1000; |
| 790 | } |
| 791 | for (;;) { |
| 792 | #ifdef QUICKWIN |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 793 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 794 | _wyield(); |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 795 | Py_END_ALLOW_THREADS |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 796 | #endif |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 797 | if (PyErr_CheckSignals()) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 798 | return -1; |
| 799 | ftime(&t1); |
| 800 | if (t1.time > t2.time || |
| 801 | t1.time == t2.time && t1.millitm >= t2.millitm) |
| 802 | break; |
| 803 | } |
| 804 | #else /* !MSDOS */ |
Guido van Rossum | b2fb364 | 1996-09-07 00:47:35 +0000 | [diff] [blame] | 805 | #ifdef MS_WIN32 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 806 | /* XXX Can't interrupt this sleep */ |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 807 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 808 | Sleep((int)(secs*1000)); |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 809 | Py_END_ALLOW_THREADS |
Guido van Rossum | b2fb364 | 1996-09-07 00:47:35 +0000 | [diff] [blame] | 810 | #else /* !MS_WIN32 */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 811 | #ifdef PYOS_OS2 |
| 812 | /* This Sleep *IS* Interruptable by Exceptions */ |
Guido van Rossum | 1d0d7e4 | 1997-12-29 20:03:10 +0000 | [diff] [blame] | 813 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 814 | if (DosSleep(secs * 1000) != NO_ERROR) { |
Guido van Rossum | 1d0d7e4 | 1997-12-29 20:03:10 +0000 | [diff] [blame] | 815 | Py_BLOCK_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 816 | PyErr_SetFromErrno(PyExc_IOError); |
| 817 | return -1; |
| 818 | } |
Guido van Rossum | 1d0d7e4 | 1997-12-29 20:03:10 +0000 | [diff] [blame] | 819 | Py_END_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 820 | #else /* !PYOS_OS2 */ |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 821 | #ifdef __BEOS__ |
| 822 | /* This sleep *CAN BE* interrupted. */ |
| 823 | { |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 824 | if( secs <= 0.0 ) { |
| 825 | return; |
| 826 | } |
Guido van Rossum | d3eb577 | 1999-03-09 16:07:23 +0000 | [diff] [blame] | 827 | |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 828 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d3eb577 | 1999-03-09 16:07:23 +0000 | [diff] [blame] | 829 | /* BeOS snooze() is in microseconds... */ |
| 830 | if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) { |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 831 | Py_BLOCK_THREADS |
| 832 | PyErr_SetFromErrno( PyExc_IOError ); |
| 833 | return -1; |
| 834 | } |
| 835 | Py_END_ALLOW_THREADS |
| 836 | } |
| 837 | #else /* !__BEOS__ */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 838 | /* XXX Can't interrupt this sleep */ |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 839 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 840 | sleep((int)secs); |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 841 | Py_END_ALLOW_THREADS |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 842 | #endif /* !__BEOS__ */ |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 843 | #endif /* !PYOS_OS2 */ |
Guido van Rossum | b2fb364 | 1996-09-07 00:47:35 +0000 | [diff] [blame] | 844 | #endif /* !MS_WIN32 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 845 | #endif /* !MSDOS */ |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 846 | #endif /* !__WATCOMC__ || __QNX__ */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 847 | #endif /* !macintosh */ |
| 848 | #endif /* !HAVE_SELECT */ |
| 849 | return 0; |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 850 | } |