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