#define _PYTIME_FROMSECONDS(seconds) \
((PyTime_t)(seconds) * (1000 * 1000 * 1000))
-// Create a timestamp from a number of nanoseconds.
-// Export for '_testinternalcapi' shared extension.
-PyAPI_FUNC(PyTime_t) _PyTime_FromNanoseconds(PyTime_t ns);
-
// Create a timestamp from a number of microseconds.
// Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
extern PyTime_t _PyTime_FromMicrosecondsClamp(PyTime_t us);
-// Create a timestamp from nanoseconds (Python int).
+// Create a timestamp from a Python int object (number of nanoseconds).
// Export for '_lsprof' shared extension.
-PyAPI_FUNC(int) _PyTime_FromNanosecondsObject(PyTime_t *t,
+PyAPI_FUNC(int) _PyTime_FromLong(PyTime_t *t,
PyObject *obj);
// Convert a number of seconds (Python float or int) to a timestamp.
_PyTime_round_t round);
#endif
-// Convert timestamp to a number of nanoseconds (10^-9 seconds) as a Python int
-// object.
+// Convert a timestamp (number of nanoseconds) as a Python int object.
// Export for '_testinternalcapi' shared extension.
-PyAPI_FUNC(PyObject*) _PyTime_AsNanosecondsObject(PyTime_t t);
+PyAPI_FUNC(PyObject*) _PyTime_AsLong(PyTime_t t);
#ifndef MS_WINDOWS
// Create a timestamp from a timeval structure.
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _PyEval_SetProfile()
#include "pycore_pystate.h" // _PyThreadState_GET()
-#include "pycore_time.h" // _PyTime_FromNanosecondsObject()
+#include "pycore_time.h" // _PyTime_FromLong()
#include "rotatingtree.h"
if (pObj->externalTimerUnit > 0.0) {
/* interpret the result as an integer that will be scaled
in profiler_getstats() */
- err = _PyTime_FromNanosecondsObject(&result, o);
+ err = _PyTime_FromLong(&result, o);
}
else {
/* interpret the result as a double measured in seconds.
return NULL;
}
PyTime_t ts = _PyTime_FromSeconds(seconds);
- return _PyTime_AsNanosecondsObject(ts);
+ return _PyTime_AsLong(ts);
}
static int
if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) {
return NULL;
}
- return _PyTime_AsNanosecondsObject(ts);
+ return _PyTime_AsLong(ts);
}
static PyObject *
return NULL;
}
PyTime_t t;
- if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
+ if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
struct timeval tv;
return NULL;
}
PyTime_t t;
- if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
+ if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
struct timeval tv;
return NULL;
}
PyTime_t t;
- if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
+ if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
struct timespec ts;
return NULL;
}
PyTime_t t;
- if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
+ if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
struct timespec ts;
return NULL;
}
PyTime_t t;
- if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
+ if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
PyTime_t ms = _PyTime_AsMilliseconds(t, round);
- PyTime_t ns = _PyTime_FromNanoseconds(ms);
- return _PyTime_AsNanosecondsObject(ns);
+ return _PyTime_AsLong(ms);
}
static PyObject *
return NULL;
}
PyTime_t t;
- if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
+ if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
PyTime_t us = _PyTime_AsMicroseconds(t, round);
- PyTime_t ns = _PyTime_FromNanoseconds(us);
- return _PyTime_AsNanosecondsObject(ns);
+ return _PyTime_AsLong(us);
}
static PyObject *
if (PyTime_Time(&t) < 0) {
return NULL;
}
- return _PyTime_AsNanosecondsObject(t);
+ return _PyTime_AsLong(t);
}
PyDoc_STRVAR(time_ns_doc,
"or its value cannot be represented");
return -1;
}
- PyTime_t ns = _PyTimeFraction_Mul(ticks, base);
- *tp = _PyTime_FromNanoseconds(ns);
+ *tp = _PyTimeFraction_Mul(ticks, base);
return 0;
}
#endif /* HAVE_CLOCK */
if (_PyTime_FromTimespec(&t, &ts) < 0) {
return NULL;
}
- return _PyTime_AsNanosecondsObject(t);
+ return _PyTime_AsLong(t);
}
#endif /* HAVE_CLOCK_GETTIME */
return NULL;
}
- if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
+ if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
if (_PyTime_AsTimespec(t, &ts) == -1) {
if (PyTime_Monotonic(&t) < 0) {
return NULL;
}
- return _PyTime_AsNanosecondsObject(t);
+ return _PyTime_AsLong(t);
}
PyDoc_STRVAR(monotonic_ns_doc,
if (PyTime_PerfCounter(&t) < 0) {
return NULL;
}
- return _PyTime_AsNanosecondsObject(t);
+ return _PyTime_AsLong(t);
}
PyDoc_STRVAR(perf_counter_ns_doc,
PyTime_t ns;
ns = _PyTimeFraction_Mul(process.tms_utime, base);
ns += _PyTimeFraction_Mul(process.tms_stime, base);
- *tp = _PyTime_FromNanoseconds(ns);
+ *tp = ns;
return 1;
}
#endif
HANDLE process;
FILETIME creation_time, exit_time, kernel_time, user_time;
ULARGE_INTEGER large;
- PyTime_t ktime, utime, t;
+ PyTime_t ktime, utime;
BOOL ok;
process = GetCurrentProcess();
utime = large.QuadPart;
/* ktime and utime have a resolution of 100 nanoseconds */
- t = _PyTime_FromNanoseconds((ktime + utime) * 100);
- *tp = t;
+ *tp = (ktime + utime) * 100;
return 0;
#else
if (py_process_time(state, &t, NULL) < 0) {
return NULL;
}
- return _PyTime_AsNanosecondsObject(t);
+ return _PyTime_AsLong(t);
}
PyDoc_STRVAR(process_time_ns_doc,
HANDLE thread;
FILETIME creation_time, exit_time, kernel_time, user_time;
ULARGE_INTEGER large;
- PyTime_t ktime, utime, t;
+ PyTime_t ktime, utime;
BOOL ok;
thread = GetCurrentThread();
utime = large.QuadPart;
/* ktime and utime have a resolution of 100 nanoseconds */
- t = _PyTime_FromNanoseconds((ktime + utime) * 100);
- *tp = t;
+ *tp = (ktime + utime) * 100;
return 0;
}
info->adjustable = 0;
info->resolution = 1e-9;
}
- *tp = _PyTime_FromNanoseconds(tc.stime + tc.utime);
+ *tp = (tc.stime + tc.utime);
return 0;
}
info->monotonic = 1;
info->adjustable = 0;
}
- *tp = _PyTime_FromNanoseconds(gethrvtime());
+ *tp = gethrvtime();
return 0;
}
if (_PyTime_GetThreadTimeWithInfo(&t, NULL) < 0) {
return NULL;
}
- return _PyTime_AsNanosecondsObject(t);
+ return _PyTime_AsLong(t);
}
PyDoc_STRVAR(thread_time_ns_doc,
}
-static inline PyTime_t
-pytime_from_nanoseconds(PyTime_t t)
-{
- // PyTime_t is a number of nanoseconds
- return t;
-}
-
-
-static inline PyTime_t
-pytime_as_nanoseconds(PyTime_t t)
-{
- // PyTime_t is a number of nanoseconds: see pytime_from_nanoseconds()
- return t;
-}
-
-
// Compute t1 + t2. Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
static inline int
pytime_add(PyTime_t *t1, PyTime_t t2)
// Convert PyTime_t to long.
// Return 0 on success. Return -1 and clamp the value on overflow.
static int
-_PyTime_AsLong(PyTime_t t, long *t2)
+_PyTime_AsCLong(PyTime_t t, long *t2)
{
#if SIZEOF_LONG < _SIZEOF_PYTIME_T
if ((PyTime_t)LONG_MAX < t) {
assert((t >= 0 && t <= PyTime_MAX / SEC_TO_NS)
|| (t < 0 && t >= PyTime_MIN / SEC_TO_NS));
t *= SEC_TO_NS;
- return pytime_from_nanoseconds(t);
-}
-
-
-PyTime_t
-_PyTime_FromNanoseconds(PyTime_t ns)
-{
- return pytime_from_nanoseconds(ns);
+ return t;
}
_PyTime_FromMicrosecondsClamp(PyTime_t us)
{
PyTime_t ns = _PyTime_Mul(us, US_TO_NS);
- return pytime_from_nanoseconds(ns);
+ return ns;
}
int
-_PyTime_FromNanosecondsObject(PyTime_t *tp, PyObject *obj)
+_PyTime_FromLong(PyTime_t *tp, PyObject *obj)
{
-
if (!PyLong_Check(obj)) {
PyErr_Format(PyExc_TypeError, "expect int, got %s",
Py_TYPE(obj)->tp_name);
}
PyTime_t t = (PyTime_t)nsec;
- *tp = pytime_from_nanoseconds(t);
+ *tp = t;
return 0;
}
tv_nsec = ts->tv_nsec;
int res2 = pytime_add(&t, tv_nsec);
- *tp = pytime_from_nanoseconds(t);
+ *tp = t;
if (raise_exc && (res1 < 0 || res2 < 0)) {
pytime_overflow();
PyTime_t usec = (PyTime_t)tv->tv_usec * US_TO_NS;
int res2 = pytime_add(&t, usec);
- *tp = pytime_from_nanoseconds(t);
+ *tp = t;
if (raise_exc && (res1 < 0 || res2 < 0)) {
pytime_overflow();
}
PyTime_t ns = (PyTime_t)d;
- *tp = pytime_from_nanoseconds(ns);
+ *tp = ns;
return 0;
}
return -1;
}
- *tp = pytime_from_nanoseconds(ns);
+ *tp = ns;
return 0;
}
}
double
-PyTime_AsSecondsDouble(PyTime_t t)
+PyTime_AsSecondsDouble(PyTime_t ns)
{
/* volatile avoids optimization changing how numbers are rounded */
volatile double d;
- PyTime_t ns = pytime_as_nanoseconds(t);
if (ns % SEC_TO_NS == 0) {
/* Divide using integers to avoid rounding issues on the integer part.
1e-9 cannot be stored exactly in IEEE 64-bit. */
PyObject *
-_PyTime_AsNanosecondsObject(PyTime_t t)
+_PyTime_AsLong(PyTime_t ns)
{
- PyTime_t ns = pytime_as_nanoseconds(t);
static_assert(sizeof(long long) >= sizeof(PyTime_t),
"PyTime_t is larger than long long");
return PyLong_FromLongLong((long long)ns);
#ifdef MS_WINDOWS
PyTime_t
-_PyTime_As100Nanoseconds(PyTime_t t, _PyTime_round_t round)
+_PyTime_As100Nanoseconds(PyTime_t ns, _PyTime_round_t round)
{
- PyTime_t ns = pytime_as_nanoseconds(t);
return pytime_divide(ns, NS_TO_100NS, round);
}
#endif
PyTime_t
-_PyTime_AsMicroseconds(PyTime_t t, _PyTime_round_t round)
+_PyTime_AsMicroseconds(PyTime_t ns, _PyTime_round_t round)
{
- PyTime_t ns = pytime_as_nanoseconds(t);
return pytime_divide(ns, NS_TO_US, round);
}
PyTime_t
-_PyTime_AsMilliseconds(PyTime_t t, _PyTime_round_t round)
+_PyTime_AsMilliseconds(PyTime_t ns, _PyTime_round_t round)
{
- PyTime_t ns = pytime_as_nanoseconds(t);
return pytime_divide(ns, NS_TO_MS, round);
}
static int
-pytime_as_timeval(PyTime_t t, PyTime_t *ptv_sec, int *ptv_usec,
+pytime_as_timeval(PyTime_t ns, PyTime_t *ptv_sec, int *ptv_usec,
_PyTime_round_t round)
{
- PyTime_t ns = pytime_as_nanoseconds(t);
PyTime_t us = pytime_divide(ns, US_TO_NS, round);
PyTime_t tv_sec, tv_usec;
int res2;
#ifdef MS_WINDOWS
// On Windows, timeval.tv_sec type is long
- res2 = _PyTime_AsLong(tv_sec, &tv->tv_sec);
+ res2 = _PyTime_AsCLong(tv_sec, &tv->tv_sec);
#else
res2 = _PyTime_AsTime_t(tv_sec, &tv->tv_sec);
#endif
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
static int
-pytime_as_timespec(PyTime_t t, struct timespec *ts, int raise_exc)
+pytime_as_timespec(PyTime_t ns, struct timespec *ts, int raise_exc)
{
- PyTime_t ns = pytime_as_nanoseconds(t);
PyTime_t tv_sec, tv_nsec;
int res = pytime_divmod(ns, SEC_TO_NS, &tv_sec, &tv_nsec);
the 1st january 1601 and the 1st january 1970 (369 years + 89 leap
days). */
PyTime_t ns = large.QuadPart * 100 - 11644473600000000000;
- *tp = pytime_from_nanoseconds(ns);
+ *tp = ns;
if (info) {
DWORD timeAdjustment, timeIncrement;
BOOL isTimeAdjustmentDisabled, ok;
PyTime_t ticks = (PyTime_t)uticks;
PyTime_t ns = _PyTimeFraction_Mul(ticks, &base);
- *tp = pytime_from_nanoseconds(ns);
+ *tp = ns;
#elif defined(__hpux)
- hrtime_t time;
-
- time = gethrtime();
+ hrtime_t time = gethrtime();
if (time == -1) {
if (raise_exc) {
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
- *tp = pytime_from_nanoseconds(time);
+ *tp = time;
if (info) {
info->implementation = "gethrtime()";
ticks = (PyTime_t)ticksll;
PyTime_t ns = _PyTimeFraction_Mul(ticks, &base);
- *tp = pytime_from_nanoseconds(ns);
+ *tp = ns;
return 0;
}
#endif // MS_WINDOWS
// Define PY_TIMEOUT_MAX constant.
#ifdef _POSIX_THREADS
- // PyThread_acquire_lock_timed() uses _PyTime_FromNanoseconds(us * 1000),
- // convert microseconds to nanoseconds.
+ // PyThread_acquire_lock_timed() uses (us * 1000) to convert microseconds
+ // to nanoseconds.
# define PY_TIMEOUT_MAX_VALUE (LLONG_MAX / 1000)
#elif defined (NT_THREADS)
// WaitForSingleObject() accepts timeout in milliseconds in the range
}
} else if (milliseconds != 0) {
/* wait at least until the deadline */
- PyTime_t nanoseconds = _PyTime_FromNanoseconds((PyTime_t)milliseconds * 1000000);
+ PyTime_t nanoseconds = (PyTime_t)milliseconds * (1000 * 1000);
PyTime_t deadline = _PyTime_Add(_PyTime_PerfCounterUnchecked(), nanoseconds);
while (mutex->locked) {
PyTime_t microseconds = _PyTime_AsMicroseconds(nanoseconds,
timeout = _PyTime_FromMicrosecondsClamp(microseconds);
}
else {
- timeout = _PyTime_FromNanoseconds(-1);
+ timeout = -1;
}
#ifdef HAVE_SEM_CLOCKWAIT