|
20 | 20 | use std::convert::TryFrom; |
21 | 21 |
|
22 | 22 | use anyhow::bail; |
23 | | -use chrono::{FixedOffset, Utc}; |
24 | 23 | use itertools::{process_results, Itertools}; |
25 | 24 | use serde::{Deserialize, Serialize}; |
26 | 25 | use serde_json::{self, Value as JsonValue}; |
27 | 26 | use tantivy::schema::{ |
28 | 27 | BytesOptions, Cardinality, DocParsingError as TantivyDocParser, FieldType, IndexRecordOption, |
29 | 28 | NumericOptions, TextFieldIndexing, TextOptions, Value, |
30 | 29 | }; |
| 30 | +use tantivy::time::format_description::well_known::Rfc3339; |
| 31 | +use tantivy::time::OffsetDateTime; |
| 32 | +use tantivy::DateTime; |
31 | 33 | use thiserror::Error; |
32 | 34 |
|
33 | 35 | use super::{default_as_true, FieldMappingType}; |
@@ -335,17 +337,16 @@ impl FieldMappingEntry { |
335 | 337 | )? |
336 | 338 | } |
337 | 339 | JsonValue::String(value_as_str) => { |
338 | | - let dt_with_fixed_tz: chrono::DateTime<FixedOffset> = |
339 | | - chrono::DateTime::parse_from_rfc3339(&value_as_str).map_err(|err| { |
| 340 | + let date_time_utc = DateTime::new_utc( |
| 341 | + OffsetDateTime::parse(&value_as_str, &Rfc3339).map_err(|err| { |
340 | 342 | DocParsingError::ValueError( |
341 | 343 | self.name.clone(), |
342 | 344 | format!("Expected RFC 3339 date, got '{}'. {:?}", value_as_str, err), |
343 | 345 | ) |
344 | | - })?; |
345 | | - vec![( |
346 | | - FieldPath::new(&self.name), |
347 | | - Value::Date(dt_with_fixed_tz.with_timezone(&Utc)), |
348 | | - )] |
| 346 | + })?, |
| 347 | + ); |
| 348 | + |
| 349 | + vec![(FieldPath::new(&self.name), Value::Date(date_time_utc))] |
349 | 350 | } |
350 | 351 | JsonValue::Null => { |
351 | 352 | vec![] |
@@ -747,10 +748,11 @@ impl From<TantivyDocParser> for DocParsingError { |
747 | 748 | #[cfg(test)] |
748 | 749 | mod tests { |
749 | 750 | use anyhow::bail; |
750 | | - use chrono::{NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc}; |
751 | 751 | use matches::matches; |
752 | 752 | use serde_json::json; |
753 | 753 | use tantivy::schema::{Cardinality, Value}; |
| 754 | + use tantivy::time::{Date, Month, PrimitiveDateTime, Time}; |
| 755 | + use tantivy::DateTime; |
754 | 756 |
|
755 | 757 | use super::FieldMappingEntry; |
756 | 758 | use crate::default_doc_mapper::FieldMappingType; |
@@ -1332,11 +1334,14 @@ mod tests { |
1332 | 1334 |
|
1333 | 1335 | // Successful parsing |
1334 | 1336 | let parsed_value = entry.parse(json!("2021-12-19T16:39:57-01:00"))?; |
1335 | | - let datetime = NaiveDateTime::new( |
1336 | | - NaiveDate::from_ymd(2021, 12, 19), |
1337 | | - NaiveTime::from_hms(17, 39, 57), |
| 1337 | + |
| 1338 | + let datetime = PrimitiveDateTime::new( |
| 1339 | + Date::from_calendar_date(2021, Month::December, 19).unwrap(), |
| 1340 | + Time::from_hms(17, 39, 57).unwrap(), |
1338 | 1341 | ); |
1339 | | - let datetime_utc = Utc.from_utc_datetime(&datetime); |
| 1342 | + // let datetime = datetime!(2021-12-19 17:39:57); |
| 1343 | + |
| 1344 | + let datetime_utc = DateTime::new_primitive(datetime); // Utc.from_utc_datetime(&datetime); |
1340 | 1345 | assert_eq!(parsed_value.len(), 1); |
1341 | 1346 | assert_eq!(parsed_value[0].1, Value::Date(datetime_utc)); |
1342 | 1347 |
|
|
0 commit comments