summaryrefslogtreecommitdiffstats
path: root/Modules/timemodule.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2012-02-08 22:03:19 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2012-02-08 22:03:19 (GMT)
commit4195b5caea0fe1446160e78d69420732ead7e78b (patch)
tree5c01264aed26942fcb932f9aab73856dd65af684 /Modules/timemodule.c
parent72476eae5fe4d7bc4d4c4e30471cd83a501707de (diff)
downloadcpython-4195b5caea0fe1446160e78d69420732ead7e78b.zip
cpython-4195b5caea0fe1446160e78d69420732ead7e78b.tar.gz
cpython-4195b5caea0fe1446160e78d69420732ead7e78b.tar.bz2
Backout f8409b3d6449: the PEP 410 is not accepted yet
Diffstat (limited to 'Modules/timemodule.c')
-rw-r--r--Modules/timemodule.c241
1 files changed, 74 insertions, 167 deletions
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index 16940fe..34c7019 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -40,30 +40,24 @@
#include <sys/time.h>
#endif
-#if (defined(MS_WINDOWS) && !defined(__BORLANDC__)) || defined(HAVE_CLOCK)
-# define HAVE_PYCLOCK
-#endif
-
/* Forward declarations */
static int floatsleep(double);
+static double floattime(void);
static PyObject *
-time_time(PyObject *self, PyObject *args, PyObject *kwargs)
+time_time(PyObject *self, PyObject *unused)
{
- static char *kwlist[] = {"timestamp", NULL};
- PyObject *timestamp = NULL;
- _PyTime_t ts;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:time", kwlist,
- &timestamp))
+ double secs;
+ secs = floattime();
+ if (secs == 0.0) {
+ PyErr_SetFromErrno(PyExc_IOError);
return NULL;
-
- _PyTime_get(&ts);
- return _PyTime_Convert(&ts, timestamp);
+ }
+ return PyFloat_FromDouble(secs);
}
PyDoc_STRVAR(time_doc,
-"time(timestamp=float) -> floating point number\n\
+"time() -> floating point number\n\
\n\
Return the current time in seconds since the Epoch.\n\
Fractions of a second may be present if the system clock provides them.");
@@ -78,91 +72,65 @@ Fractions of a second may be present if the system clock provides them.");
#endif
#endif
-static int
-pyclock(_PyTime_t *ts)
+static PyObject *
+pyclock(void)
{
- clock_t processor_time;
- processor_time = clock();
- if (processor_time == (clock_t)-1) {
+ clock_t value;
+ value = clock();
+ if (value == (clock_t)-1) {
PyErr_SetString(PyExc_RuntimeError,
"the processor time used is not available "
"or its value cannot be represented");
- return -1;
+ return NULL;
}
- ts->seconds = 0;
- assert(sizeof(clock_t) <= sizeof(_PyTime_fraction_t));
- ts->numerator = Py_SAFE_DOWNCAST(processor_time,
- clock_t, _PyTime_fraction_t);
- ts->denominator = CLOCKS_PER_SEC;
- return 0;
+ return PyFloat_FromDouble((double)value / CLOCKS_PER_SEC);
}
#endif /* HAVE_CLOCK */
#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
/* Win32 has better clock replacement; we have our own version, due to Mark
Hammond and Tim Peters */
-static int
-win32_clock(_PyTime_t *ts, int fallback)
+static PyObject *
+win32_clock(int fallback)
{
static LONGLONG cpu_frequency = 0;
- static LONGLONG start;
+ static LONGLONG ctrStart;
LARGE_INTEGER now;
- LONGLONG dt;
+ double diff;
if (cpu_frequency == 0) {
LARGE_INTEGER freq;
QueryPerformanceCounter(&now);
- start = now.QuadPart;
+ ctrStart = now.QuadPart;
if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
/* Unlikely to happen - this works on all intel
machines at least! Revert to clock() */
- if (fallback) {
- return pyclock(ts);
- }
- else {
- PyErr_SetFromWindowsErr(0);
- return -1;
- }
+ if (fallback)
+ return pyclock();
+ else
+ return PyErr_SetFromWindowsErr(0);
}
cpu_frequency = freq.QuadPart;
}
QueryPerformanceCounter(&now);
- dt = now.QuadPart - start;
-
- ts->seconds = 0;
- assert(sizeof(LONGLONG) <= sizeof(_PyTime_fraction_t));
- ts->numerator = Py_SAFE_DOWNCAST(dt,
- LONGLONG, _PyTime_fraction_t);
- ts->denominator = Py_SAFE_DOWNCAST(cpu_frequency,
- LONGLONG, _PyTime_fraction_t);
- return 0;
+ diff = (double)(now.QuadPart - ctrStart);
+ return PyFloat_FromDouble(diff / (double)cpu_frequency);
}
#endif
#if (defined(MS_WINDOWS) && !defined(__BORLANDC__)) || defined(HAVE_CLOCK)
static PyObject *
-time_clock(PyObject *self, PyObject *args, PyObject *kwargs)
+time_clock(PyObject *self, PyObject *unused)
{
- static char *kwlist[] = {"timestamp", NULL};
- _PyTime_t ts;
- PyObject *timestamp = NULL;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:clock", kwlist,
- &timestamp))
- return NULL;
-
#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
- if (win32_clock(&ts, 1) == -1)
- return NULL;
+ return win32_clock(1);
#else
- if (pyclock(&ts) == -1)
- return NULL;
+ return pyclock();
#endif
- return _PyTime_Convert(&ts, timestamp);
}
PyDoc_STRVAR(clock_doc,
-"clock(timestamp=float) -> floating point number\n\
+"clock() -> floating point number\n\
\n\
Return the CPU time or real time since the start of the process or since\n\
the first call to clock(). This has as much precision as the system\n\
@@ -171,17 +139,13 @@ records.");
#ifdef HAVE_CLOCK_GETTIME
static PyObject *
-time_clock_gettime(PyObject *self, PyObject *args, PyObject *kwargs)
+time_clock_gettime(PyObject *self, PyObject *args)
{
- static char *kwlist[] = {"clk_id", "timestamp", NULL};
- PyObject *timestamp = NULL;
int ret;
clockid_t clk_id;
struct timespec tp;
- _PyTime_t ts;
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|O:clock_gettime", kwlist,
- &clk_id, &timestamp))
+ if (!PyArg_ParseTuple(args, "i:clock_gettime", &clk_id))
return NULL;
ret = clock_gettime((clockid_t)clk_id, &tp);
@@ -189,31 +153,25 @@ time_clock_gettime(PyObject *self, PyObject *args, PyObject *kwargs)
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
- ts.seconds = tp.tv_sec;
- ts.numerator = tp.tv_nsec;
- ts.denominator = 1000000000;
- return _PyTime_Convert(&ts, timestamp);
+
+ return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
}
PyDoc_STRVAR(clock_gettime_doc,
-"clock_gettime(clk_id, timestamp=float) -> floating point number\n\
+"clock_gettime(clk_id) -> floating point number\n\
\n\
Return the time of the specified clock clk_id.");
#endif
#ifdef HAVE_CLOCK_GETRES
static PyObject *
-time_clock_getres(PyObject *self, PyObject *args, PyObject *kwargs)
+time_clock_getres(PyObject *self, PyObject *args)
{
- static char *kwlist[] = {"clk_id", "timestamp", NULL};
- PyObject *timestamp = NULL;
int ret;
clockid_t clk_id;
struct timespec tp;
- _PyTime_t ts;
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|O:clock_getres", kwlist,
- &clk_id, &timestamp))
+ if (!PyArg_ParseTuple(args, "i:clock_getres", &clk_id))
return NULL;
ret = clock_getres((clockid_t)clk_id, &tp);
@@ -221,14 +179,12 @@ time_clock_getres(PyObject *self, PyObject *args, PyObject *kwargs)
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
- ts.seconds = tp.tv_sec;
- ts.numerator = tp.tv_nsec;
- ts.denominator = 1000000000;
- return _PyTime_Convert(&ts, timestamp);
+
+ return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
}
PyDoc_STRVAR(clock_getres_doc,
-"clock_getres(clk_id, timestamp=float) -> floating point number\n\
+"clock_getres(clk_id) -> floating point number\n\
\n\
Return the resolution (precision) of the specified clock clk_id.");
#endif
@@ -751,19 +707,10 @@ not present, current time as returned by localtime() is used.");
#ifdef HAVE_MKTIME
static PyObject *
-time_mktime(PyObject *self, PyObject *args, PyObject *kwargs)
+time_mktime(PyObject *self, PyObject *tup)
{
- static char *kwlist[] = {"t", "timestamp", NULL};
- PyObject *timestamp = NULL;
- PyObject *tup;
struct tm buf;
time_t tt;
- _PyTime_t ts;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O:mktime", kwlist,
- &tup, &timestamp))
- return NULL;
-
if (!gettmarg(tup, &buf))
return NULL;
buf.tm_wday = -1; /* sentinel; original value ignored */
@@ -775,10 +722,7 @@ time_mktime(PyObject *self, PyObject *args, PyObject *kwargs)
"mktime argument out of range");
return NULL;
}
- ts.seconds = tt;
- ts.numerator = 0;
- ts.denominator = 1;
- return _PyTime_Convert(&ts, timestamp);
+ return PyFloat_FromDouble((double)tt);
}
PyDoc_STRVAR(mktime_doc,
@@ -824,14 +768,12 @@ the local timezone used by methods such as localtime, but this behaviour\n\
should not be relied on.");
#endif /* HAVE_WORKING_TZSET */
-static int
-pywallclock(_PyTime_t *ts)
+static PyObject *
+time_wallclock(PyObject *self, PyObject *unused)
{
#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
- return win32_clock(ts, 1);
-#else
-
-#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
+ return win32_clock(1);
+#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
static int clk_index = 0;
clockid_t clk_ids[] = {
#ifdef CLOCK_MONOTONIC_RAW
@@ -851,41 +793,20 @@ pywallclock(_PyTime_t *ts)
clockid_t clk_id = clk_ids[clk_index];
ret = clock_gettime(clk_id, &tp);
if (ret == 0)
- {
- ts->seconds = tp.tv_sec;
- ts->numerator = tp.tv_nsec;
- ts->denominator = 1000000000;
- return 0;
- }
+ return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
clk_index++;
if (Py_ARRAY_LENGTH(clk_ids) <= clk_index)
clk_index = -1;
}
-#endif /* defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) */
-
- _PyTime_get(ts);
- return 0;
+ return time_time(self, NULL);
+#else
+ return time_time(self, NULL);
#endif
}
-static PyObject *
-time_wallclock(PyObject *self, PyObject *args, PyObject *kwargs)
-{
- static char *kwlist[] = {"timestamp", NULL};
- PyObject *timestamp = NULL;
- _PyTime_t ts;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:wallclock", kwlist,
- &timestamp))
- return NULL;
- if (pywallclock(&ts))
- return NULL;
- return _PyTime_Convert(&ts, timestamp);
-}
-
PyDoc_STRVAR(wallclock_doc,
-"wallclock(timestamp=float)\n\
+"wallclock() -> float\n\
\n\
Return the current time in fractions of a second to the system's best\n\
ability. Use this when the most accurate representation of wall-clock is\n\
@@ -900,11 +821,11 @@ calls is valid.");
#ifdef HAVE_PYTIME_MONOTONIC
static PyObject *
-time_monotonic(PyObject *self, PyObject *args, PyObject *kwargs)
+time_monotonic(PyObject *self, PyObject *unused)
{
- static char *kwlist[] = {"timestamp", NULL};
- PyObject *timestamp = NULL;
-#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
+#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
+ return win32_clock(0);
+#else
static int clk_index = 0;
clockid_t clk_ids[] = {
#ifdef CLOCK_MONOTONIC_RAW
@@ -914,28 +835,12 @@ time_monotonic(PyObject *self, PyObject *args, PyObject *kwargs)
};
int ret;
struct timespec tp;
-#endif
- _PyTime_t ts;
-
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:monotonic", kwlist,
- &timestamp))
- return NULL;
-#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
- if (win32_clock(&ts, 0) == -1)
- return NULL;
- return _PyTime_Convert(&ts, timestamp);
-#else
while (0 <= clk_index) {
clockid_t clk_id = clk_ids[clk_index];
ret = clock_gettime(clk_id, &tp);
if (ret == 0)
- {
- ts.seconds = tp.tv_sec;
- ts.numerator = tp.tv_nsec;
- ts.denominator = 1000000000;
- return _PyTime_Convert(&ts, timestamp);
- }
+ return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
clk_index++;
if (Py_ARRAY_LENGTH(clk_ids) <= clk_index)
@@ -1063,19 +968,15 @@ PyInit_timezone(PyObject *m) {
static PyMethodDef time_methods[] = {
- {"time", (PyCFunction)time_time,
- METH_VARARGS | METH_KEYWORDS, time_doc},
-#ifdef HAVE_PYCLOCK
- {"clock", (PyCFunction)time_clock,
- METH_VARARGS | METH_KEYWORDS, clock_doc},
+ {"time", time_time, METH_NOARGS, time_doc},
+#if (defined(MS_WINDOWS) && !defined(__BORLANDC__)) || defined(HAVE_CLOCK)
+ {"clock", time_clock, METH_NOARGS, clock_doc},
#endif
#ifdef HAVE_CLOCK_GETTIME
- {"clock_gettime", (PyCFunction)time_clock_gettime,
- METH_VARARGS | METH_KEYWORDS, clock_gettime_doc},
+ {"clock_gettime", time_clock_gettime, METH_VARARGS, clock_gettime_doc},
#endif
#ifdef HAVE_CLOCK_GETRES
- {"clock_getres", (PyCFunction)time_clock_getres,
- METH_VARARGS | METH_KEYWORDS, clock_getres_doc},
+ {"clock_getres", time_clock_getres, METH_VARARGS, clock_getres_doc},
#endif
{"sleep", time_sleep, METH_VARARGS, sleep_doc},
{"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
@@ -1083,12 +984,10 @@ static PyMethodDef time_methods[] = {
{"asctime", time_asctime, METH_VARARGS, asctime_doc},
{"ctime", time_ctime, METH_VARARGS, ctime_doc},
#ifdef HAVE_MKTIME
- {"mktime", (PyCFunction)time_mktime,
- METH_VARARGS | METH_KEYWORDS, mktime_doc},
+ {"mktime", time_mktime, METH_O, mktime_doc},
#endif
#ifdef HAVE_PYTIME_MONOTONIC
- {"monotonic", (PyCFunction)time_monotonic,
- METH_VARARGS | METH_KEYWORDS, monotonic_doc},
+ {"monotonic", time_monotonic, METH_NOARGS, monotonic_doc},
#endif
#ifdef HAVE_STRFTIME
{"strftime", time_strftime, METH_VARARGS, strftime_doc},
@@ -1097,8 +996,7 @@ static PyMethodDef time_methods[] = {
#ifdef HAVE_WORKING_TZSET
{"tzset", time_tzset, METH_NOARGS, tzset_doc},
#endif
- {"wallclock", (PyCFunction)time_wallclock,
- METH_VARARGS | METH_KEYWORDS, wallclock_doc},
+ {"wallclock", time_wallclock, METH_NOARGS, wallclock_doc},
{NULL, NULL} /* sentinel */
};
@@ -1183,6 +1081,15 @@ PyInit_time(void)
return m;
}
+static double
+floattime(void)
+{
+ _PyTime_timeval t;
+ _PyTime_gettimeofday(&t);
+ return (double)t.tv_sec + t.tv_usec*0.000001;
+}
+
+
/* Implement floatsleep() for various platforms.
When interrupted (or when another error occurs), return -1 and
set an exception; else return 0. */