Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09225b7

Browse files
author
Victor Stinner
committed
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
to have a resolution of 100 ns instead of 1 ms (the clock accuracy is between 0.5 ms and 15 ms).
1 parent 8b30201 commit 09225b7

2 files changed

Lines changed: 29 additions & 8 deletions

File tree

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,10 @@ Core and Builtins
466466
Library
467467
-------
468468

469+
- Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of
470+
ftime() to have a resolution of 100 ns instead of 1 ms (the clock accuracy is
471+
between 0.5 ms and 15 ms).
472+
469473
- Issue #13846: Add time.monotonic(), monotonic clock.
470474

471475
- Issue #10811: Fix recursive usage of cursors. Instead of crashing,

Python/pytime.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "Python.h"
2+
#ifdef MS_WINDOWS
3+
#include <windows.h>
4+
#endif
25

3-
#ifdef __APPLE__
4-
#if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME)
6+
#if defined(__APPLE__) && defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME)
57
/*
68
* _PyTime_gettimeofday falls back to ftime when getttimeofday fails because the latter
79
* might fail on some platforms. This fallback is unwanted on MacOSX because
@@ -10,18 +12,30 @@
1012
*/
1113
# undef HAVE_FTIME
1214
#endif
13-
#endif
1415

15-
#ifdef HAVE_FTIME
16+
#if defined(HAVE_FTIME) && !defined(MS_WINDOWS)
1617
#include <sys/timeb.h>
17-
#if !defined(MS_WINDOWS) && !defined(PYOS_OS2)
1818
extern int ftime(struct timeb *);
19-
#endif /* MS_WINDOWS */
20-
#endif /* HAVE_FTIME */
19+
#endif
2120

2221
void
2322
_PyTime_gettimeofday(_PyTime_timeval *tp)
2423
{
24+
#ifdef MS_WINDOWS
25+
FILETIME system_time;
26+
ULARGE_INTEGER large;
27+
ULONGLONG microseconds;
28+
29+
GetSystemTimeAsFileTime(&system_time);
30+
large.u.LowPart = system_time.dwLowDateTime;
31+
large.u.HighPart = system_time.dwHighDateTime;
32+
/* 11,644,473,600,000,000: number of microseconds between
33+
the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
34+
days). */
35+
microseconds = large.QuadPart / 10 - 11644473600000000;
36+
tp->tv_sec = microseconds / 1000000;
37+
tp->tv_usec = microseconds % 1000000;
38+
#else
2539
/* There are three ways to get the time:
2640
(1) gettimeofday() -- resolution in microseconds
2741
(2) ftime() -- resolution in milliseconds
@@ -30,6 +44,7 @@ _PyTime_gettimeofday(_PyTime_timeval *tp)
3044
Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
3145
fail, so we fall back on ftime() or time().
3246
Note: clock resolution does not imply clock accuracy! */
47+
3348
#ifdef HAVE_GETTIMEOFDAY
3449
#ifdef GETTIMEOFDAY_NO_TZ
3550
if (gettimeofday(tp) == 0)
@@ -39,6 +54,7 @@ _PyTime_gettimeofday(_PyTime_timeval *tp)
3954
return;
4055
#endif /* !GETTIMEOFDAY_NO_TZ */
4156
#endif /* !HAVE_GETTIMEOFDAY */
57+
4258
#if defined(HAVE_FTIME)
4359
{
4460
struct timeb t;
@@ -50,7 +66,8 @@ _PyTime_gettimeofday(_PyTime_timeval *tp)
5066
tp->tv_sec = time(NULL);
5167
tp->tv_usec = 0;
5268
#endif /* !HAVE_FTIME */
53-
return;
69+
70+
#endif /* MS_WINDOWS */
5471
}
5572

5673
void

0 commit comments

Comments
 (0)