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