changeset 51917:1d6982827c4b

rust: fix the deprecation warning in NaiveDateTime::from_timestamp This warning appears between chrono 0.4.34 and 0.4.38, so isn't affecting the current lock file, but it would come when we upgraded the version.
author Arseniy Alekseyev <aalekseyev@janestreet.com>
date Fri, 27 Sep 2024 17:25:15 +0100
parents 6223892833db
children 813226b3b4ca
files rust/hg-core/src/revlog/changelog.rs
diffstat 1 files changed, 14 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/revlog/changelog.rs	Sat Jul 20 03:04:48 2024 +0200
+++ b/rust/hg-core/src/revlog/changelog.rs	Fri Sep 27 17:25:15 2024 +0100
@@ -4,7 +4,7 @@
 use std::fmt::{Debug, Formatter};
 use std::{iter, str};
 
-use chrono::{DateTime, FixedOffset, NaiveDateTime};
+use chrono::{DateTime, FixedOffset, Utc};
 use itertools::{Either, Itertools};
 
 use crate::errors::HgError;
@@ -339,7 +339,7 @@
             HgError::corrupted(format!("failed to parse timestamp: {e}"))
         })
         .and_then(|secs| {
-            NaiveDateTime::from_timestamp_opt(secs, 0).ok_or_else(|| {
+            DateTime::from_timestamp(secs, 0).ok_or_else(|| {
                 HgError::corrupted(format!(
                     "integer timestamp out of valid range: {secs}"
                 ))
@@ -364,14 +364,17 @@
     let timezone = FixedOffset::west_opt(timezone_secs)
         .ok_or_else(|| HgError::corrupted("timezone offset out of bounds"))?;
 
-    Ok(DateTime::from_naive_utc_and_offset(timestamp_utc, timezone))
+    Ok(DateTime::from_naive_utc_and_offset(
+        timestamp_utc.naive_utc(),
+        timezone,
+    ))
 }
 
 /// Attempt to parse the given string as floating-point timestamp, and
 /// convert the result into a `chrono::NaiveDateTime`.
 fn parse_float_timestamp(
     timestamp_str: &str,
-) -> Result<NaiveDateTime, HgError> {
+) -> Result<DateTime<Utc>, HgError> {
     let timestamp = timestamp_str.parse::<f64>().map_err(|e| {
         HgError::corrupted(format!("failed to parse timestamp: {e}"))
     })?;
@@ -399,7 +402,7 @@
     // precision with present-day timestamps.)
     let nsecs = (subsecs * 1_000_000_000.0) as u32;
 
-    NaiveDateTime::from_timestamp_opt(secs, nsecs).ok_or_else(|| {
+    DateTime::from_timestamp(secs, nsecs).ok_or_else(|| {
         HgError::corrupted(format!(
             "float timestamp out of valid range: {timestamp}"
         ))
@@ -647,19 +650,19 @@
     fn test_parse_float_timestamp() {
         let test_cases = [
             // Zero should map to the UNIX epoch.
-            ("0.0", "1970-01-01 00:00:00"),
+            ("0.0", "1970-01-01 00:00:00 UTC"),
             // Negative zero should be the same as positive zero.
-            ("-0.0", "1970-01-01 00:00:00"),
+            ("-0.0", "1970-01-01 00:00:00 UTC"),
             // Values without fractional components should work like integers.
             // (Assuming the timestamp is within the limits of f64 precision.)
-            ("1115154970.0", "2005-05-03 21:16:10"),
+            ("1115154970.0", "2005-05-03 21:16:10 UTC"),
             // We expect some loss of precision in the fractional component
             // when parsing arbitrary floating-point values.
-            ("1115154970.123456789", "2005-05-03 21:16:10.123456716"),
+            ("1115154970.123456789", "2005-05-03 21:16:10.123456716 UTC"),
             // But representable f64 values should parse losslessly.
-            ("1115154970.123456716", "2005-05-03 21:16:10.123456716"),
+            ("1115154970.123456716", "2005-05-03 21:16:10.123456716 UTC"),
             // Negative fractional components are subtracted from the epoch.
-            ("-1.333", "1969-12-31 23:59:58.667"),
+            ("-1.333", "1969-12-31 23:59:58.667 UTC"),
         ];
 
         for (input, expected) in test_cases {