mirror of
https://github.com/bspeice/dtparse
synced 2025-07-13 11:44:57 -04:00
Compare commits
12 Commits
v1.1.0
...
ed919e84ef
Author | SHA1 | Date | |
---|---|---|---|
ed919e84ef | |||
4d8ade4b05 | |||
6a88885ef5 | |||
a193a79afa | |||
5b3be160f6 | |||
8f6a3b179d | |||
028c45e3fe | |||
f1ca4e4129 | |||
61d3ed025e | |||
440ba918f6 | |||
fe773b0d9f | |||
bf456f466f |
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "dtparse"
|
||||
version = "1.1.0"
|
||||
version = "1.3.0"
|
||||
authors = ["Bradlee Speice <bradlee@speice.io>"]
|
||||
description = "A dateutil-compatible timestamp parser for Rust"
|
||||
repository = "https://github.com/bspeice/dtparse.git"
|
||||
@ -17,7 +17,9 @@ name = "dtparse"
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4"
|
||||
chrono-tz = "0.5"
|
||||
lazy_static = "1.1"
|
||||
num-traits = "0.2"
|
||||
rust_decimal = "^0.10.1"
|
||||
rust_decimal = { version = "1.17.0", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
base64 = "0.13"
|
||||
|
33
src/lib.rs
33
src/lib.rs
@ -74,7 +74,6 @@
|
||||
extern crate lazy_static;
|
||||
|
||||
extern crate chrono;
|
||||
extern crate chrono_tz;
|
||||
extern crate num_traits;
|
||||
extern crate rust_decimal;
|
||||
|
||||
@ -951,8 +950,7 @@ impl Parser {
|
||||
let m = res.month.unwrap_or_else(|| default.month() as i32) as u32;
|
||||
|
||||
let d_offset = if res.weekday.is_some() && res.day.is_none() {
|
||||
// TODO: Unwrap not justified
|
||||
let dow = day_of_week(y as u32, m, default.day()).unwrap();
|
||||
let dow = day_of_week(y as u32, m, default.day())?;
|
||||
|
||||
// UNWRAP: We've already check res.weekday() is some
|
||||
let actual_weekday = (res.weekday.unwrap() + 1) % 7;
|
||||
@ -963,14 +961,15 @@ impl Parser {
|
||||
};
|
||||
|
||||
// TODO: Change month/day to u32
|
||||
let d = NaiveDate::from_ymd(
|
||||
let d = NaiveDate::from_ymd_opt(
|
||||
y,
|
||||
m,
|
||||
min(
|
||||
res.day.unwrap_or(default.day() as i32) as u32,
|
||||
days_in_month(y, m as i32)?,
|
||||
),
|
||||
);
|
||||
)
|
||||
.ok_or_else(|| ParseError::ImpossibleTimestamp("Invalid date range given"))?;
|
||||
|
||||
let d = d + d_offset;
|
||||
|
||||
@ -1092,9 +1091,8 @@ impl Parser {
|
||||
} else if let Some(hms_idx) = self.find_hms_index(idx, tokens, info, true) {
|
||||
// HH[ ]h or MM[ ]m or SS[.ss][ ]s
|
||||
let (new_idx, hms) = self.parse_hms(idx, tokens, info, Some(hms_idx));
|
||||
if hms.is_some() {
|
||||
// TODO: This unwrap is unjustified.
|
||||
self.assign_hms(res, value_repr, hms.unwrap());
|
||||
if let Some(hms) = hms {
|
||||
self.assign_hms(res, value_repr, hms)?;
|
||||
}
|
||||
idx = new_idx;
|
||||
} else if idx + 2 < len_l && tokens[idx + 1] == ":" {
|
||||
@ -1102,7 +1100,7 @@ impl Parser {
|
||||
// TODO: Better story around Decimal handling
|
||||
res.hour = Some(value.floor().to_i64().unwrap() as i32);
|
||||
// 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);
|
||||
res.minute = Some(min_sec.0);
|
||||
res.second = min_sec.1;
|
||||
@ -1153,7 +1151,9 @@ impl Parser {
|
||||
res.hour = Some(self.adjust_ampm(hour, ampm));
|
||||
idx += 1;
|
||||
} 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;
|
||||
@ -1266,11 +1266,11 @@ impl Parser {
|
||||
}
|
||||
}
|
||||
|
||||
fn assign_hms(&self, res: &mut ParsingResult, value_repr: &str, hms: usize) {
|
||||
let value = self.to_decimal(value_repr);
|
||||
fn assign_hms(&self, res: &mut ParsingResult, value_repr: &str, hms: usize) -> ParseResult<()> {
|
||||
let value = self.to_decimal(value_repr)?;
|
||||
|
||||
if hms == 0 {
|
||||
res.hour = Some(value.to_i64().unwrap() as i32);
|
||||
res.hour = value.to_i32();
|
||||
if !close_to_integer(&value) {
|
||||
res.minute = Some((*SIXTY * (value % *ONE)).to_i64().unwrap() as i32);
|
||||
}
|
||||
@ -1283,11 +1283,12 @@ impl Parser {
|
||||
res.second = Some(sec);
|
||||
res.microsecond = Some(micro);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn to_decimal(&self, value: &str) -> Decimal {
|
||||
// TODO: Justify unwrap
|
||||
Decimal::from_str(value).unwrap()
|
||||
fn to_decimal(&self, value: &str) -> ParseResult<Decimal> {
|
||||
Decimal::from_str(value).or_else(|_| Err(ParseError::InvalidNumeric(value.to_owned())))
|
||||
}
|
||||
|
||||
fn parse_min_sec(&self, value: Decimal) -> (i32, Option<i32>) {
|
||||
|
@ -1,5 +1,6 @@
|
||||
use chrono::NaiveDate;
|
||||
use std::collections::HashMap;
|
||||
use std::str;
|
||||
|
||||
use parse;
|
||||
use ParseError;
|
||||
@ -44,7 +45,53 @@ fn large_int() {
|
||||
assert!(parse_result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn another_large_int() {
|
||||
let parse_result = parse("1412409095009");
|
||||
assert!(parse_result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_even_larger_int() {
|
||||
let parse_result = parse("1566997680962280");
|
||||
assert!(parse_result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_string() {
|
||||
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