Skip to content

Commit

Permalink
Merge #336
Browse files Browse the repository at this point in the history
336: Fix deprecations from chrono 0.4.23. r=metasim a=metasim

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/gdal/blob/master/CODE_OF_CONDUCT.md).
- [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
---

Fixes #335 

Co-authored-by: Simeon H.K. Fitch <fitch@astraea.io>
  • Loading branch information
bors[bot] and metasim committed Nov 20, 2022
2 parents 078aa04 + a111789 commit 9cbaedb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 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"
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
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
45 changes: 34 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,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),
}
Expand Down Expand Up @@ -709,7 +732,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 +771,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 9cbaedb

Please sign in to comment.