From 1acef755ca8a0a0433a98e4a66433c63ee0a3b09 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 30 Oct 2023 08:45:45 -0700 Subject: [PATCH] chore: remove usage of chrono::Utc::now() (#20995) Remove usage of Chrono's clock feature which pulls in iana-time-zone -> core-foundation --- Cargo.lock | 50 -------------------------------- Cargo.toml | 4 ++- cli/tools/jupyter/jupyter_msg.rs | 5 ++-- ext/kv/lib.rs | 4 +-- ext/kv/remote.rs | 2 +- ext/kv/time.rs | 19 ++++++++++++ 6 files changed, 28 insertions(+), 56 deletions(-) create mode 100644 ext/kv/time.rs diff --git a/Cargo.lock b/Cargo.lock index c5fb440f12..ea7e774f34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -135,21 +135,6 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - [[package]] name = "anstream" version = "0.3.2" @@ -553,11 +538,8 @@ version = "0.4.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38" dependencies = [ - "android-tzdata", - "iana-time-zone", "num-traits", "serde", - "windows-targets", ] [[package]] @@ -2840,29 +2822,6 @@ dependencies = [ "tokio-rustls", ] -[[package]] -name = "iana-time-zone" -version = "0.1.57" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fad5b825842d2b38bd206f3e81d6957625fd7f0a361e345c30e01a0ae2dd613" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "wasm-bindgen", - "windows", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - [[package]] name = "idna" version = "0.2.3" @@ -6542,15 +6501,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "windows" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" -dependencies = [ - "windows-targets", -] - [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 7b967a16f4..c2765ab660 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,7 +78,9 @@ brotli = "3.3.4" bytes = "1.4.0" cache_control = "=0.2.0" cbc = { version = "=0.1.2", features = ["alloc"] } -chrono = { version = "0.4", default-features = false, features = ["std", "serde", "clock"] } +# Note: Do not use the "clock" feature of chrono, as it links us to CoreFoundation on macOS. +# Instead use util::time::utc_now() +chrono = { version = "0.4", default-features = false, features = ["std", "serde"] } console_static_text = "=0.8.1" data-url = "=0.3.0" data-encoding = "2.3.3" diff --git a/cli/tools/jupyter/jupyter_msg.rs b/cli/tools/jupyter/jupyter_msg.rs index da6654cae8..8540a7f37f 100644 --- a/cli/tools/jupyter/jupyter_msg.rs +++ b/cli/tools/jupyter/jupyter_msg.rs @@ -4,7 +4,6 @@ // Copyright 2020 The Evcxr Authors. MIT license. use bytes::Bytes; -use chrono::Utc; use data_encoding::HEXLOWER; use deno_core::anyhow::anyhow; use deno_core::anyhow::bail; @@ -15,6 +14,8 @@ use ring::hmac; use std::fmt; use uuid::Uuid; +use crate::util::time::utc_now; + pub(crate) struct Connection { pub(crate) socket: S, /// Will be None if our key was empty (digest authentication disabled). @@ -177,7 +178,7 @@ impl JupyterMessage { header["msg_type"] = serde_json::Value::String(msg_type.to_owned()); header["username"] = serde_json::Value::String("kernel".to_owned()); header["msg_id"] = serde_json::Value::String(Uuid::new_v4().to_string()); - header["date"] = serde_json::Value::String(Utc::now().to_rfc3339()); + header["date"] = serde_json::Value::String(utc_now().to_rfc3339()); JupyterMessage { zmq_identities: Vec::new(), diff --git a/ext/kv/lib.rs b/ext/kv/lib.rs index 20f774033c..fb68596fa9 100644 --- a/ext/kv/lib.rs +++ b/ext/kv/lib.rs @@ -6,6 +6,7 @@ mod interface; mod proto; pub mod remote; pub mod sqlite; +mod time; use std::borrow::Cow; use std::cell::RefCell; @@ -14,7 +15,6 @@ use std::rc::Rc; use base64::prelude::BASE64_URL_SAFE; use base64::Engine; -use chrono::Utc; use codec::decode_key; use codec::encode_key; use deno_core::anyhow::Context; @@ -610,7 +610,7 @@ async fn op_kv_atomic_write( where DBH: DatabaseHandler + 'static, { - let current_timestamp = Utc::now().timestamp_millis() as u64; + let current_timestamp = time::utc_now().timestamp_millis() as u64; let db = { let state = state.borrow(); let resource = diff --git a/ext/kv/remote.rs b/ext/kv/remote.rs index 38b233cc37..0a061b35b0 100644 --- a/ext/kv/remote.rs +++ b/ext/kv/remote.rs @@ -426,7 +426,7 @@ async fn metadata_refresh_task( metadata .expires_at .timestamp_millis() - .saturating_sub(Utc::now().timestamp_millis()), + .saturating_sub(crate::time::utc_now().timestamp_millis()), ) .unwrap_or_default(); diff --git a/ext/kv/time.rs b/ext/kv/time.rs new file mode 100644 index 0000000000..60375818b6 --- /dev/null +++ b/ext/kv/time.rs @@ -0,0 +1,19 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +/// Identical to chrono::Utc::now() but without the system "clock" +/// feature flag. +/// +/// The "clock" feature flag pulls in the "iana-time-zone" crate +/// which links to macOS's "CoreFoundation" framework which increases +/// startup time for the CLI. +pub fn utc_now() -> chrono::DateTime { + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .expect("system time before Unix epoch"); + let naive = chrono::NaiveDateTime::from_timestamp_opt( + now.as_secs() as i64, + now.subsec_nanos(), + ) + .unwrap(); + chrono::DateTime::from_naive_utc_and_offset(naive, chrono::Utc) +}