From 1acef755ca8a0a0433a98e4a66433c63ee0a3b09 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Mon, 30 Oct 2023 08:45:45 -0700 Subject: chore: remove usage of chrono::Utc::now() (#20995) Remove usage of Chrono's clock feature which pulls in iana-time-zone -> core-foundation --- ext/kv/lib.rs | 4 ++-- ext/kv/remote.rs | 2 +- ext/kv/time.rs | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 ext/kv/time.rs (limited to 'ext') diff --git a/ext/kv/lib.rs b/ext/kv/lib.rs index 20f774033..fb68596fa 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 38b233cc3..0a061b35b 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 000000000..60375818b --- /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) +} -- cgit v1.2.3