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