annotate Modules/timemodule.c @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 2476373f554d
children e38335d4df91 b586ca3a3062
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 143
diff changeset
1
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Time module */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
4 #include "Python.h"
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
5 #include "structseq.h"
32388
5cf507b64f31 Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents: 32384
diff changeset
6 #include "timefuncs.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
7
37934
ea67628ddae5 Patch 1471925 - Weak linking support for OSX
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 37772
diff changeset
8 #ifdef __APPLE__
ea67628ddae5 Patch 1471925 - Weak linking support for OSX
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 37772
diff changeset
9 #if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME)
ea67628ddae5 Patch 1471925 - Weak linking support for OSX
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 37772
diff changeset
10 /*
ea67628ddae5 Patch 1471925 - Weak linking support for OSX
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 37772
diff changeset
11 * floattime falls back to ftime when getttimeofday fails because the latter
ea67628ddae5 Patch 1471925 - Weak linking support for OSX
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 37772
diff changeset
12 * might fail on some platforms. This fallback is unwanted on MacOSX because
ea67628ddae5 Patch 1471925 - Weak linking support for OSX
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 37772
diff changeset
13 * that makes it impossible to use a binary build on OSX 10.4 on earlier
ea67628ddae5 Patch 1471925 - Weak linking support for OSX
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 37772
diff changeset
14 * releases of the OS. Therefore claim we don't support ftime.
ea67628ddae5 Patch 1471925 - Weak linking support for OSX
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 37772
diff changeset
15 */
ea67628ddae5 Patch 1471925 - Weak linking support for OSX
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 37772
diff changeset
16 # undef HAVE_FTIME
ea67628ddae5 Patch 1471925 - Weak linking support for OSX
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 37772
diff changeset
17 #endif
ea67628ddae5 Patch 1471925 - Weak linking support for OSX
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 37772
diff changeset
18 #endif
ea67628ddae5 Patch 1471925 - Weak linking support for OSX
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 37772
diff changeset
19
8470
933882c0e9fb Added strptime, if it exists. Modified after a patch by Jesse Hallio.
Guido van Rossum <guido@python.org>
parents: 8161
diff changeset
20 #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
21
38716
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 38489
diff changeset
22 #ifdef HAVE_SYS_TYPES_H
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
23 #include <sys/types.h>
38716
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 38489
diff changeset
24 #endif /* HAVE_SYS_TYPES_H */
1177
533e36de1e4d * Configure.py: use #!/usr/local/bin/python
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
25
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
26 #ifdef QUICKWIN
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
27 #include <io.h>
956
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
28 #endif
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
29
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
30 #ifdef HAVE_FTIME
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
31 #include <sys/timeb.h>
6626
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6548
diff changeset
32 #if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
13667
a6c7bfdd6777 Even more ANSIfication: fix as many function pointers and declarations as
Thomas Wouters <thomas@python.org>
parents: 13651
diff changeset
33 extern int ftime(struct timeb *);
4618
06c7c7de6976 Added #ifdef around ``extern int ftime();'' for MS WINDOWS (which has
Guido van Rossum <guido@python.org>
parents: 4616
diff changeset
34 #endif /* MS_WINDOWS */
06c7c7de6976 Added #ifdef around ``extern int ftime();'' for MS WINDOWS (which has
Guido van Rossum <guido@python.org>
parents: 4616
diff changeset
35 #endif /* HAVE_FTIME */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
36
6716
d854a1c7e74d Patch for QNX, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
37 #if defined(__WATCOMC__) && !defined(__QNX__)
3592
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
38 #include <i86.h>
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
39 #else
4216
0a5f01d4701c Some changes for better Windows portability.
Guido van Rossum <guido@python.org>
parents: 3979
diff changeset
40 #ifdef MS_WINDOWS
24243
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
41 #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
42 #include <windows.h>
24243
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
43 #include "pythread.h"
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
44
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
45 /* 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
46 static HANDLE hInterruptEvent = NULL;
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
47 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
48 {
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
49 SetEvent(hInterruptEvent);
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
50 /* allow other default handlers to be called.
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
51 Default Python handler will setup the
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
52 KeyboardInterrupt exception.
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
53 */
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
54 return FALSE;
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
55 }
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
56 static long main_thread;
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
57
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
58
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23832
diff changeset
59 #if defined(__BORLANDC__)
4232
886c0f775c97 Sleep() turns out to be win32 specific.
Guido van Rossum <guido@python.org>
parents: 4216
diff changeset
60 /* 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
61 #define timezone _timezone
2533
e506ec4fb2b1 tiny additions for Windows 3.1
Guido van Rossum <guido@python.org>
parents: 2506
diff changeset
62 #define tzname _tzname
e506ec4fb2b1 tiny additions for Windows 3.1
Guido van Rossum <guido@python.org>
parents: 2506
diff changeset
63 #define daylight _daylight
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23832
diff changeset
64 #endif /* __BORLANDC__ */
4216
0a5f01d4701c Some changes for better Windows portability.
Guido van Rossum <guido@python.org>
parents: 3979
diff changeset
65 #endif /* MS_WINDOWS */
6716
d854a1c7e74d Patch for QNX, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 6626
diff changeset
66 #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
67
38326
9f884d34a0ec Some Win64 pre-release in 2000 didn't support
Tim Peters <tim.peters@gmail.com>
parents: 38202
diff changeset
68 #if defined(MS_WINDOWS) && !defined(__BORLANDC__)
38330
087287009658 Repair idiot typo, and complete the job of trying to
Tim Peters <tim.peters@gmail.com>
parents: 38326
diff changeset
69 /* Win32 has better clock replacement; we have our own version below. */
087287009658 Repair idiot typo, and complete the job of trying to
Tim Peters <tim.peters@gmail.com>
parents: 38326
diff changeset
70 #undef HAVE_CLOCK
38326
9f884d34a0ec Some Win64 pre-release in 2000 didn't support
Tim Peters <tim.peters@gmail.com>
parents: 38202
diff changeset
71 #endif /* MS_WINDOWS && !defined(__BORLANDC__) */
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
72
22114
c454b76d51e7 OS/2 EMX port changes (Modules part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21938
diff changeset
73 #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
74 #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
75 #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
76 #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
77 #endif
c454b76d51e7 OS/2 EMX port changes (Modules part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21938
diff changeset
78
6626
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6548
diff changeset
79 #if defined(PYCC_VACPP)
9160
f0a92c0a115e Fixes for OS/2 by Jeff Rush.
Guido van Rossum <guido@python.org>
parents: 9042
diff changeset
80 #include <sys/time.h>
6626
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6548
diff changeset
81 #endif
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6548
diff changeset
82
8823
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
83 #ifdef __BEOS__
14001
73d976afa8d4 Chris Herborth <chrish@pobox.com>:
Fred Drake <fdrake@acm.org>
parents: 13820
diff changeset
84 #include <time.h>
8823
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
85 /* For bigtime_t, snooze(). - [cjh] */
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
86 #include <support/SupportDefs.h>
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
87 #include <kernel/OS.h>
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
88 #endif
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
89
29066
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28569
diff changeset
90 #ifdef RISCOS
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28569
diff changeset
91 extern int riscos_sleep(double);
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28569
diff changeset
92 #endif
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28569
diff changeset
93
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
94 /* Forward declarations */
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13329
diff changeset
95 static int floatsleep(double);
13691
0f1c6053e8d0 Fix floattime prototype ((void), not (double))
Thomas Wouters <thomas@python.org>
parents: 13690
diff changeset
96 static double floattime(void);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
9042
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
98 /* For Y2K check */
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
99 static PyObject *moddict;
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
100
32388
5cf507b64f31 Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents: 32384
diff changeset
101 /* Exposed in timefuncs.h. */
5cf507b64f31 Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents: 32384
diff changeset
102 time_t
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
103 _PyTime_DoubleToTimet(double x)
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
104 {
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
105 time_t result;
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
106 double diff;
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
107
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
108 result = (time_t)x;
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
109 /* How much info did we lose? time_t may be an integral or
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
110 * floating type, and we don't know which. If it's integral,
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
111 * we don't know whether C truncates, rounds, returns the floor,
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
112 * etc. If we lost a second or more, the C rounding is
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
113 * unreasonable, or the input just doesn't fit in a time_t;
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
114 * call it an error regardless. Note that the original cast to
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
115 * time_t can cause a C error too, but nothing we can do to
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
116 * worm around that.
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
117 */
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
118 diff = x - (double)result;
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
119 if (diff <= -1.0 || diff >= 1.0) {
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
120 PyErr_SetString(PyExc_ValueError,
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
121 "timestamp out of range for platform time_t");
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
122 result = (time_t)-1;
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
123 }
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
124 return result;
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
125 }
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
126
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
127 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38330
diff changeset
128 time_time(PyObject *self, PyObject *unused)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
130 double secs;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
131 secs = floattime();
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
132 if (secs == 0.0) {
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
133 PyErr_SetFromErrno(PyExc_IOError);
1177
533e36de1e4d * Configure.py: use #!/usr/local/bin/python
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
134 return NULL;
533e36de1e4d * Configure.py: use #!/usr/local/bin/python
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
135 }
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
136 return PyFloat_FromDouble(secs);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
137 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
138
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
139 PyDoc_STRVAR(time_doc,
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
140 "time() -> floating point number\n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
141 \n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
142 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
143 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
144
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
145 #ifdef HAVE_CLOCK
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
146
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
147 #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
148 #ifdef CLK_TCK
e045f9132b07 default CLOCKS_PER_SEC to CLK_TCK if it exists
Guido van Rossum <guido@python.org>
parents: 3388
diff changeset
149 #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
150 #else
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
151 #define CLOCKS_PER_SEC 1000000
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
152 #endif
3419
e045f9132b07 default CLOCKS_PER_SEC to CLK_TCK if it exists
Guido van Rossum <guido@python.org>
parents: 3388
diff changeset
153 #endif
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
154
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
155 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38330
diff changeset
156 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
157 {
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
158 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
160 #endif /* HAVE_CLOCK */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
38330
087287009658 Repair idiot typo, and complete the job of trying to
Tim Peters <tim.peters@gmail.com>
parents: 38326
diff changeset
162 #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
163 /* Due to Mark Hammond and Tim Peters */
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
164 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38330
diff changeset
165 time_clock(PyObject *self, PyObject *unused)
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
166 {
21938
4ab29b06a76c Windows time_clock(): rewrite to get rid of horrid casting tricks.
Tim Peters <tim.peters@gmail.com>
parents: 21934
diff changeset
167 static LARGE_INTEGER ctrStart;
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
168 static double divisor = 0.0;
21938
4ab29b06a76c Windows time_clock(): rewrite to get rid of horrid casting tricks.
Tim Peters <tim.peters@gmail.com>
parents: 21934
diff changeset
169 LARGE_INTEGER now;
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
170 double diff;
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
171
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
172 if (divisor == 0.0) {
21938
4ab29b06a76c Windows time_clock(): rewrite to get rid of horrid casting tricks.
Tim Peters <tim.peters@gmail.com>
parents: 21934
diff changeset
173 LARGE_INTEGER freq;
4ab29b06a76c Windows time_clock(): rewrite to get rid of horrid casting tricks.
Tim Peters <tim.peters@gmail.com>
parents: 21934
diff changeset
174 QueryPerformanceCounter(&ctrStart);
4ab29b06a76c Windows time_clock(): rewrite to get rid of horrid casting tricks.
Tim Peters <tim.peters@gmail.com>
parents: 21934
diff changeset
175 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
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
176 /* Unlikely to happen - this works on all intel
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
177 machines at least! Revert to clock() */
41321
e3349d412681 In Windows' time.clock(), when QueryPerformanceFrequency() fails,
Georg Brandl <georg@python.org>
parents: 40503
diff changeset
178 return PyFloat_FromDouble(((double)clock()) /
e3349d412681 In Windows' time.clock(), when QueryPerformanceFrequency() fails,
Georg Brandl <georg@python.org>
parents: 40503
diff changeset
179 CLOCKS_PER_SEC);
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
180 }
21938
4ab29b06a76c Windows time_clock(): rewrite to get rid of horrid casting tricks.
Tim Peters <tim.peters@gmail.com>
parents: 21934
diff changeset
181 divisor = (double)freq.QuadPart;
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
182 }
21938
4ab29b06a76c Windows time_clock(): rewrite to get rid of horrid casting tricks.
Tim Peters <tim.peters@gmail.com>
parents: 21934
diff changeset
183 QueryPerformanceCounter(&now);
4ab29b06a76c Windows time_clock(): rewrite to get rid of horrid casting tricks.
Tim Peters <tim.peters@gmail.com>
parents: 21934
diff changeset
184 diff = (double)(now.QuadPart - ctrStart.QuadPart);
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
185 return PyFloat_FromDouble(diff / divisor);
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
186 }
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
187
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
188 #define HAVE_CLOCK /* So it gets included in the methods */
38330
087287009658 Repair idiot typo, and complete the job of trying to
Tim Peters <tim.peters@gmail.com>
parents: 38326
diff changeset
189 #endif /* MS_WINDOWS && !defined(__BORLANDC__) */
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
190
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
191 #ifdef HAVE_CLOCK
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
192 PyDoc_STRVAR(clock_doc,
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
193 "clock() -> floating point number\n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
194 \n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
195 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
196 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
197 records.");
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
198 #endif
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
199
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
200 static PyObject *
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
201 time_sleep(PyObject *self, PyObject *args)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 {
1181
950cd37fcddc * Makefile: use cp -r to install the library
Guido van Rossum <guido@python.org>
parents: 1177
diff changeset
203 double secs;
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
204 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 return NULL;
6548
dd1e83323c45 Move the Py_{{BEGIN,END}_ALLOW,BLOCK}_THREADS macros in time_sleep()
Guido van Rossum <guido@python.org>
parents: 6459
diff changeset
206 if (floatsleep(secs) != 0)
dd1e83323c45 Move the Py_{{BEGIN,END}_ALLOW,BLOCK}_THREADS macros in time_sleep()
Guido van Rossum <guido@python.org>
parents: 6459
diff changeset
207 return NULL;
dd1e83323c45 Move the Py_{{BEGIN,END}_ALLOW,BLOCK}_THREADS macros in time_sleep()
Guido van Rossum <guido@python.org>
parents: 6459
diff changeset
208 Py_INCREF(Py_None);
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
209 return Py_None;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
212 PyDoc_STRVAR(sleep_doc,
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
213 "sleep(seconds)\n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
214 \n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
215 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
216 a floating point number for subsecond precision.");
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
217
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
218 static PyStructSequence_Field struct_time_type_fields[] = {
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
219 {"tm_year", NULL},
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
220 {"tm_mon", NULL},
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
221 {"tm_mday", NULL},
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
222 {"tm_hour", NULL},
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
223 {"tm_min", NULL},
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
224 {"tm_sec", NULL},
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
225 {"tm_wday", NULL},
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
226 {"tm_yday", NULL},
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
227 {"tm_isdst", NULL},
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
228 {0}
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
229 };
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
230
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
231 static PyStructSequence_Desc struct_time_type_desc = {
21313
91dd223829c6 Patch supplied by Burton Radons for his own SF bug #487390: Modifying
Guido van Rossum <guido@python.org>
parents: 21133
diff changeset
232 "time.struct_time",
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
233 NULL,
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
234 struct_time_type_fields,
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
235 9,
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
236 };
21938
4ab29b06a76c Windows time_clock(): rewrite to get rid of horrid casting tricks.
Tim Peters <tim.peters@gmail.com>
parents: 21934
diff changeset
237
37772
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 37666
diff changeset
238 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
239 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
240
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
241 static PyObject *
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
242 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
243 {
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
244 PyObject *v = PyStructSequence_New(&StructTimeType);
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
245 if (v == NULL)
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
246 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
247
20539
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
248 #define SET(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val))
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
249
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
250 SET(0, p->tm_year + 1900);
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
251 SET(1, p->tm_mon + 1); /* Want January == 1 */
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
252 SET(2, p->tm_mday);
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
253 SET(3, p->tm_hour);
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
254 SET(4, p->tm_min);
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
255 SET(5, p->tm_sec);
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
256 SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
257 SET(7, p->tm_yday + 1); /* Want January, 1 == 1 */
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
258 SET(8, p->tm_isdst);
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
259 #undef SET
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
260 if (PyErr_Occurred()) {
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
261 Py_XDECREF(v);
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
262 return NULL;
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
263 }
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
264
2d599a36b67a SF patch #462296: Add attributes to os.stat results; by Nick Mathewson.
Guido van Rossum <guido@python.org>
parents: 20080
diff changeset
265 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
266 }
933882c0e9fb Added strptime, if it exists. Modified after a patch by Jesse Hallio.
Guido van Rossum <guido@python.org>
parents: 8161
diff changeset
267
933882c0e9fb Added strptime, if it exists. Modified after a patch by Jesse Hallio.
Guido van Rossum <guido@python.org>
parents: 8161
diff changeset
268 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
269 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
270 {
4373
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
271 struct tm *p;
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
272 time_t whent = _PyTime_DoubleToTimet(when);
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
273
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
274 if (whent == (time_t)-1 && PyErr_Occurred())
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
275 return NULL;
4373
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
276 errno = 0;
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
277 p = function(&whent);
4373
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
278 if (p == NULL) {
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
279 #ifdef EINVAL
4514
f6976b9a85a4 Should compare errno to 0, not NULL
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
280 if (errno == 0)
4373
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
281 errno = EINVAL;
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
282 #endif
27281
5a050200f736 When time.localtime() is passed a tick count the platform C localtime()
Tim Peters <tim.peters@gmail.com>
parents: 25810
diff changeset
283 return PyErr_SetFromErrno(PyExc_ValueError);
4373
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
284 }
8470
933882c0e9fb Added strptime, if it exists. Modified after a patch by Jesse Hallio.
Guido van Rossum <guido@python.org>
parents: 8161
diff changeset
285 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
286 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
287
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
288 /* 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
289 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
290 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
291 */
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
292 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
293 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
294 {
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
295 PyObject *ot = NULL;
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
296
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
297 if (!PyArg_ParseTuple(args, format, &ot))
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
298 return 0;
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
299 if (ot == NULL || ot == Py_None)
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
300 *pwhen = floattime();
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
301 else {
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
302 double when = PyFloat_AsDouble(ot);
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
303 if (PyErr_Occurred())
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
304 return 0;
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
305 *pwhen = when;
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
306 }
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
307 return 1;
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
308 }
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
309
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
310 static PyObject *
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
311 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
312 {
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
313 double when;
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
314 if (!parse_time_double_args(args, "|O:gmtime", &when))
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
315 return NULL;
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
316 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
317 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
318
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
319 PyDoc_STRVAR(gmtime_doc,
44374
863692dc411e Fix the docstrings of time.localtime() and gmtime() for the tm_mday field.
Brett Cannon <bcannon@gmail.com>
parents: 41321
diff changeset
320 "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
321 tm_sec, tm_wday, tm_yday, tm_isdst)\n\
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
322 \n\
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
323 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
324 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
325
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
326 static PyObject *
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
327 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
328 {
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
329 double when;
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
330 if (!parse_time_double_args(args, "|O:localtime", &when))
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
331 return NULL;
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
332 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
333 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
334
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
335 PyDoc_STRVAR(localtime_doc,
44374
863692dc411e Fix the docstrings of time.localtime() and gmtime() for the tm_mday field.
Brett Cannon <bcannon@gmail.com>
parents: 41321
diff changeset
336 "localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,\n\
863692dc411e Fix the docstrings of time.localtime() and gmtime() for the tm_mday field.
Brett Cannon <bcannon@gmail.com>
parents: 41321
diff changeset
337 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
338 \n\
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
339 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
340 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
341
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
342 static int
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
343 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
344 {
9042
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
345 int y;
13709
4e2aaf1e722a Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents: 13691
diff changeset
346 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
347
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
348 if (!PyArg_Parse(args, "(iiiiiiiii)",
9042
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
349 &y,
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
350 &p->tm_mon,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
351 &p->tm_mday,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
352 &p->tm_hour,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
353 &p->tm_min,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
354 &p->tm_sec,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
355 &p->tm_wday,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
356 &p->tm_yday,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
357 &p->tm_isdst))
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
358 return 0;
9042
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
359 if (y < 1900) {
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
360 PyObject *accept = PyDict_GetItemString(moddict,
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
361 "accept2dyear");
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
362 if (accept == NULL || !PyInt_Check(accept) ||
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
363 PyInt_AsLong(accept) == 0) {
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
364 PyErr_SetString(PyExc_ValueError,
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
365 "year >= 1900 required");
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
366 return 0;
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
367 }
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
368 if (69 <= y && y <= 99)
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
369 y += 1900;
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
370 else if (0 <= y && y <= 68)
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
371 y += 2000;
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
372 else {
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
373 PyErr_SetString(PyExc_ValueError,
19426
75269ab26d03 make the gettmarg error message more correct by making it more vague ;-)
Skip Montanaro <skip@pobox.com>
parents: 18808
diff changeset
374 "year out of range");
9042
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
375 return 0;
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
376 }
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
377 }
9fd915d23cfa Y2K fix affecting asctime(), mktime(), strftime().
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
378 p->tm_year = y - 1900;
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
379 p->tm_mon--;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
380 p->tm_wday = (p->tm_wday + 1) % 7;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
381 p->tm_yday--;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
382 return 1;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
383 }
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
384
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
385 #ifdef HAVE_STRFTIME
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
386 static PyObject *
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
387 time_strftime(PyObject *self, PyObject *args)
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
388 {
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
389 PyObject *tup = NULL;
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
390 struct tm buf;
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
391 const char *fmt;
13086
de4e30962fab Trent Mick: use size_t instead of int where appropriate (time_strftime()).
Guido van Rossum <guido@python.org>
parents: 12644
diff changeset
392 size_t fmtlen, buflen;
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
393 char *outbuf = 0;
13086
de4e30962fab Trent Mick: use size_t instead of int where appropriate (time_strftime()).
Guido van Rossum <guido@python.org>
parents: 12644
diff changeset
394 size_t i;
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
395
13709
4e2aaf1e722a Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents: 13691
diff changeset
396 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
397
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
398 if (!PyArg_ParseTuple(args, "s|O:strftime", &fmt, &tup))
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
399 return NULL;
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
400
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
401 if (tup == NULL) {
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
402 time_t tt = time(NULL);
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
403 buf = *localtime(&tt);
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
404 } else if (!gettmarg(tup, &buf))
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
405 return NULL;
20080
f48a41c97021 SF patch #459385 (Norman Vine): time.timezone fix for Cygwin.
Guido van Rossum <guido@python.org>
parents: 19426
diff changeset
406
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
407 /* Checks added to make sure strftime() does not crash Python by
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
408 indexing blindly into some array for a textual representation
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
409 by some bad index (fixes bug #897625).
32388
5cf507b64f31 Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents: 32384
diff changeset
410
39114
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
411 Also support values of zero from Python code for arguments in which
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
412 that is out of range by forcing that value to the lowest value that
39134
079e42e0b826 Remove an XXX marker in a comment.
Brett Cannon <bcannon@gmail.com>
parents: 39114
diff changeset
413 is valid (fixed bug #1520914).
39114
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
414
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
415 Valid ranges based on what is allowed in struct tm:
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
416
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
417 - tm_year: [0, max(int)] (1)
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
418 - tm_mon: [0, 11] (2)
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
419 - tm_mday: [1, 31]
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
420 - tm_hour: [0, 23]
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
421 - tm_min: [0, 59]
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
422 - tm_sec: [0, 60]
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
423 - tm_wday: [0, 6] (1)
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
424 - tm_yday: [0, 365] (2)
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
425 - tm_isdst: [-max(int), max(int)]
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
426
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
427 (1) gettmarg() handles bounds-checking.
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
428 (2) Python's acceptable range is one greater than the range in C,
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
429 thus need to check against automatic decrement by gettmarg().
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
430 */
39114
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
431 if (buf.tm_mon == -1)
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
432 buf.tm_mon = 0;
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
433 else if (buf.tm_mon < 0 || buf.tm_mon > 11) {
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
434 PyErr_SetString(PyExc_ValueError, "month out of range");
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
435 return NULL;
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
436 }
39114
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
437 if (buf.tm_mday == 0)
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
438 buf.tm_mday = 1;
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
439 else if (buf.tm_mday < 0 || buf.tm_mday > 31) {
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
440 PyErr_SetString(PyExc_ValueError, "day of month out of range");
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
441 return NULL;
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
442 }
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
443 if (buf.tm_hour < 0 || buf.tm_hour > 23) {
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
444 PyErr_SetString(PyExc_ValueError, "hour out of range");
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
445 return NULL;
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
446 }
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
447 if (buf.tm_min < 0 || buf.tm_min > 59) {
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
448 PyErr_SetString(PyExc_ValueError, "minute out of range");
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
449 return NULL;
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
450 }
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
451 if (buf.tm_sec < 0 || buf.tm_sec > 61) {
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
452 PyErr_SetString(PyExc_ValueError, "seconds out of range");
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
453 return NULL;
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
454 }
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
455 /* tm_wday does not need checking of its upper-bound since taking
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
456 ``% 7`` in gettmarg() automatically restricts the range. */
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
457 if (buf.tm_wday < 0) {
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
458 PyErr_SetString(PyExc_ValueError, "day of week out of range");
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
459 return NULL;
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
460 }
39114
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
461 if (buf.tm_yday == -1)
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
462 buf.tm_yday = 0;
979e390e2bb4 Fix bug #1520914. Starting in 2.4, time.strftime() began to check the bounds
Brett Cannon <bcannon@gmail.com>
parents: 38999
diff changeset
463 else if (buf.tm_yday < 0 || buf.tm_yday > 365) {
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
464 PyErr_SetString(PyExc_ValueError, "day of year out of range");
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
465 return NULL;
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
466 }
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
467 if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
468 PyErr_SetString(PyExc_ValueError,
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
469 "daylight savings flag out of range");
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
470 return NULL;
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
471 }
ae8dbf500172 Have strftime() check its time tuple argument to make sure the tuple's values
Brett Cannon <bcannon@gmail.com>
parents: 30979
diff changeset
472
10275
632bd3a5ddef Patch by Tadayoshi Funaba (with some changes) to be smarter about
Guido van Rossum <guido@python.org>
parents: 10269
diff changeset
473 fmtlen = strlen(fmt);
632bd3a5ddef Patch by Tadayoshi Funaba (with some changes) to be smarter about
Guido van Rossum <guido@python.org>
parents: 10269
diff changeset
474
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
475 /* I hate these functions that presume you know how big the output
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
476 * will be ahead of time...
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
477 */
10275
632bd3a5ddef Patch by Tadayoshi Funaba (with some changes) to be smarter about
Guido van Rossum <guido@python.org>
parents: 10269
diff changeset
478 for (i = 1024; ; i += i) {
37666
5a9fcd676b3f C++ compiler changes. casts, rename variables with reserved names.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36324
diff changeset
479 outbuf = (char *)malloc(i);
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
480 if (outbuf == NULL) {
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
481 return PyErr_NoMemory();
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
482 }
10275
632bd3a5ddef Patch by Tadayoshi Funaba (with some changes) to be smarter about
Guido van Rossum <guido@python.org>
parents: 10269
diff changeset
483 buflen = strftime(outbuf, i, fmt, &buf);
632bd3a5ddef Patch by Tadayoshi Funaba (with some changes) to be smarter about
Guido van Rossum <guido@python.org>
parents: 10269
diff changeset
484 if (buflen > 0 || i >= 256 * fmtlen) {
632bd3a5ddef Patch by Tadayoshi Funaba (with some changes) to be smarter about
Guido van Rossum <guido@python.org>
parents: 10269
diff changeset
485 /* If the buffer is 256 times as long as the format,
632bd3a5ddef Patch by Tadayoshi Funaba (with some changes) to be smarter about
Guido van Rossum <guido@python.org>
parents: 10269
diff changeset
486 it's probably not failing for lack of room!
632bd3a5ddef Patch by Tadayoshi Funaba (with some changes) to be smarter about
Guido van Rossum <guido@python.org>
parents: 10269
diff changeset
487 More likely, the format yields an empty result,
632bd3a5ddef Patch by Tadayoshi Funaba (with some changes) to be smarter about
Guido van Rossum <guido@python.org>
parents: 10269
diff changeset
488 e.g. an empty format, or %Z when the timezone
632bd3a5ddef Patch by Tadayoshi Funaba (with some changes) to be smarter about
Guido van Rossum <guido@python.org>
parents: 10269
diff changeset
489 is unknown. */
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
490 PyObject *ret;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
491 ret = PyString_FromStringAndSize(outbuf, buflen);
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
492 free(outbuf);
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
493 return ret;
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
494 }
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
495 free(outbuf);
38999
75bdb1eb0766 Fix build problems with the platform SDK on windows. It is not sufficient to test for the C compiler version when determining if we have the secure CRT from microsoft. Must test with an undocumented macro, __STDC_SECURE_LIB__ too.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 38779
diff changeset
496 #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
38779
4c74ec831a6c Fix the CRT argument error handling for VisualStudio .NET 2005. Install a CRT error handler and disable the assertion for debug builds. This causes CRT to set errno to EINVAL.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 38716
diff changeset
497 /* VisualStudio .NET 2005 does this properly */
4c74ec831a6c Fix the CRT argument error handling for VisualStudio .NET 2005. Install a CRT error handler and disable the assertion for debug builds. This causes CRT to set errno to EINVAL.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 38716
diff changeset
498 if (buflen == 0 && errno == EINVAL) {
4c74ec831a6c Fix the CRT argument error handling for VisualStudio .NET 2005. Install a CRT error handler and disable the assertion for debug builds. This causes CRT to set errno to EINVAL.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 38716
diff changeset
499 PyErr_SetString(PyExc_ValueError, "Invalid format string");
4c74ec831a6c Fix the CRT argument error handling for VisualStudio .NET 2005. Install a CRT error handler and disable the assertion for debug builds. This causes CRT to set errno to EINVAL.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 38716
diff changeset
500 return 0;
4c74ec831a6c Fix the CRT argument error handling for VisualStudio .NET 2005. Install a CRT error handler and disable the assertion for debug builds. This causes CRT to set errno to EINVAL.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 38716
diff changeset
501 }
4c74ec831a6c Fix the CRT argument error handling for VisualStudio .NET 2005. Install a CRT error handler and disable the assertion for debug builds. This causes CRT to set errno to EINVAL.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 38716
diff changeset
502 #endif
4c74ec831a6c Fix the CRT argument error handling for VisualStudio .NET 2005. Install a CRT error handler and disable the assertion for debug builds. This causes CRT to set errno to EINVAL.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 38716
diff changeset
503
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
504 }
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
505 }
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
506
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
507 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
508 "strftime(format[, tuple]) -> string\n\
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
509 \n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
510 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
511 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
512 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
513 #endif /* HAVE_STRFTIME */
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
514
24323
ab1877a7b262 Patch to call the Pure python strptime implementation if there's no
Guido van Rossum <guido@python.org>
parents: 24243
diff changeset
515 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
516 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
517 {
44439
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44374
diff changeset
518 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
519 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
520
27294
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
521 if (!strptime_module)
24323
ab1877a7b262 Patch to call the Pure python strptime implementation if there's no
Guido van Rossum <guido@python.org>
parents: 24243
diff changeset
522 return NULL;
45767
212e9c8ad087 add %f format to datetime - issue 1158
Skip Montanaro <skip@pobox.com>
parents: 44439
diff changeset
523 strptime_result = PyObject_CallMethod(strptime_module, "_strptime_time", "O", args);
28569
75113759252c SF patch #718867: Fix reference leak for time.strptime
Raymond Hettinger <python@rcn.com>
parents: 28299
diff changeset
524 Py_DECREF(strptime_module);
75113759252c SF patch #718867: Fix reference leak for time.strptime
Raymond Hettinger <python@rcn.com>
parents: 28299
diff changeset
525 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
526 }
ab1877a7b262 Patch to call the Pure python strptime implementation if there's no
Guido van Rossum <guido@python.org>
parents: 24243
diff changeset
527
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
528 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
529 "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
530 \n\
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
531 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
532 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
533
8470
933882c0e9fb Added strptime, if it exists. Modified after a patch by Jesse Hallio.
Guido van Rossum <guido@python.org>
parents: 8161
diff changeset
534
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
535 static PyObject *
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
536 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
537 {
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
538 PyObject *tup = NULL;
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
539 struct tm buf;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
540 char *p;
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38330
diff changeset
541 if (!PyArg_UnpackTuple(args, "asctime", 0, 1, &tup))
11650
99a01cb3ddb5 The functions asctime() and mktime() are documented to take a 9-tuple
Guido van Rossum <guido@python.org>
parents: 11447
diff changeset
542 return NULL;
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
543 if (tup == NULL) {
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
544 time_t tt = time(NULL);
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
545 buf = *localtime(&tt);
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
546 } else if (!gettmarg(tup, &buf))
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
547 return NULL;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
548 p = asctime(&buf);
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
549 if (p[24] == '\n')
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
550 p[24] = '\0';
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
551 return PyString_FromString(p);
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
552 }
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
553
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
554 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
555 "asctime([tuple]) -> string\n\
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
556 \n\
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
557 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
558 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
559 is used.");
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
560
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
561 static PyObject *
13501
ca32ff7a067e ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
562 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
563 {
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
564 PyObject *ot = NULL;
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
565 time_t tt;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
566 char *p;
20080
f48a41c97021 SF patch #459385 (Norman Vine): time.timezone fix for Cygwin.
Guido van Rossum <guido@python.org>
parents: 19426
diff changeset
567
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38330
diff changeset
568 if (!PyArg_UnpackTuple(args, "ctime", 0, 1, &ot))
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
569 return NULL;
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
570 if (ot == NULL || ot == Py_None)
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
571 tt = time(NULL);
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
572 else {
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
573 double dt = PyFloat_AsDouble(ot);
582eb51b8492 allow ctime(), gmtime(), and localtime() to take None as equivalent to an omitted arg
Fred Drake <fdrake@acm.org>
parents: 32772
diff changeset
574 if (PyErr_Occurred())
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
575 return NULL;
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
576 tt = _PyTime_DoubleToTimet(dt);
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
577 if (tt == (time_t)-1 && PyErr_Occurred())
ec64a6be32f7 Raise ValueError when value being stored in a time_t variable will result in
Brett Cannon <bcannon@gmail.com>
parents: 31608
diff changeset
578 return NULL;
16054
4cc9a1ebea62 Make the 'time' argument to the timemodule functions strftime, asctime,
Thomas Wouters <thomas@python.org>
parents: 15539
diff changeset
579 }
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
580 p = ctime(&tt);
7532
bf236fbf11da Raise ValueError: "unconvertible time" when ctime() returns NULL,
Guido van Rossum <guido@python.org>
parents: 6973
diff changeset
581 if (p == NULL) {
bf236fbf11da Raise ValueError: "unconvertible time" when ctime() returns NULL,
Guido van Rossum <guido@python.org>
parents: 6973
diff changeset
582 PyErr_SetString(PyExc_ValueError, "unconvertible time");
bf236fbf11da Raise ValueError: "unconvertible time" when ctime() returns NULL,
Guido van Rossum <guido@python.org>
parents: 6973
diff changeset
583 return NULL;
bf236fbf11da Raise ValueError: "unconvertible time" when ctime() returns NULL,
Guido van Rossum <guido@python.org>
parents: 6973
diff changeset
584 }
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
585 if (p[24] == '\n')
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
586 p[24] = '\0';
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
587 return PyString_FromString(p);
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
588 }
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
589
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
590 PyDoc_STRVAR(ctime_doc,
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
591 "ctime(seconds) -> string\n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
592 \n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
593 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
594 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
595 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
596
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
597 #ifdef HAVE_MKTIME
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
598 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38330
diff changeset
599 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
600 {
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
601 struct tm buf;
3592
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
602 time_t tt;
11650
99a01cb3ddb5 The functions asctime() and mktime() are documented to take a 9-tuple
Guido van Rossum <guido@python.org>
parents: 11447
diff changeset
603 if (!gettmarg(tup, &buf))
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
604 return NULL;
3592
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
605 tt = mktime(&buf);
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
606 if (tt == (time_t)(-1)) {
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
607 PyErr_SetString(PyExc_OverflowError,
20080
f48a41c97021 SF patch #459385 (Norman Vine): time.timezone fix for Cygwin.
Guido van Rossum <guido@python.org>
parents: 19426
diff changeset
608 "mktime argument out of range");
3592
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
609 return NULL;
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
610 }
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
611 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
612 }
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
613
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
614 PyDoc_STRVAR(mktime_doc,
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
615 "mktime(tuple) -> floating point number\n\
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
616 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22957
diff changeset
617 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
618 #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
619
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
620 #ifdef HAVE_WORKING_TZSET
46317
864a5671c7a4 Declare inittimezone static.
Skip Montanaro <skip@pobox.com>
parents: 45767
diff changeset
621 static void inittimezone(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
622
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
623 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38330
diff changeset
624 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
625 {
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
626 PyObject* m;
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
627
44439
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44374
diff changeset
628 m = PyImport_ImportModuleNoBlock("time");
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
629 if (m == NULL) {
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
630 return NULL;
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
631 }
14346
5397fdf88ec2 Do the absolute minimal amount of modifications to eradicate
Barry Warsaw <barry@python.org>
parents: 14001
diff changeset
632
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
633 tzset();
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
634
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
635 /* Reset timezone, altzone, daylight and tzname */
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
636 inittimezone(m);
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
637 Py_DECREF(m);
32388
5cf507b64f31 Bug 975996: Add _PyTime_DoubleToTimet to C API
Tim Peters <tim.peters@gmail.com>
parents: 32384
diff changeset
638
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
639 Py_INCREF(Py_None);
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
640 return Py_None;
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
641 }
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
642
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
643 PyDoc_STRVAR(tzset_doc,
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
644 "tzset(zone)\n\
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
645 \n\
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
646 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
647 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
648 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
649 (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
650 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
651 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
652 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
653 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
654 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
655 #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
656
46317
864a5671c7a4 Declare inittimezone static.
Skip Montanaro <skip@pobox.com>
parents: 45767
diff changeset
657 static void
864a5671c7a4 Declare inittimezone static.
Skip Montanaro <skip@pobox.com>
parents: 45767
diff changeset
658 inittimezone(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
659 /* This code moved from inittime wholesale to allow calling it from
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
660 time_tzset. In the future, some parts of it can be moved back
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
661 (for platforms that don't HAVE_WORKING_TZSET, when we know what they
46324
Skip Montanaro <skip@pobox.com>
parents: 46317
diff changeset
662 are), and the extraneous calls to tzset(3) should be removed.
40503
94ebf4a2f738 Fix typo.
Walter Dörwald <walter@livinglogic.de>
parents: 39134
diff changeset
663 I haven't done this yet, as I don't want to change this code as
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
664 little as possible when introducing the time.tzset and time.tzsetwall
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
665 methods. This should simply be a method of doing the following once,
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
666 at the top of this function and removing the call to tzset() from
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
667 time_tzset():
8568
8716816e0859 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8470
diff changeset
668
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
669 #ifdef HAVE_TZSET
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
670 tzset()
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
671 #endif
22728
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
672
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
673 And I'm lazy and hate C so nyer.
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
674 */
20080
f48a41c97021 SF patch #459385 (Norman Vine): time.timezone fix for Cygwin.
Guido van Rossum <guido@python.org>
parents: 19426
diff changeset
675 #if defined(HAVE_TZNAME) && !defined(__GLIBC__) && !defined(__CYGWIN__)
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
676 tzset();
9160
f0a92c0a115e Fixes for OS/2 by Jeff Rush.
Guido van Rossum <guido@python.org>
parents: 9042
diff changeset
677 #ifdef PYOS_OS2
22728
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
678 PyModule_AddIntConstant(m, "timezone", _timezone);
9160
f0a92c0a115e Fixes for OS/2 by Jeff Rush.
Guido van Rossum <guido@python.org>
parents: 9042
diff changeset
679 #else /* !PYOS_OS2 */
22728
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
680 PyModule_AddIntConstant(m, "timezone", timezone);
9160
f0a92c0a115e Fixes for OS/2 by Jeff Rush.
Guido van Rossum <guido@python.org>
parents: 9042
diff changeset
681 #endif /* PYOS_OS2 */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
682 #ifdef HAVE_ALTZONE
22728
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
683 PyModule_AddIntConstant(m, "altzone", altzone);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
684 #else
9160
f0a92c0a115e Fixes for OS/2 by Jeff Rush.
Guido van Rossum <guido@python.org>
parents: 9042
diff changeset
685 #ifdef PYOS_OS2
22728
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
686 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
687 #else /* !PYOS_OS2 */
22728
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
688 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
689 #endif /* PYOS_OS2 */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
690 #endif
22728
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
691 PyModule_AddIntConstant(m, "daylight", daylight);
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
692 PyModule_AddObject(m, "tzname",
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
693 Py_BuildValue("(zz)", tzname[0], tzname[1]));
20080
f48a41c97021 SF patch #459385 (Norman Vine): time.timezone fix for Cygwin.
Guido van Rossum <guido@python.org>
parents: 19426
diff changeset
694 #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
695 #ifdef HAVE_STRUCT_TM_TM_ZONE
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
696 {
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
697 #define YEAR ((time_t)((365 * 24 + 6) * 3600))
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
698 time_t t;
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
699 struct tm *p;
10493
f5d7c4945d5f Chris Lawrence writes:
Guido van Rossum <guido@python.org>
parents: 10362
diff changeset
700 long janzone, julyzone;
f5d7c4945d5f Chris Lawrence writes:
Guido van Rossum <guido@python.org>
parents: 10362
diff changeset
701 char janname[10], julyname[10];
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
702 t = (time((time_t *)0) / YEAR) * YEAR;
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
703 p = localtime(&t);
10493
f5d7c4945d5f Chris Lawrence writes:
Guido van Rossum <guido@python.org>
parents: 10362
diff changeset
704 janzone = -p->tm_gmtoff;
f5d7c4945d5f Chris Lawrence writes:
Guido van Rossum <guido@python.org>
parents: 10362
diff changeset
705 strncpy(janname, p->tm_zone ? p->tm_zone : " ", 9);
f5d7c4945d5f Chris Lawrence writes:
Guido van Rossum <guido@python.org>
parents: 10362
diff changeset
706 janname[9] = '\0';
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
707 t += YEAR/2;
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
708 p = localtime(&t);
10493
f5d7c4945d5f Chris Lawrence writes:
Guido van Rossum <guido@python.org>
parents: 10362
diff changeset
709 julyzone = -p->tm_gmtoff;
f5d7c4945d5f Chris Lawrence writes:
Guido van Rossum <guido@python.org>
parents: 10362
diff changeset
710 strncpy(julyname, p->tm_zone ? p->tm_zone : " ", 9);
f5d7c4945d5f Chris Lawrence writes:
Guido van Rossum <guido@python.org>
parents: 10362
diff changeset
711 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
712
10493
f5d7c4945d5f Chris Lawrence writes:
Guido van Rossum <guido@python.org>
parents: 10362
diff changeset
713 if( janzone < julyzone ) {
f5d7c4945d5f Chris Lawrence writes:
Guido van Rossum <guido@python.org>
parents: 10362
diff changeset
714 /* DST is reversed in the southern hemisphere */
22728
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
715 PyModule_AddIntConstant(m, "timezone", julyzone);
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
716 PyModule_AddIntConstant(m, "altzone", janzone);
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
717 PyModule_AddIntConstant(m, "daylight",
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
718 janzone != julyzone);
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
719 PyModule_AddObject(m, "tzname",
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
720 Py_BuildValue("(zz)",
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
721 julyname, janname));
10493
f5d7c4945d5f Chris Lawrence writes:
Guido van Rossum <guido@python.org>
parents: 10362
diff changeset
722 } else {
22728
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
723 PyModule_AddIntConstant(m, "timezone", janzone);
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
724 PyModule_AddIntConstant(m, "altzone", julyzone);
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
725 PyModule_AddIntConstant(m, "daylight",
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
726 janzone != julyzone);
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
727 PyModule_AddObject(m, "tzname",
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
728 Py_BuildValue("(zz)",
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
729 janname, julyname));
10493
f5d7c4945d5f Chris Lawrence writes:
Guido van Rossum <guido@python.org>
parents: 10362
diff changeset
730 }
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
731 }
6459
b65eb44422ba timezone support for macintosh (Jack)
Guido van Rossum <guido@python.org>
parents: 5238
diff changeset
732 #else
25810
915112671152 Patch #623780: Replace obsolete struct macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 24471
diff changeset
733 #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
734 #ifdef __CYGWIN__
bc0555be2212 SF patch 407758, "timemodule patches for Cygwin", from Norman Vine.
Tim Peters <tim.peters@gmail.com>
parents: 16997
diff changeset
735 tzset();
22728
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
736 PyModule_AddIntConstant(m, "timezone", _timezone);
38202
bfb76ae82c91 Patch #1490224: set time.altzone correctly on Cygwin.
Georg Brandl <georg@python.org>
parents: 37934
diff changeset
737 PyModule_AddIntConstant(m, "altzone", _timezone-3600);
22728
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
738 PyModule_AddIntConstant(m, "daylight", _daylight);
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
739 PyModule_AddObject(m, "tzname",
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
740 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
741 #endif /* __CYGWIN__ */
20080
f48a41c97021 SF patch #459385 (Norman Vine): time.timezone fix for Cygwin.
Guido van Rossum <guido@python.org>
parents: 19426
diff changeset
742 #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
743 }
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
744
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
745
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
746 static PyMethodDef time_methods[] = {
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38330
diff changeset
747 {"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
748 #ifdef HAVE_CLOCK
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38330
diff changeset
749 {"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
750 #endif
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
751 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
752 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
753 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
754 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
755 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
756 #ifdef HAVE_MKTIME
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38330
diff changeset
757 {"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
758 #endif
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
759 #ifdef HAVE_STRFTIME
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
760 {"strftime", time_strftime, METH_VARARGS, strftime_doc},
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
761 #endif
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
762 {"strptime", time_strptime, METH_VARARGS, strptime_doc},
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
763 #ifdef HAVE_WORKING_TZSET
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38330
diff changeset
764 {"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
765 #endif
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
766 {NULL, NULL} /* sentinel */
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
767 };
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
768
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
769
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
770 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
771 "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
772 \n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
773 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
774 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
775 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
776 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
777 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
778 \n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
779 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
780 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
781 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
782 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
783 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
784 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
785 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
786 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
787 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
788 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
789 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
790 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
791 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
792 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
793 \n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
794 Variables:\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
795 \n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
796 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
797 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
798 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
799 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
800 \n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
801 Functions:\n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
802 \n\
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
803 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
804 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
805 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
806 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
807 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
808 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
809 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
810 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
811 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
812 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
813 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
814
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
815
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
816 PyMODINIT_FUNC
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
817 inittime(void)
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
818 {
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
819 PyObject *m;
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
820 char *p;
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
821 m = Py_InitModule3("time", time_methods, module_doc);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32939
diff changeset
822 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 32939
diff changeset
823 return;
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
824
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
825 /* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
826 p = Py_GETENV("PYTHONY2K");
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
827 PyModule_AddIntConstant(m, "accept2dyear", (long) (!p || !*p));
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
828 /* Squirrel away the module's dictionary for the y2k check */
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
829 moddict = PyModule_GetDict(m);
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
830 Py_INCREF(moddict);
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
831
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
832 /* Set, or reset, module variables like time.timezone */
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
833 inittimezone(m);
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
834
24243
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
835 #ifdef MS_WINDOWS
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
836 /* Helper to allow interrupts for Windows.
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
837 If Ctrl+C event delivered while not sleeping
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
838 it will be ignored.
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
839 */
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
840 main_thread = PyThread_get_thread_ident();
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
841 hInterruptEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
842 SetConsoleCtrlHandler( PyCtrlHandler, TRUE);
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
843 #endif /* MS_WINDOWS */
37772
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 37666
diff changeset
844 if (!initialized) {
38326
9f884d34a0ec Some Win64 pre-release in 2000 didn't support
Tim Peters <tim.peters@gmail.com>
parents: 38202
diff changeset
845 PyStructSequence_InitType(&StructTimeType,
37772
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 37666
diff changeset
846 &struct_time_type_desc);
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 37666
diff changeset
847 }
22728
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
848 Py_INCREF(&StructTimeType);
14b7986a92e1 Remove all but one use of the module dict.
Fred Drake <fdrake@acm.org>
parents: 22256
diff changeset
849 PyModule_AddObject(m, "struct_time", (PyObject*) &StructTimeType);
37772
b90a7c6021bc Initialize structseq types only once.
Martin v. Löwis <martin@v.loewis.de>
parents: 37666
diff changeset
850 initialized = 1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
851 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
852
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
853
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
854 /* Implement floattime() for various platforms */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
855
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
856 static double
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13501
diff changeset
857 floattime(void)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
858 {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
859 /* There are three ways to get the time:
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
860 (1) gettimeofday() -- resolution in microseconds
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
861 (2) ftime() -- resolution in milliseconds
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
862 (3) time() -- resolution in seconds
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
863 In all cases the return value is a float in seconds.
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
864 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
865 fail, so we fall back on ftime() or time().
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
866 Note: clock resolution does not imply clock accuracy! */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
867 #ifdef HAVE_GETTIMEOFDAY
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
868 {
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
869 struct timeval t;
2055
cf31eab6ab5a Another bulky set of minor changes.
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
870 #ifdef GETTIMEOFDAY_NO_TZ
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
871 if (gettimeofday(&t) == 0)
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
872 return (double)t.tv_sec + t.tv_usec*0.000001;
2055
cf31eab6ab5a Another bulky set of minor changes.
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
873 #else /* !GETTIMEOFDAY_NO_TZ */
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
874 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
875 return (double)t.tv_sec + t.tv_usec*0.000001;
2055
cf31eab6ab5a Another bulky set of minor changes.
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
876 #endif /* !GETTIMEOFDAY_NO_TZ */
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
877 }
37934
ea67628ddae5 Patch 1471925 - Weak linking support for OSX
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 37772
diff changeset
878
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
879 #endif /* !HAVE_GETTIMEOFDAY */
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
880 {
10362
1409e7687a9a Patch by Chris Herborth for BeOS code.
Guido van Rossum <guido@python.org>
parents: 10275
diff changeset
881 #if defined(HAVE_FTIME)
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
882 struct timeb t;
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
883 ftime(&t);
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
884 return (double)t.time + (double)t.millitm * (double)0.001;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
885 #else /* !HAVE_FTIME */
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
886 time_t secs;
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
887 time(&secs);
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
888 return (double)secs;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
889 #endif /* !HAVE_FTIME */
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
890 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
891 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
892
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
893
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
894 /* Implement floatsleep() for various platforms.
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
895 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
896 set an exception; else return 0. */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
897
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
898 static int
2506
9d6e97d66670 changes for MPW
Guido van Rossum <guido@python.org>
parents: 2215
diff changeset
899 floatsleep(double secs)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
900 {
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23832
diff changeset
901 /* XXX Should test for MS_WINDOWS first! */
22114
c454b76d51e7 OS/2 EMX port changes (Modules part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21938
diff changeset
902 #if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
143
1870d82de069 Added BSD implementations of millisleep.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
903 struct timeval t;
1181
950cd37fcddc * Makefile: use cp -r to install the library
Guido van Rossum <guido@python.org>
parents: 1177
diff changeset
904 double frac;
950cd37fcddc * Makefile: use cp -r to install the library
Guido van Rossum <guido@python.org>
parents: 1177
diff changeset
905 frac = fmod(secs, 1.0);
950cd37fcddc * Makefile: use cp -r to install the library
Guido van Rossum <guido@python.org>
parents: 1177
diff changeset
906 secs = floor(secs);
950cd37fcddc * Makefile: use cp -r to install the library
Guido van Rossum <guido@python.org>
parents: 1177
diff changeset
907 t.tv_sec = (long)secs;
950cd37fcddc * Makefile: use cp -r to install the library
Guido van Rossum <guido@python.org>
parents: 1177
diff changeset
908 t.tv_usec = (long)(frac*1000000.0);
6548
dd1e83323c45 Move the Py_{{BEGIN,END}_ALLOW,BLOCK}_THREADS macros in time_sleep()
Guido van Rossum <guido@python.org>
parents: 6459
diff changeset
909 Py_BEGIN_ALLOW_THREADS
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
910 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
911 #ifdef EINTR
11206
98d457383a2d In floatsleep(), when using select(), ignore EINTR error.
Guido van Rossum <guido@python.org>
parents: 10734
diff changeset
912 if (errno != EINTR) {
11447
239ca4788bec Jack Jansen: Mac has no EINTR.
Guido van Rossum <guido@python.org>
parents: 11206
diff changeset
913 #else
239ca4788bec Jack Jansen: Mac has no EINTR.
Guido van Rossum <guido@python.org>
parents: 11206
diff changeset
914 if (1) {
239ca4788bec Jack Jansen: Mac has no EINTR.
Guido van Rossum <guido@python.org>
parents: 11206
diff changeset
915 #endif
12046
a384b0f69512 Fix probable bug; if errno == EINTR, floatsleep() doesn't break out of
Andrew M. Kuchling <amk@amk.ca>
parents: 12003
diff changeset
916 Py_BLOCK_THREADS
11206
98d457383a2d In floatsleep(), when using select(), ignore EINTR error.
Guido van Rossum <guido@python.org>
parents: 10734
diff changeset
917 PyErr_SetFromErrno(PyExc_IOError);
98d457383a2d In floatsleep(), when using select(), ignore EINTR error.
Guido van Rossum <guido@python.org>
parents: 10734
diff changeset
918 return -1;
98d457383a2d In floatsleep(), when using select(), ignore EINTR error.
Guido van Rossum <guido@python.org>
parents: 10734
diff changeset
919 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
920 }
6548
dd1e83323c45 Move the Py_{{BEGIN,END}_ALLOW,BLOCK}_THREADS macros in time_sleep()
Guido van Rossum <guido@python.org>
parents: 6459
diff changeset
921 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
922 #elif defined(__WATCOMC__) && !defined(__QNX__)
3592
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
923 /* XXX Can't interrupt this sleep */
6548
dd1e83323c45 Move the Py_{{BEGIN,END}_ALLOW,BLOCK}_THREADS macros in time_sleep()
Guido van Rossum <guido@python.org>
parents: 6459
diff changeset
924 Py_BEGIN_ALLOW_THREADS
3592
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
925 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
6548
dd1e83323c45 Move the Py_{{BEGIN,END}_ALLOW,BLOCK}_THREADS macros in time_sleep()
Guido van Rossum <guido@python.org>
parents: 6459
diff changeset
926 Py_END_ALLOW_THREADS
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23832
diff changeset
927 #elif defined(MS_WINDOWS)
13154
357b71c6933b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13152
diff changeset
928 {
357b71c6933b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13152
diff changeset
929 double millisecs = secs * 1000.0;
27294
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
930 unsigned long ul_millis;
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
931
13154
357b71c6933b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13152
diff changeset
932 if (millisecs > (double)ULONG_MAX) {
27294
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
933 PyErr_SetString(PyExc_OverflowError,
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
934 "sleep length is too large");
13154
357b71c6933b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13152
diff changeset
935 return -1;
357b71c6933b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13152
diff changeset
936 }
357b71c6933b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13152
diff changeset
937 Py_BEGIN_ALLOW_THREADS
27294
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
938 /* Allow sleep(0) to maintain win32 semantics, and as decreed
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
939 * by Guido, only the main thread can be interrupted.
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
940 */
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
941 ul_millis = (unsigned long)millisecs;
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
942 if (ul_millis == 0 ||
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
943 main_thread != PyThread_get_thread_ident())
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
944 Sleep(ul_millis);
24243
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
945 else {
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
946 DWORD rc;
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
947 ResetEvent(hInterruptEvent);
27294
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
948 rc = WaitForSingleObject(hInterruptEvent, ul_millis);
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
949 if (rc == WAIT_OBJECT_0) {
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
950 /* Yield to make sure real Python signal
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
951 * handler called.
7055f4c1edcc Windows flavor of floatsleep(): folded long lines, introduced a temp
Tim Peters <tim.peters@gmail.com>
parents: 27281
diff changeset
952 */
24243
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
953 Sleep(1);
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
954 Py_BLOCK_THREADS
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
955 errno = EINTR;
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
956 PyErr_SetFromErrno(PyExc_IOError);
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
957 return -1;
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
958 }
c423a88b22ce Fix bug 581232 - [Windows] Can not interrupt time.sleep()
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
959 }
13154
357b71c6933b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13152
diff changeset
960 Py_END_ALLOW_THREADS
357b71c6933b Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13152
diff changeset
961 }
21738
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
962 #elif defined(PYOS_OS2)
6626
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6548
diff changeset
963 /* This Sleep *IS* Interruptable by Exceptions */
6973
bbad2c684ad7 At Jeff Rush' request, add Py_BEGIN/END_ALLOW_THREADS around call to
Guido van Rossum <guido@python.org>
parents: 6785
diff changeset
964 Py_BEGIN_ALLOW_THREADS
6626
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6548
diff changeset
965 if (DosSleep(secs * 1000) != NO_ERROR) {
6973
bbad2c684ad7 At Jeff Rush' request, add Py_BEGIN/END_ALLOW_THREADS around call to
Guido van Rossum <guido@python.org>
parents: 6785
diff changeset
966 Py_BLOCK_THREADS
6626
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6548
diff changeset
967 PyErr_SetFromErrno(PyExc_IOError);
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6548
diff changeset
968 return -1;
5e31d381fa47 os2 patch by Jeff Rush
Guido van Rossum <guido@python.org>
parents: 6548
diff changeset
969 }
6973
bbad2c684ad7 At Jeff Rush' request, add Py_BEGIN/END_ALLOW_THREADS around call to
Guido van Rossum <guido@python.org>
parents: 6785
diff changeset
970 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
971 #elif defined(__BEOS__)
8823
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
972 /* This sleep *CAN BE* interrupted. */
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
973 {
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
974 if( secs <= 0.0 ) {
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
975 return;
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
976 }
20080
f48a41c97021 SF patch #459385 (Norman Vine): time.timezone fix for Cygwin.
Guido van Rossum <guido@python.org>
parents: 19426
diff changeset
977
8823
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
978 Py_BEGIN_ALLOW_THREADS
10362
1409e7687a9a Patch by Chris Herborth for BeOS code.
Guido van Rossum <guido@python.org>
parents: 10275
diff changeset
979 /* BeOS snooze() is in microseconds... */
1409e7687a9a Patch by Chris Herborth for BeOS code.
Guido van Rossum <guido@python.org>
parents: 10275
diff changeset
980 if( snooze( (bigtime_t)( secs * 1000.0 * 1000.0 ) ) == B_INTERRUPTED ) {
8823
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
981 Py_BLOCK_THREADS
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
982 PyErr_SetFromErrno( PyExc_IOError );
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
983 return -1;
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
984 }
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
985 Py_END_ALLOW_THREADS
f2a4a2b60860 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8568
diff changeset
986 }
21738
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
987 #elif defined(RISCOS)
17396
beb9d2b02846 Updated version of RISCOS support. SF patch 411213 by Dietmar Schwertberger
Guido van Rossum <guido@python.org>
parents: 17079
diff changeset
988 if (secs <= 0.0)
beb9d2b02846 Updated version of RISCOS support. SF patch 411213 by Dietmar Schwertberger
Guido van Rossum <guido@python.org>
parents: 17079
diff changeset
989 return 0;
beb9d2b02846 Updated version of RISCOS support. SF patch 411213 by Dietmar Schwertberger
Guido van Rossum <guido@python.org>
parents: 17079
diff changeset
990 Py_BEGIN_ALLOW_THREADS
beb9d2b02846 Updated version of RISCOS support. SF patch 411213 by Dietmar Schwertberger
Guido van Rossum <guido@python.org>
parents: 17079
diff changeset
991 /* This sleep *CAN BE* interrupted. */
29066
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28569
diff changeset
992 if ( riscos_sleep(secs) )
17396
beb9d2b02846 Updated version of RISCOS support. SF patch 411213 by Dietmar Schwertberger
Guido van Rossum <guido@python.org>
parents: 17079
diff changeset
993 return -1;
beb9d2b02846 Updated version of RISCOS support. SF patch 411213 by Dietmar Schwertberger
Guido van Rossum <guido@python.org>
parents: 17079
diff changeset
994 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
995 #elif defined(PLAN9)
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
996 {
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
997 double millisecs = secs * 1000.0;
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
998 if (millisecs > (double)LONG_MAX) {
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
999 PyErr_SetString(PyExc_OverflowError, "sleep length is too large");
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1000 return -1;
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1001 }
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1002 /* This sleep *CAN BE* interrupted. */
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1003 Py_BEGIN_ALLOW_THREADS
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1004 if(sleep((long)millisecs) < 0){
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1005 Py_BLOCK_THREADS
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1006 PyErr_SetFromErrno(PyExc_IOError);
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1007 return -1;
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1008 }
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1009 Py_END_ALLOW_THREADS
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1010 }
ca1b67f5a3ec Patch #504225: add plan9 ifdef to timemodule floatsleep.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
1011 #else
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1012 /* XXX Can't interrupt this sleep */
6548
dd1e83323c45 Move the Py_{{BEGIN,END}_ALLOW,BLOCK}_THREADS macros in time_sleep()
Guido van Rossum <guido@python.org>
parents: 6459
diff changeset
1013 Py_BEGIN_ALLOW_THREADS
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1014 sleep((int)secs);
6548
dd1e83323c45 Move the Py_{{BEGIN,END}_ALLOW,BLOCK}_THREADS macros in time_sleep()
Guido van Rossum <guido@python.org>
parents: 6459
diff changeset
1015 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
1016 #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
1017
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1018 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
1019 }
28299
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
1020
14316df1c298 - New function time.tzset() provides access to the C library tzet()
Guido van Rossum <guido@python.org>
parents: 27294
diff changeset
1021