annotate Modules/timemodule.c @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 15288c8343a5
children b65eb44422ba
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 /***********************************************************
2061
172ba04a5191 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2055
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
172ba04a5191 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2055
diff changeset
3 The Netherlands.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 143
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 143
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 143
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
8 documentation for any purpose and without fee is hereby granted,
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 143
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
10 both that copyright notice and this permission notice appear in
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 143
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
15 permission.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 143
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4373
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 143
diff changeset
29
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 143
diff changeset
30 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 143
diff changeset
31
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 /* Time module */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
34 #include "Python.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
35
3979
87e53c62cac8 Include "mymath.h" instead of manually declaring math functions.
Guido van Rossum <guido@python.org>
parents: 3592
diff changeset
36 #ifdef HAVE_SELECT
87e53c62cac8 Include "mymath.h" instead of manually declaring math functions.
Guido van Rossum <guido@python.org>
parents: 3592
diff changeset
37 #include "mymath.h"
87e53c62cac8 Include "mymath.h" instead of manually declaring math functions.
Guido van Rossum <guido@python.org>
parents: 3592
diff changeset
38 #endif
87e53c62cac8 Include "mymath.h" instead of manually declaring math functions.
Guido van Rossum <guido@python.org>
parents: 3592
diff changeset
39
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
40 #ifdef macintosh
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
41 #include <time.h>
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
42 #else
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
43 #include <sys/types.h>
1177
533e36de1e4d * Configure.py: use #!/usr/local/bin/python
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
44 #endif
533e36de1e4d * Configure.py: use #!/usr/local/bin/python
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
45
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
46 #ifdef QUICKWIN
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
47 #include <io.h>
956
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
48 #endif
a60cb5321211 * macmodule.c: include allobjects.h, not .c
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
49
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
50 #ifdef HAVE_UNISTD_H
730
c81952d97ebe Initialized sigsave to shut lint up
Guido van Rossum <guido@python.org>
parents: 701
diff changeset
51 #include <unistd.h>
c81952d97ebe Initialized sigsave to shut lint up
Guido van Rossum <guido@python.org>
parents: 701
diff changeset
52 #endif
c81952d97ebe Initialized sigsave to shut lint up
Guido van Rossum <guido@python.org>
parents: 701
diff changeset
53
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
54 #ifdef HAVE_SELECT
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
55 #include "myselect.h"
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
56 #else
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
57 #include "mytime.h"
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
58 #endif
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
59
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
60 #ifdef HAVE_FTIME
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
61 #include <sys/timeb.h>
4618
06c7c7de6976 Added #ifdef around ``extern int ftime();'' for MS WINDOWS (which has
Guido van Rossum <guido@python.org>
parents: 4616
diff changeset
62 #ifndef MS_WINDOWS
4607
33a8e65eef23 Add extern decl of ftime() to make gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4514
diff changeset
63 extern int ftime();
4618
06c7c7de6976 Added #ifdef around ``extern int ftime();'' for MS WINDOWS (which has
Guido van Rossum <guido@python.org>
parents: 4616
diff changeset
64 #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
65 #endif /* HAVE_FTIME */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
66
3592
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
67 #ifdef __WATCOMC__
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
68 #include <i86.h>
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
69 #else
4216
0a5f01d4701c Some changes for better Windows portability.
Guido van Rossum <guido@python.org>
parents: 3979
diff changeset
70 #ifdef MS_WINDOWS
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
71 #include <windows.h>
4232
886c0f775c97 Sleep() turns out to be win32 specific.
Guido van Rossum <guido@python.org>
parents: 4216
diff changeset
72 #ifdef MS_WIN16
886c0f775c97 Sleep() turns out to be win32 specific.
Guido van Rossum <guido@python.org>
parents: 4216
diff changeset
73 /* 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
74 #define timezone _timezone
2533
e506ec4fb2b1 tiny additions for Windows 3.1
Guido van Rossum <guido@python.org>
parents: 2506
diff changeset
75 #define tzname _tzname
e506ec4fb2b1 tiny additions for Windows 3.1
Guido van Rossum <guido@python.org>
parents: 2506
diff changeset
76 #define daylight _daylight
e506ec4fb2b1 tiny additions for Windows 3.1
Guido van Rossum <guido@python.org>
parents: 2506
diff changeset
77 #define altzone _altzone
4232
886c0f775c97 Sleep() turns out to be win32 specific.
Guido van Rossum <guido@python.org>
parents: 4216
diff changeset
78 #endif /* MS_WIN16 */
4216
0a5f01d4701c Some changes for better Windows portability.
Guido van Rossum <guido@python.org>
parents: 3979
diff changeset
79 #endif /* MS_WINDOWS */
0a5f01d4701c Some changes for better Windows portability.
Guido van Rossum <guido@python.org>
parents: 3979
diff changeset
80 #endif /* !__WATCOMC__ */
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
81
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
82 #ifdef MS_WIN32
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
83 /* Win32 has better clock replacement */
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
84 #include <largeint.h>
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
85 #undef HAVE_CLOCK /* We have our own version down below */
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
86 #endif /* MS_WIN32 */
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
87
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
88 /* Forward declarations */
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
89 static int floatsleep Py_PROTO((double));
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
90 static double floattime Py_PROTO(());
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
92 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 time_time(self, args)
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
94 PyObject *self;
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
95 PyObject *args;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
97 double secs;
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
98 if (!PyArg_NoArgs(args))
1177
533e36de1e4d * Configure.py: use #!/usr/local/bin/python
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
99 return NULL;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
100 secs = floattime();
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
101 if (secs == 0.0) {
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
102 PyErr_SetFromErrno(PyExc_IOError);
1177
533e36de1e4d * Configure.py: use #!/usr/local/bin/python
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
103 return NULL;
533e36de1e4d * Configure.py: use #!/usr/local/bin/python
Guido van Rossum <guido@python.org>
parents: 956
diff changeset
104 }
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
105 return PyFloat_FromDouble(secs);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
106 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
107
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
108 #ifdef HAVE_CLOCK
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
109
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
110 #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
111 #ifdef CLK_TCK
e045f9132b07 default CLOCKS_PER_SEC to CLK_TCK if it exists
Guido van Rossum <guido@python.org>
parents: 3388
diff changeset
112 #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
113 #else
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
114 #define CLOCKS_PER_SEC 1000000
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
115 #endif
3419
e045f9132b07 default CLOCKS_PER_SEC to CLK_TCK if it exists
Guido van Rossum <guido@python.org>
parents: 3388
diff changeset
116 #endif
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
117
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
118 static PyObject *
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
119 time_clock(self, args)
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
120 PyObject *self;
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
121 PyObject *args;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
122 {
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
123 if (!PyArg_NoArgs(args))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 return NULL;
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
125 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
127 #endif /* HAVE_CLOCK */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
129 #ifdef MS_WIN32
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
130 /* Due to Mark Hammond */
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
131 static PyObject *
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
132 time_clock(self, args)
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
133 PyObject *self;
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
134 PyObject *args;
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
135 {
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
136 static LARGE_INTEGER ctrStart;
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
137 static LARGE_INTEGER divisor = {0,0};
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
138 LARGE_INTEGER now, diff, rem;
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
139
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
140 if (!PyArg_NoArgs(args))
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
141 return NULL;
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
142
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
143 if (LargeIntegerEqualToZero(divisor)) {
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
144 QueryPerformanceCounter(&ctrStart);
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
145 if (!QueryPerformanceFrequency(&divisor) ||
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
146 LargeIntegerEqualToZero(divisor)) {
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
147 /* Unlikely to happen -
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
148 this works on all intel machines at least!
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
149 Revert to clock() */
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
150 return PyFloat_FromDouble(clock());
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
151 }
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
152 }
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
153 QueryPerformanceCounter(&now);
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
154 diff = LargeIntegerSubtract(now, ctrStart);
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
155 diff = LargeIntegerDivide(diff, divisor, &rem);
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
156 /* XXX - we assume both divide results fit in 32 bits. This is
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
157 true on Intels. First person who can afford a machine that
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
158 doesnt deserves to fix it :-)
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
159 */
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
160 return PyFloat_FromDouble((double)diff.LowPart +
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
161 ((double)rem.LowPart / (double)divisor.LowPart));
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
162 }
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
163 #define HAVE_CLOCK /* So it gets included in the methods */
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
164 #endif /* MS_WIN32 */
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
165
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
166 static PyObject *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 time_sleep(self, args)
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
168 PyObject *self;
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
169 PyObject *args;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 {
1181
950cd37fcddc * Makefile: use cp -r to install the library
Guido van Rossum <guido@python.org>
parents: 1177
diff changeset
171 double secs;
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
172 if (!PyArg_Parse(args, "d", &secs))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 return NULL;
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
174 Py_BEGIN_ALLOW_THREADS
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
175 if (floatsleep(secs) != 0) {
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
176 Py_BLOCK_THREADS
5238
15288c8343a5 Win32 precision clock() -- Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 5100
diff changeset
177 return NULL;
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
178 }
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
179 Py_END_ALLOW_THREADS
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
180 Py_INCREF(Py_None);
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
181 return Py_None;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
184 static PyObject *
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
185 time_convert(when, function)
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
186 time_t when;
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
187 struct tm * (*function) Py_PROTO((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
188 {
4373
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
189 struct tm *p;
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
190 errno = 0;
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
191 p = function(&when);
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
192 if (p == NULL) {
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
193 #ifdef EINVAL
4514
f6976b9a85a4 Should compare errno to 0, not NULL
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
194 if (errno == 0)
4373
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
195 errno = EINVAL;
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
196 #endif
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
197 return PyErr_SetFromErrno(PyExc_IOError);
4373
98ea80051dbc Check for NULL pointer returned from localtime()/gmtime().
Guido van Rossum <guido@python.org>
parents: 4232
diff changeset
198 }
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
199 return Py_BuildValue("(iiiiiiiii)",
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
200 p->tm_year + 1900,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
201 p->tm_mon + 1, /* Want January == 1 */
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
202 p->tm_mday,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
203 p->tm_hour,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
204 p->tm_min,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
205 p->tm_sec,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
206 (p->tm_wday + 6) % 7, /* Want Monday == 0 */
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
207 p->tm_yday + 1, /* Want January, 1 == 1 */
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
208 p->tm_isdst);
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
209 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
210
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
211 static PyObject *
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
212 time_gmtime(self, args)
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
213 PyObject *self;
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
214 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
215 {
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
216 double when;
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
217 if (!PyArg_Parse(args, "d", &when))
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
218 return NULL;
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
219 return time_convert((time_t)when, gmtime);
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
220 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
221
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
222 static PyObject *
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
223 time_localtime(self, args)
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
224 PyObject *self;
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
225 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
226 {
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
227 double when;
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
228 if (!PyArg_Parse(args, "d", &when))
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
229 return NULL;
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
230 return time_convert((time_t)when, localtime);
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
231 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
232
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
233 static int
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
234 gettmarg(args, p)
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
235 PyObject *args;
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
236 struct tm *p;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
237 {
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
238 if (!PyArg_Parse(args, "(iiiiiiiii)",
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
239 &p->tm_year,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
240 &p->tm_mon,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
241 &p->tm_mday,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
242 &p->tm_hour,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
243 &p->tm_min,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
244 &p->tm_sec,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
245 &p->tm_wday,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
246 &p->tm_yday,
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
247 &p->tm_isdst))
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
248 return 0;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
249 if (p->tm_year >= 1900)
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
250 p->tm_year -= 1900;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
251 p->tm_mon--;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
252 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
253 p->tm_yday--;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
254 return 1;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
255 }
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
256
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
257 #ifdef HAVE_STRFTIME
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
258 static PyObject *
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
259 time_strftime(self, args)
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
260 PyObject *self;
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
261 PyObject *args;
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
262 {
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
263 struct tm buf;
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
264 const char *fmt;
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
265 char *outbuf = 0;
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
266 int i;
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
267
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
268 if (!PyArg_ParseTuple(args, "s(iiiiiiiii)",
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
269 &fmt,
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
270 &(buf.tm_year),
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
271 &(buf.tm_mon),
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
272 &(buf.tm_mday),
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
273 &(buf.tm_hour),
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
274 &(buf.tm_min),
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
275 &(buf.tm_sec),
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
276 &(buf.tm_wday),
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
277 &(buf.tm_yday),
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
278 &(buf.tm_isdst)))
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
279 return NULL;
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
280 if (buf.tm_year >= 1900)
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
281 buf.tm_year -= 1900;
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
282 buf.tm_mon--;
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
283 buf.tm_wday = (buf.tm_wday + 1) % 7;
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
284 buf.tm_yday--;
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
285 /* 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
286 * will be ahead of time...
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
287 */
5100
32310251d767 Issue a more meaningful error if strftime keeps returning a NULL pointer.
Guido van Rossum <guido@python.org>
parents: 4947
diff changeset
288 for (i = 1024 ; i <= 8192 ; i += 1024) {
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
289 outbuf = malloc(i);
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
290 if (outbuf == NULL) {
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
291 return PyErr_NoMemory();
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
292 }
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
293 if (strftime(outbuf, i-1, fmt, &buf) != 0) {
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
294 PyObject *ret;
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
295 ret = PyString_FromString(outbuf);
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
296 free(outbuf);
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
297 return ret;
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
298 }
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
299 free(outbuf);
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
300 }
5100
32310251d767 Issue a more meaningful error if strftime keeps returning a NULL pointer.
Guido van Rossum <guido@python.org>
parents: 4947
diff changeset
301 PyErr_SetString(PyExc_ValueError,
32310251d767 Issue a more meaningful error if strftime keeps returning a NULL pointer.
Guido van Rossum <guido@python.org>
parents: 4947
diff changeset
302 "bad strftime format or result too big");
32310251d767 Issue a more meaningful error if strftime keeps returning a NULL pointer.
Guido van Rossum <guido@python.org>
parents: 4947
diff changeset
303 return NULL;
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
304 }
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
305 #endif /* HAVE_STRFTIME */
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
306
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
307 static PyObject *
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
308 time_asctime(self, args)
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
309 PyObject *self;
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
310 PyObject *args;
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
311 {
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
312 struct tm buf;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
313 char *p;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
314 if (!gettmarg(args, &buf))
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
315 return NULL;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
316 p = asctime(&buf);
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
317 if (p[24] == '\n')
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
318 p[24] = '\0';
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
319 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
320 }
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
321
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
322 static PyObject *
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
323 time_ctime(self, args)
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
324 PyObject *self;
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
325 PyObject *args;
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
326 {
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
327 double dt;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
328 time_t tt;
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
329 char *p;
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
330 if (!PyArg_Parse(args, "d", &dt))
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
331 return NULL;
4216
0a5f01d4701c Some changes for better Windows portability.
Guido van Rossum <guido@python.org>
parents: 3979
diff changeset
332 tt = (time_t)dt;
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
333 p = ctime(&tt);
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
334 if (p[24] == '\n')
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
335 p[24] = '\0';
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
336 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
337 }
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
338
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
339 static PyObject *
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
340 time_mktime(self, args)
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
341 PyObject *self;
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
342 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
343 {
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
344 struct tm buf;
3592
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
345 time_t tt;
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
346 tt = time(&tt);
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
347 buf = *localtime(&tt);
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
348 if (!gettmarg(args, &buf))
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
349 return NULL;
3592
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
350 tt = mktime(&buf);
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
351 if (tt == (time_t)(-1)) {
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
352 PyErr_SetString(PyExc_OverflowError,
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
353 "mktime argument out of range");
3592
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
354 return NULL;
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
355 }
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
356 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
357 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
358
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
359 static PyMethodDef time_methods[] = {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
360 {"time", time_time},
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
361 #ifdef HAVE_CLOCK
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
362 {"clock", time_clock},
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
363 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 {"sleep", time_sleep},
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
365 {"gmtime", time_gmtime},
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
366 {"localtime", time_localtime},
1318
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
367 {"asctime", time_asctime},
92e6a8cf14be * pythonmain.c: -k option, usage message, more environment flags.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
368 {"ctime", time_ctime},
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
369 {"mktime", time_mktime},
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
370 #ifdef HAVE_STRFTIME
3388
1696fbf302b6 mark strftime as varargs
Guido van Rossum <guido@python.org>
parents: 3140
diff changeset
371 {"strftime", time_strftime, 1},
3086
d7eea9013e73 added time.strftime()
Guido van Rossum <guido@python.org>
parents: 2533
diff changeset
372 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 {NULL, NULL} /* sentinel */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
375
2215
a3bd61632f14 fix leaks
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
376 static void
a3bd61632f14 fix leaks
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
377 ins(d, name, v)
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
378 PyObject *d;
2215
a3bd61632f14 fix leaks
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
379 char *name;
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
380 PyObject *v;
2215
a3bd61632f14 fix leaks
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
381 {
a3bd61632f14 fix leaks
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
382 if (v == NULL)
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
383 Py_FatalError("Can't initialize time module -- NULL value");
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
384 if (PyDict_SetItemString(d, name, v) != 0)
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
385 Py_FatalError(
4618
06c7c7de6976 Added #ifdef around ``extern int ftime();'' for MS WINDOWS (which has
Guido van Rossum <guido@python.org>
parents: 4616
diff changeset
386 "Can't initialize time module -- PyDict_SetItemString failed");
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
387 Py_DECREF(v);
2215
a3bd61632f14 fix leaks
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
388 }
a3bd61632f14 fix leaks
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
389
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 void
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 inittime()
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 {
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
393 PyObject *m, *d;
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
394 m = Py_InitModule("time", time_methods);
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
395 d = PyModule_GetDict(m);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
396 #ifdef HAVE_TZNAME
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
397 tzset();
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
398 ins(d, "timezone", PyInt_FromLong((long)timezone));
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
399 #ifdef HAVE_ALTZONE
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
400 ins(d, "altzone", PyInt_FromLong((long)altzone));
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
401 #else
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
402 ins(d, "altzone", PyInt_FromLong((long)timezone-3600));
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
403 #endif
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
404 ins(d, "daylight", PyInt_FromLong((long)daylight));
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
405 ins(d, "tzname", Py_BuildValue("(zz)", tzname[0], tzname[1]));
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
406 #else /* !HAVE_TZNAME */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
407 #if HAVE_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
408 {
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
409 #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
410 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
411 struct tm *p;
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
412 long winterzone, summerzone;
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
413 char wintername[10], summername[10];
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
414 /* XXX This won't work on the southern hemisphere.
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
415 XXX Anybody got a better idea? */
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
416 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
417 p = localtime(&t);
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
418 winterzone = -p->tm_gmtoff;
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
419 strncpy(wintername, p->tm_zone ? p->tm_zone : " ", 9);
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
420 wintername[9] = '\0';
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
421 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
422 p = localtime(&t);
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
423 summerzone = -p->tm_gmtoff;
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
424 strncpy(summername, p->tm_zone ? p->tm_zone : " ", 9);
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
425 summername[9] = '\0';
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
426 ins(d, "timezone", PyInt_FromLong(winterzone));
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
427 ins(d, "altzone", PyInt_FromLong(summerzone));
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
428 ins(d, "daylight",
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
429 PyInt_FromLong((long)(winterzone != summerzone)));
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
430 ins(d, "tzname",
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
431 Py_BuildValue("(zz)", wintername, summername));
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1191
diff changeset
432 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
433 #endif /* HAVE_TM_ZONE */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
434 #endif /* !HAVE_TZNAME */
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
435 if (PyErr_Occurred())
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
436 Py_FatalError("Can't initialize time module");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
438
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
439
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
440 /* Implement floattime() for various platforms */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
441
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
442 static double
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
443 floattime()
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
445 /* There are three ways to get the time:
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
446 (1) gettimeofday() -- resolution in microseconds
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
447 (2) ftime() -- resolution in milliseconds
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
448 (3) time() -- resolution in seconds
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
449 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
450 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
451 fail, so we fall back on ftime() or time().
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
452 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
453 #ifdef HAVE_GETTIMEOFDAY
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
454 {
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
455 struct timeval t;
2055
cf31eab6ab5a Another bulky set of minor changes.
Guido van Rossum <guido@python.org>
parents: 1827
diff changeset
456 #ifdef GETTIMEOFDAY_NO_TZ
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
457 if (gettimeofday(&t) == 0)
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
458 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
459 #else /* !GETTIMEOFDAY_NO_TZ */
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
460 if (gettimeofday(&t, (struct timezone *)NULL) == 0)
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
461 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
462 #endif /* !GETTIMEOFDAY_NO_TZ */
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
463 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
464 #endif /* !HAVE_GETTIMEOFDAY */
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
465 {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
466 #ifdef HAVE_FTIME
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
467 struct timeb t;
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
468 ftime(&t);
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
469 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
470 #else /* !HAVE_FTIME */
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
471 time_t secs;
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
472 time(&secs);
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
473 return (double)secs;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
474 #endif /* !HAVE_FTIME */
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
475 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
476 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
477
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
478
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
479 /* Implement floatsleep() for various platforms.
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
480 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
481 set an exception; else return 0. */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
482
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
483 static int
2506
9d6e97d66670 changes for MPW
Guido van Rossum <guido@python.org>
parents: 2215
diff changeset
484 #ifdef MPW
9d6e97d66670 changes for MPW
Guido van Rossum <guido@python.org>
parents: 2215
diff changeset
485 floatsleep(double secs)
9d6e97d66670 changes for MPW
Guido van Rossum <guido@python.org>
parents: 2215
diff changeset
486 #else
4947
78d880043129 Formatting, and minor error detection
Barry Warsaw <barry@python.org>
parents: 4618
diff changeset
487 floatsleep(secs)
1181
950cd37fcddc * Makefile: use cp -r to install the library
Guido van Rossum <guido@python.org>
parents: 1177
diff changeset
488 double secs;
2506
9d6e97d66670 changes for MPW
Guido van Rossum <guido@python.org>
parents: 2215
diff changeset
489 #endif /* MPW */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 {
5100
32310251d767 Issue a more meaningful error if strftime keeps returning a NULL pointer.
Guido van Rossum <guido@python.org>
parents: 4947
diff changeset
491 /* XXX Should test for MS_WIN32 first! */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
492 #ifdef HAVE_SELECT
143
1870d82de069 Added BSD implementations of millisleep.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
493 struct timeval t;
1181
950cd37fcddc * Makefile: use cp -r to install the library
Guido van Rossum <guido@python.org>
parents: 1177
diff changeset
494 double frac;
950cd37fcddc * Makefile: use cp -r to install the library
Guido van Rossum <guido@python.org>
parents: 1177
diff changeset
495 frac = fmod(secs, 1.0);
950cd37fcddc * Makefile: use cp -r to install the library
Guido van Rossum <guido@python.org>
parents: 1177
diff changeset
496 secs = floor(secs);
950cd37fcddc * Makefile: use cp -r to install the library
Guido van Rossum <guido@python.org>
parents: 1177
diff changeset
497 t.tv_sec = (long)secs;
950cd37fcddc * Makefile: use cp -r to install the library
Guido van Rossum <guido@python.org>
parents: 1177
diff changeset
498 t.tv_usec = (long)(frac*1000000.0);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
499 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
500 PyErr_SetFromErrno(PyExc_IOError);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
501 return -1;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
502 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
503 #else /* !HAVE_SELECT */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
504 #ifdef macintosh
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
505 #define MacTicks (* (long *)0x16A)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
506 long deadline;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
507 deadline = MacTicks + (long)(secs * 60.0);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
508 while (MacTicks < deadline) {
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
509 if (PyErr_CheckSignals())
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
510 return -1;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
511 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
512 #else /* !macintosh */
3592
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
513 #ifdef __WATCOMC__
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
514 /* XXX Can't interrupt this sleep */
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
515 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
516 #else /* !__WATCOMC__ */
1322
f3c907efe5d7 * config.c: different default PYTHONPATH for MS-DOS
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
517 #ifdef MSDOS
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
518 struct timeb t1, t2;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
519 double frac;
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
520 extern double fmod Py_PROTO((double, double));
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
521 extern double floor Py_PROTO((double));
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
522 if (secs <= 0.0)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
523 return;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
524 frac = fmod(secs, 1.0);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
525 secs = floor(secs);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
526 ftime(&t1);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
527 t2.time = t1.time + (int)secs;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
528 t2.millitm = t1.millitm + (int)(frac*1000.0);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
529 while (t2.millitm >= 1000) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
530 t2.time++;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
531 t2.millitm -= 1000;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
532 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
533 for (;;) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
534 #ifdef QUICKWIN
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
535 _wyield();
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
536 #endif
4615
87617701d721 Greatly renamed.
Barry Warsaw <barry@python.org>
parents: 4607
diff changeset
537 if (PyErr_CheckSignals())
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
538 return -1;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
539 ftime(&t1);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
540 if (t1.time > t2.time ||
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
541 t1.time == t2.time && t1.millitm >= t2.millitm)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
542 break;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
543 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
544 #else /* !MSDOS */
4232
886c0f775c97 Sleep() turns out to be win32 specific.
Guido van Rossum <guido@python.org>
parents: 4216
diff changeset
545 #ifdef MS_WIN32
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
546 /* XXX Can't interrupt this sleep */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
547 Sleep((int)(secs*1000));
4232
886c0f775c97 Sleep() turns out to be win32 specific.
Guido van Rossum <guido@python.org>
parents: 4216
diff changeset
548 #else /* !MS_WIN32 */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
549 /* XXX Can't interrupt this sleep */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
550 sleep((int)secs);
4232
886c0f775c97 Sleep() turns out to be win32 specific.
Guido van Rossum <guido@python.org>
parents: 4216
diff changeset
551 #endif /* !MS_WIN32 */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
552 #endif /* !MSDOS */
3592
2403620ca931 Watcom changes.
Guido van Rossum <guido@python.org>
parents: 3419
diff changeset
553 #endif /* !__WATCOMC__ */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
554 #endif /* !macintosh */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
555 #endif /* !HAVE_SELECT */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
556 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
557 }