Home > Enterprise >  How do I get a SystemTime from a human-readable date format in a cross-platform way? (Rust)
How do I get a SystemTime from a human-readable date format in a cross-platform way? (Rust)

Time:01-03

How would I convert a human readable time (in any format, such as Tue, 1 Jul 2003 10:52:37 0200) into a SystemTime, in a cross-platform way? I know about chrono::DateTime::from_rfc2822(), but I've been searching for quite a while and I can't find a way to convert a DateTime into a SystemTime. This conversion also needs to be cross-platform, so I can't use the platform-specific epochs (such as UNIX_EPOCH).

Does anyone have any advice or ideas on how to do this?

CodePudding user response:

There is a conversion available for DateTime<Tz> to SystemTime, so you just need to call .into():

let system_time: SystemTime = DateTime::parse_from_rfc2822("Tue, 1 Jul 2003 10:52:37  0200").unwrap().into();

Playground

  • Related