Replies: 1 comment 3 replies
-
Rocket already supports date/time formats using time: https://api.rocket.rs/v0.5-rc/rocket/form/trait.FromForm.html#provided-implementations . #[macro_use]
extern crate rocket;
use rocket::form::Form;
use rocket::time::PrimitiveDateTime;
#[derive(FromForm, Debug)]
struct Task<'r> {
name: &'r str,
due: PrimitiveDateTime,
}
#[post("/", data = "<task>")]
fn index(task: Form<Task<'_>>) -> String {
format!("{task:?}")
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
}
#[cfg(test)]
mod test {
use super::rocket;
use rocket::http::{ContentType, Status};
use rocket::local::blocking::Client;
#[test]
fn it_works() {
let client = Client::tracked(rocket()).expect("valid rocket instance");
let response = client
.post(uri!(super::index))
.header(ContentType::Form)
.body("name=Run&due=2023-12-25T11:00")
.dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(
response.into_string().unwrap(),
"Form(Task { name: \"Run\", due: 2023-12-25 11:00:00.0 })"
);
}
} So you could use this functionality and map from that to chrono DateTime if you still need aditional functionality from the chrono crate. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is there support in rocket for chrono::DateTime or another datetime format? I need to handle forms that come with date data on it and can't find any imports.
Beta Was this translation helpful? Give feedback.
All reactions