I am trying to get some data using Bungie API. There is last played date in json response in format like "2022-09-18T02:17:59Z". I would like to parse it and create DateTime object.
I have following code (personal data replaced by XXX):
const API_ROOT: &str = "https://www.bungie.net/Platform";
const API_KEY_HEADER: &str = "X-API-Key";
const MY_API_KEY: &str = "XXX";
const MEMBERSHIP_TYPE: &str = "XXX";
const MEMBERSHIP_ID: &str = "XXX";
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let url = format!("{}/Destiny2/{}/Profile/{}/?components=100", API_ROOT, MEMBERSHIP_TYPE, MEMBERSHIP_ID);
let req = reqwest::Client::new()
.get(url)
.header(API_KEY_HEADER, MY_API_KEY)
.send()
.await?
.text()
.await?;
let data: JsonValue = serde_json::from_str(&req).expect("Failed to read json from response!");
let name = &data["Response"]["profile"]["data"]["userInfo"]["displayName"].to_string();
println!("Name: {}", name);
let last_played_raw = &data["Response"]["profile"]["data"]["dateLastPlayed"].to_string();
println!("{}", last_played_raw);
let last_played_raw = last_played_raw.replace("Z", "+00:00");
println!("{}", last_played_raw);
let last_played = DateTime::parse_from_rfc3339(&last_played_raw).unwrap();
println!("Last played: {}", last_played);
Ok(())
}
Problem is that parsing last_played_raw string panicks:
"2022-09-18T02:17:59Z"
"2022-09-18T02:17:59+00:00"
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ParseError(Invalid)', src\main.rs:37:70
Can somebody please tell me, what could be wrong?