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