diff --git a/Cargo.lock b/Cargo.lock index 4141b2591..af6817ae4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -525,9 +525,9 @@ dependencies = [ [[package]] name = "chrono" -version = "0.4.34" +version = "0.4.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bc015644b92d5890fab7489e49d21f879d5c990186827d42ec511919404f38b" +checksum = "8eaf5903dcbc0a39312feb77df2ff4c76387d591b9fc7b04a238dcf8bb62639a" dependencies = [ "android-tzdata", "iana-time-zone", diff --git a/aggregator/src/binary_utils/job_driver.rs b/aggregator/src/binary_utils/job_driver.rs index b9f3f479f..ce5c86ecc 100644 --- a/aggregator/src/binary_utils/job_driver.rs +++ b/aggregator/src/binary_utils/job_driver.rs @@ -254,7 +254,7 @@ where // remains until the expiry time, minus the clock skew allowance. All math saturates, since // we want to timeout immediately if any of these subtractions would underflow. Duration::from_secs( - u64::try_from(lease_expiry.timestamp()) + u64::try_from(lease_expiry.and_utc().timestamp()) .unwrap_or_default() .saturating_sub(self.clock.now().as_seconds_since_epoch()) .saturating_sub(self.worker_lease_clock_skew_allowance.as_secs()), @@ -265,7 +265,7 @@ where #[cfg(test)] mod tests { use super::JobDriver; - use chrono::NaiveDateTime; + use chrono::{DateTime, NaiveDateTime, Utc}; use janus_aggregator_core::{ datastore::{self, models::Lease}, test_util::noop_meter, @@ -330,12 +330,12 @@ mod tests { IncompleteJob { task_id: random(), job_id: random(), - lease_expiry: NaiveDateTime::from_timestamp_opt(100, 0).unwrap(), + lease_expiry: DateTime::::from_timestamp(100, 0).unwrap().naive_utc(), }, IncompleteJob { task_id: random(), job_id: random(), - lease_expiry: NaiveDateTime::from_timestamp_opt(200, 0).unwrap(), + lease_expiry: DateTime::::from_timestamp(200, 0).unwrap().naive_utc(), }, ]), // Second job finder call will be immediately after the first: no more jobs @@ -347,12 +347,12 @@ mod tests { IncompleteJob { task_id: random(), job_id: random(), - lease_expiry: NaiveDateTime::from_timestamp_opt(300, 0).unwrap(), + lease_expiry: DateTime::::from_timestamp(300, 0).unwrap().naive_utc(), }, IncompleteJob { task_id: random(), job_id: random(), - lease_expiry: NaiveDateTime::from_timestamp_opt(400, 0).unwrap(), + lease_expiry: DateTime::::from_timestamp(400, 0).unwrap().naive_utc(), }, ]), ])); diff --git a/aggregator_core/src/datastore/tests.rs b/aggregator_core/src/datastore/tests.rs index 4378fdc18..2c854edfc 100644 --- a/aggregator_core/src/datastore/tests.rs +++ b/aggregator_core/src/datastore/tests.rs @@ -2836,7 +2836,8 @@ async fn run_collection_job_acquire_test_case( test_case.query_type, VdafInstance::Fake, ), - clock.now().as_naive_date_time().unwrap() + chrono::Duration::seconds(100), + clock.now().as_naive_date_time().unwrap() + + chrono::Duration::try_seconds(100).unwrap(), ) }) .collect(); @@ -2969,7 +2970,8 @@ async fn time_interval_collection_job_acquire_release_happy_path( assert_eq!(acquired_job.leased(), reacquired_job.leased()); assert_eq!( *acquired_job.lease_expiry_time(), - *reacquired_job.lease_expiry_time() + chrono::Duration::seconds(100), + *reacquired_job.lease_expiry_time() + + chrono::Duration::try_seconds(100).unwrap(), ); } @@ -3099,7 +3101,8 @@ async fn fixed_size_collection_job_acquire_release_happy_path( assert_eq!(acquired_job.leased(), reacquired_job.leased()); assert_eq!( *acquired_job.lease_expiry_time(), - *reacquired_job.lease_expiry_time() + chrono::Duration::seconds(100), + *reacquired_job.lease_expiry_time() + + chrono::Duration::try_seconds(100).unwrap(), ); } @@ -3563,7 +3566,8 @@ async fn collection_job_acquire_job_max(ephemeral_datastore: EphemeralDatastore) task::QueryType::TimeInterval, VdafInstance::Fake, ), - clock.now().as_naive_date_time().unwrap() + chrono::Duration::seconds(100), + clock.now().as_naive_date_time().unwrap() + + chrono::Duration::try_seconds(100).unwrap(), ) }) .collect(); diff --git a/collector/src/lib.rs b/collector/src/lib.rs index 5aef85123..1b349ae82 100644 --- a/collector/src/lib.rs +++ b/collector/src/lib.rs @@ -661,7 +661,7 @@ mod tests { PollResult, }; use assert_matches::assert_matches; - use chrono::{NaiveDateTime, TimeZone, Utc}; + use chrono::{DateTime, TimeZone, Utc}; #[cfg(feature = "fpvec_bounded_l2")] use fixed_macro::fixed; use janus_core::{ @@ -912,10 +912,8 @@ mod tests { PartialBatchSelector::new_time_interval(), 1, ( - Utc.from_utc_datetime( - &NaiveDateTime::from_timestamp_opt(1_000_000, 0).unwrap(), - ), - chrono::Duration::seconds(3600), + DateTime::::from_timestamp(1_000_000, 0).unwrap(), + chrono::Duration::try_seconds(3600).unwrap(), ), 1, ), @@ -980,10 +978,8 @@ mod tests { PartialBatchSelector::new_time_interval(), 1, ( - Utc.from_utc_datetime( - &NaiveDateTime::from_timestamp_opt(1_000_000, 0).unwrap(), - ), - chrono::Duration::seconds(3600), + DateTime::::from_timestamp(1_000_000, 0).unwrap(), + chrono::Duration::try_seconds(3600).unwrap(), ), 144 ) @@ -1047,10 +1043,8 @@ mod tests { PartialBatchSelector::new_time_interval(), 1, ( - Utc.from_utc_datetime( - &NaiveDateTime::from_timestamp_opt(1_000_000, 0).unwrap(), - ), - chrono::Duration::seconds(3600), + DateTime::::from_timestamp(1_000_000, 0).unwrap(), + chrono::Duration::try_seconds(3600).unwrap(), ), Vec::from([0, 0, 0, 1, 0]) ) @@ -1123,10 +1117,8 @@ mod tests { PartialBatchSelector::new_time_interval(), 1, ( - Utc.from_utc_datetime( - &NaiveDateTime::from_timestamp_opt(1_000_000, 0).unwrap(), - ), - chrono::Duration::seconds(3600), + DateTime::::from_timestamp(1_000_000, 0).unwrap(), + chrono::Duration::try_seconds(3600).unwrap(), ), Vec::from([0.0625, 0.125, 0.25]) ) @@ -1192,8 +1184,8 @@ mod tests { PartialBatchSelector::new_fixed_size(batch_id), 1, ( - Utc.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(0, 0).unwrap()), - chrono::Duration::seconds(1), + DateTime::::from_timestamp(0, 0).unwrap(), + chrono::Duration::try_seconds(1).unwrap(), ), 1 ) @@ -1270,10 +1262,8 @@ mod tests { PartialBatchSelector::new_time_interval(), 1, ( - Utc.from_utc_datetime( - &NaiveDateTime::from_timestamp_opt(1_000_000, 0).unwrap(), - ), - chrono::Duration::seconds(3600), + DateTime::::from_timestamp(1_000_000, 0).unwrap(), + chrono::Duration::try_seconds(3600).unwrap(), ), 1, ) diff --git a/core/src/time.rs b/core/src/time.rs index e68f4f460..cda1ecefb 100644 --- a/core/src/time.rs +++ b/core/src/time.rs @@ -1,6 +1,6 @@ //! Utilities for timestamps and durations. -use chrono::{NaiveDateTime, Utc}; +use chrono::{DateTime, NaiveDateTime, Utc}; use janus_messages::{Duration, Error, Interval, Time}; use std::{ fmt::{Debug, Formatter}, @@ -114,10 +114,13 @@ pub trait DurationExt: Sized { impl DurationExt for Duration { fn as_chrono_duration(&self) -> Result { - Ok(chrono::Duration::seconds( + chrono::Duration::try_seconds( self.as_seconds() .try_into() .map_err(|_| Error::IllegalTimeArithmetic("number of seconds too big for i64"))?, + ) + .ok_or(Error::IllegalTimeArithmetic( + "number of milliseconds too big for i64", )) } @@ -220,7 +223,7 @@ impl TimeExt for Time { } fn as_naive_date_time(&self) -> Result { - NaiveDateTime::from_timestamp_opt( + DateTime::::from_timestamp( self.as_seconds_since_epoch() .try_into() .map_err(|_| Error::IllegalTimeArithmetic("number of seconds too big for i64"))?, @@ -229,10 +232,11 @@ impl TimeExt for Time { .ok_or(Error::IllegalTimeArithmetic( "number of seconds is out of range", )) + .map(|dt| dt.naive_utc()) } fn from_naive_date_time(time: &NaiveDateTime) -> Self { - Self::from_seconds_since_epoch(time.timestamp() as u64) + Self::from_seconds_since_epoch(time.and_utc().timestamp() as u64) } fn add(&self, duration: &Duration) -> Result {