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