Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for chrono::NaiveDateTime in qs #82

Open
thiskevinwang opened this issue Aug 11, 2020 · 5 comments
Open

Example for chrono::NaiveDateTime in qs #82

thiskevinwang opened this issue Aug 11, 2020 · 5 comments

Comments

@thiskevinwang
Copy link

thiskevinwang commented Aug 11, 2020

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

@nox
Copy link
Owner

nox commented Aug 11, 2020

Did you check what [("foo", some_naive_date_time)] encode to? Does it work? Does it then fail to roundtrip?

@thiskevinwang
Copy link
Author

thiskevinwang commented Aug 11, 2020

Hey @nox thanks for responding!

I hope I'm doing this "roundtrip test" correctly:

  • encoding NaiveDateTime to String works ✅
  • encoding str to NaiveDateTime does not work ❌

Playground

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

fn main() {
    // ✅ Good
    // let's encode `[("foo", some_naive_date_time)]`
    let some_naive_date_time = NaiveDateTime::from_timestamp(1_000_000_000, 0);
    assert_eq!(some_naive_date_time, NaiveDate::from_ymd(2001, 9, 9).and_hms(1, 46, 40));
    
    let res = serde_urlencoded::to_string(&[("foo", some_naive_date_time)]);
    println!("{:?}", res);
    // Ok("foo=2001-09-09T01%3A46%3A40")
    
    // "round trip" fails! 😭
    let qs = "foo=2001-09-09T01%3A46%3A40";
    let res = serde_urlencoded::from_str::<Vec<(String, NaiveDate)>>(qs);
    println!("{:?}", res);
    // Err(Error { err: "trailing input" })
}

@nox
Copy link
Owner

nox commented Aug 11, 2020

Interesting, will look into that, I guess I have quite some free time right now hah.

@thiskevinwang
Copy link
Author

That would be awesome.
I'm using https://github.com/seanmonstar/warp, which uses serde_urlencoded internally

@nox
Copy link
Owner

nox commented Aug 13, 2020

Note that I didn't forget about that but I got laid off so I'm just a bit distracted. :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants