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