遇到一个简单问题困扰。我有一个不包含时区信息的字符串需要解析成DateTime结构体。我可以将其作为UTC获取,但无法获取本地时间:
let from = NaiveDateTime::parse_from_str(&start_date, "%Y-%m-%dT%H:%M:%S")?;
let from_utc = DateTime::<Utc>::from_utc(from, Utc);
Local.from_local_datetime()
将NaiveDateTime
转换为DateTime<Local>
:let from: NaiveDateTime = start_date.parse().unwrap();
let date_time = Local.from_local_datetime(&from).unwrap();
不可否认,在文档中找到这部分内容确实不容易。
你提供的第一行代码同样有效。对于这种特定格式(RFC3339),使用str::parse()
更容易。