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" |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 6 | #include "timefuncs.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 7 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8 | #ifdef __APPLE__ |
| 9 | #if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME) |
| 10 | /* |
| 11 | * floattime falls back to ftime when getttimeofday fails because the latter |
| 12 | * might fail on some platforms. This fallback is unwanted on MacOSX because |
| 13 | * that makes it impossible to use a binary build on OSX 10.4 on earlier |
| 14 | * releases of the OS. Therefore claim we don't support ftime. |
| 15 | */ |
| 16 | # undef HAVE_FTIME |
| 17 | #endif |
| 18 | #endif |
| 19 | |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 20 | #include <ctype.h> |
| 21 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 22 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 23 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 24 | #endif /* HAVE_SYS_TYPES_H */ |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 26 | #ifdef QUICKWIN |
| 27 | #include <io.h> |
| 28 | #endif |
| 29 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 30 | #ifdef HAVE_FTIME |
| 31 | #include <sys/timeb.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 32 | #if !defined(MS_WINDOWS) && !defined(PYOS_OS2) |
Thomas Wouters | bd4bc4e | 2000-07-22 23:57:55 +0000 | [diff] [blame] | 33 | extern int ftime(struct timeb *); |
Guido van Rossum | 5217457 | 1996-12-09 18:38:52 +0000 | [diff] [blame] | 34 | #endif /* MS_WINDOWS */ |
| 35 | #endif /* HAVE_FTIME */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 37 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 38 | #include <i86.h> |
| 39 | #else |
Guido van Rossum | cac6c72 | 1996-09-06 13:34:02 +0000 | [diff] [blame] | 40 | #ifdef MS_WINDOWS |
Mark Hammond | 975e392 | 2002-07-16 01:29:19 +0000 | [diff] [blame] | 41 | #define WIN32_LEAN_AND_MEAN |
Guido van Rossum | 258ccd4 | 2001-03-02 06:53:29 +0000 | [diff] [blame] | 42 | #include <windows.h> |
Mark Hammond | 975e392 | 2002-07-16 01:29:19 +0000 | [diff] [blame] | 43 | #include "pythread.h" |
| 44 | |
| 45 | /* helper to allow us to interrupt sleep() on Windows*/ |
| 46 | static HANDLE hInterruptEvent = NULL; |
| 47 | static BOOL WINAPI PyCtrlHandler(DWORD dwCtrlType) |
| 48 | { |
| 49 | SetEvent(hInterruptEvent); |
| 50 | /* allow other default handlers to be called. |
| 51 | Default Python handler will setup the |
| 52 | KeyboardInterrupt exception. |
| 53 | */ |
| 54 | return FALSE; |
| 55 | } |
| 56 | static long main_thread; |
| 57 | |
| 58 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 59 | #if defined(__BORLANDC__) |
Guido van Rossum | b2fb364 | 1996-09-07 00:47:35 +0000 | [diff] [blame] | 60 | /* These overrides not needed for Win32 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 61 | #define timezone _timezone |
Guido van Rossum | cc08112 | 1995-03-14 15:05:41 +0000 | [diff] [blame] | 62 | #define tzname _tzname |
| 63 | #define daylight _daylight |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 64 | #endif /* __BORLANDC__ */ |
Guido van Rossum | cac6c72 | 1996-09-06 13:34:02 +0000 | [diff] [blame] | 65 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 7bf22de | 1997-12-02 20:34:19 +0000 | [diff] [blame] | 66 | #endif /* !__WATCOMC__ || __QNX__ */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 67 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 68 | #if defined(MS_WINDOWS) && !defined(__BORLANDC__) |
| 69 | /* Win32 has better clock replacement; we have our own version below. */ |
| 70 | #undef HAVE_CLOCK |
| 71 | #endif /* MS_WINDOWS && !defined(__BORLANDC__) */ |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 72 | |
Andrew MacIntyre | 7bf6833 | 2002-03-03 02:59:16 +0000 | [diff] [blame] | 73 | #if defined(PYOS_OS2) |
| 74 | #define INCL_DOS |
| 75 | #define INCL_ERRORS |
| 76 | #include <os2.h> |
| 77 | #endif |
| 78 | |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 79 | #if defined(PYCC_VACPP) |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 80 | #include <sys/time.h> |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 81 | #endif |
| 82 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 83 | /* Forward declarations */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 84 | static int floatsleep(double); |
Thomas Wouters | ed77bac | 2000-07-24 15:26:39 +0000 | [diff] [blame] | 85 | static double floattime(void); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 87 | /* For Y2K check */ |
| 88 | static PyObject *moddict; |
| 89 | |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 90 | /* Exposed in timefuncs.h. */ |
| 91 | time_t |
Brett Cannon | 298c380 | 2004-06-19 20:48:43 +0000 | [diff] [blame] | 92 | _PyTime_DoubleToTimet(double x) |
| 93 | { |
| 94 | time_t result; |
| 95 | double diff; |
| 96 | |
| 97 | result = (time_t)x; |
| 98 | /* How much info did we lose? time_t may be an integral or |
| 99 | * floating type, and we don't know which. If it's integral, |
| 100 | * we don't know whether C truncates, rounds, returns the floor, |
| 101 | * etc. If we lost a second or more, the C rounding is |
| 102 | * unreasonable, or the input just doesn't fit in a time_t; |
| 103 | * call it an error regardless. Note that the original cast to |
| 104 | * time_t can cause a C error too, but nothing we can do to |
| 105 | * worm around that. |
| 106 | */ |
| 107 | diff = x - (double)result; |
| 108 | if (diff <= -1.0 || diff >= 1.0) { |
| 109 | PyErr_SetString(PyExc_ValueError, |
| 110 | "timestamp out of range for platform time_t"); |
| 111 | result = (time_t)-1; |
| 112 | } |
| 113 | return result; |
| 114 | } |
| 115 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 116 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 117 | time_time(PyObject *self, PyObject *unused) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 118 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 119 | double secs; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 120 | secs = floattime(); |
| 121 | if (secs == 0.0) { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 122 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a2b7f40 | 1993-01-04 09:09:59 +0000 | [diff] [blame] | 123 | return NULL; |
| 124 | } |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 125 | return PyFloat_FromDouble(secs); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 128 | PyDoc_STRVAR(time_doc, |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 129 | "time() -> floating point number\n\ |
| 130 | \n\ |
| 131 | Return the current time in seconds since the Epoch.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 132 | Fractions of a second may be present if the system clock provides them."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 134 | #ifdef HAVE_CLOCK |
| 135 | |
| 136 | #ifndef CLOCKS_PER_SEC |
Guido van Rossum | 1b66a4f | 1996-02-25 04:50:33 +0000 | [diff] [blame] | 137 | #ifdef CLK_TCK |
| 138 | #define CLOCKS_PER_SEC CLK_TCK |
| 139 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 140 | #define CLOCKS_PER_SEC 1000000 |
| 141 | #endif |
Guido van Rossum | 1b66a4f | 1996-02-25 04:50:33 +0000 | [diff] [blame] | 142 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 143 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 144 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 145 | time_clock(PyObject *self, PyObject *unused) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 146 | { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 147 | return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 148 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 149 | #endif /* HAVE_CLOCK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 150 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 151 | #if defined(MS_WINDOWS) && !defined(__BORLANDC__) |
Mark Hammond | 7ba5e81 | 2002-02-12 04:02:33 +0000 | [diff] [blame] | 152 | /* Due to Mark Hammond and Tim Peters */ |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 153 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 154 | time_clock(PyObject *self, PyObject *unused) |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 155 | { |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 156 | static LARGE_INTEGER ctrStart; |
Mark Hammond | 7ba5e81 | 2002-02-12 04:02:33 +0000 | [diff] [blame] | 157 | static double divisor = 0.0; |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 158 | LARGE_INTEGER now; |
Mark Hammond | 7ba5e81 | 2002-02-12 04:02:33 +0000 | [diff] [blame] | 159 | double diff; |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 160 | |
Mark Hammond | 7ba5e81 | 2002-02-12 04:02:33 +0000 | [diff] [blame] | 161 | if (divisor == 0.0) { |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 162 | LARGE_INTEGER freq; |
| 163 | QueryPerformanceCounter(&ctrStart); |
| 164 | if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) { |
Mark Hammond | 7ba5e81 | 2002-02-12 04:02:33 +0000 | [diff] [blame] | 165 | /* Unlikely to happen - this works on all intel |
| 166 | machines at least! Revert to clock() */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 167 | return PyFloat_FromDouble(((double)clock()) / |
| 168 | CLOCKS_PER_SEC); |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 169 | } |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 170 | divisor = (double)freq.QuadPart; |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 171 | } |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 172 | QueryPerformanceCounter(&now); |
| 173 | diff = (double)(now.QuadPart - ctrStart.QuadPart); |
Mark Hammond | 7ba5e81 | 2002-02-12 04:02:33 +0000 | [diff] [blame] | 174 | return PyFloat_FromDouble(diff / divisor); |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 175 | } |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 176 | |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 177 | #define HAVE_CLOCK /* So it gets included in the methods */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 178 | #endif /* MS_WINDOWS && !defined(__BORLANDC__) */ |
Guido van Rossum | 3917c22 | 1997-04-02 05:35:28 +0000 | [diff] [blame] | 179 | |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 180 | #ifdef HAVE_CLOCK |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 181 | PyDoc_STRVAR(clock_doc, |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 182 | "clock() -> floating point number\n\ |
| 183 | \n\ |
| 184 | Return the CPU time or real time since the start of the process or since\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 185 | the first call to clock(). This has as much precision as the system\n\ |
| 186 | records."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 187 | #endif |
| 188 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 189 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 190 | time_sleep(PyObject *self, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 191 | { |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 192 | double secs; |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 193 | if (!PyArg_ParseTuple(args, "d:sleep", &secs)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 194 | return NULL; |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 195 | if (floatsleep(secs) != 0) |
| 196 | return NULL; |
| 197 | Py_INCREF(Py_None); |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 198 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 201 | PyDoc_STRVAR(sleep_doc, |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 202 | "sleep(seconds)\n\ |
| 203 | \n\ |
| 204 | Delay execution for a given number of seconds. The argument may be\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 205 | a floating point number for subsecond precision."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 206 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 207 | static PyStructSequence_Field struct_time_type_fields[] = { |
| 208 | {"tm_year", NULL}, |
| 209 | {"tm_mon", NULL}, |
| 210 | {"tm_mday", NULL}, |
| 211 | {"tm_hour", NULL}, |
| 212 | {"tm_min", NULL}, |
| 213 | {"tm_sec", NULL}, |
| 214 | {"tm_wday", NULL}, |
| 215 | {"tm_yday", NULL}, |
| 216 | {"tm_isdst", NULL}, |
| 217 | {0} |
| 218 | }; |
| 219 | |
| 220 | static PyStructSequence_Desc struct_time_type_desc = { |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 221 | "time.struct_time", |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 222 | NULL, |
| 223 | struct_time_type_fields, |
| 224 | 9, |
| 225 | }; |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 226 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 227 | static int initialized; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 228 | static PyTypeObject StructTimeType; |
| 229 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 230 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 231 | tmtotuple(struct tm *p) |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 232 | { |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 233 | PyObject *v = PyStructSequence_New(&StructTimeType); |
| 234 | if (v == NULL) |
| 235 | return NULL; |
Tim Peters | 9ad4b68 | 2002-02-13 05:14:18 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 237 | #define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val)) |
| 238 | |
| 239 | SET(0, p->tm_year + 1900); |
| 240 | SET(1, p->tm_mon + 1); /* Want January == 1 */ |
| 241 | SET(2, p->tm_mday); |
| 242 | SET(3, p->tm_hour); |
| 243 | SET(4, p->tm_min); |
| 244 | SET(5, p->tm_sec); |
| 245 | SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */ |
| 246 | SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */ |
| 247 | SET(8, p->tm_isdst); |
| 248 | #undef SET |
| 249 | if (PyErr_Occurred()) { |
| 250 | Py_XDECREF(v); |
| 251 | return NULL; |
| 252 | } |
| 253 | |
| 254 | return v; |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static PyObject * |
Brett Cannon | 298c380 | 2004-06-19 20:48:43 +0000 | [diff] [blame] | 258 | time_convert(double when, struct tm * (*function)(const time_t *)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 259 | { |
Guido van Rossum | 6e8583d | 1996-10-08 14:19:52 +0000 | [diff] [blame] | 260 | struct tm *p; |
Brett Cannon | 298c380 | 2004-06-19 20:48:43 +0000 | [diff] [blame] | 261 | time_t whent = _PyTime_DoubleToTimet(when); |
| 262 | |
| 263 | if (whent == (time_t)-1 && PyErr_Occurred()) |
| 264 | return NULL; |
Guido van Rossum | 6e8583d | 1996-10-08 14:19:52 +0000 | [diff] [blame] | 265 | errno = 0; |
Brett Cannon | 298c380 | 2004-06-19 20:48:43 +0000 | [diff] [blame] | 266 | p = function(&whent); |
Guido van Rossum | 6e8583d | 1996-10-08 14:19:52 +0000 | [diff] [blame] | 267 | if (p == NULL) { |
| 268 | #ifdef EINVAL |
Guido van Rossum | 0b1ff66 | 1996-11-02 17:31:22 +0000 | [diff] [blame] | 269 | if (errno == 0) |
Guido van Rossum | 6e8583d | 1996-10-08 14:19:52 +0000 | [diff] [blame] | 270 | errno = EINVAL; |
| 271 | #endif |
Tim Peters | 8b19a93 | 2003-01-17 20:08:54 +0000 | [diff] [blame] | 272 | return PyErr_SetFromErrno(PyExc_ValueError); |
Guido van Rossum | 6e8583d | 1996-10-08 14:19:52 +0000 | [diff] [blame] | 273 | } |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 274 | return tmtotuple(p); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 277 | /* Parse arg tuple that can contain an optional float-or-None value; |
| 278 | format needs to be "|O:name". |
| 279 | Returns non-zero on success (parallels PyArg_ParseTuple). |
| 280 | */ |
| 281 | static int |
| 282 | parse_time_double_args(PyObject *args, char *format, double *pwhen) |
| 283 | { |
| 284 | PyObject *ot = NULL; |
| 285 | |
| 286 | if (!PyArg_ParseTuple(args, format, &ot)) |
| 287 | return 0; |
| 288 | if (ot == NULL || ot == Py_None) |
| 289 | *pwhen = floattime(); |
| 290 | else { |
| 291 | double when = PyFloat_AsDouble(ot); |
| 292 | if (PyErr_Occurred()) |
| 293 | return 0; |
| 294 | *pwhen = when; |
| 295 | } |
| 296 | return 1; |
| 297 | } |
| 298 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 299 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 300 | time_gmtime(PyObject *self, PyObject *args) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 301 | { |
| 302 | double when; |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 303 | if (!parse_time_double_args(args, "|O:gmtime", &when)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 304 | return NULL; |
Brett Cannon | 298c380 | 2004-06-19 20:48:43 +0000 | [diff] [blame] | 305 | return time_convert(when, gmtime); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 308 | PyDoc_STRVAR(gmtime_doc, |
Fred Drake | 193a3f6 | 2002-03-12 21:38:49 +0000 | [diff] [blame] | 309 | "gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,\n\ |
| 310 | tm_sec, tm_wday, tm_yday, tm_isdst)\n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 311 | \n\ |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 312 | Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 313 | 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] | 314 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 315 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 316 | time_localtime(PyObject *self, PyObject *args) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 317 | { |
| 318 | double when; |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 319 | if (!parse_time_double_args(args, "|O:localtime", &when)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 320 | return NULL; |
Brett Cannon | 298c380 | 2004-06-19 20:48:43 +0000 | [diff] [blame] | 321 | return time_convert(when, localtime); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 324 | PyDoc_STRVAR(localtime_doc, |
Fred Drake | 193a3f6 | 2002-03-12 21:38:49 +0000 | [diff] [blame] | 325 | "localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)\n\ |
Martin v. Löwis | b3cfc1d | 2001-12-02 12:27:43 +0000 | [diff] [blame] | 326 | \n\ |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 327 | Convert seconds since the Epoch to a time tuple expressing local time.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 328 | When 'seconds' is not passed in, convert the current time instead."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 329 | |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 330 | static int |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 331 | gettmarg(PyObject *args, struct tm *p) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 332 | { |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 333 | int y; |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 334 | memset((void *) p, '\0', sizeof(struct tm)); |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 335 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 336 | if (!PyArg_Parse(args, "(iiiiiiiii)", |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 337 | &y, |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 338 | &p->tm_mon, |
| 339 | &p->tm_mday, |
| 340 | &p->tm_hour, |
| 341 | &p->tm_min, |
| 342 | &p->tm_sec, |
| 343 | &p->tm_wday, |
| 344 | &p->tm_yday, |
| 345 | &p->tm_isdst)) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 346 | return 0; |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 347 | if (y < 1900) { |
| 348 | PyObject *accept = PyDict_GetItemString(moddict, |
| 349 | "accept2dyear"); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 350 | if (accept == NULL || !PyInt_CheckExact(accept) || |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 351 | PyInt_AsLong(accept) == 0) { |
| 352 | PyErr_SetString(PyExc_ValueError, |
| 353 | "year >= 1900 required"); |
| 354 | return 0; |
| 355 | } |
| 356 | if (69 <= y && y <= 99) |
| 357 | y += 1900; |
| 358 | else if (0 <= y && y <= 68) |
| 359 | y += 2000; |
| 360 | else { |
| 361 | PyErr_SetString(PyExc_ValueError, |
Skip Montanaro | 1a10aac | 2001-08-22 12:39:16 +0000 | [diff] [blame] | 362 | "year out of range"); |
Guido van Rossum | cfbaecc | 1998-08-25 14:51:12 +0000 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | } |
| 366 | p->tm_year = y - 1900; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 367 | p->tm_mon--; |
| 368 | p->tm_wday = (p->tm_wday + 1) % 7; |
| 369 | p->tm_yday--; |
| 370 | return 1; |
| 371 | } |
| 372 | |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 373 | #ifdef HAVE_STRFTIME |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 374 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 375 | time_strftime(PyObject *self, PyObject *args) |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 376 | { |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 377 | PyObject *tup = NULL; |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 378 | struct tm buf; |
| 379 | const char *fmt; |
Guido van Rossum | fa48116 | 2000-06-28 21:33:59 +0000 | [diff] [blame] | 380 | size_t fmtlen, buflen; |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 381 | char *outbuf = 0; |
Guido van Rossum | fa48116 | 2000-06-28 21:33:59 +0000 | [diff] [blame] | 382 | size_t i; |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 383 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 384 | memset((void *) &buf, '\0', sizeof(buf)); |
Guido van Rossum | 1f41f84 | 1998-04-27 19:04:26 +0000 | [diff] [blame] | 385 | |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 386 | if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup)) |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 387 | return NULL; |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 388 | |
| 389 | if (tup == NULL) { |
| 390 | time_t tt = time(NULL); |
| 391 | buf = *localtime(&tt); |
| 392 | } else if (!gettmarg(tup, &buf)) |
| 393 | return NULL; |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 394 | |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 395 | /* Checks added to make sure strftime() does not crash Python by |
| 396 | indexing blindly into some array for a textual representation |
| 397 | by some bad index (fixes bug #897625). |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 398 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 399 | Also support values of zero from Python code for arguments in which |
| 400 | that is out of range by forcing that value to the lowest value that |
| 401 | is valid (fixed bug #1520914). |
| 402 | |
| 403 | Valid ranges based on what is allowed in struct tm: |
| 404 | |
| 405 | - tm_year: [0, max(int)] (1) |
| 406 | - tm_mon: [0, 11] (2) |
| 407 | - tm_mday: [1, 31] |
| 408 | - tm_hour: [0, 23] |
| 409 | - tm_min: [0, 59] |
| 410 | - tm_sec: [0, 60] |
| 411 | - tm_wday: [0, 6] (1) |
| 412 | - tm_yday: [0, 365] (2) |
| 413 | - tm_isdst: [-max(int), max(int)] |
| 414 | |
| 415 | (1) gettmarg() handles bounds-checking. |
| 416 | (2) Python's acceptable range is one greater than the range in C, |
| 417 | thus need to check against automatic decrement by gettmarg(). |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 418 | */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 419 | if (buf.tm_mon == -1) |
| 420 | buf.tm_mon = 0; |
| 421 | else if (buf.tm_mon < 0 || buf.tm_mon > 11) { |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 422 | PyErr_SetString(PyExc_ValueError, "month out of range"); |
| 423 | return NULL; |
| 424 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 425 | if (buf.tm_mday == 0) |
| 426 | buf.tm_mday = 1; |
| 427 | else if (buf.tm_mday < 0 || buf.tm_mday > 31) { |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 428 | PyErr_SetString(PyExc_ValueError, "day of month out of range"); |
| 429 | return NULL; |
| 430 | } |
| 431 | if (buf.tm_hour < 0 || buf.tm_hour > 23) { |
| 432 | PyErr_SetString(PyExc_ValueError, "hour out of range"); |
| 433 | return NULL; |
| 434 | } |
| 435 | if (buf.tm_min < 0 || buf.tm_min > 59) { |
| 436 | PyErr_SetString(PyExc_ValueError, "minute out of range"); |
| 437 | return NULL; |
| 438 | } |
| 439 | if (buf.tm_sec < 0 || buf.tm_sec > 61) { |
| 440 | PyErr_SetString(PyExc_ValueError, "seconds out of range"); |
| 441 | return NULL; |
| 442 | } |
| 443 | /* tm_wday does not need checking of its upper-bound since taking |
| 444 | ``% 7`` in gettmarg() automatically restricts the range. */ |
| 445 | if (buf.tm_wday < 0) { |
| 446 | PyErr_SetString(PyExc_ValueError, "day of week out of range"); |
| 447 | return NULL; |
| 448 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 449 | if (buf.tm_yday == -1) |
| 450 | buf.tm_yday = 0; |
| 451 | else if (buf.tm_yday < 0 || buf.tm_yday > 365) { |
Brett Cannon | d1080a3 | 2004-03-02 04:38:10 +0000 | [diff] [blame] | 452 | PyErr_SetString(PyExc_ValueError, "day of year out of range"); |
| 453 | return NULL; |
| 454 | } |
| 455 | if (buf.tm_isdst < -1 || buf.tm_isdst > 1) { |
| 456 | PyErr_SetString(PyExc_ValueError, |
| 457 | "daylight savings flag out of range"); |
| 458 | return NULL; |
| 459 | } |
| 460 | |
Guido van Rossum | c222ec2 | 1999-02-23 00:00:10 +0000 | [diff] [blame] | 461 | fmtlen = strlen(fmt); |
| 462 | |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 463 | /* I hate these functions that presume you know how big the output |
| 464 | * will be ahead of time... |
| 465 | */ |
Guido van Rossum | c222ec2 | 1999-02-23 00:00:10 +0000 | [diff] [blame] | 466 | for (i = 1024; ; i += i) { |
Walter Dörwald | cf47af4 | 2007-05-31 19:23:17 +0000 | [diff] [blame] | 467 | outbuf = (char *)PyMem_Malloc(i); |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 468 | if (outbuf == NULL) { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 469 | return PyErr_NoMemory(); |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 470 | } |
Guido van Rossum | c222ec2 | 1999-02-23 00:00:10 +0000 | [diff] [blame] | 471 | buflen = strftime(outbuf, i, fmt, &buf); |
| 472 | if (buflen > 0 || i >= 256 * fmtlen) { |
| 473 | /* If the buffer is 256 times as long as the format, |
| 474 | it's probably not failing for lack of room! |
| 475 | More likely, the format yields an empty result, |
| 476 | e.g. an empty format, or %Z when the timezone |
| 477 | is unknown. */ |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 478 | PyObject *ret; |
Walter Dörwald | cf47af4 | 2007-05-31 19:23:17 +0000 | [diff] [blame] | 479 | ret = PyUnicode_FromStringAndSize(outbuf, buflen); |
| 480 | PyMem_Free(outbuf); |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 481 | return ret; |
| 482 | } |
Walter Dörwald | cf47af4 | 2007-05-31 19:23:17 +0000 | [diff] [blame] | 483 | PyMem_Free(outbuf); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 484 | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
| 485 | /* VisualStudio .NET 2005 does this properly */ |
| 486 | if (buflen == 0 && errno == EINVAL) { |
| 487 | PyErr_SetString(PyExc_ValueError, "Invalid format string"); |
| 488 | return 0; |
| 489 | } |
| 490 | #endif |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 491 | } |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 492 | } |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 493 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 494 | PyDoc_STRVAR(strftime_doc, |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 495 | "strftime(format[, tuple]) -> string\n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 496 | \n\ |
| 497 | 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] | 498 | See the library reference manual for formatting codes. When the time tuple\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 499 | is not present, current time as returned by localtime() is used."); |
Guido van Rossum | 8d8c1ee | 1995-09-13 17:38:35 +0000 | [diff] [blame] | 500 | #endif /* HAVE_STRFTIME */ |
| 501 | |
Guido van Rossum | d3c46d5 | 2002-07-19 17:06:47 +0000 | [diff] [blame] | 502 | static PyObject * |
| 503 | time_strptime(PyObject *self, PyObject *args) |
| 504 | { |
| 505 | PyObject *strptime_module = PyImport_ImportModule("_strptime"); |
Raymond Hettinger | 502168a | 2003-04-10 16:03:22 +0000 | [diff] [blame] | 506 | PyObject *strptime_result; |
Guido van Rossum | d3c46d5 | 2002-07-19 17:06:47 +0000 | [diff] [blame] | 507 | |
Tim Peters | 513a1cd | 2003-01-19 04:54:58 +0000 | [diff] [blame] | 508 | if (!strptime_module) |
Guido van Rossum | d3c46d5 | 2002-07-19 17:06:47 +0000 | [diff] [blame] | 509 | return NULL; |
Raymond Hettinger | 502168a | 2003-04-10 16:03:22 +0000 | [diff] [blame] | 510 | strptime_result = PyObject_CallMethod(strptime_module, "strptime", "O", args); |
| 511 | Py_DECREF(strptime_module); |
| 512 | return strptime_result; |
Guido van Rossum | d3c46d5 | 2002-07-19 17:06:47 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 515 | PyDoc_STRVAR(strptime_doc, |
Brett Cannon | 20def8b | 2003-07-01 05:16:08 +0000 | [diff] [blame] | 516 | "strptime(string, format) -> struct_time\n\ |
Martin v. Löwis | b3cfc1d | 2001-12-02 12:27:43 +0000 | [diff] [blame] | 517 | \n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 518 | Parse a string to a time tuple according to a format specification.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 519 | See the library reference manual for formatting codes (same as strftime())."); |
Guido van Rossum | d3c46d5 | 2002-07-19 17:06:47 +0000 | [diff] [blame] | 520 | |
Guido van Rossum | 87ce7bb | 1998-06-09 16:30:31 +0000 | [diff] [blame] | 521 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 522 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 523 | time_asctime(PyObject *self, PyObject *args) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 524 | { |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 525 | PyObject *tup = NULL; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 526 | struct tm buf; |
| 527 | char *p; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 528 | if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup)) |
Guido van Rossum | b2b42dd | 2000-01-12 16:38:20 +0000 | [diff] [blame] | 529 | return NULL; |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 530 | if (tup == NULL) { |
| 531 | time_t tt = time(NULL); |
| 532 | buf = *localtime(&tt); |
| 533 | } else if (!gettmarg(tup, &buf)) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 534 | return NULL; |
| 535 | p = asctime(&buf); |
| 536 | if (p[24] == '\n') |
| 537 | p[24] = '\0'; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 538 | return PyString_FromString(p); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 541 | PyDoc_STRVAR(asctime_doc, |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 542 | "asctime([tuple]) -> string\n\ |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 543 | \n\ |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 544 | Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\ |
| 545 | When the time tuple is not present, current time as returned by localtime()\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 546 | is used."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 547 | |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 548 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 549 | time_ctime(PyObject *self, PyObject *args) |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 550 | { |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 551 | PyObject *ot = NULL; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 552 | time_t tt; |
| 553 | char *p; |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 554 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 555 | if (!PyArg_UnpackTuple(args, "ctime", 0, 1, &ot)) |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 556 | return NULL; |
| 557 | if (ot == NULL || ot == Py_None) |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 558 | tt = time(NULL); |
| 559 | else { |
Fred Drake | f901abd | 2004-08-03 17:58:55 +0000 | [diff] [blame] | 560 | double dt = PyFloat_AsDouble(ot); |
| 561 | if (PyErr_Occurred()) |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 562 | return NULL; |
Brett Cannon | 298c380 | 2004-06-19 20:48:43 +0000 | [diff] [blame] | 563 | tt = _PyTime_DoubleToTimet(dt); |
| 564 | if (tt == (time_t)-1 && PyErr_Occurred()) |
| 565 | return NULL; |
Thomas Wouters | fe38525 | 2001-01-19 23:16:56 +0000 | [diff] [blame] | 566 | } |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 567 | p = ctime(&tt); |
Guido van Rossum | 7853570 | 1998-03-03 22:19:10 +0000 | [diff] [blame] | 568 | if (p == NULL) { |
| 569 | PyErr_SetString(PyExc_ValueError, "unconvertible time"); |
| 570 | return NULL; |
| 571 | } |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 572 | if (p[24] == '\n') |
| 573 | p[24] = '\0'; |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 574 | return PyString_FromString(p); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 577 | PyDoc_STRVAR(ctime_doc, |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 578 | "ctime(seconds) -> string\n\ |
| 579 | \n\ |
| 580 | 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] | 581 | This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 582 | not present, current time as returned by localtime() is used."); |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 583 | |
Guido van Rossum | 60cd813 | 1998-03-06 17:16:21 +0000 | [diff] [blame] | 584 | #ifdef HAVE_MKTIME |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 585 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 586 | time_mktime(PyObject *self, PyObject *tup) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 587 | { |
| 588 | struct tm buf; |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 589 | time_t tt; |
| 590 | tt = time(&tt); |
| 591 | buf = *localtime(&tt); |
Guido van Rossum | b2b42dd | 2000-01-12 16:38:20 +0000 | [diff] [blame] | 592 | if (!gettmarg(tup, &buf)) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 593 | return NULL; |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 594 | tt = mktime(&buf); |
| 595 | if (tt == (time_t)(-1)) { |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 596 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 597 | "mktime argument out of range"); |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 598 | return NULL; |
| 599 | } |
Barry Warsaw | 9a2a8a8 | 1996-12-06 23:32:14 +0000 | [diff] [blame] | 600 | return PyFloat_FromDouble((double)tt); |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 601 | } |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 602 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 603 | PyDoc_STRVAR(mktime_doc, |
Guido van Rossum | 0ef577b | 1998-06-27 20:38:36 +0000 | [diff] [blame] | 604 | "mktime(tuple) -> floating point number\n\ |
| 605 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 606 | 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] | 607 | #endif /* HAVE_MKTIME */ |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 608 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 609 | #ifdef HAVE_WORKING_TZSET |
| 610 | void inittimezone(PyObject *module); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 611 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 612 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 613 | time_tzset(PyObject *self, PyObject *unused) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 614 | { |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 615 | PyObject* m; |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 616 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 617 | m = PyImport_ImportModule("time"); |
| 618 | if (m == NULL) { |
| 619 | return NULL; |
| 620 | } |
| 621 | |
| 622 | tzset(); |
| 623 | |
| 624 | /* Reset timezone, altzone, daylight and tzname */ |
| 625 | inittimezone(m); |
| 626 | Py_DECREF(m); |
Tim Peters | 1b6f7a9 | 2004-06-20 02:50:16 +0000 | [diff] [blame] | 627 | |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 628 | Py_INCREF(Py_None); |
| 629 | return Py_None; |
| 630 | } |
| 631 | |
| 632 | PyDoc_STRVAR(tzset_doc, |
| 633 | "tzset(zone)\n\ |
| 634 | \n\ |
| 635 | Initialize, or reinitialize, the local timezone to the value stored in\n\ |
| 636 | os.environ['TZ']. The TZ environment variable should be specified in\n\ |
Neal Norwitz | dc8e194 | 2004-07-20 22:34:37 +0000 | [diff] [blame] | 637 | standard Unix timezone format as documented in the tzset man page\n\ |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 638 | (eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\ |
| 639 | fall back to UTC. If the TZ environment variable is not set, the local\n\ |
| 640 | timezone is set to the systems best guess of wallclock time.\n\ |
| 641 | Changing the TZ environment variable without calling tzset *may* change\n\ |
| 642 | the local timezone used by methods such as localtime, but this behaviour\n\ |
| 643 | should not be relied on."); |
| 644 | #endif /* HAVE_WORKING_TZSET */ |
| 645 | |
| 646 | void inittimezone(PyObject *m) { |
| 647 | /* This code moved from inittime wholesale to allow calling it from |
| 648 | time_tzset. In the future, some parts of it can be moved back |
| 649 | (for platforms that don't HAVE_WORKING_TZSET, when we know what they |
| 650 | are), and the extranious calls to tzset(3) should be removed. |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 651 | I haven't done this yet, as I don't want to change this code as |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 652 | little as possible when introducing the time.tzset and time.tzsetwall |
| 653 | methods. This should simply be a method of doing the following once, |
| 654 | at the top of this function and removing the call to tzset() from |
| 655 | time_tzset(): |
| 656 | |
| 657 | #ifdef HAVE_TZSET |
| 658 | tzset() |
| 659 | #endif |
| 660 | |
| 661 | And I'm lazy and hate C so nyer. |
| 662 | */ |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 663 | #if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__) |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 664 | tzset(); |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 665 | #ifdef PYOS_OS2 |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 666 | PyModule_AddIntConstant(m, "timezone", _timezone); |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 667 | #else /* !PYOS_OS2 */ |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 668 | PyModule_AddIntConstant(m, "timezone", timezone); |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 669 | #endif /* PYOS_OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 670 | #ifdef HAVE_ALTZONE |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 671 | PyModule_AddIntConstant(m, "altzone", altzone); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 672 | #else |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 673 | #ifdef PYOS_OS2 |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 674 | PyModule_AddIntConstant(m, "altzone", _timezone-3600); |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 675 | #else /* !PYOS_OS2 */ |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 676 | PyModule_AddIntConstant(m, "altzone", timezone-3600); |
Guido van Rossum | 2645241 | 1998-09-28 22:07:11 +0000 | [diff] [blame] | 677 | #endif /* PYOS_OS2 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 678 | #endif |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 679 | PyModule_AddIntConstant(m, "daylight", daylight); |
| 680 | PyModule_AddObject(m, "tzname", |
| 681 | Py_BuildValue("(zz)", tzname[0], tzname[1])); |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 682 | #else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 683 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 684 | { |
| 685 | #define YEAR ((time_t)((365 * 24 + 6) * 3600)) |
| 686 | time_t t; |
| 687 | struct tm *p; |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 688 | long janzone, julyzone; |
| 689 | char janname[10], julyname[10]; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 690 | t = (time((time_t *)0) / YEAR) * YEAR; |
| 691 | p = localtime(&t); |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 692 | janzone = -p->tm_gmtoff; |
| 693 | strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9); |
| 694 | janname[9] = '\0'; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 695 | t += YEAR/2; |
| 696 | p = localtime(&t); |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 697 | julyzone = -p->tm_gmtoff; |
| 698 | strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9); |
| 699 | julyname[9] = '\0'; |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 700 | |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 701 | if( janzone < julyzone ) { |
| 702 | /* DST is reversed in the southern hemisphere */ |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 703 | PyModule_AddIntConstant(m, "timezone", julyzone); |
| 704 | PyModule_AddIntConstant(m, "altzone", janzone); |
| 705 | PyModule_AddIntConstant(m, "daylight", |
| 706 | janzone != julyzone); |
| 707 | PyModule_AddObject(m, "tzname", |
| 708 | Py_BuildValue("(zz)", |
| 709 | julyname, janname)); |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 710 | } else { |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 711 | PyModule_AddIntConstant(m, "timezone", janzone); |
| 712 | PyModule_AddIntConstant(m, "altzone", julyzone); |
| 713 | PyModule_AddIntConstant(m, "daylight", |
| 714 | janzone != julyzone); |
| 715 | PyModule_AddObject(m, "tzname", |
| 716 | Py_BuildValue("(zz)", |
| 717 | janname, julyname)); |
Guido van Rossum | 5773160 | 1999-03-29 19:12:04 +0000 | [diff] [blame] | 718 | } |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 719 | } |
Guido van Rossum | e6a4b7b | 1997-10-08 15:27:56 +0000 | [diff] [blame] | 720 | #else |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 721 | #endif /* HAVE_STRUCT_TM_TM_ZONE */ |
Tim Peters | 26ae7cd | 2001-03-20 03:26:49 +0000 | [diff] [blame] | 722 | #ifdef __CYGWIN__ |
| 723 | tzset(); |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 724 | PyModule_AddIntConstant(m, "timezone", _timezone); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 725 | PyModule_AddIntConstant(m, "altzone", _timezone-3600); |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 726 | PyModule_AddIntConstant(m, "daylight", _daylight); |
| 727 | PyModule_AddObject(m, "tzname", |
| 728 | Py_BuildValue("(zz)", _tzname[0], _tzname[1])); |
Tim Peters | 26ae7cd | 2001-03-20 03:26:49 +0000 | [diff] [blame] | 729 | #endif /* __CYGWIN__ */ |
Guido van Rossum | 10b164a | 2001-09-25 13:59:01 +0000 | [diff] [blame] | 730 | #endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | |
| 734 | static PyMethodDef time_methods[] = { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 735 | {"time", time_time, METH_NOARGS, time_doc}, |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 736 | #ifdef HAVE_CLOCK |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 737 | {"clock", time_clock, METH_NOARGS, clock_doc}, |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 738 | #endif |
| 739 | {"sleep", time_sleep, METH_VARARGS, sleep_doc}, |
| 740 | {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc}, |
| 741 | {"localtime", time_localtime, METH_VARARGS, localtime_doc}, |
| 742 | {"asctime", time_asctime, METH_VARARGS, asctime_doc}, |
| 743 | {"ctime", time_ctime, METH_VARARGS, ctime_doc}, |
| 744 | #ifdef HAVE_MKTIME |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 745 | {"mktime", time_mktime, METH_O, mktime_doc}, |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 746 | #endif |
| 747 | #ifdef HAVE_STRFTIME |
| 748 | {"strftime", time_strftime, METH_VARARGS, strftime_doc}, |
| 749 | #endif |
| 750 | {"strptime", time_strptime, METH_VARARGS, strptime_doc}, |
| 751 | #ifdef HAVE_WORKING_TZSET |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 752 | {"tzset", time_tzset, METH_NOARGS, tzset_doc}, |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 753 | #endif |
| 754 | {NULL, NULL} /* sentinel */ |
| 755 | }; |
| 756 | |
| 757 | |
| 758 | PyDoc_STRVAR(module_doc, |
| 759 | "This module provides various functions to manipulate time values.\n\ |
| 760 | \n\ |
| 761 | There are two standard representations of time. One is the number\n\ |
| 762 | of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\ |
| 763 | or a floating point number (to represent fractions of seconds).\n\ |
| 764 | The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\ |
| 765 | The actual value can be retrieved by calling gmtime(0).\n\ |
| 766 | \n\ |
| 767 | The other representation is a tuple of 9 integers giving local time.\n\ |
| 768 | The tuple items are:\n\ |
| 769 | year (four digits, e.g. 1998)\n\ |
| 770 | month (1-12)\n\ |
| 771 | day (1-31)\n\ |
| 772 | hours (0-23)\n\ |
| 773 | minutes (0-59)\n\ |
| 774 | seconds (0-59)\n\ |
| 775 | weekday (0-6, Monday is 0)\n\ |
| 776 | Julian day (day in the year, 1-366)\n\ |
| 777 | DST (Daylight Savings Time) flag (-1, 0 or 1)\n\ |
| 778 | If the DST flag is 0, the time is given in the regular time zone;\n\ |
| 779 | if it is 1, the time is given in the DST time zone;\n\ |
| 780 | if it is -1, mktime() should guess based on the date and time.\n\ |
| 781 | \n\ |
| 782 | Variables:\n\ |
| 783 | \n\ |
| 784 | timezone -- difference in seconds between UTC and local standard time\n\ |
| 785 | altzone -- difference in seconds between UTC and local DST time\n\ |
| 786 | daylight -- whether local time should reflect DST\n\ |
| 787 | tzname -- tuple of (standard time zone name, DST time zone name)\n\ |
| 788 | \n\ |
| 789 | Functions:\n\ |
| 790 | \n\ |
| 791 | time() -- return current time in seconds since the Epoch as a float\n\ |
| 792 | clock() -- return CPU time since process start as a float\n\ |
| 793 | sleep() -- delay for a number of seconds given as a float\n\ |
| 794 | gmtime() -- convert seconds since Epoch to UTC tuple\n\ |
| 795 | localtime() -- convert seconds since Epoch to local time tuple\n\ |
| 796 | asctime() -- convert time tuple to string\n\ |
| 797 | ctime() -- convert time in seconds to string\n\ |
| 798 | mktime() -- convert local time tuple to seconds since Epoch\n\ |
| 799 | strftime() -- convert time tuple to string according to format specification\n\ |
| 800 | strptime() -- parse string to time tuple according to format specification\n\ |
| 801 | tzset() -- change the local timezone"); |
| 802 | |
| 803 | |
| 804 | PyMODINIT_FUNC |
| 805 | inittime(void) |
| 806 | { |
| 807 | PyObject *m; |
| 808 | char *p; |
| 809 | m = Py_InitModule3("time", time_methods, module_doc); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 810 | if (m == NULL) |
| 811 | return; |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 812 | |
| 813 | /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */ |
| 814 | p = Py_GETENV("PYTHONY2K"); |
| 815 | PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p)); |
| 816 | /* Squirrel away the module's dictionary for the y2k check */ |
| 817 | moddict = PyModule_GetDict(m); |
| 818 | Py_INCREF(moddict); |
| 819 | |
| 820 | /* Set, or reset, module variables like time.timezone */ |
| 821 | inittimezone(m); |
| 822 | |
Mark Hammond | 975e392 | 2002-07-16 01:29:19 +0000 | [diff] [blame] | 823 | #ifdef MS_WINDOWS |
| 824 | /* Helper to allow interrupts for Windows. |
| 825 | If Ctrl+C event delivered while not sleeping |
| 826 | it will be ignored. |
| 827 | */ |
| 828 | main_thread = PyThread_get_thread_ident(); |
| 829 | hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 830 | SetConsoleCtrlHandler( PyCtrlHandler, TRUE); |
| 831 | #endif /* MS_WINDOWS */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 832 | if (!initialized) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 833 | PyStructSequence_InitType(&StructTimeType, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 834 | &struct_time_type_desc); |
| 835 | } |
Fred Drake | 9bb7432 | 2002-04-01 14:49:59 +0000 | [diff] [blame] | 836 | Py_INCREF(&StructTimeType); |
| 837 | PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 838 | initialized = 1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 842 | /* Implement floattime() for various platforms */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 843 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 844 | static double |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 845 | floattime(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 846 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 847 | /* There are three ways to get the time: |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 848 | (1) gettimeofday() -- resolution in microseconds |
| 849 | (2) ftime() -- resolution in milliseconds |
| 850 | (3) time() -- resolution in seconds |
| 851 | In all cases the return value is a float in seconds. |
| 852 | Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may |
| 853 | fail, so we fall back on ftime() or time(). |
| 854 | Note: clock resolution does not imply clock accuracy! */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 855 | #ifdef HAVE_GETTIMEOFDAY |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 856 | { |
| 857 | struct timeval t; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 858 | #ifdef GETTIMEOFDAY_NO_TZ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 859 | if (gettimeofday(&t) == 0) |
| 860 | return (double)t.tv_sec + t.tv_usec*0.000001; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 861 | #else /* !GETTIMEOFDAY_NO_TZ */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 862 | if (gettimeofday(&t, (struct timezone *)NULL) == 0) |
| 863 | return (double)t.tv_sec + t.tv_usec*0.000001; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 864 | #endif /* !GETTIMEOFDAY_NO_TZ */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 865 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 866 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 867 | #endif /* !HAVE_GETTIMEOFDAY */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 868 | { |
Guido van Rossum | d3eb577 | 1999-03-09 16:07:23 +0000 | [diff] [blame] | 869 | #if defined(HAVE_FTIME) |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 870 | struct timeb t; |
| 871 | ftime(&t); |
| 872 | return (double)t.time + (double)t.millitm * (double)0.001; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 873 | #else /* !HAVE_FTIME */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 874 | time_t secs; |
| 875 | time(&secs); |
| 876 | return (double)secs; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 877 | #endif /* !HAVE_FTIME */ |
Barry Warsaw | 4a6cf41 | 1997-01-13 22:44:55 +0000 | [diff] [blame] | 878 | } |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 881 | |
| 882 | /* Implement floatsleep() for various platforms. |
| 883 | When interrupted (or when another error occurs), return -1 and |
| 884 | set an exception; else return 0. */ |
| 885 | |
| 886 | static int |
Guido van Rossum | a320fd3 | 1995-03-09 12:14:15 +0000 | [diff] [blame] | 887 | floatsleep(double secs) |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 888 | { |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 889 | /* XXX Should test for MS_WINDOWS first! */ |
Skip Montanaro | eb33e5a | 2007-08-17 12:57:41 +0000 | [diff] [blame^] | 890 | #if defined(HAVE_SELECT) && !defined(__EMX__) |
Guido van Rossum | 426035c | 1991-02-19 12:27:35 +0000 | [diff] [blame] | 891 | struct timeval t; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 892 | double frac; |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 893 | frac = fmod(secs, 1.0); |
| 894 | secs = floor(secs); |
| 895 | t.tv_sec = (long)secs; |
| 896 | t.tv_usec = (long)(frac*1000000.0); |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 897 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 898 | 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] | 899 | #ifdef EINTR |
Guido van Rossum | a5456d5 | 1999-08-19 14:40:27 +0000 | [diff] [blame] | 900 | if (errno != EINTR) { |
Guido van Rossum | 09cbb01 | 1999-11-08 15:32:27 +0000 | [diff] [blame] | 901 | #else |
| 902 | if (1) { |
| 903 | #endif |
Andrew M. Kuchling | c24ca4b | 2000-03-24 20:35:20 +0000 | [diff] [blame] | 904 | Py_BLOCK_THREADS |
Guido van Rossum | a5456d5 | 1999-08-19 14:40:27 +0000 | [diff] [blame] | 905 | PyErr_SetFromErrno(PyExc_IOError); |
| 906 | return -1; |
| 907 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 908 | } |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 909 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 02af964 | 2002-01-16 11:04:06 +0000 | [diff] [blame] | 910 | #elif defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 911 | /* XXX Can't interrupt this sleep */ |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 912 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | bceeac8 | 1996-05-23 22:53:47 +0000 | [diff] [blame] | 913 | delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */ |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 914 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 915 | #elif defined(MS_WINDOWS) |
Fred Drake | 0e12395 | 2000-06-29 21:31:02 +0000 | [diff] [blame] | 916 | { |
| 917 | double millisecs = secs * 1000.0; |
Tim Peters | 513a1cd | 2003-01-19 04:54:58 +0000 | [diff] [blame] | 918 | unsigned long ul_millis; |
| 919 | |
Fred Drake | 0e12395 | 2000-06-29 21:31:02 +0000 | [diff] [blame] | 920 | if (millisecs > (double)ULONG_MAX) { |
Tim Peters | 513a1cd | 2003-01-19 04:54:58 +0000 | [diff] [blame] | 921 | PyErr_SetString(PyExc_OverflowError, |
| 922 | "sleep length is too large"); |
Fred Drake | 0e12395 | 2000-06-29 21:31:02 +0000 | [diff] [blame] | 923 | return -1; |
| 924 | } |
Fred Drake | 0e12395 | 2000-06-29 21:31:02 +0000 | [diff] [blame] | 925 | Py_BEGIN_ALLOW_THREADS |
Tim Peters | 513a1cd | 2003-01-19 04:54:58 +0000 | [diff] [blame] | 926 | /* Allow sleep(0) to maintain win32 semantics, and as decreed |
| 927 | * by Guido, only the main thread can be interrupted. |
| 928 | */ |
| 929 | ul_millis = (unsigned long)millisecs; |
| 930 | if (ul_millis == 0 || |
| 931 | main_thread != PyThread_get_thread_ident()) |
| 932 | Sleep(ul_millis); |
Mark Hammond | 975e392 | 2002-07-16 01:29:19 +0000 | [diff] [blame] | 933 | else { |
| 934 | DWORD rc; |
| 935 | ResetEvent(hInterruptEvent); |
Tim Peters | 513a1cd | 2003-01-19 04:54:58 +0000 | [diff] [blame] | 936 | rc = WaitForSingleObject(hInterruptEvent, ul_millis); |
| 937 | if (rc == WAIT_OBJECT_0) { |
| 938 | /* Yield to make sure real Python signal |
| 939 | * handler called. |
| 940 | */ |
Mark Hammond | 975e392 | 2002-07-16 01:29:19 +0000 | [diff] [blame] | 941 | Sleep(1); |
| 942 | Py_BLOCK_THREADS |
Mark Hammond | 975e392 | 2002-07-16 01:29:19 +0000 | [diff] [blame] | 943 | errno = EINTR; |
| 944 | PyErr_SetFromErrno(PyExc_IOError); |
| 945 | return -1; |
| 946 | } |
| 947 | } |
Fred Drake | 0e12395 | 2000-06-29 21:31:02 +0000 | [diff] [blame] | 948 | Py_END_ALLOW_THREADS |
| 949 | } |
Martin v. Löwis | 02af964 | 2002-01-16 11:04:06 +0000 | [diff] [blame] | 950 | #elif defined(PYOS_OS2) |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 951 | /* This Sleep *IS* Interruptable by Exceptions */ |
Guido van Rossum | 1d0d7e4 | 1997-12-29 20:03:10 +0000 | [diff] [blame] | 952 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 953 | if (DosSleep(secs * 1000) != NO_ERROR) { |
Guido van Rossum | 1d0d7e4 | 1997-12-29 20:03:10 +0000 | [diff] [blame] | 954 | Py_BLOCK_THREADS |
Guido van Rossum | 8e9ebfd | 1997-11-22 21:53:48 +0000 | [diff] [blame] | 955 | PyErr_SetFromErrno(PyExc_IOError); |
| 956 | return -1; |
| 957 | } |
Guido van Rossum | 1d0d7e4 | 1997-12-29 20:03:10 +0000 | [diff] [blame] | 958 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 02af964 | 2002-01-16 11:04:06 +0000 | [diff] [blame] | 959 | #elif defined(PLAN9) |
| 960 | { |
| 961 | double millisecs = secs * 1000.0; |
| 962 | if (millisecs > (double)LONG_MAX) { |
| 963 | PyErr_SetString(PyExc_OverflowError, "sleep length is too large"); |
| 964 | return -1; |
| 965 | } |
| 966 | /* This sleep *CAN BE* interrupted. */ |
| 967 | Py_BEGIN_ALLOW_THREADS |
| 968 | if(sleep((long)millisecs) < 0){ |
| 969 | Py_BLOCK_THREADS |
| 970 | PyErr_SetFromErrno(PyExc_IOError); |
| 971 | return -1; |
| 972 | } |
| 973 | Py_END_ALLOW_THREADS |
| 974 | } |
| 975 | #else |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 976 | /* XXX Can't interrupt this sleep */ |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 977 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 978 | sleep((int)secs); |
Guido van Rossum | 8607ae2 | 1997-11-03 22:04:46 +0000 | [diff] [blame] | 979 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 02af964 | 2002-01-16 11:04:06 +0000 | [diff] [blame] | 980 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 981 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 982 | return 0; |
Guido van Rossum | 80c9d88 | 1991-04-16 08:47:51 +0000 | [diff] [blame] | 983 | } |
Guido van Rossum | d11b62e | 2003-03-14 21:51:36 +0000 | [diff] [blame] | 984 | |
| 985 | |