From 6b332b18f11d7b1be53f22faf2df97bf4afb4f0f Mon Sep 17 00:00:00 2001 From: Enyium <123484196+Enyium@users.noreply.github.com> Date: Mon, 27 Nov 2023 21:40:15 +0100 Subject: [PATCH 1/4] Correct `from_i64()`. Calculation of `nsecs` was off by a factor of 100! I didn't touch the tests; since they will have worked with the incorrect code, they will be incorrect also. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index e288a09..dfe15b8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,7 +100,7 @@ impl FileTime { pub fn from_i64(filetime: i64) -> Self { assert!(filetime >= 0, "Only positive values allowed"); let secs: i64 = filetime / Self::HUNDREDS_OF_NANOSECONDS; - let nsecs: i64 = filetime % Self::HUNDREDS_OF_NANOSECONDS; + let nsecs: i64 = filetime % Self::HUNDREDS_OF_NANOSECONDS * 100; Self { secs, nsecs } } From ffc1d8516711f3e193ace504b620f1bf76916664 Mon Sep 17 00:00:00 2001 From: tuxuser <462620+tuxuser@users.noreply.github.com> Date: Wed, 29 Nov 2023 04:52:21 +0100 Subject: [PATCH 2/4] tests: Correct tests after algorithm fixup --- src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index dfe15b8..18d3665 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -186,13 +186,13 @@ mod test { #[test] fn from_datetime() { let dt = Utc.from_utc_datetime( - &DateTime::parse_from_rfc3339("2009-07-25T23:00:00.000001000Z") + &DateTime::parse_from_rfc3339("2009-07-25T23:00:00.0001Z") .unwrap() .naive_utc(), ); assert_eq!( - FileTime::from_datetime(dt), - FileTime::from_i64(128930364000001000) + dt, + FileTime::from_i64(128930364000001000).into() ); } @@ -204,7 +204,7 @@ mod test { ft, FileTime { secs: 13013971283, - nsecs: 1482830 + nsecs: 148283000 } ); } @@ -243,7 +243,7 @@ mod test { let dt = Utc .with_ymd_and_hms(30828, 9, 14, 2, 48, 5) .unwrap() - .checked_add_signed(Duration::nanoseconds(4775807)) + .checked_add_signed(Duration::nanoseconds(477580700)) .unwrap(); assert_eq!(ft.to_datetime(), dt); } @@ -252,7 +252,7 @@ mod test { fn filetime_one() { let ft = FileTime::from_i64(1); assert_eq!(ft.seconds(), 0); - assert_eq!(ft.nanoseconds(), 1); + assert_eq!(ft.nanoseconds(), 100); } #[test] From bedc4004a57c14074acbdad6d3e5232a31852839 Mon Sep 17 00:00:00 2001 From: tuxuser <462620+tuxuser@users.noreply.github.com> Date: Wed, 29 Nov 2023 04:58:49 +0100 Subject: [PATCH 3/4] ci: Bump minimum rust version to 1.57.0 --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6b9dd96..81320a4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,7 @@ jobs: strategy: fail-fast: false matrix: - rust: [beta, stable, 1.56.0] + rust: [beta, stable, 1.57.0] steps: - uses: actions/checkout@v3 From a70af719d45b5ce6f40d2b00a7031a59aad5be56 Mon Sep 17 00:00:00 2001 From: tuxuser <462620+tuxuser@users.noreply.github.com> Date: Wed, 29 Nov 2023 05:12:46 +0100 Subject: [PATCH 4/4] Correct from_datetime() and filetime() methods --- src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 18d3665..f0bf20f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,7 +81,7 @@ impl FileTime { /// let ft_i64 = FileTime::now().filetime(); /// ``` pub fn filetime(&self) -> i64 { - (self.secs * Self::HUNDREDS_OF_NANOSECONDS) + self.nsecs + (self.secs * Self::HUNDREDS_OF_NANOSECONDS) + self.nsecs.checked_div(100).unwrap_or(0) } /// Return FILETIME epoch as DateTime @@ -115,7 +115,7 @@ impl FileTime { pub fn from_datetime(dt: DateTime) -> Self { let nsecs = Self::EPOCH_AS_FILETIME + (dt.timestamp() * Self::HUNDREDS_OF_NANOSECONDS) - + dt.timestamp_subsec_nanos() as i64; + + dt.timestamp_subsec_nanos().checked_div(100).unwrap_or(0) as i64; Self::from_i64(nsecs) } @@ -214,7 +214,7 @@ mod test { let bytes = [0xCE_u8, 0xEB, 0x7D, 0x1A, 0x61, 0x59, 0xCE, 0x01]; let ft: [u8; 8] = FileTime { secs: 13013971283, - nsecs: 1482830, + nsecs: 148283000, } .into(); assert_eq!(ft, bytes);