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 | 99d90c0 | 1996-08-08 19:17:45 +0000 | [diff] [blame^] | 31 | #ifdef HAVE_SELECT |
| 32 | #include "mymath.h" |
| 33 | #endif |
| 34 | |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 35 | #ifdef macintosh |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 36 | #include <time.h> |
| 37 | #else |
| 38 | #include <sys/types.h> |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 41 | #ifdef QUICKWIN |
| 42 | #include <io.h> |
| 43 | #endif |
| 44 | |
| 45 | #ifdef HAVE_UNISTD_H |
Guido van Rossum | 2762f25 | 1992-03-27 17:22:13 +0000 | [diff] [blame] | 46 | #include <unistd.h> |
| 47 | #endif |
| 48 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 49 | #ifdef HAVE_SELECT |
| 50 | #include "myselect.h" |
| 51 | #else |
| 52 | #include "mytime.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 HAVE_FTIME |
| 56 | #include <sys/timeb.h> |
| 57 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 59 | #ifdef __WATCOMC__ |
| 60 | #include <i86.h> |
| 61 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 62 | #ifdef _M_IX86 |
| 63 | #include <windows.h> |
| 64 | #define timezone _timezone |
Guido van Rossum | cc08112 | 1995-03-14 15:05:41 +0000 | [diff] [blame] | 65 | #define tzname _tzname |
| 66 | #define daylight _daylight |
| 67 | #define altzone _altzone |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 68 | #endif |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 69 | #endif |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 70 | |
| 71 | /* Forward declarations */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 72 | static int floatsleep PROTO((double)); |
| 73 | static double floattime PROTO(()); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 74 | |
| 75 | static object * |
| 76 | time_time(self, args) |
| 77 | object *self; |
| 78 | object *args; |
| 79 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 80 | double secs; |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 81 | if (!getnoarg(args)) |
| 82 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 83 | secs = floattime(); |
| 84 | if (secs == 0.0) { |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 85 | err_errno(IOError); |
| 86 | return NULL; |
| 87 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 88 | return newfloatobject(secs); |
| 89 | } |
| 90 | |
| 91 | #ifdef HAVE_CLOCK |
| 92 | |
| 93 | #ifndef CLOCKS_PER_SEC |
Guido van Rossum | 1b66a4f | 1996-02-25 04:50:33 +0000 | [diff] [blame] | 94 | #ifdef CLK_TCK |
| 95 | #define CLOCKS_PER_SEC CLK_TCK |
| 96 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 97 | #define CLOCKS_PER_SEC 1000000 |
| 98 | #endif |
Guido van Rossum | 1b66a4f | 1996-02-25 04:50:33 +0000 | [diff] [blame] | 99 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 100 | |
| 101 | static object * |
| 102 | time_clock(self, args) |
| 103 | object *self; |
| 104 | object *args; |
| 105 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 106 | if (!getnoarg(args)) |
| 107 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 108 | return newfloatobject(((double)clock()) / CLOCKS_PER_SEC); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 110 | #endif /* HAVE_CLOCK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 111 | |
| 112 | static object * |
| 113 | time_sleep(self, args) |
| 114 | object *self; |
| 115 | object *args; |
| 116 | { |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 117 | double secs; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 118 | if (!getargs(args, "d", &secs)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 119 | return NULL; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 120 | BGN_SAVE |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 121 | if (floatsleep(secs) != 0) { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 122 | RET_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 123 | return NULL; |
| 124 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 125 | END_SAVE |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | INCREF(None); |
| 127 | return None; |
| 128 | } |
| 129 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 130 | static object * |
| 131 | time_convert(when, function) |
| 132 | time_t when; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 133 | struct tm * (*function) PROTO((const time_t *)); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 134 | { |
| 135 | struct tm *p = function(&when); |
| 136 | return mkvalue("(iiiiiiiii)", |
| 137 | p->tm_year + 1900, |
| 138 | p->tm_mon + 1, /* Want January == 1 */ |
| 139 | p->tm_mday, |
| 140 | p->tm_hour, |
| 141 | p->tm_min, |
| 142 | p->tm_sec, |
| 143 | (p->tm_wday + 6) % 7, /* Want Monday == 0 */ |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 144 | p->tm_yday + 1, /* Want January, 1 == 1 */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 145 | p->tm_isdst); |
| 146 | } |
| 147 | |
| 148 | static object * |
| 149 | time_gmtime(self, args) |
| 150 | object *self; |
| 151 | object *args; |
| 152 | { |
| 153 | double when; |
| 154 | if (!getargs(args, "d", &when)) |
| 155 | return NULL; |
| 156 | return time_convert((time_t)when, gmtime); |
| 157 | } |
| 158 | |
| 159 | static object * |
| 160 | time_localtime(self, args) |
| 161 | object *self; |
| 162 | object *args; |
| 163 | { |
| 164 | double when; |
| 165 | if (!getargs(args, "d", &when)) |
| 166 | return NULL; |
| 167 | return time_convert((time_t)when, localtime); |
| 168 | } |
| 169 | |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 170 | static int |
| 171 | gettmarg(args, p) |
| 172 | object *args; |
| 173 | struct tm *p; |
| 174 | { |
| 175 | if (!getargs(args, "(iiiiiiiii)", |
| 176 | &p->tm_year, |
| 177 | &p->tm_mon, |
| 178 | &p->tm_mday, |
| 179 | &p->tm_hour, |
| 180 | &p->tm_min, |
| 181 | &p->tm_sec, |
| 182 | &p->tm_wday, |
| 183 | &p->tm_yday, |
| 184 | &p->tm_isdst)) |
| 185 | return 0; |
| 186 | if (p->tm_year >= 1900) |
| 187 | p->tm_year -= 1900; |
| 188 | p->tm_mon--; |
| 189 | p->tm_wday = (p->tm_wday + 1) % 7; |
| 190 | p->tm_yday--; |
| 191 | return 1; |
| 192 | } |
| 193 | |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 194 | #ifdef HAVE_STRFTIME |
| 195 | static object * |
| 196 | time_strftime(self, args) |
| 197 | object *self; |
| 198 | object *args; |
| 199 | { |
| 200 | struct tm buf; |
| 201 | const char *fmt; |
| 202 | char *outbuf = 0; |
| 203 | int i; |
| 204 | |
| 205 | if (!PyArg_ParseTuple(args, "s(iiiiiiiii)", |
| 206 | &fmt, |
| 207 | &(buf.tm_year), |
| 208 | &(buf.tm_mon), |
| 209 | &(buf.tm_mday), |
| 210 | &(buf.tm_hour), |
| 211 | &(buf.tm_min), |
| 212 | &(buf.tm_sec), |
| 213 | &(buf.tm_wday), |
| 214 | &(buf.tm_yday), |
| 215 | &(buf.tm_isdst))) |
| 216 | return NULL; |
| 217 | if (buf.tm_year >= 1900) |
| 218 | buf.tm_year -= 1900; |
| 219 | buf.tm_mon--; |
| 220 | buf.tm_wday = (buf.tm_wday + 1) % 7; |
| 221 | buf.tm_yday--; |
| 222 | /* I hate these functions that presume you know how big the output */ |
| 223 | /* will be ahead of time... */ |
| 224 | for (i = 1024 ; i < 8192 ; i += 1024) { |
| 225 | outbuf = malloc(i); |
| 226 | if (outbuf == NULL) { |
| 227 | return err_nomem(); |
| 228 | } |
| 229 | if (strftime(outbuf, i-1, fmt, &buf) != 0) { |
| 230 | object *ret; |
| 231 | ret = newstringobject(outbuf); |
| 232 | free(outbuf); |
| 233 | return ret; |
| 234 | } |
| 235 | free(outbuf); |
| 236 | } |
| 237 | return err_nomem(); |
| 238 | } |
| 239 | #endif /* HAVE_STRFTIME */ |
| 240 | |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 241 | static object * |
| 242 | time_asctime(self, args) |
| 243 | object *self; |
| 244 | object *args; |
| 245 | { |
| 246 | struct tm buf; |
| 247 | char *p; |
| 248 | if (!gettmarg(args, &buf)) |
| 249 | return NULL; |
| 250 | p = asctime(&buf); |
| 251 | if (p[24] == '\n') |
| 252 | p[24] = '\0'; |
| 253 | return newstringobject(p); |
| 254 | } |
| 255 | |
| 256 | static object * |
| 257 | time_ctime(self, args) |
| 258 | object *self; |
| 259 | object *args; |
| 260 | { |
| 261 | double dt; |
| 262 | time_t tt; |
| 263 | char *p; |
| 264 | if (!getargs(args, "d", &dt)) |
| 265 | return NULL; |
| 266 | tt = dt; |
| 267 | p = ctime(&tt); |
| 268 | if (p[24] == '\n') |
| 269 | p[24] = '\0'; |
| 270 | return newstringobject(p); |
| 271 | } |
| 272 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 273 | static object * |
| 274 | time_mktime(self, args) |
| 275 | object *self; |
| 276 | object *args; |
| 277 | { |
| 278 | struct tm buf; |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 279 | time_t tt; |
| 280 | tt = time(&tt); |
| 281 | buf = *localtime(&tt); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 282 | if (!gettmarg(args, &buf)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 283 | return NULL; |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 284 | tt = mktime(&buf); |
| 285 | if (tt == (time_t)(-1)) { |
| 286 | err_setstr(OverflowError, "mktime argument out of range"); |
| 287 | return NULL; |
| 288 | } |
| 289 | return newfloatobject((double)tt); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | static struct methodlist time_methods[] = { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 293 | {"time", time_time}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 294 | #ifdef HAVE_CLOCK |
| 295 | {"clock", time_clock}, |
| 296 | #endif |
| 297 | {"sleep", time_sleep}, |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 298 | {"gmtime", time_gmtime}, |
| 299 | {"localtime", time_localtime}, |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 300 | {"asctime", time_asctime}, |
| 301 | {"ctime", time_ctime}, |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 302 | {"mktime", time_mktime}, |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 303 | #ifdef HAVE_STRFTIME |
Guido van Rossum | 5416e20 | 1996-02-13 00:14:09 +0000 | [diff] [blame] | 304 | {"strftime", time_strftime, 1}, |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 305 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 306 | {NULL, NULL} /* sentinel */ |
| 307 | }; |
| 308 | |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame] | 309 | static void |
| 310 | ins(d, name, v) |
| 311 | object *d; |
| 312 | char *name; |
| 313 | object *v; |
| 314 | { |
| 315 | if (v == NULL) |
| 316 | fatal("Can't initialize time module -- NULL value"); |
| 317 | if (dictinsert(d, name, v) != 0) |
| 318 | fatal("Can't initialize time module -- dictinsert failed"); |
| 319 | DECREF(v); |
| 320 | } |
| 321 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 322 | void |
| 323 | inittime() |
| 324 | { |
Jack Jansen | 8ccfc93 | 1995-10-03 14:39:44 +0000 | [diff] [blame] | 325 | object *m, *d; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 326 | m = initmodule("time", time_methods); |
| 327 | d = getmoduledict(m); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 328 | #ifdef HAVE_TZNAME |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 329 | tzset(); |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame] | 330 | ins(d, "timezone", newintobject((long)timezone)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 331 | #ifdef HAVE_ALTZONE |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame] | 332 | ins(d, "altzone", newintobject((long)altzone)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 333 | #else |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame] | 334 | ins(d, "altzone", newintobject((long)timezone-3600)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 335 | #endif |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame] | 336 | ins(d, "daylight", newintobject((long)daylight)); |
| 337 | ins(d, "tzname", mkvalue("(zz)", tzname[0], tzname[1])); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 338 | #else /* !HAVE_TZNAME */ |
| 339 | #if HAVE_TM_ZONE |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 340 | { |
| 341 | #define YEAR ((time_t)((365 * 24 + 6) * 3600)) |
| 342 | time_t t; |
| 343 | struct tm *p; |
| 344 | long winterzone, summerzone; |
| 345 | char wintername[10], summername[10]; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 346 | /* XXX This won't work on the southern hemisphere. |
| 347 | XXX Anybody got a better idea? */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 348 | t = (time((time_t *)0) / YEAR) * YEAR; |
| 349 | p = localtime(&t); |
| 350 | winterzone = -p->tm_gmtoff; |
| 351 | strncpy(wintername, p->tm_zone ? p->tm_zone : " ", 9); |
| 352 | wintername[9] = '\0'; |
| 353 | t += YEAR/2; |
| 354 | p = localtime(&t); |
| 355 | summerzone = -p->tm_gmtoff; |
| 356 | strncpy(summername, p->tm_zone ? p->tm_zone : " ", 9); |
| 357 | summername[9] = '\0'; |
Guido van Rossum | 8239f0f | 1995-01-22 00:49:01 +0000 | [diff] [blame] | 358 | ins(d, "timezone", newintobject(winterzone)); |
| 359 | ins(d, "altzone", newintobject(summerzone)); |
| 360 | ins(d, "daylight", newintobject((long)(winterzone != summerzone))); |
| 361 | ins(d, "tzname", mkvalue("(zz)", wintername, summername)); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 362 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 363 | #endif /* HAVE_TM_ZONE */ |
| 364 | #endif /* !HAVE_TZNAME */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 368 | /* Implement floattime() for various platforms */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 369 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 370 | static double |
| 371 | floattime() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 372 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 373 | /* There are three ways to get the time: |
| 374 | (1) gettimeofday() -- resolution in microseconds |
| 375 | (2) ftime() -- resolution in milliseconds |
| 376 | (3) time() -- resolution in seconds |
| 377 | In all cases the return value is a float in seconds. |
| 378 | Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may |
| 379 | fail, so we fall back on ftime() or time(). |
| 380 | Note: clock resolution does not imply clock accuracy! */ |
| 381 | #ifdef HAVE_GETTIMEOFDAY |
| 382 | { |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 383 | struct timeval t; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 384 | #ifdef GETTIMEOFDAY_NO_TZ |
| 385 | if (gettimeofday(&t) == 0) |
| 386 | return (double)t.tv_sec + t.tv_usec*0.000001; |
| 387 | #else /* !GETTIMEOFDAY_NO_TZ */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 388 | if (gettimeofday(&t, (struct timezone *)NULL) == 0) |
| 389 | return (double)t.tv_sec + t.tv_usec*0.000001; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 390 | #endif /* !GETTIMEOFDAY_NO_TZ */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 391 | } |
| 392 | #endif /* !HAVE_GETTIMEOFDAY */ |
| 393 | { |
| 394 | #ifdef HAVE_FTIME |
| 395 | struct timeb t; |
| 396 | ftime(&t); |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 397 | return (double)t.time + (double)t.millitm * (double)0.001; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 398 | #else /* !HAVE_FTIME */ |
| 399 | time_t secs; |
| 400 | time(&secs); |
| 401 | return (double)secs; |
| 402 | #endif /* !HAVE_FTIME */ |
| 403 | } |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 406 | |
| 407 | /* Implement floatsleep() for various platforms. |
| 408 | When interrupted (or when another error occurs), return -1 and |
| 409 | set an exception; else return 0. */ |
| 410 | |
| 411 | static int |
Guido van Rossum | a320fd3 | 1995-03-09 12:14:15 +0000 | [diff] [blame] | 412 | #ifdef MPW |
| 413 | floatsleep(double secs) |
| 414 | #else |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 415 | floatsleep(secs) |
| 416 | double secs; |
Guido van Rossum | a320fd3 | 1995-03-09 12:14:15 +0000 | [diff] [blame] | 417 | #endif /* MPW */ |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 418 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 419 | #ifdef HAVE_SELECT |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 420 | struct timeval t; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 421 | double frac; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 422 | frac = fmod(secs, 1.0); |
| 423 | secs = floor(secs); |
| 424 | t.tv_sec = (long)secs; |
| 425 | t.tv_usec = (long)(frac*1000000.0); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 426 | if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) { |
| 427 | err_errno(IOError); |
| 428 | return -1; |
| 429 | } |
| 430 | #else /* !HAVE_SELECT */ |
| 431 | #ifdef macintosh |
| 432 | #define MacTicks (* (long *)0x16A) |
| 433 | long deadline; |
| 434 | deadline = MacTicks + (long)(secs * 60.0); |
| 435 | while (MacTicks < deadline) { |
| 436 | if (sigcheck()) |
| 437 | return -1; |
| 438 | } |
| 439 | #else /* !macintosh */ |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 440 | #ifdef __WATCOMC__ |
| 441 | /* XXX Can't interrupt this sleep */ |
| 442 | delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */ |
| 443 | #else /* !__WATCOMC__ */ |
Guido van Rossum | e22e644 | 1993-07-09 10:51:31 +0000 | [diff] [blame] | 444 | #ifdef MSDOS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 445 | struct timeb t1, t2; |
| 446 | double frac; |
| 447 | extern double fmod PROTO((double, double)); |
| 448 | extern double floor PROTO((double)); |
| 449 | if (secs <= 0.0) |
| 450 | return; |
| 451 | frac = fmod(secs, 1.0); |
| 452 | secs = floor(secs); |
| 453 | ftime(&t1); |
| 454 | t2.time = t1.time + (int)secs; |
| 455 | t2.millitm = t1.millitm + (int)(frac*1000.0); |
| 456 | while (t2.millitm >= 1000) { |
| 457 | t2.time++; |
| 458 | t2.millitm -= 1000; |
| 459 | } |
| 460 | for (;;) { |
| 461 | #ifdef QUICKWIN |
| 462 | _wyield(); |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 463 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 464 | if (sigcheck()) |
| 465 | return -1; |
| 466 | ftime(&t1); |
| 467 | if (t1.time > t2.time || |
| 468 | t1.time == t2.time && t1.millitm >= t2.millitm) |
| 469 | break; |
| 470 | } |
| 471 | #else /* !MSDOS */ |
| 472 | #ifdef _M_IX86 |
| 473 | /* XXX Can't interrupt this sleep */ |
| 474 | Sleep((int)(secs*1000)); |
| 475 | #else /* _M_IX86 */ |
| 476 | /* XXX Can't interrupt this sleep */ |
| 477 | sleep((int)secs); |
| 478 | #endif /* _M_IX86 */ |
| 479 | #endif /* !MSDOS */ |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 480 | #endif /* !__WATCOMC__ */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 481 | #endif /* !macintosh */ |
| 482 | #endif /* !HAVE_SELECT */ |
| 483 | return 0; |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 484 | } |