Rust Chrono 解析日期字符串、ParseError(NotEnough) 和 ParseError(TooShort) [英] Rust Chrono parse date String, ParseError(NotEnough) and ParseError(TooShort)
问题描述
如何将字符串转换为 chrono::DateTime 或 chrono::NaiveDateTime
How to convert a String to a chrono::DateTime or chrono::NaiveDateTime
ParseError(NotEnough) 或 ParseError(TooShort) 是什么意思?
And what does the ParseError(NotEnough) or ParseError(TooShort) mean?
推荐答案
将 String 转换为 Chrono 对象时,您必须知道该字符串的输入格式包含哪些部分.
这些部分是:日期、时间、时区
示例:
When converting a String into a Chrono object you have to know what parts the input format of the string has.
The parts are: Date, Time, TimeZone
Examples:
- "2020-04-12"=>日期 = NaiveDate
- 22:10"=>时间 = NaiveTime
- "2020-04-12 22:10:57"=>日期 + 时间 = NaiveDateTime
- 2020-04-12 22:10:57+02:00"=>日期 + 时间 + 时区 = DateTime
ParseError(NotEnough) 当没有足够的信息来填充整个对象时会出现.例如,缺少日期、时间或时区.
当格式与字符串不匹配时,您会收到 ParseError(TooShort) 或 ParseError(Invalid) 错误.
The ParseError(NotEnough) shows up when there is not enough information to fill out the whole object. For example the date, time or timezone is missing.
When the formats doesn't match the string you get a ParseError(TooShort) or ParseError(Invalid) error.
字符串格式的规范,例如%Y-%m-%d %H:%M:%S"
:https://docs.rs/chrono/latest/chrono/format/strftime/index.html
Specification for string format e.g. "%Y-%m-%d %H:%M:%S"
: https://docs.rs/chrono/latest/chrono/format/strftime/index.html
要转换 RFC2822 字符串,请使用 parse_from_rfc2822(..) 函数.
To convert a RFC2822 string use the parse_from_rfc2822(..) function.
let date_str = "Tue, 1 Jul 2003 10:52:37 +0200";
let datetime = DateTime::parse_from_rfc2822(date_str).unwrap();
RFC3339 = 日期 + 时间 + 时区
要转换 RFC3339 或 ISO 8601 字符串,请使用 parse_from_rfc3339(..) 函数.
RFC3339 = Date + Time + TimeZone
To convert a RFC3339 or ISO 8601 string use the parse_from_rfc3339(..) function.
let date_str = "2020-04-12T22:10:57+02:00";
// convert the string into DateTime<FixedOffset>
let datetime = DateTime::parse_from_rfc3339(date_str).unwrap();
// convert the string into DateTime<Utc> or other timezone
let datetime_utc = datetime.with_timezone(&Utc);
日期 + 时间 + 时区(其他或非标准)
要转换其他日期时间字符串,请使用 parse_from_str(..) 函数.
let date_str = "2020-04-12 22:10:57 +02:00";
let datetime = DateTime::parse_from_str(date_str, "%Y-%m-%d %H:%M:%S %z").unwrap();
日期 + 时间
当您没有时区时,您需要使用 NaiveDateTime.此对象不存储时区:
let date_str = "2020-04-12 22:10:57";
let naive_datetime = NaiveDateTime::parse_from_str(date_str, "%Y-%m-%d %H:%M:%S").unwrap();
日期
如果我们解析日期(没有时间),我们可以将它存储在 NaiveDate.此对象不存储时间或时区:
Date
If we where parsing a date (with no time) we can store it in a NaiveDate. This object does not store time or a timezone:
let date_str = "2020-04-12";
let naive_date = NaiveDate::parse_from_str(date_str, "%Y-%m-%d").unwrap();
时间
如果我们解析时间(没有日期),我们可以将它存储在 NaiveTime.此对象不存储日期或时区:
Time
If we where parsing a time (with no date) we can store it in a NaiveTime. This object does not store a date or a timezone:
let time_str = "22:10:57";
let naive_time = NaiveTime::parse_from_str(time_str, "%H:%M:%S").unwrap();
添加日期、时间和/或时区
如果我们有一些字符串并且想要添加更多信息,我们可以更改类型.但您必须自己提供这些信息.
Add Date, Time and/or Timezone
If we have some string and want to add more information we can change the type. But you have to provide this information yourself.
let date_str = "2020-04-12";
// From string to a NaiveDate
let naive_date = NaiveDate::parse_from_str(date_str, "%Y-%m-%d").unwrap();
// Add some default time to convert it into a NaiveDateTime
let naive_datetime: NaiveDateTime = naive_date.and_hms(0,0,0);
// Add a timezone to the object to convert it into a DateTime<UTC>
let datetime_utc = DateTime::<Utc>::from_utc(naive_datetime, Utc);
示例代码游乐场://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d2b83b3980a5f8fb2e798271766b4541
这篇关于Rust Chrono 解析日期字符串、ParseError(NotEnough) 和 ParseError(TooShort)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!