|
| 1 | +# Copyright DataStax, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +import datetime |
| 17 | +from cassandra.cqltypes import DateType |
| 18 | +from cassandra.marshal import int64_pack |
| 19 | + |
| 20 | + |
| 21 | +class TimestampPrecisionTests(unittest.TestCase): |
| 22 | + """ |
| 23 | + Tests for timestamp precision with large values (far from epoch). |
| 24 | + See: https://github.com/scylladb/python-driver/issues/XXX |
| 25 | + """ |
| 26 | + |
| 27 | + def test_large_timestamp_roundtrip(self): |
| 28 | + """ |
| 29 | + Test that timestamps far from epoch (> 300 years) maintain precision |
| 30 | + through serialize/deserialize cycle. |
| 31 | + """ |
| 32 | + # Timestamp for "2300-01-01 00:00:00.001" in milliseconds |
| 33 | + # This is far enough from epoch that float precision is lost |
| 34 | + original_ms = 10413792000001 # 2300-01-01 00:00:00.001 |
| 35 | + |
| 36 | + # Pack as int64 (simulating database storage) |
| 37 | + packed = int64_pack(original_ms) |
| 38 | + |
| 39 | + # Deserialize back |
| 40 | + dt = DateType.deserialize(packed, 0) |
| 41 | + |
| 42 | + # Serialize again |
| 43 | + repacked = DateType.serialize(dt, 0) |
| 44 | + |
| 45 | + # Unpack and compare |
| 46 | + from cassandra.marshal import int64_unpack |
| 47 | + result_ms = int64_unpack(repacked) |
| 48 | + |
| 49 | + # Should be exactly equal |
| 50 | + assert result_ms == original_ms, \ |
| 51 | + f"Expected {original_ms}, got {result_ms}, difference: {result_ms - original_ms}" |
| 52 | + |
| 53 | + def test_year_2300_timestamp_precision(self): |
| 54 | + """ |
| 55 | + Test the specific case from the issue report: |
| 56 | + timestamp "2300-01-01 00:00:00.001" should maintain precision. |
| 57 | + """ |
| 58 | + # Create datetime for 2300-01-01 00:00:00.001 |
| 59 | + dt = datetime.datetime(2300, 1, 1, 0, 0, 0, 1000) # 1000 microseconds = 1 millisecond |
| 60 | + |
| 61 | + # Serialize to bytes |
| 62 | + packed = DateType.serialize(dt, 0) |
| 63 | + |
| 64 | + # Deserialize back |
| 65 | + dt_restored = DateType.deserialize(packed, 0) |
| 66 | + |
| 67 | + # Serialize again |
| 68 | + repacked = DateType.serialize(dt_restored, 0) |
| 69 | + |
| 70 | + # They should be exactly equal |
| 71 | + assert packed == repacked, \ |
| 72 | + f"Serialization not stable: {packed.hex()} != {repacked.hex()}" |
| 73 | + |
| 74 | + # The microseconds should be preserved |
| 75 | + assert dt_restored.microsecond == 1000, \ |
| 76 | + f"Expected 1000 microseconds, got {dt_restored.microsecond}" |
| 77 | + |
| 78 | + def test_various_large_timestamps(self): |
| 79 | + """ |
| 80 | + Test multiple timestamps far from epoch to ensure precision is maintained. |
| 81 | + """ |
| 82 | + # Various timestamps > 300 years from epoch (in milliseconds) |
| 83 | + test_timestamps_ms = [ |
| 84 | + 10413792000001, # 2300-01-01 00:00:00.001 |
| 85 | + 10413792000999, # 2300-01-01 00:00:00.999 |
| 86 | + 15768000000000, # 2469-12-31 12:00:00.000 |
| 87 | + 20000000000001, # ~2603 with millisecond precision |
| 88 | + -10413792000001, # ~1640 BCE |
| 89 | + ] |
| 90 | + |
| 91 | + for original_ms in test_timestamps_ms: |
| 92 | + with self.subTest(timestamp_ms=original_ms): |
| 93 | + # Pack as int64 |
| 94 | + packed = int64_pack(original_ms) |
| 95 | + |
| 96 | + # Deserialize |
| 97 | + dt = DateType.deserialize(packed, 0) |
| 98 | + |
| 99 | + # Serialize again |
| 100 | + repacked = DateType.serialize(dt, 0) |
| 101 | + |
| 102 | + # Unpack and compare |
| 103 | + from cassandra.marshal import int64_unpack |
| 104 | + result_ms = int64_unpack(repacked) |
| 105 | + |
| 106 | + # Should be exactly equal |
| 107 | + assert result_ms == original_ms, \ |
| 108 | + f"Expected {original_ms}, got {result_ms}, difference: {result_ms - original_ms}" |
| 109 | + |
| 110 | + def test_small_timestamp_still_works(self): |
| 111 | + """ |
| 112 | + Ensure that timestamps close to epoch still work correctly. |
| 113 | + """ |
| 114 | + # Timestamp close to epoch (well within float precision) |
| 115 | + original_ms = 1000000000000 # 2001-09-09 01:46:40.000 |
| 116 | + |
| 117 | + packed = int64_pack(original_ms) |
| 118 | + dt = DateType.deserialize(packed, 0) |
| 119 | + repacked = DateType.serialize(dt, 0) |
| 120 | + |
| 121 | + from cassandra.marshal import int64_unpack |
| 122 | + result_ms = int64_unpack(repacked) |
| 123 | + |
| 124 | + assert result_ms == original_ms |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == '__main__': |
| 128 | + unittest.main() |
0 commit comments