blob: d23ce75b43f44224475620eee545697079879b0b [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
21void
Victor Stinner4195b5c2012-02-08 23:03:19 +010022_PyTime_gettimeofday(_PyTime_timeval *tp)
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 Stinner09225b72012-02-07 23:41:01 +010038#else
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000039 /* There are three ways to get the time:
40 (1) gettimeofday() -- resolution in microseconds
41 (2) ftime() -- resolution in milliseconds
42 (3) time() -- resolution in seconds
43 In all cases the return value in a timeval struct.
44 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
45 fail, so we fall back on ftime() or time().
46 Note: clock resolution does not imply clock accuracy! */
Victor Stinner09225b72012-02-07 23:41:01 +010047
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000048#ifdef HAVE_GETTIMEOFDAY
49#ifdef GETTIMEOFDAY_NO_TZ
Victor Stinner4195b5c2012-02-08 23:03:19 +010050 if (gettimeofday(tp) == 0)
Victor Stinnerccd57152012-02-08 14:31:50 +010051 return;
Victor Stinner4195b5c2012-02-08 23:03:19 +010052#else /* !GETTIMEOFDAY_NO_TZ */
53 if (gettimeofday(tp, (struct timezone *)NULL) == 0)
54 return;
55#endif /* !GETTIMEOFDAY_NO_TZ */
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000056#endif /* !HAVE_GETTIMEOFDAY */
Victor Stinner09225b72012-02-07 23:41:01 +010057
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000058#if defined(HAVE_FTIME)
Victor Stinner4195b5c2012-02-08 23:03:19 +010059 {
60 struct timeb t;
61 ftime(&t);
62 tp->tv_sec = t.time;
63 tp->tv_usec = t.millitm * 1000;
64 }
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000065#else /* !HAVE_FTIME */
Victor Stinner4195b5c2012-02-08 23:03:19 +010066 tp->tv_sec = time(NULL);
67 tp->tv_usec = 0;
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000068#endif /* !HAVE_FTIME */
Victor Stinner09225b72012-02-07 23:41:01 +010069
70#endif /* MS_WINDOWS */
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000071}
72
Victor Stinner643cd682012-03-02 22:54:03 +010073int
74_PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec)
75{
76 if (PyFloat_Check(obj)) {
77 double d, intpart, floatpart, err;
78
79 d = PyFloat_AsDouble(obj);
80 floatpart = modf(d, &intpart);
81 if (floatpart < 0) {
82 floatpart = 1.0 + floatpart;
83 intpart -= 1.0;
84 }
85
86 *sec = (time_t)intpart;
87 err = intpart - (double)*sec;
88 if (err <= -1.0 || err >= 1.0)
89 goto overflow;
90
91 floatpart *= 1e9;
92 *nsec = (long)floatpart;
93 return 0;
94 }
95 else {
96#if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
97 *sec = PyLong_AsLongLong(obj);
98#else
99 assert(sizeof(time_t) <= sizeof(long));
100 *sec = PyLong_AsLong(obj);
101#endif
102 if (*sec == -1 && PyErr_Occurred()) {
103 if (PyErr_ExceptionMatches(PyExc_OverflowError))
104 goto overflow;
105 else
106 return -1;
107 }
108 *nsec = 0;
109 return 0;
110 }
111
112overflow:
113 PyErr_SetString(PyExc_OverflowError,
114 "timestamp out of range for platform time_t");
115 return -1;
116}
117
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000118void
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000119_PyTime_Init()
120{
121 /* Do nothing. Needed to force linking. */
122}