From 7e2242c2d65b0b0c66ec04a87fc649cbd62bfed3 Mon Sep 17 00:00:00 2001 From: ArcticLampyrid Date: Sun, 8 Dec 2024 03:10:49 +0800 Subject: [PATCH] fix: remove nanoseconds in creationdate for Windows compatibility --- src/handle_props.rs | 6 +++--- src/util.rs | 13 +++++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/handle_props.rs b/src/handle_props.rs index 2bf9e40..41e22d7 100644 --- a/src/handle_props.rs +++ b/src/handle_props.rs @@ -24,7 +24,7 @@ use crate::fs::*; use crate::handle_lock::{list_lockdiscovery, list_supportedlock}; use crate::ls::*; use crate::util::MemBuffer; -use crate::util::{dav_xml_error, systemtime_to_httpdate, systemtime_to_rfc3339}; +use crate::util::{dav_xml_error, systemtime_to_httpdate, systemtime_to_rfc3339_without_nanosecond}; use crate::{DavInner, DavResult}; const NS_APACHE_URI: &str = "http://apache.org/dav/props/"; @@ -693,7 +693,7 @@ impl PropWriter { match prop.name.as_str() { "creationdate" => { if let Ok(time) = meta.created() { - let tm = systemtime_to_rfc3339(time); + let tm = systemtime_to_rfc3339_without_nanosecond(time); return self.build_elem(docontent, pfx, prop, tm); } // use ctime instead - apache seems to do this. @@ -704,7 +704,7 @@ impl PropWriter { time = mtime; } } - let tm = systemtime_to_rfc3339(time); + let tm = systemtime_to_rfc3339_without_nanosecond(time); return self.build_elem(docontent, pfx, prop, tm); } } diff --git a/src/util.rs b/src/util.rs index 323fef6..0aecb42 100644 --- a/src/util.rs +++ b/src/util.rs @@ -168,9 +168,13 @@ pub(crate) fn systemtime_to_httpdate(t: SystemTime) -> String { v[0].to_str().unwrap().to_owned() } -pub(crate) fn systemtime_to_rfc3339(t: SystemTime) -> String { +pub(crate) fn systemtime_to_rfc3339_without_nanosecond(t: SystemTime) -> String { // 1996-12-19T16:39:57Z - systemtime_to_offsetdatetime(t).format(&Rfc3339).unwrap() + systemtime_to_offsetdatetime(t) + .replace_nanosecond(0) + .ok() + .and_then(|x| x.format(&Rfc3339).ok()) + .unwrap_or("1970-01-01T00:00:00Z".into()) } // A buffer that implements "Write". @@ -205,7 +209,8 @@ mod tests { use std::time::UNIX_EPOCH; #[test] - fn test_rfc3339() { - assert!(systemtime_to_rfc3339(UNIX_EPOCH) == "1970-01-01T00:00:00Z"); + fn test_rfc3339_no_nanosecond() { + let t = UNIX_EPOCH + std::time::Duration::new(1, 5); + assert!(systemtime_to_rfc3339_without_nanosecond(t) == "1970-01-01T00:00:01Z"); } }