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