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