Debian Codegraph @

test/test_timestamp.py

raw
1 import datetime
2
3 import pytest
4
5 import msgpack
6 from msgpack.ext import Timestamp
7
8
9 def test_timestamp():
10 # timestamp32
11 ts = Timestamp(2**32 - 1)
12 assert ts.to_bytes() == b"\xff\xff\xff\xff"
13 packed = msgpack.packb(ts)
14 assert packed == b"\xd6\xff" + ts.to_bytes()
15 unpacked = msgpack.unpackb(packed)
16 assert ts == unpacked
17 assert ts.seconds == 2**32 - 1 and ts.nanoseconds == 0
18
19 # timestamp64
20 ts = Timestamp(2**34 - 1, 999999999)
21 assert ts.to_bytes() == b"\xee\x6b\x27\xff\xff\xff\xff\xff"
22 packed = msgpack.packb(ts)
23 assert packed == b"\xd7\xff" + ts.to_bytes()
24 unpacked = msgpack.unpackb(packed)
25 assert ts == unpacked
26 assert ts.seconds == 2**34 - 1 and ts.nanoseconds == 999999999
27
28 # timestamp96
29 ts = Timestamp(2**63 - 1, 999999999)
30 assert ts.to_bytes() == b"\x3b\x9a\xc9\xff\x7f\xff\xff\xff\xff\xff\xff\xff"
31 packed = msgpack.packb(ts)
32 assert packed == b"\xc7\x0c\xff" + ts.to_bytes()
33 unpacked = msgpack.unpackb(packed)
34 assert ts == unpacked
35 assert ts.seconds == 2**63 - 1 and ts.nanoseconds == 999999999
36
37 # negative fractional
38 ts = Timestamp.from_unix(-2.3) # s: -3, ns: 700000000
39 assert ts.seconds == -3 and ts.nanoseconds == 700000000
40 assert ts.to_bytes() == b"\x29\xb9\x27\x00\xff\xff\xff\xff\xff\xff\xff\xfd"
41 packed = msgpack.packb(ts)
42 assert packed == b"\xc7\x0c\xff" + ts.to_bytes()
43 unpacked = msgpack.unpackb(packed)
44 assert ts == unpacked
45
46
47 def test_unpack_timestamp():
48 # timestamp 32
49 assert msgpack.unpackb(b"\xd6\xff\x00\x00\x00\x00") == Timestamp(0)
50
51 # timestamp 64
52 assert msgpack.unpackb(b"\xd7\xff" + b"\x00" * 8) == Timestamp(0)
53 with pytest.raises(ValueError):
54 msgpack.unpackb(b"\xd7\xff" + b"\xff" * 8)
55
56 # timestamp 96
57 assert msgpack.unpackb(b"\xc7\x0c\xff" + b"\x00" * 12) == Timestamp(0)
58 with pytest.raises(ValueError):
59 msgpack.unpackb(b"\xc7\x0c\xff" + b"\xff" * 12) == Timestamp(0)
60
61 # Undefined
62 with pytest.raises(ValueError):
63 msgpack.unpackb(b"\xd4\xff\x00") # fixext 1
64 with pytest.raises(ValueError):
65 msgpack.unpackb(b"\xd5\xff\x00\x00") # fixext 2
66 with pytest.raises(ValueError):
67 msgpack.unpackb(b"\xc7\x00\xff") # ext8 (len=0)
68 with pytest.raises(ValueError):
69 msgpack.unpackb(b"\xc7\x03\xff\0\0\0") # ext8 (len=3)
70 with pytest.raises(ValueError):
71 msgpack.unpackb(b"\xc7\x05\xff\0\0\0\0\0") # ext8 (len=5)
72
73
74 def test_timestamp_from():
75 t = Timestamp(42, 14000)
76 assert Timestamp.from_unix(42.000014) == t
77 assert Timestamp.from_unix_nano(42000014000) == t
78
79
80 def test_timestamp_to():
81 t = Timestamp(42, 14000)
82 assert t.to_unix() == 42.000014
83 assert t.to_unix_nano() == 42000014000
84
85
86 def test_timestamp_datetime():
87 t = Timestamp(42, 14)
88 utc = datetime.timezone.utc
89 assert t.to_datetime() == datetime.datetime(1970, 1, 1, 0, 0, 42, 0, tzinfo=utc)
90
91 ts = datetime.datetime(2024, 4, 16, 8, 43, 9, 420317, tzinfo=utc)
92 ts2 = datetime.datetime(2024, 4, 16, 8, 43, 9, 420318, tzinfo=utc)
93
94 assert (
95 Timestamp.from_datetime(ts2).nanoseconds - Timestamp.from_datetime(ts).nanoseconds == 1000
96 )
97
98 ts3 = datetime.datetime(2024, 4, 16, 8, 43, 9, 4256)
99 ts4 = datetime.datetime(2024, 4, 16, 8, 43, 9, 4257)
100 assert (
101 Timestamp.from_datetime(ts4).nanoseconds - Timestamp.from_datetime(ts3).nanoseconds == 1000
102 )
103
104 assert Timestamp.from_datetime(ts).to_datetime() == ts
105
106 # Regression test: pre-epoch fractional seconds must floor toward -inf.
107 pre_epoch = datetime.datetime(1969, 12, 31, 23, 59, 59, 500000, tzinfo=utc)
108 ts_pre_epoch = Timestamp.from_datetime(pre_epoch)
109 assert ts_pre_epoch.seconds == -1
110 assert ts_pre_epoch.nanoseconds == 500000000
111 assert ts_pre_epoch.to_datetime() == pre_epoch
112
113
114 def test_unpack_datetime():
115 t = Timestamp(42, 14)
116 utc = datetime.timezone.utc
117 packed = msgpack.packb(t)
118 unpacked = msgpack.unpackb(packed, timestamp=3)
119 assert unpacked == datetime.datetime(1970, 1, 1, 0, 0, 42, 0, tzinfo=utc)
120
121
122 def test_pack_unpack_before_epoch():
123 utc = datetime.timezone.utc
124 t_in = datetime.datetime(1960, 1, 1, tzinfo=utc)
125 packed = msgpack.packb(t_in, datetime=True)
126 unpacked = msgpack.unpackb(packed, timestamp=3)
127 assert unpacked == t_in
128
129
130 def test_pack_datetime():
131 t = Timestamp(42, 14000)
132 dt = t.to_datetime()
133 utc = datetime.timezone.utc
134 assert dt == datetime.datetime(1970, 1, 1, 0, 0, 42, 14, tzinfo=utc)
135
136 packed = msgpack.packb(dt, datetime=True)
137 packed2 = msgpack.packb(t)
138 assert packed == packed2
139
140 unpacked = msgpack.unpackb(packed)
141 print(packed, unpacked)
142 assert unpacked == t
143
144 unpacked = msgpack.unpackb(packed, timestamp=3)
145 assert unpacked == dt
146
147 x = []
148 packed = msgpack.packb(dt, datetime=False, default=x.append)
149 assert x
150 assert x[0] == dt
151 assert msgpack.unpackb(packed) is None
152
153
154 def test_issue451():
155 # https://github.com/msgpack/msgpack-python/issues/451
156 utc = datetime.timezone.utc
157 dt = datetime.datetime(2100, 1, 1, 1, 1, tzinfo=utc)
158 packed = msgpack.packb(dt, datetime=True)
159 assert packed == b"\xd6\xff\xf4\x86eL"
160
161 unpacked = msgpack.unpackb(packed, timestamp=3)
162 assert dt == unpacked
163
164
165 def test_pack_datetime_without_tzinfo():
166 dt = datetime.datetime(1970, 1, 1, 0, 0, 42, 14)
167 with pytest.raises(ValueError, match="where tzinfo=None"):
168 packed = msgpack.packb(dt, datetime=True)
169
170 dt = datetime.datetime(1970, 1, 1, 0, 0, 42, 14)
171 packed = msgpack.packb(dt, datetime=True, default=lambda x: None)
172 assert packed == msgpack.packb(None)
173
174 utc = datetime.timezone.utc
175 dt = datetime.datetime(1970, 1, 1, 0, 0, 42, 14, tzinfo=utc)
176 packed = msgpack.packb(dt, datetime=True)
177 unpacked = msgpack.unpackb(packed, timestamp=3)
178 assert unpacked == dt