Skip to content

Commit cdd0c3d

Browse files
authored
feat: normalize use of chrono for time and datetime operations (#2125)
feat: normalize use of `chrono` for time and datetime operations
1 parent 3735024 commit cdd0c3d

File tree

18 files changed

+40
-75
lines changed

18 files changed

+40
-75
lines changed

.github/workflows/glean-probe-scraper.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ jobs:
1212
permissions:
1313
contents: read
1414
checks: write
15-
uses: mozilla/probe-scraper/.github/workflows/glean.yaml@6cb549542a9d81fddbbaa8d5e6fdf95bf4761488 # v1.0
15+
uses: mozilla/probe-scraper/.github/workflows/glean.yaml@78f5c5f20dfca6bcb68138a1122dae5acfd06605 # v1.0

Cargo.lock

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ slog-term = "2.9"
9191
temp-env = { version = "0.3", features = ["async_closure"] }
9292
tokio = "1"
9393
thiserror = "2.0.18"
94-
time = "0.3.47"
9594
utoipa = "5.4.0"
9695
utoipa-swagger-ui = { version = "9", features = ["actix-web"] }
9796
uuid = { version = "1.20", features = ["serde", "v4"] }

syncserver/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ syncserver-db-common = { path = "../syncserver-db-common" }
4646
syncserver-settings = { path = "../syncserver-settings" }
4747
syncstorage-db = { path = "../syncstorage-db" }
4848
syncstorage-settings = { path = "../syncstorage-settings" }
49-
time.workspace = true
5049
tokenserver-auth = { path = "../tokenserver-auth" }
5150
tokenserver-common = { path = "../tokenserver-common" }
5251
tokenserver-db = { path = "../tokenserver-db" }

syncserver/src/tokenserver/extractors.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,8 @@ mod tests {
675675

676676
use crate::tokenserver::ServerState;
677677

678+
use chrono::Utc;
678679
use std::sync::Arc;
679-
use std::time::{SystemTime, UNIX_EPOCH};
680680

681681
lazy_static! {
682682
static ref SECRETS: Arc<Secrets> = Arc::new(Secrets::new("Ted Koppel is a robot").unwrap());
@@ -886,11 +886,10 @@ mod tests {
886886
fn build_request() -> TestRequest {
887887
let fxa_uid = "test123";
888888
let oauth_verifier = {
889-
let start = SystemTime::now();
890-
let current_time = start.duration_since(UNIX_EPOCH).unwrap();
889+
let current_time = Utc::now().timestamp();
891890
let verify_output = oauth::VerifyOutput {
892891
fxa_uid: fxa_uid.to_owned(),
893-
generation: Some(current_time.as_secs() as i64),
892+
generation: Some(current_time),
894893
};
895894
let valid = true;
896895

syncserver/src/tokenserver/handlers.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
use std::{
2-
collections::HashMap,
3-
time::{Duration, SystemTime, UNIX_EPOCH},
4-
};
1+
use std::{collections::HashMap, time::Duration};
52

63
use actix_web::{Error, HttpResponse, http::StatusCode};
74
use base64::{Engine, engine};
5+
use chrono::{TimeDelta, Utc};
86
use serde::Serialize;
97
use serde_json::Value;
108
use tokenserver_auth::{MakeTokenPlaintext, Tokenlib, TokenserverOrigin};
@@ -79,10 +77,7 @@ pub async fn get_tokenserver_result(
7977
node_type: req.node_type,
8078
};
8179

82-
let timestamp = {
83-
let start = SystemTime::now();
84-
start.duration_since(UNIX_EPOCH).unwrap().as_secs()
85-
};
80+
let timestamp = Utc::now().timestamp() as u64;
8681

8782
Ok(HttpResponse::build(StatusCode::OK)
8883
.insert_header(("X-Timestamp", timestamp.to_string()))
@@ -116,13 +111,7 @@ fn get_token_plaintext(
116111
)
117112
};
118113

119-
let expires = {
120-
let start = SystemTime::now();
121-
let current_time = start.duration_since(UNIX_EPOCH).unwrap();
122-
let expires = current_time + Duration::from_secs(req.duration);
123-
124-
expires.as_secs()
125-
};
114+
let expires = (Utc::now() + TimeDelta::seconds(req.duration as i64)).timestamp() as u64;
126115

127116
Ok(MakeTokenPlaintext {
128117
node: req.user.node.to_owned(),
@@ -226,10 +215,7 @@ async fn update_user(
226215
// If the client state changed, we need to mark the current user as "replaced" and create a
227216
// new user record. Otherwise, we can update the user in place.
228217
if req.auth_data.client_state != req.user.client_state {
229-
let timestamp = SystemTime::now()
230-
.duration_since(UNIX_EPOCH)
231-
.unwrap()
232-
.as_millis() as i64;
218+
let timestamp = Utc::now().timestamp_millis();
233219

234220
// Create new user record with updated generation/keys_changed_at
235221
let post_user_params = PostUser {

syncserver/src/web/auth.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@
66
allow(dead_code, unused_imports, unused_variables)
77
)]
88

9-
use std::convert::TryInto;
10-
119
use base64::{Engine, engine};
12-
use chrono::offset::Utc;
10+
use chrono::{TimeDelta, offset::Utc};
1311
use hawk::{self, Header as HawkHeader, Key, RequestBuilder};
1412
use hmac::{Hmac, Mac};
1513
use serde::{Deserialize, Serialize};
1614
use sha2::Sha256;
1715
use syncserver_common;
1816
use syncserver_settings::Secrets;
19-
use time::Duration;
2017
use tokenserver_auth::TokenserverOrigin;
2118

2219
use actix_web::dev::ConnectionInfo;
@@ -104,8 +101,8 @@ impl HawkPayload {
104101

105102
#[cfg(not(feature = "no_auth"))]
106103
{
107-
let mut duration: std::time::Duration = Duration::weeks(52)
108-
.try_into()
104+
let mut duration = TimeDelta::weeks(52)
105+
.to_std()
109106
.map_err(|_| ApiErrorKind::Internal("Duration::weeks".to_owned()))?;
110107
if cfg!(test) {
111108
// test cases are valid until 3018. Add millenia as required.

syncstorage-settings/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ rand.workspace=true
1010
serde.workspace=true
1111

1212
syncserver-common = { path = "../syncserver-common" }
13-
time.workspace = true

syncstorage-spanner/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ edition.workspace = true
99
actix-web.workspace = true
1010
async-trait.workspace = true
1111
backtrace.workspace = true
12+
chrono.workspace = true
1213
deadpool.workspace = true
1314
futures.workspace = true
1415
http.workspace = true

syncstorage-spanner/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::time::SystemTime;
1+
use chrono::Utc;
22

33
#[macro_use]
44
extern crate slog_scope;
@@ -18,9 +18,7 @@ pub use pool::SpannerDbPool;
1818

1919
type DbResult<T> = Result<T, error::DbError>;
2020

21+
/// Return a timestamp of the seconds since Epoch, repr as `i64`.
2122
fn now() -> i64 {
22-
SystemTime::now()
23-
.duration_since(SystemTime::UNIX_EPOCH)
24-
.unwrap_or_default()
25-
.as_secs() as i64
23+
Utc::now().timestamp()
2624
}

0 commit comments

Comments
 (0)