Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upHow to convert PgTimestamp to JSON? #575
Comments
This comment has been minimized.
|
I used |
This comment has been minimized.
|
Btw. I've issued an issue in |
This comment has been minimized.
Boscop
commented
Jan 14, 2017
|
Thanks |
This comment has been minimized.
Boscop
commented
Jan 15, 2017
•
|
@golddranks Is this correct? fn time_to_json(t: NaiveDateTime) -> String {
DateTime::<UTC>::from_utc(t, UTC).to_rfc3339()
}This results in a string like And then in js: moment(new Date(post.timeStr)).fromNow()To show a post's relative time in the client's timezone using moment.js. |
This comment has been minimized.
|
I prefer using |
This comment has been minimized.
marcusball
commented
Jan 16, 2017
•
|
I don't know about using rustc_serialize, but if you use serde, I think you could use the Edit: I'm dumb, this is not necessary at all. If you're using chrono, you can just add either of the features |
This comment has been minimized.
Boscop
commented
Jan 17, 2017
•
|
@marcusball Good to know, but right now I'm converting the row struct to an api view struct anyway, because not all fields should be exposed to the json api (such as users' email). I want database::connection().get().unwrap().execute("SET TIME ZONE 'UTC';").unwrap();Would it be sufficient to do it once as a migration? |
killercup
added
postgres
question
labels
Feb 17, 2017
This comment has been minimized.
|
Not sure if there's still a question that needs answering here, but the structs prefixed with |
sgrif
closed this
Feb 24, 2017
This comment has been minimized.
|
I think that the question in a sense was: which types to use in application code models that are compatible with PgTimestamp and easily convert to JSON. To that question the answer seems now clear: use chrono::DateTime and Swede for serializing, that works out of the box. |
This comment has been minimized.
|
Serde, not Swede. Damn you smartphones XD |
This comment has been minimized.
Boscop
commented
Feb 25, 2017
•
|
I now use NaiveDateTime and store all my times in UTC, and convert them to client time in the frontend. |
Boscop commentedJan 13, 2017
Whats a good way to convert a diesel::data_types::PgTimestamp to JSON, since it's not RustcEncodable?