Merge pull request #525 from datastax/480
PYTHON-480 - cython datetime rounding
This commit is contained in:
@@ -17,6 +17,8 @@ Duplicate module of util.py, with some accelerated functions
|
|||||||
used for deserialization.
|
used for deserialization.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from libc.math cimport modf, round, fabs
|
||||||
|
|
||||||
from cpython.datetime cimport (
|
from cpython.datetime cimport (
|
||||||
timedelta_new,
|
timedelta_new,
|
||||||
# cdef inline object timedelta_new(int days, int seconds, int useconds)
|
# cdef inline object timedelta_new(int days, int seconds, int useconds)
|
||||||
@@ -44,6 +46,17 @@ cdef datetime_from_timestamp(double timestamp):
|
|||||||
cdef int days = <int> (timestamp / DAY_IN_SECONDS)
|
cdef int days = <int> (timestamp / DAY_IN_SECONDS)
|
||||||
cdef int64_t days_in_seconds = (<int64_t> days) * DAY_IN_SECONDS
|
cdef int64_t days_in_seconds = (<int64_t> days) * DAY_IN_SECONDS
|
||||||
cdef int seconds = <int> (timestamp - days_in_seconds)
|
cdef int seconds = <int> (timestamp - days_in_seconds)
|
||||||
cdef int microseconds = <int> ((timestamp - days_in_seconds - seconds) * 1000000)
|
cdef double tmp
|
||||||
|
cdef double micros_left = modf(timestamp, &tmp) * 1000000.
|
||||||
|
micros_left = modf(micros_left, &tmp)
|
||||||
|
cdef int microseconds = <int> tmp
|
||||||
|
|
||||||
|
# rounding to emulate fp math in delta_new
|
||||||
|
cdef int x_odd
|
||||||
|
tmp = round(micros_left)
|
||||||
|
if fabs(tmp - micros_left) == 0.5:
|
||||||
|
x_odd = microseconds & 1
|
||||||
|
tmp = 2.0 * round((micros_left + x_odd) * 0.5) - x_odd
|
||||||
|
microseconds += <int>tmp
|
||||||
|
|
||||||
return DATETIME_EPOC + timedelta_new(days, seconds, microseconds)
|
return DATETIME_EPOC + timedelta_new(days, seconds, microseconds)
|
||||||
|
|||||||
@@ -26,3 +26,7 @@ class TypesTest(unittest.TestCase):
|
|||||||
@cythontest
|
@cythontest
|
||||||
def test_datetype(self):
|
def test_datetype(self):
|
||||||
types_testhelper.test_datetype(self.assertEqual)
|
types_testhelper.test_datetype(self.assertEqual)
|
||||||
|
|
||||||
|
@cythontest
|
||||||
|
def test_date_side_by_side(self):
|
||||||
|
types_testhelper.test_date_side_by_side(self.assertEqual)
|
||||||
|
|||||||
@@ -12,9 +12,9 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import calendar
|
||||||
import time
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import time
|
||||||
|
|
||||||
include '../../../cassandra/ioutils.pyx'
|
include '../../../cassandra/ioutils.pyx'
|
||||||
|
|
||||||
@@ -70,3 +70,39 @@ def test_datetype(assert_equal):
|
|||||||
# Large date overflow (PYTHON-452)
|
# Large date overflow (PYTHON-452)
|
||||||
expected = 2177403010.123
|
expected = 2177403010.123
|
||||||
assert_equal(deserialize(expected), datetime.datetime(2038, 12, 31, 10, 10, 10, 123000))
|
assert_equal(deserialize(expected), datetime.datetime(2038, 12, 31, 10, 10, 10, 123000))
|
||||||
|
|
||||||
|
|
||||||
|
def test_date_side_by_side(assert_equal):
|
||||||
|
# Test pure python and cython date deserialization side-by-side
|
||||||
|
# This is meant to detect inconsistent rounding or conversion (PYTHON-480 for example)
|
||||||
|
# The test covers the full range of time deserializable in Python. It bounds through
|
||||||
|
# the range in factors of two to cover floating point scale. At each bound it sweeps
|
||||||
|
# all combinations of fractional seconds to verify rounding
|
||||||
|
|
||||||
|
cdef BytesIOReader reader
|
||||||
|
cdef Buffer buf
|
||||||
|
cdef Deserializer cython_deserializer = find_deserializer(DateType)
|
||||||
|
import time
|
||||||
|
|
||||||
|
def verify_time(ms):
|
||||||
|
blob = DateType.serialize(ms, 0)
|
||||||
|
bior = BytesIOReader(blob)
|
||||||
|
buf.ptr = bior.read()
|
||||||
|
buf.size = bior.size
|
||||||
|
cython_deserialized = from_binary(cython_deserializer, &buf, 0)
|
||||||
|
python_deserialized = DateType.deserialize(blob, 0)
|
||||||
|
assert_equal(cython_deserialized, python_deserialized)
|
||||||
|
|
||||||
|
# min -> 0
|
||||||
|
x = int(calendar.timegm(datetime.datetime(1, 1, 1).utctimetuple()) * 1000)
|
||||||
|
while x < -1: # for some reason -1 // 2 == -1 so we can't wait for zero
|
||||||
|
for y in range(1000):
|
||||||
|
verify_time(x + y)
|
||||||
|
x //= 2
|
||||||
|
|
||||||
|
# max -> 0
|
||||||
|
x = int(calendar.timegm(datetime.datetime(9999, 12, 31, 23, 59, 59, 999999).utctimetuple()) * 1000)
|
||||||
|
while x:
|
||||||
|
for ms in range(1000):
|
||||||
|
verify_time(x - ms)
|
||||||
|
x //= 2
|
||||||
|
|||||||
Reference in New Issue
Block a user