annotate Python/pytime.c @ 76451:ea46ebba8a0f

Issue #13959: Re-implement imp.source_from_cache() in Lib/imp.py.
author Brett Cannon <brett@python.org>
date Sat, 21 Apr 2012 18:52:52 -0400
parents f554043badec
children 76d2e0761d18
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
1 #include "Python.h"
74821
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
2 #ifdef MS_WINDOWS
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
3 #include <windows.h>
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
4 #endif
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
5
74821
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
6 #if defined(__APPLE__) && defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME)
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
7 /*
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
8 * _PyTime_gettimeofday falls back to ftime when getttimeofday fails because the latter
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
9 * might fail on some platforms. This fallback is unwanted on MacOSX because
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
10 * that makes it impossible to use a binary build on OSX 10.4 on earlier
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
11 * releases of the OS. Therefore claim we don't support ftime.
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
12 */
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
13 # undef HAVE_FTIME
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
14 #endif
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
15
74821
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
16 #if defined(HAVE_FTIME) && !defined(MS_WINDOWS)
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
17 #include <sys/timeb.h>
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
18 extern int ftime(struct timeb *);
74821
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
19 #endif
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
20
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
21 void
74834
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
22 _PyTime_gettimeofday(_PyTime_timeval *tp)
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
23 {
74821
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
24 #ifdef MS_WINDOWS
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
25 FILETIME system_time;
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
26 ULARGE_INTEGER large;
74834
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
27 ULONGLONG microseconds;
74821
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
28
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
29 GetSystemTimeAsFileTime(&system_time);
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
30 large.u.LowPart = system_time.dwLowDateTime;
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
31 large.u.HighPart = system_time.dwHighDateTime;
74834
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
32 /* 11,644,473,600,000,000: number of microseconds between
74821
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
33 the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
34 days). */
74834
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
35 microseconds = large.QuadPart / 10 - 11644473600000000;
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
36 tp->tv_sec = microseconds / 1000000;
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
37 tp->tv_usec = microseconds % 1000000;
74821
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
38 #else
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
39 /* There are three ways to get the time:
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
40 (1) gettimeofday() -- resolution in microseconds
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
41 (2) ftime() -- resolution in milliseconds
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
42 (3) time() -- resolution in seconds
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
43 In all cases the return value in a timeval struct.
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
44 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
45 fail, so we fall back on ftime() or time().
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
46 Note: clock resolution does not imply clock accuracy! */
74821
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
47
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
48 #ifdef HAVE_GETTIMEOFDAY
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
49 #ifdef GETTIMEOFDAY_NO_TZ
74834
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
50 if (gettimeofday(tp) == 0)
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
51 return;
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
52 #else /* !GETTIMEOFDAY_NO_TZ */
74834
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
53 if (gettimeofday(tp, (struct timezone *)NULL) == 0)
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
54 return;
74832
f8409b3d6449 PEP 410
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74821
diff changeset
55 #endif /* !GETTIMEOFDAY_NO_TZ */
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
56 #endif /* !HAVE_GETTIMEOFDAY */
74821
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
57
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
58 #if defined(HAVE_FTIME)
74834
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
59 {
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
60 struct timeb t;
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
61 ftime(&t);
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
62 tp->tv_sec = t.time;
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
63 tp->tv_usec = t.millitm * 1000;
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
64 }
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
65 #else /* !HAVE_FTIME */
74834
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
66 tp->tv_sec = time(NULL);
90eda29a8e03 Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74832
diff changeset
67 tp->tv_usec = 0;
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
68 #endif /* !HAVE_FTIME */
74821
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
69
bee7943d38c6 Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents: 63757
diff changeset
70 #endif /* MS_WINDOWS */
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
71 }
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
72
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
73 static void
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
74 error_time_t_overflow(void)
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
75 {
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
76 PyErr_SetString(PyExc_OverflowError,
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
77 "timestamp out of range for platform time_t");
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
78 }
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
79
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
80 static time_t
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
81 _PyLong_AsTime_t(PyObject *obj)
75372
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
82 {
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
83 #if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
84 PY_LONG_LONG val;
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
85 val = PyLong_AsLongLong(obj);
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
86 #else
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
87 long val;
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
88 assert(sizeof(time_t) <= sizeof(long));
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
89 val = PyLong_AsLong(obj);
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
90 #endif
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
91 if (val == -1 && PyErr_Occurred()) {
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
92 if (PyErr_ExceptionMatches(PyExc_OverflowError))
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
93 error_time_t_overflow();
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
94 return -1;
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
95 }
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
96 return (time_t)val;
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
97 }
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
98
76423
f554043badec Issue #14127: Add st_{cma}time_ns fields to os.stat() result object.
Larry Hastings <larry@hastings.org>
parents: 75595
diff changeset
99 PyObject *
f554043badec Issue #14127: Add st_{cma}time_ns fields to os.stat() result object.
Larry Hastings <larry@hastings.org>
parents: 75595
diff changeset
100 _PyLong_FromTime_t(time_t t)
f554043badec Issue #14127: Add st_{cma}time_ns fields to os.stat() result object.
Larry Hastings <larry@hastings.org>
parents: 75595
diff changeset
101 {
f554043badec Issue #14127: Add st_{cma}time_ns fields to os.stat() result object.
Larry Hastings <larry@hastings.org>
parents: 75595
diff changeset
102 #if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
f554043badec Issue #14127: Add st_{cma}time_ns fields to os.stat() result object.
Larry Hastings <larry@hastings.org>
parents: 75595
diff changeset
103 return PyLong_FromLongLong((PY_LONG_LONG)t);
f554043badec Issue #14127: Add st_{cma}time_ns fields to os.stat() result object.
Larry Hastings <larry@hastings.org>
parents: 75595
diff changeset
104 #else
f554043badec Issue #14127: Add st_{cma}time_ns fields to os.stat() result object.
Larry Hastings <larry@hastings.org>
parents: 75595
diff changeset
105 assert(sizeof(time_t) <= sizeof(long));
f554043badec Issue #14127: Add st_{cma}time_ns fields to os.stat() result object.
Larry Hastings <larry@hastings.org>
parents: 75595
diff changeset
106 return PyLong_FromLong((long)t);
f554043badec Issue #14127: Add st_{cma}time_ns fields to os.stat() result object.
Larry Hastings <larry@hastings.org>
parents: 75595
diff changeset
107 #endif
f554043badec Issue #14127: Add st_{cma}time_ns fields to os.stat() result object.
Larry Hastings <larry@hastings.org>
parents: 75595
diff changeset
108 }
f554043badec Issue #14127: Add st_{cma}time_ns fields to os.stat() result object.
Larry Hastings <larry@hastings.org>
parents: 75595
diff changeset
109
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
110 static int
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
111 _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
112 double denominator)
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
113 {
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
114 assert(denominator <= LONG_MAX);
75372
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
115 if (PyFloat_Check(obj)) {
75595
9d69a2418d80 Issue #14180: Fix an invalid rounding when compiler optimization are enabled
Victor Stinner <vstinner@wyplay.com>
parents: 75591
diff changeset
116 double d, intpart, err;
9d69a2418d80 Issue #14180: Fix an invalid rounding when compiler optimization are enabled
Victor Stinner <vstinner@wyplay.com>
parents: 75591
diff changeset
117 /* volatile avoids unsafe optimization on float enabled by gcc -O3 */
9d69a2418d80 Issue #14180: Fix an invalid rounding when compiler optimization are enabled
Victor Stinner <vstinner@wyplay.com>
parents: 75591
diff changeset
118 volatile double floatpart;
75372
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
119
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
120 d = PyFloat_AsDouble(obj);
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
121 floatpart = modf(d, &intpart);
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
122 if (floatpart < 0) {
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
123 floatpart = 1.0 + floatpart;
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
124 intpart -= 1.0;
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
125 }
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
126
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
127 *sec = (time_t)intpart;
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
128 err = intpart - (double)*sec;
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
129 if (err <= -1.0 || err >= 1.0) {
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
130 error_time_t_overflow();
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
131 return -1;
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
132 }
75372
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
133
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
134 floatpart *= denominator;
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
135 *numerator = (long)floatpart;
75372
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
136 return 0;
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
137 }
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
138 else {
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
139 *sec = _PyLong_AsTime_t(obj);
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
140 if (*sec == (time_t)-1 && PyErr_Occurred())
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
141 return -1;
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
142 *numerator = 0;
75372
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
143 return 0;
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
144 }
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
145 }
75372
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
146
75590
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
147 int
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
148 _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec)
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
149 {
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
150 if (PyFloat_Check(obj)) {
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
151 double d, intpart, err;
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
152
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
153 d = PyFloat_AsDouble(obj);
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
154 (void)modf(d, &intpart);
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
155
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
156 *sec = (time_t)intpart;
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
157 err = intpart - (double)*sec;
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
158 if (err <= -1.0 || err >= 1.0) {
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
159 error_time_t_overflow();
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
160 return -1;
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
161 }
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
162 return 0;
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
163 }
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
164 else {
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
165 *sec = _PyLong_AsTime_t(obj);
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
166 if (*sec == (time_t)-1 && PyErr_Occurred())
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
167 return -1;
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
168 return 0;
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
169 }
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
170 }
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
171
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
172 int
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
173 _PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec)
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
174 {
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
175 return _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9);
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
176 }
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
177
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
178 int
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
179 _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec)
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
180 {
1e9cc1a03365 Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents: 75372
diff changeset
181 return _PyTime_ObjectToDenominator(obj, sec, usec, 1e6);
75372
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
182 }
67d9595a833c Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents: 74834
diff changeset
183
63757
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
184 void
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
185 _PyTime_Init()
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
186 {
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
187 /* Do nothing. Needed to force linking. */
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
188 }