blob: bec1c713e602cf636bf8ff2f74b9cea0fa106ecb [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
73void
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +000074_PyTime_Init()
75{
76 /* Do nothing. Needed to force linking. */
77}