blob: beeab87e2c7399f3a6ee0e6553317bd3b7f74752 [file] [log] [blame]
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +00001#include "Python.h"
Victor Stinner09225b72012-02-07 23:41:01 +01002#ifdef MS_WINDOWS
3#include <windows.h>
4#endif
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +00005
Victor Stinner09225b72012-02-07 23:41:01 +01006#if defined(__APPLE__) && defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME)
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +00007 /*
8 * _PyTime_gettimeofday falls back to ftime when getttimeofday fails because the latter
9 * might fail on some platforms. This fallback is unwanted on MacOSX because
10 * that makes it impossible to use a binary build on OSX 10.4 on earlier
11 * releases of the OS. Therefore claim we don't support ftime.
12 */
13# undef HAVE_FTIME
14#endif
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000015
Victor Stinner09225b72012-02-07 23:41:01 +010016#if defined(HAVE_FTIME) && !defined(MS_WINDOWS)
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000017#include <sys/timeb.h>
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000018extern int ftime(struct timeb *);
Victor Stinner09225b72012-02-07 23:41:01 +010019#endif
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000020
Victor Stinnerec895392012-04-29 02:41:27 +020021static void
22pygettimeofday(_PyTime_timeval *tp, _Py_clock_info_t *info)
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000023{
Victor Stinner09225b72012-02-07 23:41:01 +010024#ifdef MS_WINDOWS
25 FILETIME system_time;
26 ULARGE_INTEGER large;
Victor Stinner4195b5c2012-02-08 23:03:19 +010027 ULONGLONG microseconds;
Victor Stinner09225b72012-02-07 23:41:01 +010028
29 GetSystemTimeAsFileTime(&system_time);
30 large.u.LowPart = system_time.dwLowDateTime;
31 large.u.HighPart = system_time.dwHighDateTime;
Victor Stinner4195b5c2012-02-08 23:03:19 +010032 /* 11,644,473,600,000,000: number of microseconds between
Victor Stinner09225b72012-02-07 23:41:01 +010033 the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
34 days). */
Victor Stinner4195b5c2012-02-08 23:03:19 +010035 microseconds = large.QuadPart / 10 - 11644473600000000;
36 tp->tv_sec = microseconds / 1000000;
37 tp->tv_usec = microseconds % 1000000;
Victor Stinnerec895392012-04-29 02:41:27 +020038 if (info) {
39 DWORD timeAdjustment, timeIncrement;
40 BOOL isTimeAdjustmentDisabled;
41
42 info->implementation = "GetSystemTimeAsFileTime()";
Benjamin Peterson49a69e42012-05-01 09:38:34 -040043 info->monotonic = 0;
Victor Stinnerec895392012-04-29 02:41:27 +020044 (void) GetSystemTimeAdjustment(&timeAdjustment, &timeIncrement,
45 &isTimeAdjustmentDisabled);
46 info->resolution = timeIncrement * 1e-7;
Victor Stinner2b89fdf2012-06-12 22:46:37 +020047 info->adjustable = 1;
Victor Stinnerec895392012-04-29 02:41:27 +020048 }
Victor Stinner09225b72012-02-07 23:41:01 +010049#else
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000050 /* There are three ways to get the time:
51 (1) gettimeofday() -- resolution in microseconds
52 (2) ftime() -- resolution in milliseconds
53 (3) time() -- resolution in seconds
54 In all cases the return value in a timeval struct.
55 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
56 fail, so we fall back on ftime() or time().
57 Note: clock resolution does not imply clock accuracy! */
Victor Stinner09225b72012-02-07 23:41:01 +010058
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000059#ifdef HAVE_GETTIMEOFDAY
Victor Stinnerec895392012-04-29 02:41:27 +020060 int err;
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000061#ifdef GETTIMEOFDAY_NO_TZ
Victor Stinnerec895392012-04-29 02:41:27 +020062 err = gettimeofday(tp);
63#else
64 err = gettimeofday(tp, (struct timezone *)NULL);
65#endif
66 if (err == 0) {
67 if (info) {
68 info->implementation = "gettimeofday()";
69 info->resolution = 1e-6;
Benjamin Peterson49a69e42012-05-01 09:38:34 -040070 info->monotonic = 0;
Victor Stinner2b89fdf2012-06-12 22:46:37 +020071 info->adjustable = 1;
Victor Stinnerec895392012-04-29 02:41:27 +020072 }
Victor Stinnerccd57152012-02-08 14:31:50 +010073 return;
Victor Stinnerec895392012-04-29 02:41:27 +020074 }
75#endif /* HAVE_GETTIMEOFDAY */
Victor Stinner09225b72012-02-07 23:41:01 +010076
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000077#if defined(HAVE_FTIME)
Victor Stinner4195b5c2012-02-08 23:03:19 +010078 {
79 struct timeb t;
80 ftime(&t);
81 tp->tv_sec = t.time;
82 tp->tv_usec = t.millitm * 1000;
Victor Stinnerec895392012-04-29 02:41:27 +020083 if (info) {
84 info->implementation = "ftime()";
85 info->resolution = 1e-3;
Benjamin Peterson49a69e42012-05-01 09:38:34 -040086 info->monotonic = 0;
Victor Stinner2b89fdf2012-06-12 22:46:37 +020087 info->adjustable = 1;
Victor Stinnerec895392012-04-29 02:41:27 +020088 }
Victor Stinner4195b5c2012-02-08 23:03:19 +010089 }
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000090#else /* !HAVE_FTIME */
Victor Stinner4195b5c2012-02-08 23:03:19 +010091 tp->tv_sec = time(NULL);
92 tp->tv_usec = 0;
Victor Stinnerec895392012-04-29 02:41:27 +020093 if (info) {
94 info->implementation = "time()";
95 info->resolution = 1.0;
Benjamin Peterson49a69e42012-05-01 09:38:34 -040096 info->monotonic = 0;
Victor Stinner2b89fdf2012-06-12 22:46:37 +020097 info->adjustable = 1;
Victor Stinnerec895392012-04-29 02:41:27 +020098 }
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000099#endif /* !HAVE_FTIME */
Victor Stinner09225b72012-02-07 23:41:01 +0100100
101#endif /* MS_WINDOWS */
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000102}
103
Victor Stinnerec895392012-04-29 02:41:27 +0200104void
105_PyTime_gettimeofday(_PyTime_timeval *tp)
106{
107 pygettimeofday(tp, NULL);
108}
109
110void
111_PyTime_gettimeofday_info(_PyTime_timeval *tp, _Py_clock_info_t *info)
112{
113 pygettimeofday(tp, info);
114}
115
Victor Stinner5d272cc2012-03-13 13:35:55 +0100116static void
117error_time_t_overflow(void)
Victor Stinner643cd682012-03-02 22:54:03 +0100118{
Victor Stinner5d272cc2012-03-13 13:35:55 +0100119 PyErr_SetString(PyExc_OverflowError,
120 "timestamp out of range for platform time_t");
121}
122
Larry Hastings76ad59b2012-05-03 00:30:07 -0700123time_t
Victor Stinner5d272cc2012-03-13 13:35:55 +0100124_PyLong_AsTime_t(PyObject *obj)
125{
126#if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
127 PY_LONG_LONG val;
128 val = PyLong_AsLongLong(obj);
129#else
130 long val;
131 assert(sizeof(time_t) <= sizeof(long));
132 val = PyLong_AsLong(obj);
133#endif
134 if (val == -1 && PyErr_Occurred()) {
135 if (PyErr_ExceptionMatches(PyExc_OverflowError))
136 error_time_t_overflow();
137 return -1;
138 }
139 return (time_t)val;
140}
141
Larry Hastings6fe20b32012-04-19 15:07:49 -0700142PyObject *
143_PyLong_FromTime_t(time_t t)
144{
145#if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
146 return PyLong_FromLongLong((PY_LONG_LONG)t);
147#else
148 assert(sizeof(time_t) <= sizeof(long));
149 return PyLong_FromLong((long)t);
150#endif
151}
152
Victor Stinner5d272cc2012-03-13 13:35:55 +0100153static int
154_PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
155 double denominator)
156{
157 assert(denominator <= LONG_MAX);
Victor Stinner643cd682012-03-02 22:54:03 +0100158 if (PyFloat_Check(obj)) {
Victor Stinnerbd273c12012-03-13 19:12:23 +0100159 double d, intpart, err;
160 /* volatile avoids unsafe optimization on float enabled by gcc -O3 */
161 volatile double floatpart;
Victor Stinner643cd682012-03-02 22:54:03 +0100162
163 d = PyFloat_AsDouble(obj);
164 floatpart = modf(d, &intpart);
165 if (floatpart < 0) {
166 floatpart = 1.0 + floatpart;
167 intpart -= 1.0;
168 }
169
170 *sec = (time_t)intpart;
171 err = intpart - (double)*sec;
Victor Stinner5d272cc2012-03-13 13:35:55 +0100172 if (err <= -1.0 || err >= 1.0) {
173 error_time_t_overflow();
174 return -1;
175 }
Victor Stinner643cd682012-03-02 22:54:03 +0100176
Victor Stinner5d272cc2012-03-13 13:35:55 +0100177 floatpart *= denominator;
178 *numerator = (long)floatpart;
Victor Stinner643cd682012-03-02 22:54:03 +0100179 return 0;
180 }
181 else {
Victor Stinner5d272cc2012-03-13 13:35:55 +0100182 *sec = _PyLong_AsTime_t(obj);
183 if (*sec == (time_t)-1 && PyErr_Occurred())
184 return -1;
185 *numerator = 0;
Victor Stinner643cd682012-03-02 22:54:03 +0100186 return 0;
187 }
Victor Stinner5d272cc2012-03-13 13:35:55 +0100188}
Victor Stinner643cd682012-03-02 22:54:03 +0100189
Victor Stinner5d272cc2012-03-13 13:35:55 +0100190int
191_PyTime_ObjectToTime_t(PyObject *obj, time_t *sec)
192{
193 if (PyFloat_Check(obj)) {
194 double d, intpart, err;
195
Victor Stinner5d272cc2012-03-13 13:35:55 +0100196 d = PyFloat_AsDouble(obj);
197 (void)modf(d, &intpart);
198
199 *sec = (time_t)intpart;
200 err = intpart - (double)*sec;
201 if (err <= -1.0 || err >= 1.0) {
202 error_time_t_overflow();
203 return -1;
204 }
205 return 0;
206 }
207 else {
208 *sec = _PyLong_AsTime_t(obj);
209 if (*sec == (time_t)-1 && PyErr_Occurred())
210 return -1;
211 return 0;
212 }
213}
214
215int
216_PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec)
217{
218 return _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9);
219}
220
221int
222_PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec)
223{
224 return _PyTime_ObjectToDenominator(obj, sec, usec, 1e6);
Victor Stinner643cd682012-03-02 22:54:03 +0100225}
226
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000227void
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000228_PyTime_Init()
229{
230 /* Do nothing. Needed to force linking. */
231}