summaryrefslogtreecommitdiffstats
path: root/Python/pytime.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-09-09 20:32:48 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2015-09-09 20:32:48 (GMT)
commit3e2c8d84c69063b8042a75ba04b5e75b1de0704e (patch)
tree0b76315fb3c840687934abb5b40ecf9a92dbeaac /Python/pytime.c
parent9ae47dfbd9b2f094205faad758b32803bea8e463 (diff)
downloadcpython-3e2c8d84c69063b8042a75ba04b5e75b1de0704e.zip
cpython-3e2c8d84c69063b8042a75ba04b5e75b1de0704e.tar.gz
cpython-3e2c8d84c69063b8042a75ba04b5e75b1de0704e.tar.bz2
test_time: rewrite PyTime API rounding tests
Drop all hardcoded tests. Instead, reimplement each function in Python, usually using decimal.Decimal for the rounding mode. Add much more values to the dataset. Test various timestamp units from picroseconds to seconds, in integer and float. Enhance also _PyTime_AsSecondsDouble().
Diffstat (limited to 'Python/pytime.c')
-rw-r--r--Python/pytime.c16
1 files changed, 10 insertions, 6 deletions
diff --git a/Python/pytime.c b/Python/pytime.c
index 9735506..4c940c9 100644
--- a/Python/pytime.c
+++ b/Python/pytime.c
@@ -324,12 +324,16 @@ _PyTime_FromMillisecondsObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t roun
double
_PyTime_AsSecondsDouble(_PyTime_t t)
{
- _PyTime_t sec, ns;
- /* Divide using integers to avoid rounding issues on the integer part.
- 1e-9 cannot be stored exactly in IEEE 64-bit. */
- sec = t / SEC_TO_NS;
- ns = t % SEC_TO_NS;
- return (double)sec + (double)ns * 1e-9;
+ if (t % SEC_TO_NS == 0) {
+ _PyTime_t secs;
+ /* Divide using integers to avoid rounding issues on the integer part.
+ 1e-9 cannot be stored exactly in IEEE 64-bit. */
+ secs = t / SEC_TO_NS;
+ return (double)secs;
+ }
+ else {
+ return (double)t / 1e9;
+ }
}
PyObject *