Skip to content

Commit 2cdb4da

Browse files
committed
fix: implement micro second precision
1 parent 9bf6b8e commit 2cdb4da

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

src/serializers/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,11 @@ impl TemporalMode {
336336
}
337337
Self::Seconds => {
338338
let s = time_to_seconds(time).map_err(py_err_se_err)?;
339-
serializer.serialize_f32(s)
339+
serializer.serialize_f64(s)
340340
}
341341
Self::Milliseconds => {
342342
let s = time_to_milliseconds(time).map_err(py_err_se_err)?;
343-
serializer.serialize_f32(s)
343+
serializer.serialize_f64(s)
344344
}
345345
}
346346
}

src/serializers/type_serializers/datetime_etc.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,23 @@ pub(crate) fn datetime_to_string(py_dt: &Bound<'_, PyDateTime>) -> PyResult<Stri
1717
}
1818

1919
pub(crate) fn datetime_to_seconds(py_dt: &Bound<'_, PyDateTime>) -> PyResult<f64> {
20-
pydatetime_as_datetime(py_dt).map(|dt| dt.timestamp() as f64)
20+
pydatetime_as_datetime(py_dt).map(|dt|
21+
dt.date.timestamp() as f64
22+
+ dt.time.hour as f64 * 3600.0
23+
+ dt.time.minute as f64 * 60.0
24+
+ dt.time.second as f64
25+
+ dt.time.microsecond as f64 / 1_000_000.0
26+
)
2127
}
2228

2329
pub(crate) fn datetime_to_milliseconds(py_dt: &Bound<'_, PyDateTime>) -> PyResult<f64> {
24-
pydatetime_as_datetime(py_dt).map(|dt| dt.timestamp_ms() as f64)
30+
pydatetime_as_datetime(py_dt).map(|dt|
31+
dt.date.timestamp_ms() as f64
32+
+ dt.time.hour as f64 * 3_600_000.0
33+
+ dt.time.minute as f64 * 60_000.0
34+
+ dt.time.second as f64 * 1_000.0
35+
+ dt.time.microsecond as f64 / 1_000.0
36+
)
2537
}
2638

2739
pub(crate) fn date_to_seconds(py_date: &Bound<'_, PyDate>) -> PyResult<f64> {
@@ -39,12 +51,19 @@ pub(crate) fn time_to_string(py_time: &Bound<'_, PyTime>) -> PyResult<String> {
3951
pytime_as_time(py_time, None).map(|dt| dt.to_string())
4052
}
4153

42-
pub(crate) fn time_to_seconds(py_time: &Bound<'_, PyTime>) -> PyResult<f32> {
43-
pytime_as_time(py_time, None).map(|t| t.total_seconds() as f32)
54+
pub(crate) fn time_to_seconds(py_time: &Bound<'_, PyTime>) -> PyResult<f64> {
55+
pytime_as_time(py_time, None).map(|t| {
56+
t.hour as f64 * 3600.0 + t.minute as f64 * 60.0 + t.second as f64 + t.microsecond as f64 / 1_000_000.0
57+
})
4458
}
4559

46-
pub(crate) fn time_to_milliseconds(py_time: &Bound<'_, PyTime>) -> PyResult<f32> {
47-
pytime_as_time(py_time, None).map(|t| t.total_ms() as f32)
60+
pub(crate) fn time_to_milliseconds(py_time: &Bound<'_, PyTime>) -> PyResult<f64> {
61+
pytime_as_time(py_time, None).map(|t| {
62+
t.hour as f64 * 3_600_000.0
63+
+ t.minute as f64 * 60_000.0
64+
+ t.second as f64 * 1_000.0
65+
+ t.microsecond as f64 / 1_000.0
66+
})
4867
}
4968

5069
fn downcast_date_reject_datetime<'a, 'py>(py_date: &'a Bound<'py, PyAny>) -> PyResult<&'a Bound<'py, PyDate>> {

tests/serializers/test_datetime.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,22 @@ def test_date_datetime_union():
142142
b'{"1704067200000":"foo"}',
143143
'milliseconds',
144144
),
145+
(
146+
datetime(2024, 1, 1, 1, 1, 1, 23),
147+
1704070861.000023,
148+
b'1704070861.000023',
149+
{'1704070861.000023': 'foo'},
150+
b'{"1704070861.000023":"foo"}',
151+
'seconds',
152+
),
153+
(
154+
datetime(2024, 1, 1, 1, 1, 1, 23),
155+
1704070861000.023,
156+
b'1704070861000.023',
157+
{'1704070861000.023': 'foo'},
158+
b'{"1704070861000.023":"foo"}',
159+
'milliseconds',
160+
),
145161
],
146162
)
147163
def test_config_datetime(
@@ -240,18 +256,18 @@ def test_config_date(
240256
),
241257
(
242258
time(3, 14, 1, 59263),
243-
11641.0,
244-
b'11641.0',
245-
{'11641': 'foo'},
246-
b'{"11641":"foo"}',
259+
11641.059263,
260+
b'11641.059263',
261+
{'11641.059263': 'foo'},
262+
b'{"11641.059263":"foo"}',
247263
'seconds',
248264
),
249265
(
250266
time(3, 14, 1, 59263),
251-
11641059.0,
252-
b'11641059.0',
253-
{'11641059': 'foo'},
254-
b'{"11641059":"foo"}',
267+
11641059.263,
268+
b'11641059.263',
269+
{'11641059.263': 'foo'},
270+
b'{"11641059.263":"foo"}',
255271
'milliseconds',
256272
),
257273
],

0 commit comments

Comments
 (0)