summaryrefslogtreecommitdiffstats
path: root/openpgp/src
diff options
context:
space:
mode:
authorIgor Matuszewski <igor@sequoia-pgp.org>2020-10-15 17:28:53 +0200
committerIgor Matuszewski <igor@sequoia-pgp.org>2020-10-18 23:06:02 +0200
commitd98d9db3a57da2f8ee5cb28722bd1535d7387e55 (patch)
tree64521421f8e88a2747c6c907d6cdf591fcda00ab /openpgp/src
parente2fbf5b9d442f156ee433a3e46c98ec3dc89cc61 (diff)
openpgp: Implement date formatting for WASM using chrono crate
Diffstat (limited to 'openpgp/src')
-rw-r--r--openpgp/src/fmt.rs71
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)]