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