mirror of
https://github.com/bspeice/dtparse
synced 2025-07-01 13:56:12 -04:00
Set up Python parse compat
This commit is contained in:
41
src/lib.rs
41
src/lib.rs
@ -9,8 +9,6 @@ use chrono::Utc;
|
||||
use std::collections::HashMap;
|
||||
use std::vec::Vec;
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_python_compat;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
@ -674,9 +672,7 @@ impl Default for Parser {
|
||||
|
||||
impl Parser {
|
||||
pub fn new(info: ParserInfo) -> Self {
|
||||
Parser {
|
||||
info: info
|
||||
}
|
||||
Parser { info: info }
|
||||
}
|
||||
|
||||
pub fn parse(
|
||||
@ -692,34 +688,43 @@ impl Parser {
|
||||
let default_ts = NaiveDateTime::new(default_date, NaiveTime::from_hms(0, 0, 0));
|
||||
|
||||
// TODO: What should be done with the tokens?
|
||||
let (res, tokens) = self.parse_with_tokens(
|
||||
timestr, self.info.dayfirst, self.info.yearfirst, true, true)?;
|
||||
let (res, tokens) =
|
||||
self.parse_with_tokens(timestr, self.info.dayfirst, self.info.yearfirst, true, true)?;
|
||||
|
||||
let naive = self.build_naive(&res, default_ts);
|
||||
Ok(self.build_tzaware(naive, &res, default_ts))
|
||||
}
|
||||
|
||||
fn parse_with_tokens(&self, timestr: String, dayfirst: bool, yearfirst: bool, fuzzy: bool,
|
||||
fuzzy_with_tokens: bool) -> Result<(ParsingResult, Vec<String>), ParseError> {
|
||||
|
||||
fn parse_with_tokens(
|
||||
&self,
|
||||
timestr: String,
|
||||
dayfirst: bool,
|
||||
yearfirst: bool,
|
||||
fuzzy: bool,
|
||||
fuzzy_with_tokens: bool,
|
||||
) -> Result<(ParsingResult, Vec<String>), ParseError> {
|
||||
Err(ParseError::InvalidMonth)
|
||||
}
|
||||
|
||||
fn build_naive(&self, res: &ParsingResult, default: NaiveDateTime) -> NaiveDateTime {
|
||||
|
||||
Local::now().naive_local()
|
||||
}
|
||||
|
||||
fn build_tzaware(&self, dt: NaiveDateTime, res: &ParsingResult, default: NaiveDateTime) -> DateTime<Utc> {
|
||||
|
||||
fn build_tzaware(
|
||||
&self,
|
||||
dt: NaiveDateTime,
|
||||
res: &ParsingResult,
|
||||
default: NaiveDateTime,
|
||||
) -> DateTime<Utc> {
|
||||
Utc::now()
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(timestr: String, parserinfo: Option<ParserInfo>) -> Result<DateTime<Utc>, ParseError> {
|
||||
|
||||
let parserinfo = parserinfo.unwrap_or(ParserInfo::default());
|
||||
let parser = Parser::new(parserinfo);
|
||||
|
||||
fn parse_with_info(timestr: String, info: ParserInfo) -> Result<DateTime<Utc>, ParseError> {
|
||||
let parser = Parser::new(info);
|
||||
parser.parse(timestr, None, false, vec![])
|
||||
}
|
||||
|
||||
fn parse(timestr: String) -> Result<DateTime<Utc>, ParseError> {
|
||||
parse_with_info(timestr, ParserInfo::default())
|
||||
}
|
||||
|
11
src/tests.rs
11
src/tests.rs
@ -1,11 +0,0 @@
|
||||
use ParseError;
|
||||
use days_in_month;
|
||||
|
||||
#[test]
|
||||
fn test_num_days_in_month() {
|
||||
assert_eq!(days_in_month(2000, 12), Ok(31));
|
||||
assert_eq!(days_in_month(2000, 2), Ok(29));
|
||||
assert_eq!(days_in_month(2000, 4), Ok(30));
|
||||
assert_eq!(days_in_month(2001, 2), Ok(28));
|
||||
assert_eq!(days_in_month(2000, 13), Err(ParseError::InvalidMonth))
|
||||
}
|
25
src/tests/compat_parse.rs
Normal file
25
src/tests/compat_parse.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use chrono::SecondsFormat;
|
||||
|
||||
use parse;
|
||||
|
||||
#[test]
|
||||
fn test_python_compat() {
|
||||
assert_eq!(
|
||||
parse("2018.5.15".to_owned())
|
||||
.unwrap()
|
||||
.to_rfc3339_opts(SecondsFormat::Micros, false),
|
||||
"2018-05-15 04:00:00+00:00"
|
||||
);
|
||||
assert_eq!(
|
||||
parse("May 5, 2018".to_owned())
|
||||
.unwrap()
|
||||
.to_rfc3339_opts(SecondsFormat::Micros, false),
|
||||
"2018-05-05 04:00:00+00:00"
|
||||
);
|
||||
assert_eq!(
|
||||
parse("Mar. 5, 2018".to_owned())
|
||||
.unwrap()
|
||||
.to_rfc3339_opts(SecondsFormat::Micros, false),
|
||||
"2018-03-05 05:00:00+00:00"
|
||||
);
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
use super::Token;
|
||||
use super::tokenize;
|
||||
use Token;
|
||||
use tokenize;
|
||||
|
||||
#[test]
|
||||
fn test_python_compat() {
|
2
src/tests/mod.rs
Normal file
2
src/tests/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
mod compat_parse;
|
||||
mod compat_split_string;
|
Reference in New Issue
Block a user