Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix durartion to round
  • Loading branch information
youknowone committed Feb 3, 2026
commit f939a06aa9123015ecfdeca5122527f880bfd2f8
19 changes: 17 additions & 2 deletions crates/vm/src/convert/try_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,25 @@ impl TryFromObject for core::time::Duration {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
if let Some(float) = obj.downcast_ref::<PyFloat>() {
let f = float.to_f64();
if f.is_nan() {
return Err(
vm.new_value_error("Invalid value NaN (not a number)".to_owned())
);
}
if f < 0.0 {
return Err(vm.new_value_error("negative duration"));
return Err(vm.new_value_error("negative duration".to_owned()));
}
if !f.is_finite() || f > u64::MAX as f64 {
return Err(vm.new_overflow_error(
"timestamp too large to convert to C PyTime_t".to_owned(),
));
}
Ok(Self::from_secs_f64(f))
// Convert float to Duration using floor rounding (_PyTime_ROUND_FLOOR)
let secs = f.trunc() as u64;
let frac = f.fract();
// Use floor to round down the nanoseconds
let nanos = (frac * 1_000_000_000.0).floor() as u32;
Ok(Self::new(secs, nanos))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
} else if let Some(int) = obj.try_index_opt(vm) {
let int = int?;
let bigint = int.as_bigint();
Expand Down