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