Skip to content

Include timezone when serializing JSON responses #1146

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

Merged
merged 1 commit into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct EncodableCategory {
pub category: String,
pub slug: String,
pub description: String,
pub created_at: NaiveDateTime,
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
pub crates_cnt: i32,
}

Expand All @@ -45,7 +45,7 @@ pub struct EncodableCategoryWithSubcategories {
pub category: String,
pub slug: String,
pub description: String,
pub created_at: NaiveDateTime,
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
pub crates_cnt: i32,
pub subcategories: Vec<EncodableCategory>,
}
Expand Down
2 changes: 1 addition & 1 deletion src/crate_owner_invitation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub struct EncodableCrateOwnerInvitation {
pub invited_by_username: String,
pub crate_name: String,
pub crate_id: i32,
pub created_at: NaiveDateTime,
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
}

/// Handles the `GET /me/crate_owner_invitations` route.
Expand Down
2 changes: 1 addition & 1 deletion src/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct CrateKeyword {
pub struct EncodableKeyword {
pub id: String,
pub keyword: String,
pub created_at: NaiveDateTime,
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
pub crates_cnt: i32,
}

Expand Down
4 changes: 2 additions & 2 deletions src/krate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ type ByName<'a> = diesel::dsl::Filter<All, WithName<'a>>;
pub struct EncodableCrate {
pub id: String,
pub name: String,
pub updated_at: NaiveDateTime,
#[serde(with = "::util::rfc3339")] pub updated_at: NaiveDateTime,
pub versions: Option<Vec<i32>>,
pub keywords: Option<Vec<String>>,
pub categories: Option<Vec<String>>,
pub badges: Option<Vec<EncodableBadge>>,
pub created_at: NaiveDateTime,
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
pub downloads: i32,
pub recent_downloads: Option<i64>,
pub max_version: String,
Expand Down
8 changes: 4 additions & 4 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub struct ApiToken {
#[serde(skip)] pub user_id: i32,
#[serde(skip)] pub token: String,
pub name: String,
pub created_at: NaiveDateTime,
pub last_used_at: Option<NaiveDateTime>,
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
#[serde(with = "::util::rfc3339::option")] pub last_used_at: Option<NaiveDateTime>,
}

/// The serialization format for the `ApiToken` model with its token value.
Expand All @@ -30,8 +30,8 @@ pub struct EncodableApiTokenWithToken {
pub id: i32,
pub name: String,
pub token: String,
pub created_at: NaiveDateTime,
pub last_used_at: Option<NaiveDateTime>,
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
#[serde(with = "::util::rfc3339::option")] pub last_used_at: Option<NaiveDateTime>,
}

impl ApiToken {
Expand Down
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub use self::io_util::{read_fill, LimitErrorReader, read_le_u32};
pub use self::request_proxy::RequestProxy;

pub mod errors;
pub mod rfc3339;
mod hasher;
mod head;
mod io_util;
Expand Down
48 changes: 48 additions & 0 deletions src/util/rfc3339.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! Convenience functions for serializing and deserializing times in RFC 3339 format.
//! Used for returning time values in JSON API responses.
//! Example: `2012-02-22T14:53:18+00:00`.

use chrono::{DateTime, NaiveDateTime, Utc};
use serde::{self, Deserialize, Deserializer, Serializer};

pub fn serialize<S>(dt: &NaiveDateTime, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let s = DateTime::<Utc>::from_utc(*dt, Utc).to_rfc3339();
serializer.serialize_str(&s)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<NaiveDateTime, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
let dt = DateTime::parse_from_rfc3339(&s).map_err(serde::de::Error::custom)?;
Ok(dt.naive_utc())
}

/// Wrapper for dealing with Option<NaiveDateTime>
pub mod option {
use chrono::NaiveDateTime;
use serde::{Deserializer, Serializer};

pub fn serialize<S>(dt: &Option<NaiveDateTime>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *dt {
Some(dt) => super::serialize(&dt, serializer),
None => serializer.serialize_none(),
}
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<NaiveDateTime>, D::Error>
where
D: Deserializer<'de>,
{
match super::deserialize(deserializer) {
Ok(dt) => Ok(Some(dt)),
Err(_) => Ok(None),
}
}
}
4 changes: 2 additions & 2 deletions src/version/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ pub struct EncodableVersion {
pub num: String,
pub dl_path: String,
pub readme_path: String,
pub updated_at: NaiveDateTime,
pub created_at: NaiveDateTime,
#[serde(with = "::util::rfc3339")] pub updated_at: NaiveDateTime,
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
pub downloads: i32,
pub features: HashMap<String, Vec<String>>,
pub yanked: bool,
Expand Down