Skip to content

Commit

Permalink
Fix deprecations from chrono 0.4.23.
Browse files Browse the repository at this point in the history
  • Loading branch information
metasim committed Nov 14, 2022
1 parent 078aa04 commit 67824b2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[alias]
# Run doctests, displaying compiler output
dto = "test --doc -- --show-output"
dto = "test --doc -- --show-output"
nowarn = "clippy --all-targets -- -D warnings"
2 changes: 2 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")]
Expand Down
41 changes: 30 additions & 11 deletions src/vector/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -614,7 +629,11 @@ 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),
}
Expand Down Expand Up @@ -709,7 +728,7 @@ pub enum FieldValue {
StringListValue(Vec<String>),
RealValue(f64),
RealListValue(Vec<f64>),
DateValue(Date<FixedOffset>),
DateValue(NaiveDate),
DateTimeValue(DateTime<FixedOffset>),
}

Expand Down Expand Up @@ -748,11 +767,11 @@ impl FieldValue {
}
}

/// Interpret the value as `Date`. Returns `None` if the value is something else.
pub fn into_date(self) -> Option<Date<FixedOffset>> {
/// Interpret the value as `NaiveDate`. Returns `None` if the value is something else.
pub fn into_date(self) -> Option<NaiveDate> {
match self {
FieldValue::DateValue(rv) => Some(rv),
FieldValue::DateTimeValue(rv) => Some(rv.date()),
FieldValue::DateTimeValue(rv) => Some(rv.date_naive()),
_ => None,
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/vector/vector_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit 67824b2

Please sign in to comment.