diff options
Diffstat (limited to 'openpgp/src')
-rw-r--r-- | openpgp/src/fmt.rs | 71 |
1 files changed, 40 insertions, 31 deletions
diff --git a/openpgp/src/fmt.rs b/openpgp/src/fmt.rs index f7bb8e31..d36217d2 100644 --- a/openpgp/src/fmt.rs +++ b/openpgp/src/fmt.rs @@ -269,40 +269,49 @@ pub(crate) fn from_hex(hex: &str, pretty: bool) -> Result<Vec<u8>> { /// is not representable using unsigned UNIX time, we return the debug /// formatting. pub(crate) fn time(t: &std::time::SystemTime) -> String { - extern "C" { - fn strftime( - s: *mut libc::c_char, - max: libc::size_t, - format: *const libc::c_char, - tm: *const libc::tm, - ) -> usize; + // Actually use a chrono dependency for WASM since there's no strftime + // (except for WASI). + #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] { + chrono::DateTime::<chrono::Utc>::from(t.clone()) + .format("%Y-%m-%dT%H:%M:%SZ") + .to_string() } + #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] { + extern "C" { + fn strftime( + s: *mut libc::c_char, + max: libc::size_t, + format: *const libc::c_char, + tm: *const libc::tm, + ) -> usize; + } - let t = match t.duration_since(std::time::UNIX_EPOCH) { - Ok(t) => t.as_secs() as libc::time_t, - Err(_) => return format!("{:?}", t), - }; - let fmt = b"%Y-%m-%dT%H:%M:%SZ\x00"; - assert_eq!(b"2020-03-26T10:08:10Z\x00".len(), 21); - let mut s = [0u8; 21]; - - unsafe { - let mut tm: libc::tm = std::mem::zeroed(); - - #[cfg(unix)] - libc::gmtime_r(&t, &mut tm); - #[cfg(windows)] - libc::gmtime_s(&mut tm, &t); - - strftime(s.as_mut_ptr() as *mut libc::c_char, - s.len(), - fmt.as_ptr() as *const libc::c_char, - &tm); - } + let t = match t.duration_since(std::time::UNIX_EPOCH) { + Ok(t) => t.as_secs() as libc::time_t, + Err(_) => return format!("{:?}", t), + }; + let fmt = b"%Y-%m-%dT%H:%M:%SZ\x00"; + assert_eq!(b"2020-03-26T10:08:10Z\x00".len(), 21); + let mut s = [0u8; 21]; + + unsafe { + let mut tm: libc::tm = std::mem::zeroed(); + + #[cfg(unix)] + libc::gmtime_r(&t, &mut tm); + #[cfg(windows)] + libc::gmtime_s(&mut tm, &t); + + strftime(s.as_mut_ptr() as *mut libc::c_char, + s.len(), + fmt.as_ptr() as *const libc::c_char, + &tm); + } - std::ffi::CStr::from_bytes_with_nul(&s) - .expect("strftime nul terminates string") - .to_string_lossy().into() + std::ffi::CStr::from_bytes_with_nul(&s) + .expect("strftime nul terminates string") + .to_string_lossy().into() + } } #[cfg(test)] |