annotate Modules/timemodule.c @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents b4321f93e3f2
children 478492476b1f da851aa6aa3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Time module */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
3 #include "Python.h"
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 62364
diff changeset
4 #include "_time.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
5
8470
933882c0e9fb Added strptime, if it exists. Modified after a patch by Jesse Hallio.
Guido van Rossum <guido@python.org>
parents: 8161
diff changeset
6 #include <ctype.h>
933882c0e9fb Added strptime, if it exists. Modified after a patch by Jesse Hallio.
Guido van Rossum <guido@python.org>
parents: 8161
diff changeset
7
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
8 #ifdef HAVE_SYS_TYPES_H
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
9 #include <sys/types.h>
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
10 #endif /* HAVE_SYS_TYPES_H */
1177
533e36de1e4d * Configure.py: use #!/usr/local/bin/python
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
11
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
12 #ifdef QUICKWIN
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
13 #include <io.h>
956
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
14 #endif
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
15
6716
d854a1c7e74d Patch for QNX, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
16 #if defined(__WATCOMC__) && !defined(__QNX__)
3592
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
17 #include <i86.h>
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
18 #else
4216
0a5f01d4701c Some changes for better Windows portability.
Guido van Rossum <guido@python.org>
parents: 3979
diff changeset
19 #ifdef MS_WINDOWS
24243
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
20 #define WIN32_LEAN_AND_MEAN
16953
9fd20cae7b76 Fix typo in RISCOS patch inside MS #ifdef. (Probably my own fingers.)
Guido van Rossum <guido@python.org>
parents: 16947
diff changeset
21 #include <windows.h>
24243
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
22 #include "pythread.h"
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
23
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
24 /* helper to allow us to interrupt sleep() on Windows*/
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
25 static HANDLE hInterruptEvent = NULL;
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
26 static BOOL WINAPI PyCtrlHandler(DWORD dwCtrlType)
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
27 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
28 SetEvent(hInterruptEvent);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
29 /* allow other default handlers to be called.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
30 Default Python handler will setup the
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
31 KeyboardInterrupt exception.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
32 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
33 return FALSE;
24243
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
34 }
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
35 static long main_thread;
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
36
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23832
diff changeset
37 #if defined(__BORLANDC__)
4232
886c0f775c97 Sleep() turns out to be win32 specific.
Guido van Rossum <guido@python.org>
parents: 4216
diff changeset
38 /* These overrides not needed for Win32 */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
39 #define timezone _timezone
2533
e506ec4fb2b1 tiny additions for Windows 3.1
Guido van Rossum <guido@python.org>
parents: 2506
diff changeset
40 #define tzname _tzname
e506ec4fb2b1 tiny additions for Windows 3.1
Guido van Rossum <guido@python.org>
parents: 2506
diff changeset
41 #define daylight _daylight
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23832
diff changeset
42 #endif /* __BORLANDC__ */
4216
0a5f01d4701c Some changes for better Windows portability.
Guido van Rossum <guido@python.org>
parents: 3979
diff changeset
43 #endif /* MS_WINDOWS */
6716
d854a1c7e74d Patch for QNX, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
44 #endif /* !__WATCOMC__ || __QNX__ */
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
45
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
46 #if defined(MS_WINDOWS) && !defined(__BORLANDC__)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
47 /* Win32 has better clock replacement; we have our own version below. */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
48 #undef HAVE_CLOCK
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
49 #endif /* MS_WINDOWS && !defined(__BORLANDC__) */
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
50
22114
c454b76d51e7 OS/2 EMX port changes (Modules part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21938
diff changeset
51 #if defined(PYOS_OS2)
c454b76d51e7 OS/2 EMX port changes (Modules part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21938
diff changeset
52 #define INCL_DOS
c454b76d51e7 OS/2 EMX port changes (Modules part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21938
diff changeset
53 #define INCL_ERRORS
c454b76d51e7 OS/2 EMX port changes (Modules part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21938
diff changeset
54 #include <os2.h>
c454b76d51e7 OS/2 EMX port changes (Modules part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21938
diff changeset
55 #endif
c454b76d51e7 OS/2 EMX port changes (Modules part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21938
diff changeset
56
6626
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6548
diff changeset
57 #if defined(PYCC_VACPP)
9160
f0a92c0a115e Fixes for OS/2 by Jeff Rush.
Guido van Rossum <guido@python.org>
parents: 9042
diff changeset
58 #include <sys/time.h>
6626
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6548
diff changeset
59 #endif
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6548
diff changeset
60
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
61 /* Forward declarations */
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13329
diff changeset
62 static int floatsleep(double);
13691
0f1c6053e8d0 Fix floattime prototype ((void), not (double))
Thomas Wouters <thomas@python.org>
parents: 13690
diff changeset
63 static double floattime(void);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
9042
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
65 /* For Y2K check */
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
66 static PyObject *moddict;
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
67
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
68 static PyObject *
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
69 time_time(PyObject *self, PyObject *unused)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
71 double secs;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
72 secs = floattime();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
73 if (secs == 0.0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
74 PyErr_SetFromErrno(PyExc_IOError);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
75 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
76 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
77 return PyFloat_FromDouble(secs);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
78 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
79
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
80 PyDoc_STRVAR(time_doc,
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
81 "time() -> floating point number\n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
82 \n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
83 Return the current time in seconds since the Epoch.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
84 Fractions of a second may be present if the system clock provides them.");
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
85
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
86 #ifdef HAVE_CLOCK
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
87
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
88 #ifndef CLOCKS_PER_SEC
3419
e045f9132b07 default CLOCKS_PER_SEC to CLK_TCK if it exists
Guido van Rossum <guido@python.org>
parents: 3388
diff changeset
89 #ifdef CLK_TCK
e045f9132b07 default CLOCKS_PER_SEC to CLK_TCK if it exists
Guido van Rossum <guido@python.org>
parents: 3388
diff changeset
90 #define CLOCKS_PER_SEC CLK_TCK
e045f9132b07 default CLOCKS_PER_SEC to CLK_TCK if it exists
Guido van Rossum <guido@python.org>
parents: 3388
diff changeset
91 #else
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
92 #define CLOCKS_PER_SEC 1000000
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
93 #endif
3419
e045f9132b07 default CLOCKS_PER_SEC to CLK_TCK if it exists
Guido van Rossum <guido@python.org>
parents: 3388
diff changeset
94 #endif
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
95
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
96 static PyObject *
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
97 time_clock(PyObject *self, PyObject *unused)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
98 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
99 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
101 #endif /* HAVE_CLOCK */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
103 #if defined(MS_WINDOWS) && !defined(__BORLANDC__)
21934
71ffc9fb5986 Ensure we also build on VC7. Involves replacing largeint.h helper functions with msvc's native 64 bit integers.
Mark Hammond <mhammond@skippinet.com.au>
parents: 21738
diff changeset
104 /* Due to Mark Hammond and Tim Peters */
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
105 static PyObject *
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
106 time_clock(PyObject *self, PyObject *unused)
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
107 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
108 static LARGE_INTEGER ctrStart;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
109 static double divisor = 0.0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
110 LARGE_INTEGER now;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
111 double diff;
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
112
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
113 if (divisor == 0.0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
114 LARGE_INTEGER freq;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
115 QueryPerformanceCounter(&ctrStart);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
116 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
117 /* Unlikely to happen - this works on all intel
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
118 machines at least! Revert to clock() */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
119 return PyFloat_FromDouble(((double)clock()) /
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
120 CLOCKS_PER_SEC);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
121 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
122 divisor = (double)freq.QuadPart;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
123 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
124 QueryPerformanceCounter(&now);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
125 diff = (double)(now.QuadPart - ctrStart.QuadPart);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
126 return PyFloat_FromDouble(diff / divisor);
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
127 }
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
128
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
129 #define HAVE_CLOCK /* So it gets included in the methods */
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
130 #endif /* MS_WINDOWS && !defined(__BORLANDC__) */
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
131
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
132 #ifdef HAVE_CLOCK
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
133 PyDoc_STRVAR(clock_doc,
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
134 "clock() -> floating point number\n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
135 \n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
136 Return the CPU time or real time since the start of the process or since\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
137 the first call to clock(). This has as much precision as the system\n\
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
138 records.");
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
139 #endif
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
140
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
141 static PyObject *
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
142 time_sleep(PyObject *self, PyObject *args)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
144 double secs;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
145 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
146 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
147 if (floatsleep(secs) != 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
148 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
149 Py_INCREF(Py_None);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
150 return Py_None;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
153 PyDoc_STRVAR(sleep_doc,
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
154 "sleep(seconds)\n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
155 \n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
156 Delay execution for a given number of seconds. The argument may be\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
157 a floating point number for subsecond precision.");
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
158
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
159 static PyStructSequence_Field struct_time_type_fields[] = {
62118
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
160 {"tm_year", "year, for example, 1993"},
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
161 {"tm_mon", "month of year, range [1, 12]"},
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
162 {"tm_mday", "day of month, range [1, 31]"},
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
163 {"tm_hour", "hours, range [0, 23]"},
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
164 {"tm_min", "minutes, range [0, 59]"},
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
165 {"tm_sec", "seconds, range [0, 61])"},
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
166 {"tm_wday", "day of week, range [0, 6], Monday is 0"},
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
167 {"tm_yday", "day of year, range [1, 366]"},
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
168 {"tm_isdst", "1 if summer time is in effect, 0 if not, and -1 if unknown"},
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
169 {0}
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
170 };
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
171
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
172 static PyStructSequence_Desc struct_time_type_desc = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
173 "time.struct_time",
62118
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
174 "The time value as returned by gmtime(), localtime(), and strptime(), and\n"
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
175 " accepted by asctime(), mktime() and strftime(). May be considered as a\n"
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
176 " sequence of 9 integers.\n\n"
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
177 " Note that several fields' values are not the same as those defined by\n"
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
178 " the C language standard for struct tm. For example, the value of the\n"
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
179 " field tm_year is the actual year, not year - 1900. See individual\n"
0a4b33e4ee23 Merged revisions 81756 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 61466
diff changeset
180 " fields' descriptions for details.",
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
181 struct_time_type_fields,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
182 9,
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
183 };
21938
4ab29b06a76c Windows time_clock(): rewrite to get rid of horrid casting tricks.
Tim Peters <tim.peters@gmail.com>
parents: 21934
diff changeset
184
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36324
diff changeset
185 static int initialized;
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
186 static PyTypeObject StructTimeType;
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
187
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
188 static PyObject *
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
189 tmtotuple(struct tm *p)
8470
933882c0e9fb Added strptime, if it exists. Modified after a patch by Jesse Hallio.
Guido van Rossum <guido@python.org>
parents: 8161
diff changeset
190 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
191 PyObject *v = PyStructSequence_New(&StructTimeType);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
192 if (v == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
193 return NULL;
21938
4ab29b06a76c Windows time_clock(): rewrite to get rid of horrid casting tricks.
Tim Peters <tim.peters@gmail.com>
parents: 21934
diff changeset
194
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43653
diff changeset
195 #define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val))
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
196
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
197 SET(0, p->tm_year + 1900);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
198 SET(1, p->tm_mon + 1); /* Want January == 1 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
199 SET(2, p->tm_mday);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
200 SET(3, p->tm_hour);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
201 SET(4, p->tm_min);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
202 SET(5, p->tm_sec);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
203 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
204 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
205 SET(8, p->tm_isdst);
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
206 #undef SET
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
207 if (PyErr_Occurred()) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
208 Py_XDECREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
209 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
210 }
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
211
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
212 return v;
8470
933882c0e9fb Added strptime, if it exists. Modified after a patch by Jesse Hallio.
Guido van Rossum <guido@python.org>
parents: 8161
diff changeset
213 }
933882c0e9fb Added strptime, if it exists. Modified after a patch by Jesse Hallio.
Guido van Rossum <guido@python.org>
parents: 8161
diff changeset
214
933882c0e9fb Added strptime, if it exists. Modified after a patch by Jesse Hallio.
Guido van Rossum <guido@python.org>
parents: 8161
diff changeset
215 static PyObject *
32384
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
216 time_convert(double when, struct tm * (*function)(const time_t *))
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
217 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
218 struct tm *p;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
219 time_t whent = _PyTime_DoubleToTimet(when);
32384
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
220
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
221 if (whent == (time_t)-1 && PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
222 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
223 errno = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
224 p = function(&whent);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
225 if (p == NULL) {
4373
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
226 #ifdef EINVAL
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
227 if (errno == 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
228 errno = EINVAL;
4373
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
229 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
230 return PyErr_SetFromErrno(PyExc_ValueError);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
231 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
232 return tmtotuple(p);
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
233 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
234
32939
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
235 /* Parse arg tuple that can contain an optional float-or-None value;
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
236 format needs to be "|O:name".
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
237 Returns non-zero on success (parallels PyArg_ParseTuple).
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
238 */
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
239 static int
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
240 parse_time_double_args(PyObject *args, char *format, double *pwhen)
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
241 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
242 PyObject *ot = NULL;
32939
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
243
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
244 if (!PyArg_ParseTuple(args, format, &ot))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
245 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
246 if (ot == NULL || ot == Py_None)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
247 *pwhen = floattime();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
248 else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
249 double when = PyFloat_AsDouble(ot);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
250 if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
251 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
252 *pwhen = when;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
253 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
254 return 1;
32939
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
255 }
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
256
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
257 static PyObject *
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
258 time_gmtime(PyObject *self, PyObject *args)
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
259 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
260 double when;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
261 if (!parse_time_double_args(args, "|O:gmtime", &when))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
262 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
263 return time_convert(when, gmtime);
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
264 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
265
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
266 PyDoc_STRVAR(gmtime_doc,
44382
71bb70f6097b Merged revisions 59595-59604 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
267 "gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,\n\
22256
54b636160acd Update docstrings to use te attribute names of the new structures returned
Fred Drake <fdrake@acm.org>
parents: 22114
diff changeset
268 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
269 \n\
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
270 Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
271 GMT). When 'seconds' is not passed in, convert the current time instead.");
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
272
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
273 static PyObject *
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
274 time_localtime(PyObject *self, PyObject *args)
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
275 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
276 double when;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
277 if (!parse_time_double_args(args, "|O:localtime", &when))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
278 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
279 return time_convert(when, localtime);
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
280 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
281
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
282 PyDoc_STRVAR(localtime_doc,
44382
71bb70f6097b Merged revisions 59595-59604 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
283 "localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,\n\
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
284 tm_sec,tm_wday,tm_yday,tm_isdst)\n\
21133
ec88fc430127 Patch #481718: Time module doc string changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 20654
diff changeset
285 \n\
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
286 Convert seconds since the Epoch to a time tuple expressing local time.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
287 When 'seconds' is not passed in, convert the current time instead.");
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
288
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
289 /* Convert 9-item tuple to tm structure. Return 1 on success, set
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
290 * an exception and return 0 on error.
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
291 */
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
292 static int
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
293 gettmarg(PyObject *args, struct tm *p)
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
294 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
295 int y;
42818
fb58fb3bddc1 Reorder code and decls so it's compilable with gcc 2.96 (C89).
Guido van Rossum <guido@python.org>
parents: 42796
diff changeset
296
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
297 memset((void *) p, '\0', sizeof(struct tm));
9042
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
298
67424
7e5777e1f0fd Further simplify gettmarg()
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67409
diff changeset
299 if (!PyTuple_Check(args)) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
300 PyErr_SetString(PyExc_TypeError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
301 "Tuple or struct_time argument required");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
302 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
303 }
42796
e1a36559d8b6 Remove PyArg_Parse usage from time module. (An extra set of eyeballs on
Skip Montanaro <skip@pobox.com>
parents: 42763
diff changeset
304
67424
7e5777e1f0fd Further simplify gettmarg()
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67409
diff changeset
305 if (!PyArg_ParseTuple(args, "iiiiiiiii",
7e5777e1f0fd Further simplify gettmarg()
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67409
diff changeset
306 &y, &p->tm_mon, &p->tm_mday,
7e5777e1f0fd Further simplify gettmarg()
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67409
diff changeset
307 &p->tm_hour, &p->tm_min, &p->tm_sec,
7e5777e1f0fd Further simplify gettmarg()
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67409
diff changeset
308 &p->tm_wday, &p->tm_yday, &p->tm_isdst))
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
309 return 0;
42796
e1a36559d8b6 Remove PyArg_Parse usage from time module. (An extra set of eyeballs on
Skip Montanaro <skip@pobox.com>
parents: 42763
diff changeset
310
67436
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
311 /* If year is specified with less than 4 digits, its interpretation
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
312 * depends on the accept2dyear value.
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
313 *
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
314 * If accept2dyear is true (default), a backward compatibility behavior is
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
315 * invoked as follows:
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
316 *
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
317 * - for 2-digit year, century is guessed according to POSIX rules for
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
318 * %y strptime format: 21st century for y < 69, 20th century
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
319 * otherwise. A deprecation warning is issued when century
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
320 * information is guessed in this way.
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
321 *
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
322 * - for 3-digit or negative year, a ValueError exception is raised.
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
323 *
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
324 * If accept2dyear is false (set by the program or as a result of a
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
325 * non-empty value assigned to PYTHONY2K environment variable) all year
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
326 * values are interpreted as given.
67390
92f61a692e39 - time.accept2dyear = True is now equivalent to time.accept2dyear = 1
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67352
diff changeset
327 */
67436
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
328 if (y < 1000) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
329 PyObject *accept = PyDict_GetItemString(moddict,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
330 "accept2dyear");
67455
e480604767c4 Issue #1777412: Remove all limits on tm_year from time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67452
diff changeset
331 if (accept != NULL) {
67451
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
332 int acceptval = PyObject_IsTrue(accept);
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
333 if (acceptval == -1)
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
334 return 0;
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
335 if (acceptval) {
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
336 if (0 <= y && y < 69)
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
337 y += 2000;
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
338 else if (69 <= y && y < 100)
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
339 y += 1900;
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
340 else {
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
341 PyErr_SetString(PyExc_ValueError,
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
342 "year out of range");
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
343 return 0;
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
344 }
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
345 if (PyErr_WarnEx(PyExc_DeprecationWarning,
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
346 "Century info guessed for a 2-digit year.", 1) != 0)
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
347 return 0;
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
348 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
349 }
67451
c6b8c5b564a8 Fixed error handling branches. Thanks
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67450
diff changeset
350 else
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
351 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
352 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
353 p->tm_year = y - 1900;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
354 p->tm_mon--;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
355 p->tm_wday = (p->tm_wday + 1) % 7;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
356 p->tm_yday--;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
357 return 1;
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
358 }
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
359
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
360 /* Check values of the struct tm fields before it is passed to strftime() and
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
361 * asctime(). Return 1 if all values are valid, otherwise set an exception
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
362 * and returns 0.
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
363 */
65156
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
364 static int
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
365 checktm(struct tm* buf)
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
366 {
65156
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
367 /* Checks added to make sure strftime() and asctime() does not crash Python by
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
368 indexing blindly into some array for a textual representation
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
369 by some bad index (fixes bug #897625 and #6608).
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
370
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
371 Also support values of zero from Python code for arguments in which
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
372 that is out of range by forcing that value to the lowest value that
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
373 is valid (fixed bug #1520914).
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
374
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
375 Valid ranges based on what is allowed in struct tm:
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
376
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
377 - tm_year: [0, max(int)] (1)
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
378 - tm_mon: [0, 11] (2)
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
379 - tm_mday: [1, 31]
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
380 - tm_hour: [0, 23]
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
381 - tm_min: [0, 59]
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
382 - tm_sec: [0, 60]
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
383 - tm_wday: [0, 6] (1)
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
384 - tm_yday: [0, 365] (2)
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
385 - tm_isdst: [-max(int), max(int)]
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
386
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
387 (1) gettmarg() handles bounds-checking.
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
388 (2) Python's acceptable range is one greater than the range in C,
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
389 thus need to check against automatic decrement by gettmarg().
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
390 */
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
391 if (buf->tm_mon == -1)
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
392 buf->tm_mon = 0;
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
393 else if (buf->tm_mon < 0 || buf->tm_mon > 11) {
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
394 PyErr_SetString(PyExc_ValueError, "month out of range");
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
395 return 0;
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
396 }
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
397 if (buf->tm_mday == 0)
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
398 buf->tm_mday = 1;
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
399 else if (buf->tm_mday < 0 || buf->tm_mday > 31) {
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
400 PyErr_SetString(PyExc_ValueError, "day of month out of range");
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
401 return 0;
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
402 }
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
403 if (buf->tm_hour < 0 || buf->tm_hour > 23) {
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
404 PyErr_SetString(PyExc_ValueError, "hour out of range");
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
405 return 0;
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
406 }
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
407 if (buf->tm_min < 0 || buf->tm_min > 59) {
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
408 PyErr_SetString(PyExc_ValueError, "minute out of range");
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
409 return 0;
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
410 }
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
411 if (buf->tm_sec < 0 || buf->tm_sec > 61) {
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
412 PyErr_SetString(PyExc_ValueError, "seconds out of range");
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
413 return 0;
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
414 }
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
415 /* tm_wday does not need checking of its upper-bound since taking
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
416 ``% 7`` in gettmarg() automatically restricts the range. */
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
417 if (buf->tm_wday < 0) {
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
418 PyErr_SetString(PyExc_ValueError, "day of week out of range");
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
419 return 0;
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
420 }
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
421 if (buf->tm_yday == -1)
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
422 buf->tm_yday = 0;
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
423 else if (buf->tm_yday < 0 || buf->tm_yday > 365) {
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
424 PyErr_SetString(PyExc_ValueError, "day of year out of range");
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
425 return 0;
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
426 }
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
427 return 1;
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
428 }
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
429
72919
e3d9c5e690fc Issue #10653: On Windows, use strftime() instead of wcsftime() because
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69167
diff changeset
430 #ifdef MS_WINDOWS
e3d9c5e690fc Issue #10653: On Windows, use strftime() instead of wcsftime() because
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69167
diff changeset
431 /* wcsftime() doesn't format correctly time zones, see issue #10653 */
e3d9c5e690fc Issue #10653: On Windows, use strftime() instead of wcsftime() because
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69167
diff changeset
432 # undef HAVE_WCSFTIME
e3d9c5e690fc Issue #10653: On Windows, use strftime() instead of wcsftime() because
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69167
diff changeset
433 #endif
e3d9c5e690fc Issue #10653: On Windows, use strftime() instead of wcsftime() because
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69167
diff changeset
434
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
435 #ifdef HAVE_STRFTIME
54442
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
436 #ifdef HAVE_WCSFTIME
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
437 #define time_char wchar_t
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
438 #define format_time wcsftime
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
439 #define time_strlen wcslen
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
440 #else
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
441 #define time_char char
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
442 #define format_time strftime
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
443 #define time_strlen strlen
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
444 #endif
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
445
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
446 static PyObject *
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
447 time_strftime(PyObject *self, PyObject *args)
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
448 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
449 PyObject *tup = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
450 struct tm buf;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
451 const time_char *fmt;
65010
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
452 #ifdef HAVE_WCSFTIME
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
453 wchar_t *format;
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
454 #else
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
455 PyObject *format;
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
456 #endif
65156
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
457 PyObject *format_arg;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
458 size_t fmtlen, buflen;
65010
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
459 time_char *outbuf = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
460 size_t i;
65010
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
461 PyObject *ret = NULL;
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
462
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
463 memset((void *) &buf, '\0', sizeof(buf));
8161
125bfd896967 Instead of calling mktime(), which has all sorts of unwanted side
Guido van Rossum <guido@python.org>
parents: 7574
diff changeset
464
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
465 /* Will always expect a unicode string to be passed as format.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
466 Given that there's no str type anymore in py3k this seems safe.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
467 */
65156
c1e9542b6c74 time: fix gcc warning
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65036
diff changeset
468 if (!PyArg_ParseTuple(args, "U|O:strftime", &format_arg, &tup))
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
469 return NULL;
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
470
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
471 if (tup == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
472 time_t tt = time(NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
473 buf = *localtime(&tt);
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
474 }
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
475 else if (!gettmarg(tup, &buf) || !checktm(&buf))
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
476 return NULL;
20080
f48a41c97021 SF patch #459385 (Norman Vine): time.timezone fix for Cygwin.
Guido van Rossum <guido@python.org>
parents: 19426
diff changeset
477
67457
0e1f4df478f6 Issue #10864: limit year to [1; 9999] for strftime() on Solaris
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67455
diff changeset
478 #if defined(_MSC_VER) || defined(sun)
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
479 if (buf.tm_year + 1900 < 1 || 9999 < buf.tm_year + 1900) {
68783
be74f446b4b8 time.strftime(): replace PyErr_Format() by PyErr_SetString()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
480 PyErr_SetString(PyExc_ValueError,
be74f446b4b8 time.strftime(): replace PyErr_Format() by PyErr_SetString()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68586
diff changeset
481 "strftime() requires year in [1; 9999]");
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
482 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
483 }
67452
8757cd659291 Issue #1777412: strftime() accepts year >= 1 instead of year >= 1900
Victor Stinner <victor.stinner@haypocalc.com>
parents: 67451
diff changeset
484 #endif
67436
12074f3ab809 Issue #10827: Changed the rules for 2-digit years. The time.asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67424
diff changeset
485
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59029
diff changeset
486 /* Normalize tm_isdst just in case someone foolishly implements %Z
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59029
diff changeset
487 based on the assumption that tm_isdst falls within the range of
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59029
diff changeset
488 [-1, 1] */
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59029
diff changeset
489 if (buf.tm_isdst < -1)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59029
diff changeset
490 buf.tm_isdst = -1;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59029
diff changeset
491 else if (buf.tm_isdst > 1)
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59029
diff changeset
492 buf.tm_isdst = 1;
31608
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
493
54442
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
494 #ifdef HAVE_WCSFTIME
65157
e3ae5b00ba15 PyUnicode_AsWideCharString() takes a PyObject*, not a PyUnicodeObject*
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65156
diff changeset
495 format = PyUnicode_AsWideCharString(format_arg, NULL);
65010
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
496 if (format == NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
497 return NULL;
65010
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
498 fmt = format;
54442
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
499 #else
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
500 /* Convert the unicode string to an ascii one */
73904
8620e6901e58 Issue #5905: time.strftime() is now using the locale encoding, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72949
diff changeset
501 format = PyUnicode_EncodeFSDefault(format_arg);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
502 if (format == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
503 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
504 fmt = PyBytes_AS_STRING(format);
54442
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
505 #endif
51957
7b1741d6809e re-merge r69268 (issue4804) from trunk:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 51649
diff changeset
506
75317
b4321f93e3f2 Issue #14125: backport refleak fix (d4adbf908983).
Stefan Krah <skrah@bytereef.org>
parents: 74015
diff changeset
507 #if defined(MS_WINDOWS) && !defined(HAVE_WCSFTIME)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
508 /* check that the format string contains only valid directives */
72949
977c5753ca32 Issue #10653: Fix time.strftime() on Windows, check for invalid format strings
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72919
diff changeset
509 for(outbuf = strchr(fmt, '%');
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
510 outbuf != NULL;
72949
977c5753ca32 Issue #10653: Fix time.strftime() on Windows, check for invalid format strings
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72919
diff changeset
511 outbuf = strchr(outbuf+2, '%'))
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
512 {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
513 if (outbuf[1]=='#')
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
514 ++outbuf; /* not documented by python, */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
515 if (outbuf[1]=='\0' ||
72949
977c5753ca32 Issue #10653: Fix time.strftime() on Windows, check for invalid format strings
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72919
diff changeset
516 !strchr("aAbBcdHIjmMpSUwWxXyYzZ%", outbuf[1]))
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
517 {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
518 PyErr_SetString(PyExc_ValueError, "Invalid format string");
75317
b4321f93e3f2 Issue #14125: backport refleak fix (d4adbf908983).
Stefan Krah <skrah@bytereef.org>
parents: 74015
diff changeset
519 Py_DECREF(format);
b4321f93e3f2 Issue #14125: backport refleak fix (d4adbf908983).
Stefan Krah <skrah@bytereef.org>
parents: 74015
diff changeset
520 return NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
521 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
522 }
51957
7b1741d6809e re-merge r69268 (issue4804) from trunk:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 51649
diff changeset
523 #endif
7b1741d6809e re-merge r69268 (issue4804) from trunk:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 51649
diff changeset
524
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
525 fmtlen = time_strlen(fmt);
10275
632bd3a5ddef Patch by Tadayoshi Funaba (with some changes) to be smarter about
Guido van Rossum <guido@python.org>
parents: 10269
diff changeset
526
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
527 /* I hate these functions that presume you know how big the output
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
528 * will be ahead of time...
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
529 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
530 for (i = 1024; ; i += i) {
74015
d7862e855274 Issue #10951: Fix a compiler warning in timemodule.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73996
diff changeset
531 #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
73996
6a966179c73a Issue #10350: Read and save errno before calling a function which might overwrite it.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73904
diff changeset
532 int err;
74015
d7862e855274 Issue #10951: Fix a compiler warning in timemodule.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73996
diff changeset
533 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
534 outbuf = (time_char *)PyMem_Malloc(i*sizeof(time_char));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
535 if (outbuf == NULL) {
65010
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
536 PyErr_NoMemory();
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
537 break;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
538 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
539 buflen = format_time(outbuf, i, fmt, &buf);
74015
d7862e855274 Issue #10951: Fix a compiler warning in timemodule.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73996
diff changeset
540 #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
73996
6a966179c73a Issue #10350: Read and save errno before calling a function which might overwrite it.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73904
diff changeset
541 err = errno;
74015
d7862e855274 Issue #10951: Fix a compiler warning in timemodule.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 73996
diff changeset
542 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
543 if (buflen > 0 || i >= 256 * fmtlen) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
544 /* If the buffer is 256 times as long as the format,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
545 it's probably not failing for lack of room!
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
546 More likely, the format yields an empty result,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
547 e.g. an empty format, or %Z when the timezone
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
548 is unknown. */
54442
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
549 #ifdef HAVE_WCSFTIME
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
550 ret = PyUnicode_FromWideChar(outbuf, buflen);
54442
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
551 #else
73904
8620e6901e58 Issue #5905: time.strftime() is now using the locale encoding, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72949
diff changeset
552 ret = PyUnicode_DecodeFSDefaultAndSize(outbuf, buflen);
54442
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
553 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
554 PyMem_Free(outbuf);
65010
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
555 break;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
556 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
557 PyMem_Free(outbuf);
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
558 #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
559 /* VisualStudio .NET 2005 does this properly */
73996
6a966179c73a Issue #10350: Read and save errno before calling a function which might overwrite it.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73904
diff changeset
560 if (buflen == 0 && err == EINVAL) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
561 PyErr_SetString(PyExc_ValueError, "Invalid format string");
65010
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
562 break;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
563 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
564 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
565 }
65010
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
566 #ifdef HAVE_WCSFTIME
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
567 PyMem_Free(format);
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
568 #else
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
569 Py_DECREF(format);
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
570 #endif
191ed2ad5997 Issue #9979: Use PyUnicode_AsWideCharString() in time.strftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
571 return ret;
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
572 }
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
573
54442
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
574 #undef time_char
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
575 #undef format_time
69cd80f8e133 Issue #5562: Use wcsftime for time.strftime where available.
Martin v. Löwis <martin@v.loewis.de>
parents: 51957
diff changeset
576
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
577 PyDoc_STRVAR(strftime_doc,
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
578 "strftime(format[, tuple]) -> string\n\
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
579 \n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
580 Convert a time tuple to a string according to a format specification.\n\
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
581 See the library reference manual for formatting codes. When the time tuple\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
582 is not present, current time as returned by localtime() is used.");
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
583 #endif /* HAVE_STRFTIME */
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
584
24323
ab1877a7b262 Patch to call the Pure python strptime implementation if there's no
Guido van Rossum <guido@python.org>
parents: 24243
diff changeset
585 static PyObject *
ab1877a7b262 Patch to call the Pure python strptime implementation if there's no
Guido van Rossum <guido@python.org>
parents: 24243
diff changeset
586 time_strptime(PyObject *self, PyObject *args)
ab1877a7b262 Patch to call the Pure python strptime implementation if there's no
Guido van Rossum <guido@python.org>
parents: 24243
diff changeset
587 {
44441
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44382
diff changeset
588 PyObject *strptime_module = PyImport_ImportModuleNoBlock("_strptime");
28569
75113759252c SF patch #718867: Fix reference leak for time.strptime
Raymond Hettinger <python@rcn.com>
parents: 28299
diff changeset
589 PyObject *strptime_result;
24323
ab1877a7b262 Patch to call the Pure python strptime implementation if there's no
Guido van Rossum <guido@python.org>
parents: 24243
diff changeset
590
27294
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
591 if (!strptime_module)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59029
diff changeset
592 return NULL;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59029
diff changeset
593 strptime_result = PyObject_CallMethod(strptime_module,
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59029
diff changeset
594 "_strptime_time", "O", args);
28569
75113759252c SF patch #718867: Fix reference leak for time.strptime
Raymond Hettinger <python@rcn.com>
parents: 28299
diff changeset
595 Py_DECREF(strptime_module);
75113759252c SF patch #718867: Fix reference leak for time.strptime
Raymond Hettinger <python@rcn.com>
parents: 28299
diff changeset
596 return strptime_result;
24323
ab1877a7b262 Patch to call the Pure python strptime implementation if there's no
Guido van Rossum <guido@python.org>
parents: 24243
diff changeset
597 }
ab1877a7b262 Patch to call the Pure python strptime implementation if there's no
Guido van Rossum <guido@python.org>
parents: 24243
diff changeset
598
67350
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
599
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
600 PyDoc_STRVAR(strptime_doc,
29710
4d26547ebfc3 Make temporary change of using _strptime for time.strptime permanent.
Brett Cannon <bcannon@gmail.com>
parents: 29066
diff changeset
601 "strptime(string, format) -> struct_time\n\
21133
ec88fc430127 Patch #481718: Time module doc string changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 20654
diff changeset
602 \n\
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
603 Parse a string to a time tuple according to a format specification.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
604 See the library reference manual for formatting codes (same as strftime()).");
24323
ab1877a7b262 Patch to call the Pure python strptime implementation if there's no
Guido van Rossum <guido@python.org>
parents: 24243
diff changeset
605
67350
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
606 static PyObject *
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
607 _asctime(struct tm *timeptr)
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
608 {
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
609 /* Inspired by Open Group reference implementation available at
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
610 * http://pubs.opengroup.org/onlinepubs/009695399/functions/asctime.html */
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
611 static char wday_name[7][3] = {
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
612 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
613 };
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
614 static char mon_name[12][3] = {
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
615 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
616 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
617 };
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
618 char buf[20]; /* 'Sun Sep 16 01:03:52\0' */
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
619 int n;
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
620
67409
d2a64612d235 Use PyOS_snprintf for better portability.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67390
diff changeset
621 n = PyOS_snprintf(buf, sizeof(buf), "%.3s %.3s%3d %.2d:%.2d:%.2d",
d2a64612d235 Use PyOS_snprintf for better portability.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67390
diff changeset
622 wday_name[timeptr->tm_wday],
d2a64612d235 Use PyOS_snprintf for better portability.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67390
diff changeset
623 mon_name[timeptr->tm_mon],
d2a64612d235 Use PyOS_snprintf for better portability.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67390
diff changeset
624 timeptr->tm_mday, timeptr->tm_hour,
d2a64612d235 Use PyOS_snprintf for better portability.
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67390
diff changeset
625 timeptr->tm_min, timeptr->tm_sec);
67350
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
626 /* XXX: since the fields used by snprintf above are validated in checktm,
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
627 * the following condition should never trigger. We keep the check because
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
628 * historically fixed size buffer used in asctime was the source of
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
629 * crashes. */
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
630 if (n + 1 != sizeof(buf)) {
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
631 PyErr_SetString(PyExc_ValueError, "unconvertible time");
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
632 return NULL;
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
633 }
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
634
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
635 return PyUnicode_FromFormat("%s %d", buf, 1900 + timeptr->tm_year);
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
636 }
8470
933882c0e9fb Added strptime, if it exists. Modified after a patch by Jesse Hallio.
Guido van Rossum <guido@python.org>
parents: 8161
diff changeset
637
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
638 static PyObject *
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
639 time_asctime(PyObject *self, PyObject *args)
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
640 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
641 PyObject *tup = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
642 struct tm buf;
67350
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
643
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
644 if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
645 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
646 if (tup == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
647 time_t tt = time(NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
648 buf = *localtime(&tt);
65036
597804b41125 Issue #6608: time.asctime is now checking struct tm fields its input
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 65010
diff changeset
649 } else if (!gettmarg(tup, &buf) || !checktm(&buf))
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
650 return NULL;
67350
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
651 return _asctime(&buf);
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
652 }
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
653
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
654 PyDoc_STRVAR(asctime_doc,
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
655 "asctime([tuple]) -> string\n\
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
656 \n\
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
657 Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.\n\
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
658 When the time tuple is not present, current time as returned by localtime()\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
659 is used.");
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
660
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
661 static PyObject *
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
662 time_ctime(PyObject *self, PyObject *args)
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
663 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
664 PyObject *ot = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
665 time_t tt;
67350
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
666 struct tm *timeptr;
20080
f48a41c97021 SF patch #459385 (Norman Vine): time.timezone fix for Cygwin.
Guido van Rossum <guido@python.org>
parents: 19426
diff changeset
667
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
668 if (!PyArg_UnpackTuple(args, "ctime", 0, 1, &ot))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
669 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
670 if (ot == NULL || ot == Py_None)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
671 tt = time(NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
672 else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
673 double dt = PyFloat_AsDouble(ot);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
674 if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
675 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
676 tt = _PyTime_DoubleToTimet(dt);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
677 if (tt == (time_t)-1 && PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
678 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
679 }
67350
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
680 timeptr = localtime(&tt);
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
681 if (timeptr == NULL) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
682 PyErr_SetString(PyExc_ValueError, "unconvertible time");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
683 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
684 }
67350
1e62a0cee092 Issue #8013: time.asctime and time.ctime no longer call system asctime
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67275
diff changeset
685 return _asctime(timeptr);
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
686 }
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
687
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
688 PyDoc_STRVAR(ctime_doc,
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
689 "ctime(seconds) -> string\n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
690 \n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
691 Convert a time in seconds since the Epoch to a string in local time.\n\
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
692 This is equivalent to asctime(localtime(seconds)). When the time tuple is\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
693 not present, current time as returned by localtime() is used.");
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
694
7574
7e7d6b1b8a4b Check for HAVE_MKTIME (if you don't have it, some tests will fail, but
Guido van Rossum <guido@python.org>
parents: 7532
diff changeset
695 #ifdef HAVE_MKTIME
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
696 static PyObject *
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
697 time_mktime(PyObject *self, PyObject *tup)
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
698 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
699 struct tm buf;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
700 time_t tt;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
701 if (!gettmarg(tup, &buf))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
702 return NULL;
67946
2ee2c76082c8 Merged revisions 87919 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67917
diff changeset
703 buf.tm_wday = -1; /* sentinel; original value ignored */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
704 tt = mktime(&buf);
67946
2ee2c76082c8 Merged revisions 87919 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67917
diff changeset
705 /* Return value of -1 does not necessarily mean an error, but tm_wday
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 67946
diff changeset
706 * cannot remain set to -1 if mktime succeeded. */
67946
2ee2c76082c8 Merged revisions 87919 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 67917
diff changeset
707 if (tt == (time_t)(-1) && buf.tm_wday == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
708 PyErr_SetString(PyExc_OverflowError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
709 "mktime argument out of range");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
710 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
711 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
712 return PyFloat_FromDouble((double)tt);
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
713 }
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
714
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
715 PyDoc_STRVAR(mktime_doc,
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
716 "mktime(tuple) -> floating point number\n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
717 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
718 Convert a time tuple in local time to seconds since the Epoch.");
7574
7e7d6b1b8a4b Check for HAVE_MKTIME (if you don't have it, some tests will fail, but
Guido van Rossum <guido@python.org>
parents: 7532
diff changeset
719 #endif /* HAVE_MKTIME */
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
720
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
721 #ifdef HAVE_WORKING_TZSET
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46334
diff changeset
722 static void PyInit_timezone(PyObject *module);
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
723
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
724 static PyObject *
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
725 time_tzset(PyObject *self, PyObject *unused)
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
726 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
727 PyObject* m;
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
728
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
729 m = PyImport_ImportModuleNoBlock("time");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
730 if (m == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
731 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
732 }
14346
5397fdf88ec2 Do the absolute minimal amount of modifications to eradicate
Barry Warsaw <barry@python.org>
parents: 14001
diff changeset
733
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
734 tzset();
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
735
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
736 /* Reset timezone, altzone, daylight and tzname */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
737 PyInit_timezone(m);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
738 Py_DECREF(m);
32388
5cf507b64f31 Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents: 32384
diff changeset
739
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
740 Py_INCREF(Py_None);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
741 return Py_None;
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
742 }
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
743
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
744 PyDoc_STRVAR(tzset_doc,
67917
8084c668b637 Merged revisions 87136,87221,87256,87337-87338,87571,87839,88164 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 67279
diff changeset
745 "tzset()\n\
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
746 \n\
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
747 Initialize, or reinitialize, the local timezone to the value stored in\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
748 os.environ['TZ']. The TZ environment variable should be specified in\n\
32772
2ec451230b8e Fix SF #994580, typo in time.tzsets docstring. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents: 32388
diff changeset
749 standard Unix timezone format as documented in the tzset man page\n\
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
750 (eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
751 fall back to UTC. If the TZ environment variable is not set, the local\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
752 timezone is set to the systems best guess of wallclock time.\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
753 Changing the TZ environment variable without calling tzset *may* change\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
754 the local timezone used by methods such as localtime, but this behaviour\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
755 should not be relied on.");
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
756 #endif /* HAVE_WORKING_TZSET */
20080
f48a41c97021 SF patch #459385 (Norman Vine): time.timezone fix for Cygwin.
Guido van Rossum <guido@python.org>
parents: 19426
diff changeset
757
46334
3ab5474bde45 Merged revisions 62177-62178,62180-62193 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45773
diff changeset
758 static void
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46334
diff changeset
759 PyInit_timezone(PyObject *m) {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46334
diff changeset
760 /* This code moved from PyInit_time wholesale to allow calling it from
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
761 time_tzset. In the future, some parts of it can be moved back
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
762 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
763 are), and the extraneous calls to tzset(3) should be removed.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
764 I haven't done this yet, as I don't want to change this code as
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
765 little as possible when introducing the time.tzset and time.tzsetwall
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
766 methods. This should simply be a method of doing the following once,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
767 at the top of this function and removing the call to tzset() from
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
768 time_tzset():
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
769
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
770 #ifdef HAVE_TZSET
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
771 tzset()
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
772 #endif
22728
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
773
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
774 And I'm lazy and hate C so nyer.
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
775 */
20080
f48a41c97021 SF patch #459385 (Norman Vine): time.timezone fix for Cygwin.
Guido van Rossum <guido@python.org>
parents: 19426
diff changeset
776 #if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
777 PyObject *otz0, *otz1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
778 tzset();
9160
f0a92c0a115e Fixes for OS/2 by Jeff Rush.
Guido van Rossum <guido@python.org>
parents: 9042
diff changeset
779 #ifdef PYOS_OS2
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
780 PyModule_AddIntConstant(m, "timezone", _timezone);
9160
f0a92c0a115e Fixes for OS/2 by Jeff Rush.
Guido van Rossum <guido@python.org>
parents: 9042
diff changeset
781 #else /* !PYOS_OS2 */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
782 PyModule_AddIntConstant(m, "timezone", timezone);
9160
f0a92c0a115e Fixes for OS/2 by Jeff Rush.
Guido van Rossum <guido@python.org>
parents: 9042
diff changeset
783 #endif /* PYOS_OS2 */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
784 #ifdef HAVE_ALTZONE
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
785 PyModule_AddIntConstant(m, "altzone", altzone);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
786 #else
9160
f0a92c0a115e Fixes for OS/2 by Jeff Rush.
Guido van Rossum <guido@python.org>
parents: 9042
diff changeset
787 #ifdef PYOS_OS2
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
788 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
9160
f0a92c0a115e Fixes for OS/2 by Jeff Rush.
Guido van Rossum <guido@python.org>
parents: 9042
diff changeset
789 #else /* !PYOS_OS2 */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
790 PyModule_AddIntConstant(m, "altzone", timezone-3600);
9160
f0a92c0a115e Fixes for OS/2 by Jeff Rush.
Guido van Rossum <guido@python.org>
parents: 9042
diff changeset
791 #endif /* PYOS_OS2 */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
792 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
793 PyModule_AddIntConstant(m, "daylight", daylight);
73904
8620e6901e58 Issue #5905: time.strftime() is now using the locale encoding, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72949
diff changeset
794 otz0 = PyUnicode_DecodeFSDefaultAndSize(tzname[0], strlen(tzname[0]));
8620e6901e58 Issue #5905: time.strftime() is now using the locale encoding, instead of
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72949
diff changeset
795 otz1 = PyUnicode_DecodeFSDefaultAndSize(tzname[1], strlen(tzname[1]));
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
796 PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
20080
f48a41c97021 SF patch #459385 (Norman Vine): time.timezone fix for Cygwin.
Guido van Rossum <guido@python.org>
parents: 19426
diff changeset
797 #else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
25810
915112671152 Patch #623780: Replace obsolete struct macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 24471
diff changeset
798 #ifdef HAVE_STRUCT_TM_TM_ZONE
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
799 {
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
800 #define YEAR ((time_t)((365 * 24 + 6) * 3600))
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
801 time_t t;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
802 struct tm *p;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
803 long janzone, julyzone;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
804 char janname[10], julyname[10];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
805 t = (time((time_t *)0) / YEAR) * YEAR;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
806 p = localtime(&t);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
807 janzone = -p->tm_gmtoff;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
808 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
809 janname[9] = '\0';
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
810 t += YEAR/2;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
811 p = localtime(&t);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
812 julyzone = -p->tm_gmtoff;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
813 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
814 julyname[9] = '\0';
20080
f48a41c97021 SF patch #459385 (Norman Vine): time.timezone fix for Cygwin.
Guido van Rossum <guido@python.org>
parents: 19426
diff changeset
815
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
816 if( janzone < julyzone ) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
817 /* DST is reversed in the southern hemisphere */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
818 PyModule_AddIntConstant(m, "timezone", julyzone);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
819 PyModule_AddIntConstant(m, "altzone", janzone);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
820 PyModule_AddIntConstant(m, "daylight",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
821 janzone != julyzone);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
822 PyModule_AddObject(m, "tzname",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
823 Py_BuildValue("(zz)",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
824 julyname, janname));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
825 } else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
826 PyModule_AddIntConstant(m, "timezone", janzone);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
827 PyModule_AddIntConstant(m, "altzone", julyzone);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
828 PyModule_AddIntConstant(m, "daylight",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
829 janzone != julyzone);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
830 PyModule_AddObject(m, "tzname",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
831 Py_BuildValue("(zz)",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
832 janname, julyname));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
833 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
834 }
6459
b65eb44422ba timezone support for macintosh (Jack)
Guido van Rossum <guido@python.org>
parents: 5238
diff changeset
835 #else
25810
915112671152 Patch #623780: Replace obsolete struct macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 24471
diff changeset
836 #endif /* HAVE_STRUCT_TM_TM_ZONE */
17079
bc0555be2212 SF patch 407758, "timemodule patches for Cygwin", from Norman Vine.
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
837 #ifdef __CYGWIN__
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
838 tzset();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
839 PyModule_AddIntConstant(m, "timezone", _timezone);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
840 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
841 PyModule_AddIntConstant(m, "daylight", _daylight);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
842 PyModule_AddObject(m, "tzname",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
843 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
17079
bc0555be2212 SF patch 407758, "timemodule patches for Cygwin", from Norman Vine.
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
844 #endif /* __CYGWIN__ */
20080
f48a41c97021 SF patch #459385 (Norman Vine): time.timezone fix for Cygwin.
Guido van Rossum <guido@python.org>
parents: 19426
diff changeset
845 #endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
846 }
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
847
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
848
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
849 static PyMethodDef time_methods[] = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
850 {"time", time_time, METH_NOARGS, time_doc},
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
851 #ifdef HAVE_CLOCK
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
852 {"clock", time_clock, METH_NOARGS, clock_doc},
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
853 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
854 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
855 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
856 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
857 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
858 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
859 #ifdef HAVE_MKTIME
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
860 {"mktime", time_mktime, METH_O, mktime_doc},
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
861 #endif
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
862 #ifdef HAVE_STRFTIME
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
863 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
864 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
865 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
866 #ifdef HAVE_WORKING_TZSET
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
867 {"tzset", time_tzset, METH_NOARGS, tzset_doc},
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
868 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
869 {NULL, NULL} /* sentinel */
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
870 };
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
871
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
872
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
873 PyDoc_STRVAR(module_doc,
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
874 "This module provides various functions to manipulate time values.\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
875 \n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
876 There are two standard representations of time. One is the number\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
877 of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
878 or a floating point number (to represent fractions of seconds).\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
879 The Epoch is system-defined; on Unix, it is generally January 1st, 1970.\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
880 The actual value can be retrieved by calling gmtime(0).\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
881 \n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
882 The other representation is a tuple of 9 integers giving local time.\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
883 The tuple items are:\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
884 year (four digits, e.g. 1998)\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
885 month (1-12)\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
886 day (1-31)\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
887 hours (0-23)\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
888 minutes (0-59)\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
889 seconds (0-59)\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
890 weekday (0-6, Monday is 0)\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
891 Julian day (day in the year, 1-366)\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
892 DST (Daylight Savings Time) flag (-1, 0 or 1)\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
893 If the DST flag is 0, the time is given in the regular time zone;\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
894 if it is 1, the time is given in the DST time zone;\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
895 if it is -1, mktime() should guess based on the date and time.\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
896 \n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
897 Variables:\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
898 \n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
899 timezone -- difference in seconds between UTC and local standard time\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
900 altzone -- difference in seconds between UTC and local DST time\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
901 daylight -- whether local time should reflect DST\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
902 tzname -- tuple of (standard time zone name, DST time zone name)\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
903 \n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
904 Functions:\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
905 \n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
906 time() -- return current time in seconds since the Epoch as a float\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
907 clock() -- return CPU time since process start as a float\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
908 sleep() -- delay for a number of seconds given as a float\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
909 gmtime() -- convert seconds since Epoch to UTC tuple\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
910 localtime() -- convert seconds since Epoch to local time tuple\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
911 asctime() -- convert time tuple to string\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
912 ctime() -- convert time in seconds to string\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
913 mktime() -- convert local time tuple to seconds since Epoch\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
914 strftime() -- convert time tuple to string according to format specification\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
915 strptime() -- parse string to time tuple according to format specification\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
916 tzset() -- change the local timezone");
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
917
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
918
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46334
diff changeset
919
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46334
diff changeset
920 static struct PyModuleDef timemodule = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
921 PyModuleDef_HEAD_INIT,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
922 "time",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
923 module_doc,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
924 -1,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
925 time_methods,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
926 NULL,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
927 NULL,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
928 NULL,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
929 NULL
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46334
diff changeset
930 };
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46334
diff changeset
931
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
932 PyMODINIT_FUNC
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 46334
diff changeset
933 PyInit_time(void)
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
934 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
935 PyObject *m;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
936 char *p;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
937 m = PyModule_Create(&timemodule);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
938 if (m == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
939 return NULL;
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
940
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
941 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
942 p = Py_GETENV("PYTHONY2K");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
943 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
944 /* Squirrel away the module's dictionary for the y2k check */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
945 moddict = PyModule_GetDict(m);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
946 Py_INCREF(moddict);
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
947
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
948 /* Set, or reset, module variables like time.timezone */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
949 PyInit_timezone(m);
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
950
24243
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
951 #ifdef MS_WINDOWS
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
952 /* Helper to allow interrupts for Windows.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
953 If Ctrl+C event delivered while not sleeping
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
954 it will be ignored.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
955 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
956 main_thread = PyThread_get_thread_ident();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
957 hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
958 SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
24243
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
959 #endif /* MS_WINDOWS */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
960 if (!initialized) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
961 PyStructSequence_InitType(&StructTimeType,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
962 &struct_time_type_desc);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
963 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
964 Py_INCREF(&StructTimeType);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
965 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
966 initialized = 1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
967 return m;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
968 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
969
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
970 static double
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13501
diff changeset
971 floattime(void)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
972 {
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 62364
diff changeset
973 _PyTime_timeval t;
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 62364
diff changeset
974 _PyTime_gettimeofday(&t);
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 62364
diff changeset
975 return (double)t.tv_sec + t.tv_usec*0.000001;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
976 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
977
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
978
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
979 /* Implement floatsleep() for various platforms.
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
980 When interrupted (or when another error occurs), return -1 and
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
981 set an exception; else return 0. */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
982
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
983 static int
2506
9d6e97d66670 changes for MPW
Guido van Rossum <guido@python.org>
parents: 2215
diff changeset
984 floatsleep(double secs)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
985 {
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23832
diff changeset
986 /* XXX Should test for MS_WINDOWS first! */
42645
a5e984eda45a Remove support for BeOS
Skip Montanaro <skip@pobox.com>
parents: 42622
diff changeset
987 #if defined(HAVE_SELECT) && !defined(__EMX__)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
988 struct timeval t;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
989 double frac;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
990 frac = fmod(secs, 1.0);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
991 secs = floor(secs);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
992 t.tv_sec = (long)secs;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
993 t.tv_usec = (long)(frac*1000000.0);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
994 Py_BEGIN_ALLOW_THREADS
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
995 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
11447
239ca4788bec Jack Jansen: Mac has no EINTR.
Guido van Rossum <guido@python.org>
parents: 11206
diff changeset
996 #ifdef EINTR
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
997 if (errno != EINTR) {
11447
239ca4788bec Jack Jansen: Mac has no EINTR.
Guido van Rossum <guido@python.org>
parents: 11206
diff changeset
998 #else
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
999 if (1) {
11447
239ca4788bec Jack Jansen: Mac has no EINTR.
Guido van Rossum <guido@python.org>
parents: 11206
diff changeset
1000 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1001 Py_BLOCK_THREADS
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1002 PyErr_SetFromErrno(PyExc_IOError);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1003 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1004 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1005 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1006 Py_END_ALLOW_THREADS
21738
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1007 #elif defined(__WATCOMC__) && !defined(__QNX__)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1008 /* XXX Can't interrupt this sleep */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1009 Py_BEGIN_ALLOW_THREADS
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1010 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1011 Py_END_ALLOW_THREADS
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23832
diff changeset
1012 #elif defined(MS_WINDOWS)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1013 {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1014 double millisecs = secs * 1000.0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1015 unsigned long ul_millis;
27294
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
1016
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1017 if (millisecs > (double)ULONG_MAX) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1018 PyErr_SetString(PyExc_OverflowError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1019 "sleep length is too large");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1020 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1021 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1022 Py_BEGIN_ALLOW_THREADS
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1023 /* Allow sleep(0) to maintain win32 semantics, and as decreed
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1024 * by Guido, only the main thread can be interrupted.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1025 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1026 ul_millis = (unsigned long)millisecs;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1027 if (ul_millis == 0 ||
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1028 main_thread != PyThread_get_thread_ident())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1029 Sleep(ul_millis);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1030 else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1031 DWORD rc;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1032 ResetEvent(hInterruptEvent);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1033 rc = WaitForSingleObject(hInterruptEvent, ul_millis);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1034 if (rc == WAIT_OBJECT_0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1035 /* Yield to make sure real Python signal
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1036 * handler called.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1037 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1038 Sleep(1);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1039 Py_BLOCK_THREADS
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1040 errno = EINTR;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1041 PyErr_SetFromErrno(PyExc_IOError);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1042 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1043 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1044 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1045 Py_END_ALLOW_THREADS
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1046 }
21738
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1047 #elif defined(PYOS_OS2)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1048 /* This Sleep *IS* Interruptable by Exceptions */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1049 Py_BEGIN_ALLOW_THREADS
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1050 if (DosSleep(secs * 1000) != NO_ERROR) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1051 Py_BLOCK_THREADS
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1052 PyErr_SetFromErrno(PyExc_IOError);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1053 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1054 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1055 Py_END_ALLOW_THREADS
21738
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1056 #else
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1057 /* XXX Can't interrupt this sleep */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1058 Py_BEGIN_ALLOW_THREADS
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1059 sleep((int)secs);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1060 Py_END_ALLOW_THREADS
21738
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1061 #endif
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
1062
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54528
diff changeset
1063 return 0;
209
e486c849c50c New hacks for include files to get time_t in the most portable way.
Guido van Rossum <guido@python.org>
parents: 181
diff changeset
1064 }