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