Skip to content

Commit de8b088

Browse files
committed
Start to switch from chrono to time crate
[RUSTSEC-2020-0159](https://rustsec.org/advisories/RUSTSEC-2020-0159)
1 parent 0590fd6 commit de8b088

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ serde = "1.0"
2121
serde_json = "1.0"
2222
r2d2 = "0.8"
2323
r2d2_redis = "0.14"
24-
chrono = "0.4.19"
24+
time = "0.3"

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ sidekiq = "0.10"
3333
```rust
3434
use sidekiq::{Job, Value};
3535
use sidekiq::{Client, ClientOpts, create_redis_pool};
36-
use chrono::Duration;
36+
use time::{OffsetDateTime, Duration};
3737

3838
let ns = "test";
3939
let client_opts = ClientOpts {
@@ -65,8 +65,7 @@ match client.perform_in(interval, job) {
6565

6666
// scheduled-jobs (perform_at)
6767
let job = Job::new(class, vec![sidekiq::Value::Null], Default::default());
68-
let now: DateTime<Local> = Local::now();
69-
let start_at = now + Duration::hours(1);
68+
let start_at = OffsetDateTime::now_utc().checked_add(Duration::HOUR).unwrap();
7069
match client.perform_at(start_at, job) {
7170
Ok(_) => {},
7271
Err(err) => {

src/sidekiq/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rand::{thread_rng, Rng};
1010
use serde::ser::SerializeStruct;
1111
use serde::{Serialize, Serializer};
1212

13-
use chrono::{DateTime, Duration, Local};
13+
use time::{OffsetDateTime, Duration};
1414

1515
const REDIS_URL_ENV: &str = "REDIS_URL";
1616
const REDIS_URL_DEFAULT: &str = "redis://127.0.0.1/";
@@ -205,7 +205,7 @@ impl Client {
205205
let div: f64 = 1_000_f64;
206206
let maximum_target: f64 = 1_000_000_000_f64;
207207
let target_millsec: f64 = target_millsec_number / div;
208-
let now_millisec: f64 = Local::now().timestamp_millis() as f64 / div;
208+
let now_millisec = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as f64 / div;
209209

210210
let start_at: f64 = if target_millsec < maximum_target {
211211
now_millisec + target_millsec
@@ -221,20 +221,20 @@ impl Client {
221221
}
222222

223223
fn convert_duration_to_millsec(&self, interval: Duration) -> Option<f64> {
224-
let interval_millsec: f64 = interval.num_milliseconds() as f64;
224+
let interval_millsec: f64 = interval.subsec_milliseconds() as f64;
225225
self.calc_at(interval_millsec)
226226
}
227227

228-
fn convert_datetime_to_millsec(&self, datetime: DateTime<Local>) -> Option<f64> {
229-
let timestamp_millsec: f64 = datetime.timestamp_millis() as f64;
228+
fn convert_datetime_to_millsec(&self, datetime: OffsetDateTime) -> Option<f64> {
229+
let timestamp_millsec: f64 = datetime.millisecond() as f64;
230230
self.calc_at(timestamp_millsec)
231231
}
232232

233233
pub fn perform_in(&self, interval: Duration, job: Job) -> Result<(), ClientError> {
234234
self.raw_push(&[job], self.convert_duration_to_millsec(interval))
235235
}
236236

237-
pub fn perform_at(&self, local_datetime: DateTime<Local>, job: Job) -> Result<(), ClientError> {
237+
pub fn perform_at(&self, local_datetime: OffsetDateTime, job: Job) -> Result<(), ClientError> {
238238
self.raw_push(&[job], self.convert_datetime_to_millsec(local_datetime))
239239
}
240240

tests/lib.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
88
use serde_json::value::Value;
99
use sidekiq::{create_redis_pool, Client, ClientOpts, Job};
1010

11-
use chrono::{DateTime, Duration, Local};
11+
use time::{OffsetDateTime, Duration};
1212

1313
fn args() -> Vec<Value> {
1414
let value = json!({
@@ -117,8 +117,7 @@ fn test_client_perform_at() {
117117
let class = "MyClass".to_string();
118118
let job = Job::new(class, args(), Default::default());
119119
let client = get_client();
120-
let now: DateTime<Local> = Local::now();
121-
let start_at = now + Duration::hours(1);
120+
let start_at = OffsetDateTime::now_utc();
122121
match client.perform_at(start_at, job) {
123122
Ok(_) => {}
124123
Err(err) => {
@@ -130,9 +129,8 @@ fn test_client_perform_at() {
130129
let class = "MyClass".to_string();
131130
let job = Job::new(class, args(), Default::default());
132131
let client = get_client();
133-
let now: DateTime<Local> = Local::now();
134-
let start_datetime = now + Duration::hours(0);
135-
match client.perform_at(start_datetime, job) {
132+
let start_at = OffsetDateTime::now_utc().checked_add(Duration::HOUR).unwrap();
133+
match client.perform_at(start_at, job) {
136134
Ok(_) => {}
137135
Err(err) => {
138136
println!("Sidekiq push failed: {}", err);

0 commit comments

Comments
 (0)