1
0
mirror of https://github.com/bspeice/dtparse synced 2024-11-12 17:08:09 -05:00

Attempt to read timezones from chrono-tz

This commit is contained in:
Bradlee Speice 2019-11-13 23:12:47 -05:00
parent c310cbaa0d
commit 142712900f
4 changed files with 36 additions and 5 deletions

View File

@ -18,6 +18,7 @@ name = "dtparse"
[dependencies]
chrono = "0.4"
chrono-tz = "0.5"
lazy_static = "1.1"
num-traits = "0.2"
rust_decimal = "^0.10.1"

View File

@ -73,6 +73,7 @@
extern crate lazy_static;
extern crate chrono;
extern crate chrono_tz;
extern crate num_traits;
extern crate rust_decimal;
@ -83,7 +84,10 @@ use chrono::Local;
use chrono::NaiveDate;
use chrono::NaiveDateTime;
use chrono::NaiveTime;
use chrono::Offset;
use chrono::Timelike;
use chrono::TimeZone;
use chrono_tz::Tz;
use num_traits::cast::ToPrimitive;
use rust_decimal::Decimal;
use rust_decimal::Error as DecimalError;
@ -948,11 +952,10 @@ impl Parser {
fn build_tzaware(
&self,
_dt: &NaiveDateTime,
dt: &NaiveDateTime,
res: &ParsingResult,
tzinfos: &HashMap<String, i32>,
) -> ParseResult<Option<FixedOffset>> {
// TODO: Actual timezone support
if let Some(offset) = res.tzoffset {
Ok(Some(FixedOffset::east(offset)))
} else if res.tzoffset == None
@ -965,9 +968,15 @@ impl Parser {
*tzinfos.get(res.tzname.as_ref().unwrap()).unwrap(),
)))
} else if res.tzname.is_some() {
// TODO: Dateutil issues a warning/deprecation notice here. Should we force the issue?
println!("tzname {} identified but not understood. Ignoring for the time being, but behavior is subject to change.", res.tzname.as_ref().unwrap());
Ok(None)
let tzname = res.tzname.as_ref().unwrap();
let tz: Result<Tz, String> = tzname.parse();
if tz.is_ok() {
let offset = tz.unwrap().offset_from_local_datetime(dt).unwrap().fix();
Ok(Some(offset))
} else {
println!("tzname {} identified but not understood ({}). Ignoring for the time being, but behavior is subject to change.", tzname, tz.unwrap_err());
Ok(None)
}
} else {
Err(ParseError::TimezoneUnsupported)
}

View File

@ -1,3 +1,4 @@
mod fuzzing;
mod pycompat_parser;
mod pycompat_tokenizer;
mod tz;

20
src/tests/tz.rs Normal file
View File

@ -0,0 +1,20 @@
use crate::parse;
#[test]
fn est() {
// Issue originally reported in https://github.com/bspeice/dtparse/issues/18
let dt = parse("Fri, 21 Aug 2015 18:37:44 EST");
assert!(dt.is_ok());
assert!(dt.unwrap().1.is_some());
}
#[test]
fn cest() {
// Issue originally reported in https://github.com/bspeice/dtparse/issues/18
let dt = parse("Fri, 21 Aug 2015 18:37:44 CEST");
assert!(dt.is_ok());
// TODO: Fix
// assert!(dt.unwrap().1.is_some());
}