From 5afb33ee81f468e407c1df6afcbc40d07233afad Mon Sep 17 00:00:00 2001 From: "Simeon H.K. Fitch" Date: Mon, 14 Nov 2022 17:50:26 -0500 Subject: [PATCH 1/3] Fix deprecations from chrono 0.4.23. --- .cargo/config.toml | 3 ++- src/errors.rs | 2 ++ src/vector/feature.rs | 45 +++++++++++++++++++++++++--------- src/vector/vector_tests/mod.rs | 12 ++++++--- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index f63d51264..9490b4f19 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,4 @@ [alias] # Run doctests, displaying compiler output -dto = "test --doc -- --show-output" \ No newline at end of file +dto = "test --doc -- --show-output" +nowarn = "clippy --all-targets -- -D warnings" \ No newline at end of file diff --git a/src/errors.rs b/src/errors.rs index 41acff3f2..44a6ed818 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -73,6 +73,8 @@ pub enum GdalError { UnlinkMemFile { file_name: String }, #[error("BadArgument")] BadArgument(String), + #[error("Date conversion error: {0}")] + DateError(String), #[cfg(all(major_ge_3, minor_ge_1))] #[error("Unhandled type '{data_type}' on GDAL MD method {method_name}")] diff --git a/src/vector/feature.rs b/src/vector/feature.rs index 07459f2e0..46052cec7 100644 --- a/src/vector/feature.rs +++ b/src/vector/feature.rs @@ -7,7 +7,7 @@ use std::convert::TryInto; use std::ffi::{CString, NulError}; use std::ptr; -use chrono::{Date, DateTime, Datelike, FixedOffset, TimeZone, Timelike}; +use chrono::{DateTime, Datelike, FixedOffset, LocalResult, NaiveDate, TimeZone, Timelike}; use crate::errors::*; use std::slice; @@ -152,7 +152,7 @@ impl<'a> Feature<'a> { self._field_as_datetime(field_id)?, ))), OGRFieldType::OFTDate => Ok(Some(FieldValue::DateValue( - self._field_as_datetime(field_id)?.date(), + self._field_as_datetime(field_id)?.date_naive(), ))), _ => Err(GdalError::UnhandledFieldType { field_type, @@ -428,10 +428,25 @@ impl<'a> Feature<'a> { } else { (tzflag as i32 - 100) * 15 * 60 }; - let rv = FixedOffset::east(tzoffset_secs) - .ymd(year as i32, month as u32, day as u32) - .and_hms(hour as u32, minute as u32, second as u32); - Ok(rv) + let rv = FixedOffset::east_opt(tzoffset_secs) + .ok_or_else(|| GdalError::DateError(tzoffset_secs.to_string()))? + .with_ymd_and_hms( + year as i32, + month as u32, + day as u32, + hour as u32, + minute as u32, + second as u32, + ); + match rv { + LocalResult::None => Err( + GdalError::DateError(format!("Unable to reconstruct valid date from fields: {year}, {month}, {day}, {hour}, {minute}, {second}")) + ), + LocalResult::Single(d) => Ok(d), + LocalResult::Ambiguous(d1, d2) => Err( + GdalError::DateError(format!("ambiguous date conversion; either '{d1}' or '{d2}'")) + ) + } } /// Get the field's geometry. @@ -614,7 +629,15 @@ impl<'a> Feature<'a> { FieldValue::RealValue(value) => self.set_field_double(field_name, *value), FieldValue::RealListValue(value) => self.set_field_double_list(field_name, value), FieldValue::DateValue(value) => { - self.set_field_datetime(field_name, value.and_hms(0, 0, 0)) + let dv = value + .and_hms_opt(0, 0, 0) + .ok_or_else(|| GdalError::DateError("offset to midnight".into()))?; + let dt = DateTime::from_utc( + dv, + FixedOffset::east_opt(0) + .ok_or_else(|| GdalError::DateError("utc offset".into()))?, + ); + self.set_field_datetime(field_name, dt) } FieldValue::DateTimeValue(value) => self.set_field_datetime(field_name, *value), } @@ -709,7 +732,7 @@ pub enum FieldValue { StringListValue(Vec), RealValue(f64), RealListValue(Vec), - DateValue(Date), + DateValue(NaiveDate), DateTimeValue(DateTime), } @@ -748,11 +771,11 @@ impl FieldValue { } } - /// Interpret the value as `Date`. Returns `None` if the value is something else. - pub fn into_date(self) -> Option> { + /// Interpret the value as `NaiveDate`. Returns `None` if the value is something else. + pub fn into_date(self) -> Option { match self { FieldValue::DateValue(rv) => Some(rv), - FieldValue::DateTimeValue(rv) => Some(rv.date()), + FieldValue::DateTimeValue(rv) => Some(rv.date_naive()), _ => None, } } diff --git a/src/vector/vector_tests/mod.rs b/src/vector/vector_tests/mod.rs index 881643475..ec72327f2 100644 --- a/src/vector/vector_tests/mod.rs +++ b/src/vector/vector_tests/mod.rs @@ -393,11 +393,15 @@ mod tests { with_features("points_with_datetime.json", |mut features| { let feature = features.next().unwrap(); - let dt = FixedOffset::east(-5 * hour_secs) - .ymd(2011, 7, 14) - .and_hms(19, 43, 37); + let dt = FixedOffset::east_opt(-5 * hour_secs) + .unwrap() + .with_ymd_and_hms(2011, 7, 14, 19, 43, 37) + .unwrap(); - let d = FixedOffset::east(0).ymd(2018, 1, 4).and_hms(0, 0, 0); + let d = FixedOffset::east_opt(0) + .unwrap() + .with_ymd_and_hms(2018, 1, 4, 0, 0, 0) + .unwrap(); assert_eq!(feature.field_as_datetime_by_name("dt").unwrap(), Some(dt)); From 51e056c118909a8c0e5ca7ceb3dbf801a49c148e Mon Sep 17 00:00:00 2001 From: "Simeon H.K. Fitch" Date: Tue, 15 Nov 2022 09:24:27 -0500 Subject: [PATCH 2/3] Update .cargo/config.toml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit extra space Co-authored-by: Laurențiu Nicola --- .cargo/config.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 9490b4f19..65f3b0654 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,4 +1,4 @@ [alias] # Run doctests, displaying compiler output dto = "test --doc -- --show-output" -nowarn = "clippy --all-targets -- -D warnings" \ No newline at end of file +nowarn = "clippy --all-targets -- -D warnings" \ No newline at end of file From a11178969adfb6c440ed7f68008ab71aaa3a1792 Mon Sep 17 00:00:00 2001 From: "Simeon H.K. Fitch" Date: Sat, 19 Nov 2022 13:58:33 -0500 Subject: [PATCH 3/3] Hold back geo-types version; deprecations addressed in #339 --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fd0a83709..7ea2508e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,10 +18,10 @@ array = ["ndarray"] [dependencies] thiserror = "1.0" libc = "0.2" -geo-types = { version = "0.7" } +geo-types = { version = ">=0.7.0, <0.7.8" } gdal-sys = { path = "gdal-sys", version = "^0.8" } ndarray = { version = "0.15", optional = true } -chrono = { version = "0.4" } +chrono = { version = ">=0.4.23" } bitflags = "1.3" once_cell = "1.9"