Parsing datetimes like: 2022-10-25 02:53:28 +0100 BST
#394
-
|
I have this date time |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
Here's a concrete answer to your question: use jiff::fmt::strtime::BrokenDownTime;
fn main() -> anyhow::Result<()> {
let input = "2022-10-25 02:53:28 +0100 BST";
let (tm, _) = BrokenDownTime::parse_prefix("%Y-%m-%d %H:%M:%S %z", input)?;
let ts = tm.to_timestamp()?;
println!("{ts}");
Ok(())
}And its output: Or, if you want to parse into a use jiff::fmt::strtime::BrokenDownTime;
fn main() -> anyhow::Result<()> {
let input = "2022-10-25 02:53:28 +0100 BST";
let (tm, _) = BrokenDownTime::parse_prefix("%Y-%m-%d %H:%M:%S %z", input)?;
let zdt = tm.to_zoned()?;
println!("{zdt}");
Ok(())
}And its output: There are two problems with strings like The first is that it's a completely non-standard format. That makes it an exceptionally poor choice for structured data meant to be parsed by machines, as suggested by the The second is that This means that it's not possible to take The code sample shown above works by completely ignoring the |
Beta Was this translation helpful? Give feedback.
-
|
Also, thank you for asking this question. It's a perfect example of why a datetime library benefits from ad hoc parsing routines like what Jiff's |
Beta Was this translation helpful? Give feedback.
Here's a concrete answer to your question:
And its output:
Or, if you want to parse into a
Zoned:And its output: