Skip to content

Commit 3a4ad14

Browse files
sgrifcarols10cents
authored andcommitted
Remove the time crate
This replaces all remaining usage of the deprecated `time` crate with the `chrono` crate instead.
1 parent eb0f315 commit 3a4ad14

13 files changed

+53
-74
lines changed

Cargo.lock

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ rustdoc-args = [
2929
[dependencies]
3030
cargo-registry-s3 = { path = "src/s3", version = "0.2.0" }
3131
rand = "0.3"
32-
time = "0.1"
3332
git2 = "0.6.4"
3433
flate2 = "0.2"
3534
semver = "0.5"
@@ -46,7 +45,7 @@ hex = "0.2"
4645
license-exprs = "^1.3"
4746
dotenv = "0.10.0"
4847
toml = "0.4"
49-
diesel = { version = "0.16.0", features = ["postgres", "serde_json", "deprecated-time", "chrono"] }
48+
diesel = { version = "0.16.0", features = ["postgres", "serde_json", "chrono"] }
5049
diesel_codegen = "0.16.0"
5150
r2d2-diesel = "0.16.0"
5251
diesel_full_text_search = "0.16.0"

src/bin/render-readmes.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ extern crate docopt;
1616
extern crate flate2;
1717
extern crate itertools;
1818
extern crate tar;
19-
extern crate time;
2019
extern crate toml;
2120
extern crate url;
2221

@@ -180,7 +179,7 @@ fn get_readme(config: &Config, version: &Version, krate_name: &str) -> Option<St
180179
Some(l) => l,
181180
None => return None,
182181
};
183-
let date = time::now().rfc822z().to_string();
182+
let date = Utc::now().to_rfc2822();
184183
let url = Url::parse(&location).expect(&format!(
185184
"[{}-{}] Couldn't parse crate URL",
186185
krate_name,

src/category.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use time::Timespec;
2-
1+
use chrono::NaiveDateTime;
32
use conduit::{Request, Response};
43
use conduit_router::RequestParams;
54
use diesel::*;
@@ -17,7 +16,7 @@ pub struct Category {
1716
pub slug: String,
1817
pub description: String,
1918
pub crates_cnt: i32,
20-
pub created_at: Timespec,
19+
pub created_at: NaiveDateTime,
2120
}
2221

2322
#[derive(Associations, Insertable, Identifiable, Debug, Clone, Copy)]
@@ -36,7 +35,7 @@ pub struct EncodableCategory {
3635
pub category: String,
3736
pub slug: String,
3837
pub description: String,
39-
pub created_at: String,
38+
pub created_at: NaiveDateTime,
4039
pub crates_cnt: i32,
4140
}
4241

@@ -46,7 +45,7 @@ pub struct EncodableCategoryWithSubcategories {
4645
pub category: String,
4746
pub slug: String,
4847
pub description: String,
49-
pub created_at: String,
48+
pub created_at: NaiveDateTime,
5049
pub crates_cnt: i32,
5150
pub subcategories: Vec<EncodableCategory>,
5251
}
@@ -65,7 +64,7 @@ impl Category {
6564
id: slug.clone(),
6665
slug: slug.clone(),
6766
description: description.clone(),
68-
created_at: ::encode_time(created_at),
67+
created_at: created_at,
6968
crates_cnt: crates_cnt,
7069
category: category,
7170
}
@@ -148,7 +147,7 @@ impl Category {
148147
use diesel::expression::dsl::*;
149148
use diesel::types::Text;
150149

151-
sql(
150+
sql::<categories::SqlType>(
152151
"SELECT c.id, c.category, c.slug, c.description, \
153152
COALESCE (( \
154153
SELECT sum(c2.crates_cnt)::int \

src/crate_owner_invitation.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use chrono::NaiveDateTime;
12
use conduit::{Request, Response};
23
use diesel::prelude::*;
3-
use time::Timespec;
44
use serde_json;
55

66
use db::RequestTransaction;
@@ -17,7 +17,7 @@ pub struct CrateOwnerInvitation {
1717
pub invited_user_id: i32,
1818
pub invited_by_user_id: i32,
1919
pub crate_id: i32,
20-
pub created_at: Timespec,
20+
pub created_at: NaiveDateTime,
2121
}
2222

2323
#[derive(Insertable, Clone, Copy, Debug)]
@@ -50,7 +50,7 @@ impl CrateOwnerInvitation {
5050
invited_by_username: self.invited_by_username(conn),
5151
crate_name: self.crate_name(conn),
5252
crate_id: self.crate_id,
53-
created_at: ::encode_time(self.created_at),
53+
created_at: self.created_at,
5454
}
5555
}
5656
}
@@ -61,7 +61,7 @@ pub struct EncodableCrateOwnerInvitation {
6161
pub invited_by_username: String,
6262
pub crate_name: String,
6363
pub crate_id: i32,
64-
pub created_at: String,
64+
pub created_at: NaiveDateTime,
6565
}
6666

6767
/// Handles the `GET /me/crate_owner_invitations` route.

src/keyword.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ascii::AsciiExt;
2-
use time::Timespec;
32

3+
use chrono::NaiveDateTime;
44
use conduit::{Request, Response};
55
use conduit_router::RequestParams;
66
use diesel::prelude::*;
@@ -17,7 +17,7 @@ pub struct Keyword {
1717
pub id: i32,
1818
pub keyword: String,
1919
pub crates_cnt: i32,
20-
pub created_at: Timespec,
20+
pub created_at: NaiveDateTime,
2121
}
2222

2323
#[derive(Associations, Insertable, Identifiable, Debug, Clone, Copy)]
@@ -34,7 +34,7 @@ pub struct CrateKeyword {
3434
pub struct EncodableKeyword {
3535
pub id: String,
3636
pub keyword: String,
37-
pub created_at: String,
37+
pub created_at: NaiveDateTime,
3838
pub crates_cnt: i32,
3939
}
4040

@@ -92,7 +92,7 @@ impl Keyword {
9292
} = self;
9393
EncodableKeyword {
9494
id: keyword.clone(),
95-
created_at: ::encode_time(created_at),
95+
created_at: created_at,
9696
crates_cnt: crates_cnt,
9797
keyword: keyword,
9898
}

src/krate/mod.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::ascii::AsciiExt;
22
use std::cmp;
33
use std::collections::HashMap;
44

5+
use chrono::{NaiveDate, NaiveDateTime};
56
use conduit::{Request, Response};
67
use conduit_router::RequestParams;
78
use diesel::associations::Identifiable;
@@ -15,9 +16,7 @@ use license_exprs;
1516
use hex::ToHex;
1617
use serde_json;
1718
use semver;
18-
use time::Timespec;
1919
use url::Url;
20-
use chrono::NaiveDate;
2120

2221
use app::{App, RequestApp};
2322
use badge::EncodableBadge;
@@ -56,8 +55,8 @@ pub struct CrateDownload {
5655
pub struct Crate {
5756
pub id: i32,
5857
pub name: String,
59-
pub updated_at: Timespec,
60-
pub created_at: Timespec,
58+
pub updated_at: NaiveDateTime,
59+
pub created_at: NaiveDateTime,
6160
pub downloads: i32,
6261
pub description: Option<String>,
6362
pub homepage: Option<String>,
@@ -108,12 +107,12 @@ type CrateQuery<'a> = crates::BoxedQuery<'a, Pg, <AllColumns as Expression>::Sql
108107
pub struct EncodableCrate {
109108
pub id: String,
110109
pub name: String,
111-
pub updated_at: String,
110+
pub updated_at: NaiveDateTime,
112111
pub versions: Option<Vec<i32>>,
113112
pub keywords: Option<Vec<String>>,
114113
pub categories: Option<Vec<String>>,
115114
pub badges: Option<Vec<EncodableBadge>>,
116-
pub created_at: String,
115+
pub created_at: NaiveDateTime,
117116
pub downloads: i32,
118117
pub recent_downloads: Option<i64>,
119118
pub max_version: String,
@@ -370,8 +369,8 @@ impl Crate {
370369
EncodableCrate {
371370
id: name.clone(),
372371
name: name.clone(),
373-
updated_at: ::encode_time(updated_at),
374-
created_at: ::encode_time(created_at),
372+
updated_at: updated_at,
373+
created_at: created_at,
375374
downloads: downloads,
376375
recent_downloads: recent_downloads,
377376
versions: versions,

src/lib.rs

-13
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ extern crate serde_derive;
3939
#[macro_use]
4040
extern crate serde_json;
4141
extern crate tar;
42-
extern crate time;
4342
extern crate toml;
4443
extern crate url;
4544

@@ -298,18 +297,6 @@ pub fn middleware(app: Arc<App>) -> MiddlewareBuilder {
298297
}
299298
}
300299

301-
/// Convenience function for getting the current server time in UTC.
302-
pub fn now() -> time::Timespec {
303-
time::now_utc().to_timespec()
304-
}
305-
306-
/// Convenience function for getting a time in RFC 3339 format.
307-
///
308-
/// Example: `2012-02-22T14:53:18Z`. Used for returning time values in JSON API responses.
309-
pub fn encode_time(ts: time::Timespec) -> String {
310-
time::at_utc(ts).rfc3339().to_string()
311-
}
312-
313300
/// Convenience function requiring that an environment variable is set.
314301
///
315302
/// Ensures that we've initialized the dotenv crate in order to read environment variables

src/tests/all.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ extern crate serde_derive;
2020
#[macro_use]
2121
extern crate serde_json;
2222
extern crate tar;
23-
extern crate time;
2423
extern crate url;
2524

2625
use std::borrow::Cow;
@@ -41,6 +40,7 @@ use cargo_registry::owner::{CrateOwner, NewTeam, Team};
4140
use cargo_registry::version::NewVersion;
4241
use cargo_registry::user::AuthenticationSource;
4342
use cargo_registry::{Crate, Dependency, Replica, User, Version};
43+
use chrono::Utc;
4444
use conduit::{Method, Request};
4545
use conduit_test::MockRequest;
4646
use diesel::prelude::*;
@@ -412,7 +412,7 @@ impl<'a> CrateBuilder<'a> {
412412
// crate properties in a single DB call.
413413

414414
let old_downloads = self.downloads.unwrap_or(0) - self.recent_downloads.unwrap_or(0);
415-
let now = chrono::Utc::now();
415+
let now = Utc::now();
416416
let old_date = now.naive_utc().date() - chrono::Duration::days(91);
417417

418418
if let Some(downloads) = self.downloads {
@@ -473,8 +473,8 @@ fn krate(name: &str) -> Crate {
473473
cargo_registry::krate::Crate {
474474
id: NEXT_ID.fetch_add(1, Ordering::SeqCst) as i32,
475475
name: name.to_string(),
476-
updated_at: time::now().to_timespec(),
477-
created_at: time::now().to_timespec(),
476+
updated_at: Utc::now().naive_utc(),
477+
created_at: Utc::now().naive_utc(),
478478
downloads: 10,
479479
documentation: None,
480480
homepage: None,

src/tests/krate.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ fn summary_doesnt_die() {
984984

985985
#[test]
986986
fn download() {
987-
use time::{now_utc, strftime, Duration};
987+
use chrono::{Duration, Utc};
988988
let (_b, app, middle) = ::app();
989989
let mut req = ::req(
990990
app.clone(),
@@ -1023,27 +1023,27 @@ fn download() {
10231023
let downloads = ::json::<Downloads>(&mut resp);
10241024
assert_eq!(downloads.version_downloads.len(), 1);
10251025

1026-
let yesterday = now_utc() + Duration::days(-1);
1026+
let yesterday = Utc::today() + Duration::days(-1);
10271027
req.with_path("/api/v1/crates/FOO_DOWNLOAD/1.0.0/downloads");
1028-
req.with_query(&("before_date=".to_string() + &strftime("%Y-%m-%d", &yesterday).unwrap()));
1028+
req.with_query(&format!("before_date={}", yesterday.format("%F")));
10291029
let mut resp = ok_resp!(middle.call(&mut req));
10301030
let downloads = ::json::<Downloads>(&mut resp);
10311031
assert_eq!(downloads.version_downloads.len(), 0);
10321032
req.with_path("/api/v1/crates/FOO_DOWNLOAD/downloads");
1033-
req.with_query(&("before_date=".to_string() + &strftime("%Y-%m-%d", &yesterday).unwrap()));
1033+
req.with_query(&format!("before_date={}", yesterday.format("%F")));
10341034
let mut resp = ok_resp!(middle.call(&mut req));
10351035
let downloads = ::json::<Downloads>(&mut resp);
10361036
// crate/downloads always returns the last 90 days and ignores date params
10371037
assert_eq!(downloads.version_downloads.len(), 1);
10381038

1039-
let tomorrow = now_utc() + Duration::days(1);
1039+
let tomorrow = Utc::today() + Duration::days(1);
10401040
req.with_path("/api/v1/crates/FOO_DOWNLOAD/1.0.0/downloads");
1041-
req.with_query(&("before_date=".to_string() + &strftime("%Y-%m-%d", &tomorrow).unwrap()));
1041+
req.with_query(&format!("before_date={}", tomorrow.format("%F")));
10421042
let mut resp = ok_resp!(middle.call(&mut req));
10431043
let downloads = ::json::<Downloads>(&mut resp);
10441044
assert_eq!(downloads.version_downloads.len(), 1);
10451045
req.with_path("/api/v1/crates/FOO_DOWNLOAD/downloads");
1046-
req.with_query(&("before_date=".to_string() + &strftime("%Y-%m-%d", &tomorrow).unwrap()));
1046+
req.with_query(&format!("before_date={}", tomorrow.format("%F")));
10471047
let mut resp = ok_resp!(middle.call(&mut req));
10481048
let downloads = ::json::<Downloads>(&mut resp);
10491049
assert_eq!(downloads.version_downloads.len(), 1);

src/token.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use diesel;
2-
use diesel::prelude::*;
31
use chrono::NaiveDateTime;
42
use conduit::{Request, Response};
53
use conduit_router::RequestParams;
4+
use diesel::prelude::*;
5+
use diesel;
66
use serde_json as json;
77

88
use db::RequestTransaction;

src/user/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
use chrono::NaiveDateTime;
12
use conduit::{Request, Response};
23
use conduit_cookie::RequestSession;
34
use conduit_router::RequestParams;
5+
use diesel::expression::now;
46
use diesel::prelude::*;
57
use rand::{thread_rng, Rng};
68
use std::borrow::Cow;
79
use serde_json;
8-
use time::Timespec;
910

1011
use app::RequestApp;
1112
use db::RequestTransaction;
@@ -69,7 +70,7 @@ pub struct Token {
6970
pub id: i32,
7071
pub email_id: i32,
7172
pub token: String,
72-
pub created_at: Timespec,
73+
pub created_at: NaiveDateTime,
7374
}
7475

7576
#[derive(Debug, Insertable, AsChangeset)]
@@ -212,7 +213,6 @@ impl User {
212213
/// Queries the database for a user with a certain `api_token` value.
213214
pub fn find_by_api_token(conn: &PgConnection, token_: &str) -> CargoResult<User> {
214215
use diesel::update;
215-
use diesel::expression::now;
216216
use schema::api_tokens::dsl::{api_tokens, last_used_at, token, user_id};
217217
use schema::users::dsl::{id, users};
218218
let user_id_ = update(api_tokens.filter(token.eq(token_)))

0 commit comments

Comments
 (0)