Rust chrono 提示解析错误(NotEnough)。

3
我正在尝试将日期时间字符串解析为DateTime对象,但是当我尝试这样做时,出现了解析错误。我不理解发生了什么,请有人帮我吗?
日期时间字符串:09-January-2018 12:00:00 代码:let date = DateTime :: parse_from_str(date.trim(),“%d-%B-%Y%T”);
1个回答

10
这是:
extern crate chrono;
use chrono::DateTime;
use std::error::Error;

fn main() {
    println!("{:?}", DateTime::parse_from_str("09-January-2018 12:00:00", "%d-%B-%Y %T").unwrap_err().description());
}

(https://play.rust-lang.org/?gist=9c0231ea189c589009a46308864dd9bc&version=stable)

提供了更多信息:

"input is not enough for unique date and time"

显然,DateTime需要时区信息,而您的输入中没有提供。使用NaiveDateTime应该可以解决问题:

extern crate chrono;
use chrono::NaiveDateTime;

fn main() {
    println!("{:?}", NaiveDateTime::parse_from_str("09-January-2018 12:00:00", "%d-%B-%Y %T"));
}

(https://play.rust-lang.org/?gist=1acbae616c7f084a748e4f9cfaf1ef7f&version=stable)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接