(format: PyStrRef, t: OptionalArg<StructTimeData>, vm: &VirtualMachine)
| 737 | |
| 738 | #[pyfunction] |
| 739 | fn strftime(format: PyStrRef, t: OptionalArg<StructTimeData>, vm: &VirtualMachine) -> PyResult { |
| 740 | #[cfg(any(unix, windows))] |
| 741 | { |
| 742 | let checked_tm = match t { |
| 743 | OptionalArg::Present(value) => checked_tm_from_struct_time(&value, vm, "strftime")?, |
| 744 | OptionalArg::Missing => { |
| 745 | let now = current_time_t(); |
| 746 | let local = localtime_from_timestamp(now, vm)?; |
| 747 | checked_tm_from_struct_time(&local, vm, "strftime")? |
| 748 | } |
| 749 | }; |
| 750 | strftime_crt(&format, checked_tm, vm) |
| 751 | } |
| 752 | |
| 753 | #[cfg(not(any(unix, windows)))] |
| 754 | { |
| 755 | use core::fmt::Write; |
| 756 | |
| 757 | let fmt_lossy = format.to_string_lossy(); |
| 758 | |
| 759 | // If the struct_time can't be represented as NaiveDateTime |
| 760 | // (e.g. month=0), return the format string as-is, matching |
| 761 | // the fallback behavior for unsupported chrono formats. |
| 762 | let instant = match t.naive_or_local(vm) { |
| 763 | Ok(dt) => dt, |
| 764 | Err(_) => return Ok(vm.ctx.new_str(fmt_lossy.into_owned()).into()), |
| 765 | }; |
| 766 | |
| 767 | let mut formatted_time = String::new(); |
| 768 | write!(&mut formatted_time, "{}", instant.format(&fmt_lossy)) |
| 769 | .unwrap_or_else(|_| formatted_time = format.to_string()); |
| 770 | Ok(vm.ctx.new_str(formatted_time).into()) |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | #[pyfunction] |
| 775 | fn strptime(string: PyStrRef, format: OptionalArg<PyStrRef>, vm: &VirtualMachine) -> PyResult { |
no test coverage detected