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