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