◐ Shell
clean mode source ↗

Align os.utime float-to-duration rounding with CPython semantics by Copilot · Pull Request #6501 · RustPython/RustPython

os.utime float inputs regressed due to Rust Duration::from_secs_f64 rounding changes; CPython floors times toward -∞. This patch restores CPython-equivalent flooring to keep test_utime_by_indexed/times passing.

  • Float-to-duration conversion: Replace Duration::from_secs_f64 with explicit nanosecond flooring, sign/finite checks, and bounds enforcement against the max (u64 seconds, <1e9 ns) duration.
  • Rounding parity: Ensure flooring behavior mirrors _PyTime_DoubleToDenominator for os.utime paths while preserving integer handling.

Example:

// Before: Duration::from_secs_f64(f)
// After:
let total_nanos = (f * 1_000_000_000f64).floor();
let secs  = (total_nanos as u128 / 1_000_000_000) as u64;
let nanos = (total_nanos as u128 % 1_000_000_000) as u32;
let dur = std::time::Duration::new(secs, nanos);
Original prompt

This section details on the original issue you should resolve

<issue_title>Tracking issue of os.utime regression</issue_title>
<issue_description>std::time::Duration::from_secs_f64 behavior is changed in Rust 1.63.

related python test:

  • test_os::test_utime_by_indexed
  • test_os::test_utime_by_times

test command:

  • cargo +stable run Lib/test/test_os.py -k utime -v
  • cargo +1.62.1 run Lib/test/test_os.py -k utime -v

Rust issue including minimal reproducible code

To-do

We will stay on 1.62.1 until checking if it is a rust bug or intended change.

Once it is resolved:

<agent_instructions>Implement something similar like _PyTime_DoubleToDenominator and resolve the related python test to pass (not as expectedFailure)</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@youknowone In CPython, `_PyTime_DoubleToDenominator` provides rounding option. floor option is passed for this path. @youknowone It was an intended behavior change from Rust. we need to implement rounding option like cpython.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.