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