MCPcopy Index your code
hub / github.com/RustPython/RustPython / strftime

Function strftime

crates/vm/src/stdlib/time.rs:739–772  ·  view source on GitHub ↗
(format: PyStrRef, t: OptionalArg<StructTimeData>, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

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 {

Callers 1

strftime_asciiFunction · 0.85

Calls 10

current_time_tFunction · 0.85
localtime_from_timestampFunction · 0.85
strftime_crtFunction · 0.85
newFunction · 0.85
naive_or_localMethod · 0.80
into_ownedMethod · 0.80
to_stringMethod · 0.80
to_string_lossyMethod · 0.45
new_strMethod · 0.45

Tested by

no test coverage detected