mirror of
https://github.com/bspeice/dtparse
synced 2025-07-13 11:44:57 -04:00
Compare commits
9 Commits
v1.2.0
...
ed919e84ef
Author | SHA1 | Date | |
---|---|---|---|
ed919e84ef | |||
4d8ade4b05 | |||
6a88885ef5 | |||
a193a79afa | |||
5b3be160f6 | |||
8f6a3b179d | |||
028c45e3fe | |||
f1ca4e4129 | |||
61d3ed025e |
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "dtparse"
|
name = "dtparse"
|
||||||
version = "1.2.0"
|
version = "1.3.0"
|
||||||
authors = ["Bradlee Speice <bradlee@speice.io>"]
|
authors = ["Bradlee Speice <bradlee@speice.io>"]
|
||||||
description = "A dateutil-compatible timestamp parser for Rust"
|
description = "A dateutil-compatible timestamp parser for Rust"
|
||||||
repository = "https://github.com/bspeice/dtparse.git"
|
repository = "https://github.com/bspeice/dtparse.git"
|
||||||
@ -17,7 +17,9 @@ name = "dtparse"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
chrono-tz = "0.5"
|
|
||||||
lazy_static = "1.1"
|
lazy_static = "1.1"
|
||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
||||||
rust_decimal = "^0.10.1"
|
rust_decimal = { version = "1.17.0", default-features = false }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
base64 = "0.13"
|
||||||
|
25
src/lib.rs
25
src/lib.rs
@ -74,7 +74,6 @@
|
|||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
|
|
||||||
extern crate chrono;
|
extern crate chrono;
|
||||||
extern crate chrono_tz;
|
|
||||||
extern crate num_traits;
|
extern crate num_traits;
|
||||||
extern crate rust_decimal;
|
extern crate rust_decimal;
|
||||||
|
|
||||||
@ -1092,9 +1091,8 @@ impl Parser {
|
|||||||
} else if let Some(hms_idx) = self.find_hms_index(idx, tokens, info, true) {
|
} else if let Some(hms_idx) = self.find_hms_index(idx, tokens, info, true) {
|
||||||
// HH[ ]h or MM[ ]m or SS[.ss][ ]s
|
// HH[ ]h or MM[ ]m or SS[.ss][ ]s
|
||||||
let (new_idx, hms) = self.parse_hms(idx, tokens, info, Some(hms_idx));
|
let (new_idx, hms) = self.parse_hms(idx, tokens, info, Some(hms_idx));
|
||||||
if hms.is_some() {
|
if let Some(hms) = hms {
|
||||||
// TODO: This unwrap is unjustified.
|
self.assign_hms(res, value_repr, hms)?;
|
||||||
self.assign_hms(res, value_repr, hms.unwrap());
|
|
||||||
}
|
}
|
||||||
idx = new_idx;
|
idx = new_idx;
|
||||||
} else if idx + 2 < len_l && tokens[idx + 1] == ":" {
|
} else if idx + 2 < len_l && tokens[idx + 1] == ":" {
|
||||||
@ -1102,7 +1100,7 @@ impl Parser {
|
|||||||
// TODO: Better story around Decimal handling
|
// TODO: Better story around Decimal handling
|
||||||
res.hour = Some(value.floor().to_i64().unwrap() as i32);
|
res.hour = Some(value.floor().to_i64().unwrap() as i32);
|
||||||
// TODO: Rescope `value` here?
|
// TODO: Rescope `value` here?
|
||||||
value = self.to_decimal(&tokens[idx + 2]);
|
value = self.to_decimal(&tokens[idx + 2])?;
|
||||||
let min_sec = self.parse_min_sec(value);
|
let min_sec = self.parse_min_sec(value);
|
||||||
res.minute = Some(min_sec.0);
|
res.minute = Some(min_sec.0);
|
||||||
res.second = min_sec.1;
|
res.second = min_sec.1;
|
||||||
@ -1153,7 +1151,9 @@ impl Parser {
|
|||||||
res.hour = Some(self.adjust_ampm(hour, ampm));
|
res.hour = Some(self.adjust_ampm(hour, ampm));
|
||||||
idx += 1;
|
idx += 1;
|
||||||
} else {
|
} else {
|
||||||
ymd.append(value.floor().to_i64().unwrap() as i32, &value_repr, None)?;
|
//let value = value.floor().to_i32().ok_or(Err(ParseError::InvalidNumeric()))
|
||||||
|
let value = value.floor().to_i32().ok_or_else(|| ParseError::InvalidNumeric(value_repr.to_owned()))?;
|
||||||
|
ymd.append(value, &value_repr, None)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
idx += 1;
|
idx += 1;
|
||||||
@ -1266,11 +1266,11 @@ impl Parser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn assign_hms(&self, res: &mut ParsingResult, value_repr: &str, hms: usize) {
|
fn assign_hms(&self, res: &mut ParsingResult, value_repr: &str, hms: usize) -> ParseResult<()> {
|
||||||
let value = self.to_decimal(value_repr);
|
let value = self.to_decimal(value_repr)?;
|
||||||
|
|
||||||
if hms == 0 {
|
if hms == 0 {
|
||||||
res.hour = Some(value.to_i64().unwrap() as i32);
|
res.hour = value.to_i32();
|
||||||
if !close_to_integer(&value) {
|
if !close_to_integer(&value) {
|
||||||
res.minute = Some((*SIXTY * (value % *ONE)).to_i64().unwrap() as i32);
|
res.minute = Some((*SIXTY * (value % *ONE)).to_i64().unwrap() as i32);
|
||||||
}
|
}
|
||||||
@ -1283,11 +1283,12 @@ impl Parser {
|
|||||||
res.second = Some(sec);
|
res.second = Some(sec);
|
||||||
res.microsecond = Some(micro);
|
res.microsecond = Some(micro);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_decimal(&self, value: &str) -> Decimal {
|
fn to_decimal(&self, value: &str) -> ParseResult<Decimal> {
|
||||||
// TODO: Justify unwrap
|
Decimal::from_str(value).or_else(|_| Err(ParseError::InvalidNumeric(value.to_owned())))
|
||||||
Decimal::from_str(value).unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_min_sec(&self, value: Decimal) -> (i32, Option<i32>) {
|
fn parse_min_sec(&self, value: Decimal) -> (i32, Option<i32>) {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use chrono::NaiveDate;
|
use chrono::NaiveDate;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::str;
|
||||||
|
|
||||||
use parse;
|
use parse;
|
||||||
use ParseError;
|
use ParseError;
|
||||||
@ -60,3 +61,37 @@ fn an_even_larger_int() {
|
|||||||
fn empty_string() {
|
fn empty_string() {
|
||||||
assert_eq!(parse(""), Err(ParseError::NoDate))
|
assert_eq!(parse(""), Err(ParseError::NoDate))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn github_33() {
|
||||||
|
assert_eq!(parse("66:'"), Err(ParseError::InvalidNumeric("'".to_owned())))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn github_32() {
|
||||||
|
assert_eq!(parse("99999999999999999999999"), Err(ParseError::InvalidNumeric("99999999999999999999999".to_owned())))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn github_34() {
|
||||||
|
let parse_vec = base64::decode("KTMuLjYpGDYvLjZTNiouNjYuHzZpLjY/NkwuNh42Ry42PzYnKTMuNk02NjY2NjA2NjY2NjY2NjYTNjY2Ni82NjY2NlAuNlAuNlNI").unwrap();
|
||||||
|
let parse_str = str::from_utf8(&parse_vec).unwrap();
|
||||||
|
let parse_result = parse(parse_str);
|
||||||
|
assert!(parse_result.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn github_35() {
|
||||||
|
let parse_vec = base64::decode("KTY6LjYqNio6KjYn").unwrap();
|
||||||
|
let parse_str = str::from_utf8(&parse_vec).unwrap();
|
||||||
|
let parse_result = parse(parse_str);
|
||||||
|
assert!(parse_result.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn github_36() {
|
||||||
|
let parse_vec = base64::decode("KTYuLg==").unwrap();
|
||||||
|
let parse_str = str::from_utf8(&parse_vec).unwrap();
|
||||||
|
let parse_result = parse(parse_str);
|
||||||
|
assert!(parse_result.is_err());
|
||||||
|
}
|
Reference in New Issue
Block a user