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