Skip to content

Example for chrono::NaiveDateTime in qs #82

Description

@thiskevinwang

Description

Is there an example of how to pass a chrono::NaiveDateTime in a querystring?

Here's a sample of what i'm struggling with

Playground

use serde_urlencoded; // 0.6.1
use chrono::{NaiveDate, NaiveDateTime}; // 0.4.13

fn main() {
    // ✅ Good
    let qs = "foo=2015-09-05%2011%3A01%3A01";
    let res = serde_urlencoded::from_str::<Vec<(String, String)>>(qs);
    println!("{:?}", res);

    // ✅ Good
    let qs = "foo=2020-01-01";
    let res = serde_urlencoded::from_str::<Vec<(String, NaiveDate)>>(qs);
    println!("{:?}", res);
    
    // ❌ Bad
    let qs = "foo=2015-09-05%2011%3A01%3A01";
    let res = serde_urlencoded::from_str::<Vec<(String, NaiveDateTime)>>(qs);
    println!("{:?}", res);
}

// Ok([("foo", "2015-09-05 11:01:01")])
// Ok([("foo", 2020-01-01)])
// Err(Error { err: "input contains invalid characters" })

End goal

  • Specify start/end dates in a querystring
  • Parse and deserialize them
  • Use them in a tokio-postgres SQL query for date filtering, where the column is timestamp

Something like (which might be absurd, but I'm curious if it's possible)
http://localhost:3000/users/a5f5d36a-6677-41c2-85b8-7578b4d98972/attempts?limit=15&offset=15&start=2020-08-09%2003%3A10%3A07%2E813517&end=2020-08-09%2003%3A10%3A07%2E813517

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions