Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, 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 | |
| 31 | #include "sigtype.h" |
| 32 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 33 | #include <signal.h> |
| 34 | #include <setjmp.h> |
| 35 | |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 36 | #ifdef BSD_TIME |
| 37 | #define HAVE_GETTIMEOFDAY |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 38 | #include "myselect.h" /* Implies <sys/types.h>, <sys/time.h>, <sys/param.h> */ |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 41 | #ifdef macintosh |
| 42 | #define NO_UNISTD |
| 43 | #endif |
| 44 | |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 45 | #ifndef NO_UNISTD |
| 46 | #include <unistd.h> |
| 47 | #endif |
| 48 | |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 49 | /* What happens here is not trivial. |
| 50 | The BSD_TIME code needs <sys/time.h> (for struct timeval). |
| 51 | The rest of the code needs only time_t, except some MS-DOS |
| 52 | code which needs clock_t as well. |
| 53 | Standard C says that time_t is defined in <time.h>, and |
| 54 | does not have <sys/types.h>; THINK C agrees (MS-DOS too?). |
| 55 | What's worse, in pure 4.3 BSD, older SunOS versions, and |
| 56 | probably everything derived from BSD, you can't #include |
| 57 | both <time.h> and <sys/time.h> in the same file, since |
| 58 | <sys/time.h> includes <time.h> without any protection, |
| 59 | and <time.h> contains a typedef, which can't be parsed twice! |
| 60 | So on traditional UNIX systems we include <sys/types.h> |
| 61 | and <sys/time.h> and hope this implies <time.h> and time_t, |
| 62 | while on other systems, including conforming Standard C |
| 63 | systems (where 'unix' can't be defined), we rely on <time.h>. |
| 64 | Still one problem: BSD_TIME won't work with strict Standard C... |
| 65 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 67 | #ifdef unix |
| 68 | #include <sys/types.h> |
| 69 | #include <sys/time.h> /* Implies <time.h> everywhere, as far as I know */ |
| 70 | #else /* !unix */ |
| 71 | #include <time.h> |
| 72 | #endif /* !unix */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 74 | #ifdef SYSV |
| 75 | /* Access timezone stuff */ |
| 76 | #ifdef OLDTZ /* ANSI prepends underscore to these */ |
| 77 | #define _timezone timezone /* seconds to be added to GMT */ |
| 78 | #define _altzone 0 /* _timezone if daylight saving time */ |
| 79 | #define _daylight 0 /* if zero, _altzone is not available*/ |
| 80 | #define _tzname tzname /* Name of timezone and altzone */ |
| 81 | #endif |
| 82 | #ifdef NOALTTZ /* if system doesn't support alt tz */ |
| 83 | #undef _daylight |
| 84 | #undef _altzone |
| 85 | #define _daylight 0 |
| 86 | #define _altzone 0 |
| 87 | #endif |
| 88 | #endif /* SYSV */ |
| 89 | |
| 90 | /* Forward declarations */ |
| 91 | static void floatsleep PROTO((double)); |
| 92 | static long millitimer PROTO((void)); |
| 93 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 94 | /* Time methods */ |
| 95 | |
| 96 | static object * |
| 97 | time_time(self, args) |
| 98 | object *self; |
| 99 | object *args; |
| 100 | { |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 101 | #ifdef HAVE_GETTIMEOFDAY |
| 102 | struct timeval t; |
| 103 | struct timezone tz; |
| 104 | if (!getnoarg(args)) |
| 105 | return NULL; |
| 106 | if (gettimeofday(&t, &tz) != 0) { |
| 107 | err_errno(IOError); |
| 108 | return NULL; |
| 109 | } |
| 110 | return newfloatobject(t.tv_sec*1.0 + t.tv_usec*0.000001); |
| 111 | #else /* !HAVE_GETTIMEOFDAY */ |
Guido van Rossum | 6590d4a | 1991-04-04 10:49:03 +0000 | [diff] [blame] | 112 | time_t secs; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 113 | if (!getnoarg(args)) |
| 114 | return NULL; |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 115 | time(&secs); |
Guido van Rossum | 00c567c | 1991-07-27 23:09:30 +0000 | [diff] [blame] | 116 | #ifdef macintosh |
| 117 | /* The Mac epoch is 1904, while UNIX uses 1970; Python prefers 1970 */ |
| 118 | /* Moreover, the Mac returns local time. This we cannot fix... */ |
Guido van Rossum | 6590d4a | 1991-04-04 10:49:03 +0000 | [diff] [blame] | 119 | #define TIMEDIFF ((time_t) \ |
| 120 | (((1970-1904)*365L + (1970-1904)/4) * 24 * 3600)) |
| 121 | secs -= TIMEDIFF; |
Guido van Rossum | 00c567c | 1991-07-27 23:09:30 +0000 | [diff] [blame] | 122 | #endif |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 123 | return newfloatobject((double)secs); |
| 124 | #endif /* !HAVE_GETTIMEOFDAY */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static jmp_buf sleep_intr; |
| 128 | |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 129 | /* ARGSUSED */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | static void |
| 131 | sleep_catcher(sig) |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 132 | int sig; /* Not used but required by interface */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 133 | { |
| 134 | longjmp(sleep_intr, 1); |
| 135 | } |
| 136 | |
| 137 | static object * |
| 138 | time_sleep(self, args) |
| 139 | object *self; |
| 140 | object *args; |
| 141 | { |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 142 | double secs; |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 143 | SIGTYPE (*sigsave)() = 0; /* Initialized to shut lint up */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 144 | if (!getargs(args, "d", &secs)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 145 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 146 | BGN_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 147 | if (setjmp(sleep_intr)) { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 148 | RET_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 149 | signal(SIGINT, sigsave); |
| 150 | err_set(KeyboardInterrupt); |
| 151 | return NULL; |
| 152 | } |
| 153 | sigsave = signal(SIGINT, SIG_IGN); |
| 154 | if (sigsave != (SIGTYPE (*)()) SIG_IGN) |
| 155 | signal(SIGINT, sleep_catcher); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 156 | floatsleep(secs); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 157 | END_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 158 | signal(SIGINT, sigsave); |
| 159 | INCREF(None); |
| 160 | return None; |
| 161 | } |
| 162 | |
Guido van Rossum | 1d2a9ad | 1991-06-24 22:23:45 +0000 | [diff] [blame] | 163 | #ifdef macintosh |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 164 | #define DO_MILLI |
Guido van Rossum | 1d2a9ad | 1991-06-24 22:23:45 +0000 | [diff] [blame] | 165 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 166 | |
| 167 | #ifdef AMOEBA |
| 168 | #define DO_MILLI |
| 169 | extern long sys_milli(); |
| 170 | #define millitimer sys_milli |
| 171 | #endif /* AMOEBA */ |
| 172 | |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 173 | #ifdef BSD_TIME |
| 174 | #define DO_MILLI |
| 175 | #endif /* BSD_TIME */ |
| 176 | |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame^] | 177 | #ifdef MSDOS |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 178 | #define DO_MILLI |
| 179 | #endif |
| 180 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 181 | #ifdef DO_MILLI |
| 182 | |
| 183 | static object * |
| 184 | time_millisleep(self, args) |
| 185 | object *self; |
| 186 | object *args; |
| 187 | { |
| 188 | long msecs; |
| 189 | SIGTYPE (*sigsave)(); |
| 190 | if (!getlongarg(args, &msecs)) |
| 191 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 192 | BGN_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 193 | if (setjmp(sleep_intr)) { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 194 | RET_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | signal(SIGINT, sigsave); |
| 196 | err_set(KeyboardInterrupt); |
| 197 | return NULL; |
| 198 | } |
| 199 | sigsave = signal(SIGINT, SIG_IGN); |
| 200 | if (sigsave != (SIGTYPE (*)()) SIG_IGN) |
| 201 | signal(SIGINT, sleep_catcher); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 202 | floatsleep(msecs / 1000.0); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 203 | END_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 204 | signal(SIGINT, sigsave); |
| 205 | INCREF(None); |
| 206 | return None; |
| 207 | } |
| 208 | |
| 209 | static object * |
| 210 | time_millitimer(self, args) |
| 211 | object *self; |
| 212 | object *args; |
| 213 | { |
| 214 | long msecs; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 215 | if (!getnoarg(args)) |
| 216 | return NULL; |
| 217 | msecs = millitimer(); |
| 218 | return newintobject(msecs); |
| 219 | } |
| 220 | |
| 221 | #endif /* DO_MILLI */ |
| 222 | |
| 223 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 224 | static object * |
| 225 | time_convert(when, function) |
| 226 | time_t when; |
| 227 | struct tm * (*function) PROTO((time_t *)); |
| 228 | { |
| 229 | struct tm *p = function(&when); |
| 230 | return mkvalue("(iiiiiiiii)", |
| 231 | p->tm_year + 1900, |
| 232 | p->tm_mon + 1, /* Want January == 1 */ |
| 233 | p->tm_mday, |
| 234 | p->tm_hour, |
| 235 | p->tm_min, |
| 236 | p->tm_sec, |
| 237 | (p->tm_wday + 6) % 7, /* Want Monday == 0 */ |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 238 | p->tm_yday + 1, /* Want January, 1 == 1 */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 239 | p->tm_isdst); |
| 240 | } |
| 241 | |
| 242 | static object * |
| 243 | time_gmtime(self, args) |
| 244 | object *self; |
| 245 | object *args; |
| 246 | { |
| 247 | double when; |
| 248 | if (!getargs(args, "d", &when)) |
| 249 | return NULL; |
| 250 | return time_convert((time_t)when, gmtime); |
| 251 | } |
| 252 | |
| 253 | static object * |
| 254 | time_localtime(self, args) |
| 255 | object *self; |
| 256 | object *args; |
| 257 | { |
| 258 | double when; |
| 259 | if (!getargs(args, "d", &when)) |
| 260 | return NULL; |
| 261 | return time_convert((time_t)when, localtime); |
| 262 | } |
| 263 | |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 264 | static int |
| 265 | gettmarg(args, p) |
| 266 | object *args; |
| 267 | struct tm *p; |
| 268 | { |
| 269 | if (!getargs(args, "(iiiiiiiii)", |
| 270 | &p->tm_year, |
| 271 | &p->tm_mon, |
| 272 | &p->tm_mday, |
| 273 | &p->tm_hour, |
| 274 | &p->tm_min, |
| 275 | &p->tm_sec, |
| 276 | &p->tm_wday, |
| 277 | &p->tm_yday, |
| 278 | &p->tm_isdst)) |
| 279 | return 0; |
| 280 | if (p->tm_year >= 1900) |
| 281 | p->tm_year -= 1900; |
| 282 | p->tm_mon--; |
| 283 | p->tm_wday = (p->tm_wday + 1) % 7; |
| 284 | p->tm_yday--; |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | static object * |
| 289 | time_asctime(self, args) |
| 290 | object *self; |
| 291 | object *args; |
| 292 | { |
| 293 | struct tm buf; |
| 294 | char *p; |
| 295 | if (!gettmarg(args, &buf)) |
| 296 | return NULL; |
| 297 | p = asctime(&buf); |
| 298 | if (p[24] == '\n') |
| 299 | p[24] = '\0'; |
| 300 | return newstringobject(p); |
| 301 | } |
| 302 | |
| 303 | static object * |
| 304 | time_ctime(self, args) |
| 305 | object *self; |
| 306 | object *args; |
| 307 | { |
| 308 | double dt; |
| 309 | time_t tt; |
| 310 | char *p; |
| 311 | if (!getargs(args, "d", &dt)) |
| 312 | return NULL; |
| 313 | tt = dt; |
| 314 | p = ctime(&tt); |
| 315 | if (p[24] == '\n') |
| 316 | p[24] = '\0'; |
| 317 | return newstringobject(p); |
| 318 | } |
| 319 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 320 | /* Some very old systems may not have mktime(). Comment it out then! */ |
| 321 | |
| 322 | static object * |
| 323 | time_mktime(self, args) |
| 324 | object *self; |
| 325 | object *args; |
| 326 | { |
| 327 | struct tm buf; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 328 | if (!gettmarg(args, &buf)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 329 | return NULL; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 330 | return newintobject((long)mktime(&buf)); |
| 331 | } |
| 332 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 333 | static struct methodlist time_methods[] = { |
| 334 | #ifdef DO_MILLI |
| 335 | {"millisleep", time_millisleep}, |
| 336 | {"millitimer", time_millitimer}, |
| 337 | #endif /* DO_MILLI */ |
| 338 | {"sleep", time_sleep}, |
| 339 | {"time", time_time}, |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 340 | {"gmtime", time_gmtime}, |
| 341 | {"localtime", time_localtime}, |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 342 | {"asctime", time_asctime}, |
| 343 | {"ctime", time_ctime}, |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 344 | {"mktime", time_mktime}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 345 | {NULL, NULL} /* sentinel */ |
| 346 | }; |
| 347 | |
| 348 | |
| 349 | void |
| 350 | inittime() |
| 351 | { |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 352 | object *m, *d; |
| 353 | m = initmodule("time", time_methods); |
| 354 | d = getmoduledict(m); |
| 355 | #ifdef SYSV |
| 356 | tzset(); |
| 357 | dictinsert(d, "timezone", newintobject((long)_timezone)); |
| 358 | dictinsert(d, "altzone", newintobject((long)_altzone)); |
| 359 | dictinsert(d, "daylight", newintobject((long)_daylight)); |
| 360 | dictinsert(d, "tzname", mkvalue("(zz)", _tzname[0], _tzname[1])); |
| 361 | #else /* !SYSV */ |
| 362 | { |
| 363 | #define YEAR ((time_t)((365 * 24 + 6) * 3600)) |
| 364 | time_t t; |
| 365 | struct tm *p; |
| 366 | long winterzone, summerzone; |
| 367 | char wintername[10], summername[10]; |
| 368 | t = (time((time_t *)0) / YEAR) * YEAR; |
| 369 | p = localtime(&t); |
| 370 | winterzone = -p->tm_gmtoff; |
| 371 | strncpy(wintername, p->tm_zone ? p->tm_zone : " ", 9); |
| 372 | wintername[9] = '\0'; |
| 373 | t += YEAR/2; |
| 374 | p = localtime(&t); |
| 375 | summerzone = -p->tm_gmtoff; |
| 376 | strncpy(summername, p->tm_zone ? p->tm_zone : " ", 9); |
| 377 | summername[9] = '\0'; |
| 378 | dictinsert(d, "timezone", newintobject(winterzone)); |
| 379 | dictinsert(d, "altzone", newintobject(summerzone)); |
| 380 | dictinsert(d, "daylight", |
| 381 | newintobject((long)(winterzone != summerzone))); |
| 382 | dictinsert(d, "tzname", |
| 383 | mkvalue("(zz)", wintername, summername)); |
| 384 | } |
| 385 | #endif /* !SYSV */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | |
Guido van Rossum | 1d2a9ad | 1991-06-24 22:23:45 +0000 | [diff] [blame] | 389 | #ifdef macintosh |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 390 | |
| 391 | #define MacTicks (* (long *)0x16A) |
| 392 | |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 393 | #ifdef THINK_C_3_0 |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 394 | sleep(secs) |
| 395 | int secs; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 396 | { |
| 397 | register long deadline; |
| 398 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 399 | deadline = MacTicks + mecs * 60; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 400 | while (MacTicks < deadline) { |
| 401 | if (intrcheck()) |
| 402 | sleep_catcher(SIGINT); |
| 403 | } |
| 404 | } |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 405 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 406 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 407 | static void |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 408 | floatsleep(secs) |
| 409 | double secs; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 410 | { |
| 411 | register long deadline; |
| 412 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 413 | deadline = MacTicks + (long)(secs * 60.0); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 414 | while (MacTicks < deadline) { |
| 415 | if (intrcheck()) |
| 416 | sleep_catcher(SIGINT); |
| 417 | } |
| 418 | } |
| 419 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 420 | static long |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 421 | millitimer() |
| 422 | { |
| 423 | return MacTicks * 50 / 3; /* MacTicks * 1000 / 60 */ |
| 424 | } |
| 425 | |
Guido van Rossum | 1d2a9ad | 1991-06-24 22:23:45 +0000 | [diff] [blame] | 426 | #endif /* macintosh */ |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 427 | |
| 428 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 429 | #ifdef unix |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 430 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 431 | #ifdef BSD_TIME |
Guido van Rossum | 6a1f54c | 1991-05-05 20:15:54 +0000 | [diff] [blame] | 432 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 433 | static long |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 434 | millitimer() |
| 435 | { |
| 436 | struct timeval t; |
| 437 | struct timezone tz; |
| 438 | if (gettimeofday(&t, &tz) != 0) |
| 439 | return -1; |
| 440 | return t.tv_sec*1000 + t.tv_usec/1000; |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 443 | static void |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 444 | floatsleep(secs) |
| 445 | double secs; |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 446 | { |
| 447 | struct timeval t; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 448 | double frac; |
| 449 | extern double fmod PROTO((double, double)); |
| 450 | extern double floor PROTO((double)); |
| 451 | frac = fmod(secs, 1.0); |
| 452 | secs = floor(secs); |
| 453 | t.tv_sec = (long)secs; |
| 454 | t.tv_usec = (long)(frac*1000000.0); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 455 | (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t); |
| 456 | } |
| 457 | |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 458 | #else /* !BSD_TIME */ |
| 459 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 460 | static void |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 461 | floatsleep(secs) |
| 462 | double secs; |
| 463 | { |
| 464 | sleep((int)secs); |
| 465 | } |
| 466 | |
| 467 | #endif /* !BSD_TIME */ |
| 468 | |
| 469 | #endif /* unix */ |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 470 | |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 471 | |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame^] | 472 | #ifdef MSDOS |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 473 | |
| 474 | #ifndef CLOCKS_PER_SEC |
| 475 | #define CLOCKS_PER_SEC 55 /* 54.945 msec per tick (18.2 HZ clock) */ |
| 476 | #endif |
| 477 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 478 | static void |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 479 | floatsleep(secs) |
| 480 | double secs; |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 481 | { |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 482 | delay(long(secs/1000.0)); |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 485 | static long |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 486 | millitimer() |
| 487 | { |
| 488 | clock_t ticks; |
| 489 | |
| 490 | ticks = clock(); /* ticks since program start */ |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 491 | return ticks * CLOCKS_PER_SEC;/* XXX shouldn't this be different? */ |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 494 | floatsleep(secs) |
| 495 | double secs; |
| 496 | { |
| 497 | clock_t t= clock( ); |
| 498 | while( (clock()-t)/CLOCKS_PER_SEC<secs ) |
| 499 | ; |
| 500 | } |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame^] | 501 | |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 502 | #endif /* MSDOS */ |