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