home ~ projects ~ socials

Basic Date Parsing In Rust

//! ```cargo
//! [dependencies]
//! chrono = "0.4.31"
//! ```

use chrono::prelude::*;

fn main() {
  let date_string = "2023-12-21 13:46:47";
  let hour = 3600;
  let tz = FixedOffset::west_opt(5 * hour).unwrap();
  let ndt = NaiveDateTime::parse_from_str(date_string, "%Y-%m-%d %H:%M:%S")
    .unwrap()
    .and_local_timezone(tz)
    .unwrap();
  println!("{}", ndt);
}
Output:
2023-12-21 13:46:47 +05:00
-- end of line --