| 1018 | #[cfg(any(unix, windows))] |
| 1019 | #[cfg_attr(target_env = "musl", allow(deprecated))] |
| 1020 | fn pyobj_to_time_t(value: Either<f64, i64>, vm: &VirtualMachine) -> PyResult<libc::time_t> { |
| 1021 | match value { |
| 1022 | Either::A(float) => { |
| 1023 | if !float.is_finite() { |
| 1024 | return Err(vm.new_value_error("Invalid value for timestamp")); |
| 1025 | } |
| 1026 | let secs = float.floor(); |
| 1027 | #[cfg_attr(target_env = "musl", allow(deprecated))] |
| 1028 | if secs < libc::time_t::MIN as f64 || secs > libc::time_t::MAX as f64 { |
| 1029 | return Err(vm.new_overflow_error("timestamp out of range for platform time_t")); |
| 1030 | } |
| 1031 | #[cfg_attr(target_env = "musl", allow(deprecated))] |
| 1032 | Ok(secs as libc::time_t) |
| 1033 | } |
| 1034 | Either::B(int) => { |
| 1035 | // try_into is needed on 32-bit platforms where time_t != i64 |
| 1036 | #[allow(clippy::useless_conversion)] |
| 1037 | #[cfg_attr(target_env = "musl", allow(deprecated))] |
| 1038 | let ts: libc::time_t = int.try_into().map_err(|_| { |
| 1039 | vm.new_overflow_error("timestamp out of range for platform time_t") |
| 1040 | })?; |
| 1041 | Ok(ts) |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | #[cfg(any(unix, windows))] |
| 1047 | #[allow(unused_imports)] |