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 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Time module */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 28 | #include "modsupport.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 29 | #include "ceval.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 31 | #ifdef macintosh |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 32 | #include <time.h> |
| 33 | #else |
| 34 | #include <sys/types.h> |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 37 | #ifdef QUICKWIN |
| 38 | #include <io.h> |
| 39 | #endif |
| 40 | |
| 41 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 42 | #include <unistd.h> |
| 43 | #endif |
| 44 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 45 | #ifdef HAVE_SELECT |
| 46 | #include "myselect.h" |
| 47 | #else |
| 48 | #include "mytime.h" |
| 49 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 51 | #ifdef HAVE_FTIME |
| 52 | #include <sys/timeb.h> |
| 53 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 55 | #ifdef _M_IX86 |
| 56 | #include <windows.h> |
| 57 | #define timezone _timezone |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 58 | #endif |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 59 | |
| 60 | /* Forward declarations */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 61 | static int floatsleep PROTO((double)); |
| 62 | static double floattime PROTO(()); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 63 | |
| 64 | static object * |
| 65 | time_time(self, args) |
| 66 | object *self; |
| 67 | object *args; |
| 68 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 69 | double secs; |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 70 | if (!getnoarg(args)) |
| 71 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 72 | secs = floattime(); |
| 73 | if (secs == 0.0) { |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 74 | err_errno(IOError); |
| 75 | return NULL; |
| 76 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 77 | return newfloatobject(secs); |
| 78 | } |
| 79 | |
| 80 | #ifdef HAVE_CLOCK |
| 81 | |
| 82 | #ifndef CLOCKS_PER_SEC |
| 83 | #define CLOCKS_PER_SEC 1000000 |
| 84 | #endif |
| 85 | |
| 86 | static object * |
| 87 | time_clock(self, args) |
| 88 | object *self; |
| 89 | object *args; |
| 90 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 91 | if (!getnoarg(args)) |
| 92 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 93 | return newfloatobject(((double)clock()) / CLOCKS_PER_SEC); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 94 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 95 | #endif /* HAVE_CLOCK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 96 | |
| 97 | static object * |
| 98 | time_sleep(self, args) |
| 99 | object *self; |
| 100 | object *args; |
| 101 | { |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 102 | double secs; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 103 | if (!getargs(args, "d", &secs)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 104 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 105 | BGN_SAVE |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 106 | if (floatsleep(secs) != 0) { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 107 | RET_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 108 | return NULL; |
| 109 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 110 | END_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 111 | INCREF(None); |
| 112 | return None; |
| 113 | } |
| 114 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 115 | static object * |
| 116 | time_convert(when, function) |
| 117 | time_t when; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 118 | struct tm * (*function) PROTO((const time_t *)); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 119 | { |
| 120 | struct tm *p = function(&when); |
| 121 | return mkvalue("(iiiiiiiii)", |
| 122 | p->tm_year + 1900, |
| 123 | p->tm_mon + 1, /* Want January == 1 */ |
| 124 | p->tm_mday, |
| 125 | p->tm_hour, |
| 126 | p->tm_min, |
| 127 | p->tm_sec, |
| 128 | (p->tm_wday + 6) % 7, /* Want Monday == 0 */ |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 129 | p->tm_yday + 1, /* Want January, 1 == 1 */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 130 | p->tm_isdst); |
| 131 | } |
| 132 | |
| 133 | static object * |
| 134 | time_gmtime(self, args) |
| 135 | object *self; |
| 136 | object *args; |
| 137 | { |
| 138 | double when; |
| 139 | if (!getargs(args, "d", &when)) |
| 140 | return NULL; |
| 141 | return time_convert((time_t)when, gmtime); |
| 142 | } |
| 143 | |
| 144 | static object * |
| 145 | time_localtime(self, args) |
| 146 | object *self; |
| 147 | object *args; |
| 148 | { |
| 149 | double when; |
| 150 | if (!getargs(args, "d", &when)) |
| 151 | return NULL; |
| 152 | return time_convert((time_t)when, localtime); |
| 153 | } |
| 154 | |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 155 | static int |
| 156 | gettmarg(args, p) |
| 157 | object *args; |
| 158 | struct tm *p; |
| 159 | { |
| 160 | if (!getargs(args, "(iiiiiiiii)", |
| 161 | &p->tm_year, |
| 162 | &p->tm_mon, |
| 163 | &p->tm_mday, |
| 164 | &p->tm_hour, |
| 165 | &p->tm_min, |
| 166 | &p->tm_sec, |
| 167 | &p->tm_wday, |
| 168 | &p->tm_yday, |
| 169 | &p->tm_isdst)) |
| 170 | return 0; |
| 171 | if (p->tm_year >= 1900) |
| 172 | p->tm_year -= 1900; |
| 173 | p->tm_mon--; |
| 174 | p->tm_wday = (p->tm_wday + 1) % 7; |
| 175 | p->tm_yday--; |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | static object * |
| 180 | time_asctime(self, args) |
| 181 | object *self; |
| 182 | object *args; |
| 183 | { |
| 184 | struct tm buf; |
| 185 | char *p; |
| 186 | if (!gettmarg(args, &buf)) |
| 187 | return NULL; |
| 188 | p = asctime(&buf); |
| 189 | if (p[24] == '\n') |
| 190 | p[24] = '\0'; |
| 191 | return newstringobject(p); |
| 192 | } |
| 193 | |
| 194 | static object * |
| 195 | time_ctime(self, args) |
| 196 | object *self; |
| 197 | object *args; |
| 198 | { |
| 199 | double dt; |
| 200 | time_t tt; |
| 201 | char *p; |
| 202 | if (!getargs(args, "d", &dt)) |
| 203 | return NULL; |
| 204 | tt = dt; |
| 205 | p = ctime(&tt); |
| 206 | if (p[24] == '\n') |
| 207 | p[24] = '\0'; |
| 208 | return newstringobject(p); |
| 209 | } |
| 210 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 211 | static object * |
| 212 | time_mktime(self, args) |
| 213 | object *self; |
| 214 | object *args; |
| 215 | { |
| 216 | struct tm buf; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 217 | if (!gettmarg(args, &buf)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 218 | return NULL; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 219 | return newintobject((long)mktime(&buf)); |
| 220 | } |
| 221 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | static struct methodlist time_methods[] = { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 223 | {"time", time_time}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | #ifdef HAVE_CLOCK |
| 225 | {"clock", time_clock}, |
| 226 | #endif |
| 227 | {"sleep", time_sleep}, |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 228 | {"gmtime", time_gmtime}, |
| 229 | {"localtime", time_localtime}, |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 230 | {"asctime", time_asctime}, |
| 231 | {"ctime", time_ctime}, |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 232 | {"mktime", time_mktime}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 233 | {NULL, NULL} /* sentinel */ |
| 234 | }; |
| 235 | |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame^] | 236 | static void |
| 237 | ins(d, name, v) |
| 238 | object *d; |
| 239 | char *name; |
| 240 | object *v; |
| 241 | { |
| 242 | if (v == NULL) |
| 243 | fatal("Can't initialize time module -- NULL value"); |
| 244 | if (dictinsert(d, name, v) != 0) |
| 245 | fatal("Can't initialize time module -- dictinsert failed"); |
| 246 | DECREF(v); |
| 247 | } |
| 248 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 249 | void |
| 250 | inittime() |
| 251 | { |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame^] | 252 | object *m, *d, *v; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 253 | m = initmodule("time", time_methods); |
| 254 | d = getmoduledict(m); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | #ifdef HAVE_TZNAME |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 256 | tzset(); |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame^] | 257 | ins(d, "timezone", newintobject((long)timezone)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 258 | #ifdef HAVE_ALTZONE |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame^] | 259 | ins(d, "altzone", newintobject((long)altzone)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 260 | #else |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame^] | 261 | ins(d, "altzone", newintobject((long)timezone-3600)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 262 | #endif |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame^] | 263 | ins(d, "daylight", newintobject((long)daylight)); |
| 264 | ins(d, "tzname", mkvalue("(zz)", tzname[0], tzname[1])); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 265 | #else /* !HAVE_TZNAME */ |
| 266 | #if HAVE_TM_ZONE |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 267 | { |
| 268 | #define YEAR ((time_t)((365 * 24 + 6) * 3600)) |
| 269 | time_t t; |
| 270 | struct tm *p; |
| 271 | long winterzone, summerzone; |
| 272 | char wintername[10], summername[10]; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 273 | /* XXX This won't work on the southern hemisphere. |
| 274 | XXX Anybody got a better idea? */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 275 | t = (time((time_t *)0) / YEAR) * YEAR; |
| 276 | p = localtime(&t); |
| 277 | winterzone = -p->tm_gmtoff; |
| 278 | strncpy(wintername, p->tm_zone ? p->tm_zone : " ", 9); |
| 279 | wintername[9] = '\0'; |
| 280 | t += YEAR/2; |
| 281 | p = localtime(&t); |
| 282 | summerzone = -p->tm_gmtoff; |
| 283 | strncpy(summername, p->tm_zone ? p->tm_zone : " ", 9); |
| 284 | summername[9] = '\0'; |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame^] | 285 | ins(d, "timezone", newintobject(winterzone)); |
| 286 | ins(d, "altzone", newintobject(summerzone)); |
| 287 | ins(d, "daylight", newintobject((long)(winterzone != summerzone))); |
| 288 | ins(d, "tzname", mkvalue("(zz)", wintername, summername)); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 289 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 290 | #endif /* HAVE_TM_ZONE */ |
| 291 | #endif /* !HAVE_TZNAME */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 295 | /* Implement floattime() for various platforms */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 296 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 297 | static double |
| 298 | floattime() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 299 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 300 | /* There are three ways to get the time: |
| 301 | (1) gettimeofday() -- resolution in microseconds |
| 302 | (2) ftime() -- resolution in milliseconds |
| 303 | (3) time() -- resolution in seconds |
| 304 | In all cases the return value is a float in seconds. |
| 305 | Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may |
| 306 | fail, so we fall back on ftime() or time(). |
| 307 | Note: clock resolution does not imply clock accuracy! */ |
| 308 | #ifdef HAVE_GETTIMEOFDAY |
| 309 | { |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 310 | struct timeval t; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 311 | #ifdef GETTIMEOFDAY_NO_TZ |
| 312 | if (gettimeofday(&t) == 0) |
| 313 | return (double)t.tv_sec + t.tv_usec*0.000001; |
| 314 | #else /* !GETTIMEOFDAY_NO_TZ */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 315 | if (gettimeofday(&t, (struct timezone *)NULL) == 0) |
| 316 | return (double)t.tv_sec + t.tv_usec*0.000001; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 317 | #endif /* !GETTIMEOFDAY_NO_TZ */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 318 | } |
| 319 | #endif /* !HAVE_GETTIMEOFDAY */ |
| 320 | { |
| 321 | #ifdef HAVE_FTIME |
| 322 | struct timeb t; |
| 323 | ftime(&t); |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 324 | return (double)t.time + (double)t.millitm * (double)0.001; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 325 | #else /* !HAVE_FTIME */ |
| 326 | time_t secs; |
| 327 | time(&secs); |
| 328 | return (double)secs; |
| 329 | #endif /* !HAVE_FTIME */ |
| 330 | } |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 333 | |
| 334 | /* Implement floatsleep() for various platforms. |
| 335 | When interrupted (or when another error occurs), return -1 and |
| 336 | set an exception; else return 0. */ |
| 337 | |
| 338 | static int |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 339 | floatsleep(secs) |
| 340 | double secs; |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 341 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 342 | #ifdef HAVE_SELECT |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 343 | struct timeval t; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 344 | double frac; |
| 345 | extern double fmod PROTO((double, double)); |
| 346 | extern double floor PROTO((double)); |
| 347 | frac = fmod(secs, 1.0); |
| 348 | secs = floor(secs); |
| 349 | t.tv_sec = (long)secs; |
| 350 | t.tv_usec = (long)(frac*1000000.0); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 351 | if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) { |
| 352 | err_errno(IOError); |
| 353 | return -1; |
| 354 | } |
| 355 | #else /* !HAVE_SELECT */ |
| 356 | #ifdef macintosh |
| 357 | #define MacTicks (* (long *)0x16A) |
| 358 | long deadline; |
| 359 | deadline = MacTicks + (long)(secs * 60.0); |
| 360 | while (MacTicks < deadline) { |
| 361 | if (sigcheck()) |
| 362 | return -1; |
| 363 | } |
| 364 | #else /* !macintosh */ |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 365 | #ifdef MSDOS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 366 | struct timeb t1, t2; |
| 367 | double frac; |
| 368 | extern double fmod PROTO((double, double)); |
| 369 | extern double floor PROTO((double)); |
| 370 | if (secs <= 0.0) |
| 371 | return; |
| 372 | frac = fmod(secs, 1.0); |
| 373 | secs = floor(secs); |
| 374 | ftime(&t1); |
| 375 | t2.time = t1.time + (int)secs; |
| 376 | t2.millitm = t1.millitm + (int)(frac*1000.0); |
| 377 | while (t2.millitm >= 1000) { |
| 378 | t2.time++; |
| 379 | t2.millitm -= 1000; |
| 380 | } |
| 381 | for (;;) { |
| 382 | #ifdef QUICKWIN |
| 383 | _wyield(); |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 384 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 385 | if (sigcheck()) |
| 386 | return -1; |
| 387 | ftime(&t1); |
| 388 | if (t1.time > t2.time || |
| 389 | t1.time == t2.time && t1.millitm >= t2.millitm) |
| 390 | break; |
| 391 | } |
| 392 | #else /* !MSDOS */ |
| 393 | #ifdef _M_IX86 |
| 394 | /* XXX Can't interrupt this sleep */ |
| 395 | Sleep((int)(secs*1000)); |
| 396 | #else /* _M_IX86 */ |
| 397 | /* XXX Can't interrupt this sleep */ |
| 398 | sleep((int)secs); |
| 399 | #endif /* _M_IX86 */ |
| 400 | #endif /* !MSDOS */ |
| 401 | #endif /* !macintosh */ |
| 402 | #endif /* !HAVE_SELECT */ |
| 403 | return 0; |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 404 | } |