Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1bd18ba commit f81f0f9Copy full SHA for f81f0f9
2 files changed
Lib/test/test_time.py
@@ -647,13 +647,13 @@ def test_timespec(self):
647
(1e-9, (0, 1), _PyTime.ROUND_DOWN),
648
(1e-10, (0, 0), _PyTime.ROUND_DOWN),
649
(-1e-9, (-1, 999999999), _PyTime.ROUND_DOWN),
650
- (-1e-10, (-1, 999999999), _PyTime.ROUND_DOWN),
+ (-1e-10, (0, 0), _PyTime.ROUND_DOWN),
651
(-1.2, (-2, 800000000), _PyTime.ROUND_DOWN),
652
(0.9999999999, (0, 999999999), _PyTime.ROUND_DOWN),
653
(1.1234567890, (1, 123456789), _PyTime.ROUND_DOWN),
654
(1.1234567899, (1, 123456789), _PyTime.ROUND_DOWN),
655
(-1.1234567890, (-2, 876543211), _PyTime.ROUND_DOWN),
656
- (-1.1234567891, (-2, 876543210), _PyTime.ROUND_DOWN),
+ (-1.1234567891, (-2, 876543211), _PyTime.ROUND_DOWN),
657
# Round away from zero
658
(0, (0, 0), _PyTime.ROUND_UP),
659
(-1, (-1, 0), _PyTime.ROUND_UP),
Python/pytime.c
@@ -26,6 +26,14 @@ error_time_t_overflow(void)
26
"timestamp out of range for platform time_t");
27
}
28
29
+static int
30
+_PyTime_RoundTowardsPosInf(int is_neg, _PyTime_round_t round)
31
+{
32
+ if (round == _PyTime_ROUND_FLOOR)
33
+ return 0;
34
+ return ((round == _PyTime_ROUND_UP) ^ is_neg);
35
+}
36
+
37
time_t
38
_PyLong_AsTime_t(PyObject *obj)
39
{
@@ -74,18 +82,16 @@ _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
74
82
75
83
76
84
floatpart *= denominator;
77
- if (round == _PyTime_ROUND_UP) {
78
- if (intpart >= 0) {
79
- floatpart = ceil(floatpart);
80
- if (floatpart >= denominator) {
81
- floatpart = 0.0;
- intpart += 1.0;
- }
85
- else {
86
- floatpart = floor(floatpart);
+ if (_PyTime_RoundTowardsPosInf(intpart < 0, round)) {
+ floatpart = ceil(floatpart);
87
+ if (floatpart >= denominator) {
88
+ floatpart = 0.0;
89
+ intpart += 1.0;
90
91
92
+ else {
93
+ floatpart = floor(floatpart);
94
+ }
95
96
*sec = (time_t)intpart;
97
err = intpart - (double)*sec;
@@ -113,12 +119,10 @@ _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
113
119
double d, intpart, err;
114
120
115
121
d = PyFloat_AsDouble(obj);
116
117
- if (d >= 0)
118
- d = ceil(d);
- else
- d = floor(d);
122
+ if (_PyTime_RoundTowardsPosInf(d < 0, round))
123
+ d = ceil(d);
124
+ else
125
+ d = floor(d);
126
(void)modf(d, &intpart);
127
128
@@ -158,14 +162,6 @@ _PyTime_overflow(void)
158
162
"timestamp too large to convert to C _PyTime_t");
159
163
160
164
161
-int
-_PyTime_RoundTowardsPosInf(int is_neg, _PyTime_round_t round)
-{
- if (round == _PyTime_ROUND_FLOOR)
165
- return 0;
166
- return ((round == _PyTime_ROUND_UP) ^ is_neg);
167
-}
168
-
169
_PyTime_t
170
_PyTime_FromNanoseconds(PY_LONG_LONG ns)
171
0 commit comments